mirror of
https://github.com/wassname/libcryptomarket.git
synced 2026-09-09 11:25:52 +08:00
Clean up unused files
This commit is contained in:
@@ -48,7 +48,6 @@ clean-test: ## remove test and coverage artifacts
|
||||
rm -fr htmlcov/
|
||||
|
||||
autopep8: ## autopep8 to clean
|
||||
autopep8 --aggressive --in-place --recursive libcryptomarket/*/*/*.py
|
||||
autopep8 --aggressive --in-place --recursive libcryptomarket/*/*.py
|
||||
autopep8 --aggressive --in-place --recursive libcryptomarket/*.py
|
||||
autopep8 --aggressive --in-place --recursive tests/*.py
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
# pylint: disable-msg=W0401
|
||||
# flake8: noqa
|
||||
from libcryptomarket.api.bitfinex_api import BitfinexApi
|
||||
from libcryptomarket.api.bitmex_api import BitmexApi
|
||||
from libcryptomarket.api.bittrex_api import BittrexApi
|
||||
from libcryptomarket.api.coinmarketcap_api import CoinMarketCapApi
|
||||
from libcryptomarket.api.gdax_api import GdaxApi
|
||||
from libcryptomarket.api.poloniex_api import PoloniexApi
|
||||
@@ -1,158 +0,0 @@
|
||||
#!/bin/python
|
||||
# import requests
|
||||
# import hmac
|
||||
# import hashlib
|
||||
# import urllib
|
||||
# from functools import partial
|
||||
|
||||
from libcryptomarket.api.exchange_api import ExchangeApi
|
||||
|
||||
|
||||
class BitfinexApi(ExchangeApi):
|
||||
"""Bitfinex API.
|
||||
"""
|
||||
|
||||
def __init__(self, public_key=None, private_key=None, logger=None):
|
||||
"""Constructor.
|
||||
|
||||
:param public_key: Public key.
|
||||
:param private_key: Private key.
|
||||
:param logger: Logger.
|
||||
"""
|
||||
ExchangeApi.__init__(self, public_key, private_key, logger)
|
||||
|
||||
@classmethod
|
||||
def get_url(cls):
|
||||
"""Get API url.
|
||||
"""
|
||||
return "https://api.bitfinex.com/v2"
|
||||
|
||||
@classmethod
|
||||
def get_public_calls(cls):
|
||||
"""Get public API calls.
|
||||
"""
|
||||
return {
|
||||
'tickers': 'GET',
|
||||
'ticker': 'GET',
|
||||
'trades': 'GET',
|
||||
'book': 'GET',
|
||||
'stat1': 'GET',
|
||||
'candles': 'GET'
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def get_private_calls(cls):
|
||||
"""Get public API calls.
|
||||
"""
|
||||
return {}
|
||||
|
||||
@classmethod
|
||||
def translate_call_name(cls, name):
|
||||
"""Translate API call name.
|
||||
|
||||
The class method name is always underscored (aligned with Python
|
||||
standard.) This method is to translate underscored name to exchange
|
||||
API call name.
|
||||
|
||||
:param name: Method name (underscored).
|
||||
"""
|
||||
return name
|
||||
|
||||
def _request_public(self, name, http_method, **kwargs):
|
||||
"""Request public API call.
|
||||
|
||||
:param name: Method name.
|
||||
:param http_method: HTTP method (POST, GET, DELETE).
|
||||
"""
|
||||
if name == "ticker":
|
||||
symbol = kwargs["symbol"]
|
||||
del kwargs["symbol"]
|
||||
name = '/'.join([name, symbol])
|
||||
elif name == "trades":
|
||||
symbol = kwargs["symbol"]
|
||||
del kwargs["symbol"]
|
||||
name = '/'.join([name, symbol, "hist"])
|
||||
elif name == "book":
|
||||
symbol = kwargs["symbol"]
|
||||
precision = kwargs["precision"]
|
||||
del kwargs["symbol"]
|
||||
del kwargs["precision"]
|
||||
name = "/".join([name, symbol, precision])
|
||||
elif name == "stat1":
|
||||
key = kwargs["key"]
|
||||
size = kwargs["size"]
|
||||
symbol = kwargs["symbol"]
|
||||
section = kwargs["section"]
|
||||
del kwargs["key"]
|
||||
del kwargs["size"]
|
||||
del kwargs["symbol"]
|
||||
del kwargs["section"]
|
||||
name = "/".join([name,
|
||||
"{}:{}:{}".format(key, size, symbol),
|
||||
section])
|
||||
elif name == "candles":
|
||||
timeframe = kwargs["timeframe"]
|
||||
symbol = kwargs["symbol"]
|
||||
section = kwargs["section"]
|
||||
del kwargs["timeframe"]
|
||||
del kwargs["symbol"]
|
||||
del kwargs["section"]
|
||||
name = "/".join([name,
|
||||
"trade:{}:{}".format(timeframe, symbol),
|
||||
section])
|
||||
|
||||
self.log_info("Public request:\n" +
|
||||
"name: {}\n".format(name) +
|
||||
"http_method: {}\n".format(http_method) +
|
||||
"kwargs: {}".format(kwargs))
|
||||
|
||||
return self._send_request(
|
||||
command=name, http_method=http_method,
|
||||
public_method=True, params=kwargs)
|
||||
|
||||
def _request_private(self, name, http_method, **kwargs):
|
||||
"""Request private API call.
|
||||
|
||||
:param name: Method name.
|
||||
:param http_method: HTTP method (POST, GET, DELETE).
|
||||
"""
|
||||
raise NotImplementedError("Not support private api at this moment.")
|
||||
|
||||
# @classmethod
|
||||
# def _generate_auth(cls, public_key, private_key):
|
||||
# """Generate authentication.
|
||||
|
||||
# :param public_key: Public key.
|
||||
# :param private_key: Private key.
|
||||
# """
|
||||
# return None
|
||||
|
||||
# @classmethod
|
||||
# def _generate_headers(cls, command, http_method, params, data,
|
||||
# public_key, private_key):
|
||||
# """Generate headers.
|
||||
|
||||
# :param command: Command.
|
||||
# :param http_method: HTTP method, for example GET.
|
||||
# :param params: Parameters.
|
||||
# :param data: Data.
|
||||
# :param public_key: Public key.
|
||||
# :param private_key: Private key.
|
||||
# """
|
||||
# signature = hmac.new(private_key.encode(),
|
||||
# urllib.parse.urlencode(data).encode(),
|
||||
# digestmod=hashlib.sha512).hexdigest()
|
||||
# header = {
|
||||
# 'Key': public_key,
|
||||
# 'Sign': signature
|
||||
# }
|
||||
|
||||
# return header
|
||||
|
||||
# @classmethod
|
||||
# def _format_data(cls, data):
|
||||
# """Format the data to exchange desirable format.
|
||||
|
||||
# :param data: Data.
|
||||
# """
|
||||
# return data
|
||||
@@ -1,85 +0,0 @@
|
||||
#!/bin/python
|
||||
from libcryptomarket.api.exchange_api import ExchangeApi
|
||||
|
||||
|
||||
class BitmexApi(ExchangeApi):
|
||||
"""BitMEX API connector.
|
||||
"""
|
||||
|
||||
def __init__(self, public_key=None, private_key=None, logger=None):
|
||||
"""Constructor.
|
||||
|
||||
:param public_key: Public key.
|
||||
:param private_key: Private key.
|
||||
:param logger: Logger.
|
||||
"""
|
||||
ExchangeApi.__init__(self, public_key, private_key, logger)
|
||||
|
||||
@classmethod
|
||||
def get_url(cls):
|
||||
"""Get API url.
|
||||
"""
|
||||
return 'https://www.bitmex.com/api/v1/'
|
||||
|
||||
@classmethod
|
||||
def get_public_calls(cls):
|
||||
"""Get public API calls.
|
||||
"""
|
||||
return {
|
||||
"funding": "GET",
|
||||
"instrument": "GET",
|
||||
"instrument/active": "GET",
|
||||
"instrument/activeAndIndices": "GET",
|
||||
"instrument/activeIntervals": "GET",
|
||||
"instrument/compositeIndex": "GET",
|
||||
"instrument/indices": "GET",
|
||||
"insurance": "GET",
|
||||
"liquidation": "GET",
|
||||
"orderbook/l2": "GET",
|
||||
"quote": "GET",
|
||||
"quote/bucketed": "GET",
|
||||
"settlement": "GET",
|
||||
"stats": "GET",
|
||||
"stats/history": "GET",
|
||||
"stats/history/usd": "GET",
|
||||
"trade": "GET",
|
||||
"trade/bucketed": "GET",
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def get_private_calls(cls):
|
||||
"""Get public API calls.
|
||||
"""
|
||||
return {}
|
||||
|
||||
@classmethod
|
||||
def translate_call_name(cls, name):
|
||||
"""Translate API call name.
|
||||
|
||||
The class method name is always underscored (aligned with Python
|
||||
standard.) This method is to translate underscored name to exchange
|
||||
API call name.
|
||||
|
||||
:param name: Method name (underscored).
|
||||
"""
|
||||
return name.replace("_", "/")
|
||||
|
||||
def _request_public(self, name, http_method, **kwargs):
|
||||
"""Request public API call.
|
||||
|
||||
:param name: Method name.
|
||||
:param http_method: HTTP method (POST, GET, DELETE).
|
||||
"""
|
||||
return self._send_request(
|
||||
command=name, http_method=http_method, params=kwargs, data=None)
|
||||
|
||||
def _request_private(self, name, http_method, **kwargs):
|
||||
"""Request private API call.
|
||||
|
||||
:param name: Method name.
|
||||
:param http_method: HTTP method (POST, GET, DELETE).
|
||||
"""
|
||||
# return self._send_request(
|
||||
# command=name, http_method=http_method, params=kwargs,
|
||||
# public_key=self._public_key, private_key=self._private_key)
|
||||
raise NotImplementedError("Not support private api at this moment.")
|
||||
@@ -1,83 +0,0 @@
|
||||
#!/bin/python
|
||||
from libcryptomarket.api.exchange_api import ExchangeApi
|
||||
|
||||
|
||||
class BittrexApi(ExchangeApi):
|
||||
"""Bittrex API.
|
||||
"""
|
||||
|
||||
def __init__(self, public_key=None, private_key=None, logger=None):
|
||||
"""Constructor.
|
||||
|
||||
:param public_key: Public key.
|
||||
:param private_key: Private key.
|
||||
:param logger: Logger.
|
||||
"""
|
||||
ExchangeApi.__init__(self, public_key, private_key, logger)
|
||||
|
||||
@classmethod
|
||||
def get_url(cls):
|
||||
"""Get API url.
|
||||
"""
|
||||
return "https://bittrex.com/api"
|
||||
|
||||
@classmethod
|
||||
def get_public_calls(cls):
|
||||
"""Get public API calls.
|
||||
"""
|
||||
return {
|
||||
'getmarkets': 'GET',
|
||||
'getcurrencies': 'GET',
|
||||
'getticker': 'GET',
|
||||
'getmarketsummaries': 'GET',
|
||||
'getmarketsummary': 'GET',
|
||||
'getorderbook': 'GET',
|
||||
'getmarkethistory': 'GET',
|
||||
'getticks': 'GET',
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def get_private_calls(cls):
|
||||
"""Get public API calls.
|
||||
"""
|
||||
return {}
|
||||
|
||||
@classmethod
|
||||
def translate_call_name(cls, name):
|
||||
"""Translate API call name.
|
||||
|
||||
The class method name is always underscored (aligned with Python
|
||||
standard.) This method is to translate underscored name to exchange
|
||||
API call name.
|
||||
|
||||
:param name: Method name (underscored).
|
||||
"""
|
||||
return name.replace("_", "")
|
||||
|
||||
def _request_public(self, name, http_method, **kwargs):
|
||||
"""Request public API call.
|
||||
|
||||
:param name: Method name.
|
||||
:param http_method: HTTP method (POST, GET, DELETE).
|
||||
"""
|
||||
self.log_info("Public request:\n" +
|
||||
"name: {}\n".format(name) +
|
||||
"http_method: {}\n".format(http_method) +
|
||||
"kwargs: {}".format(kwargs))
|
||||
|
||||
if name == 'getticks':
|
||||
return self._send_request(
|
||||
command="/v2/public/" + name, http_method=http_method,
|
||||
public_method=True, params=kwargs)
|
||||
|
||||
return self._send_request(
|
||||
command="/v1.1/public/" + name, http_method=http_method,
|
||||
public_method=True, params=kwargs)
|
||||
|
||||
def _request_private(self, name, http_method, **kwargs):
|
||||
"""Request private API call.
|
||||
|
||||
:param name: Method name.
|
||||
:param http_method: HTTP method (POST, GET, DELETE).
|
||||
"""
|
||||
raise NotImplementedError("Not support private api at this moment.")
|
||||
@@ -1,76 +0,0 @@
|
||||
#!/bin/python
|
||||
from libcryptomarket.api.exchange_api import ExchangeApi
|
||||
|
||||
|
||||
class CoinMarketCapApi(ExchangeApi):
|
||||
"""Coinmarketcap API.
|
||||
"""
|
||||
|
||||
def __init__(self, logger=None):
|
||||
"""Constructor.
|
||||
|
||||
:param public_key: Public key.
|
||||
:param private_key: Private key.
|
||||
:param logger: Logger.
|
||||
"""
|
||||
ExchangeApi.__init__(self, public_key=None, private_key=None,
|
||||
logger=logger)
|
||||
|
||||
@classmethod
|
||||
def get_url(cls):
|
||||
"""Get API url.
|
||||
"""
|
||||
return "https://api.coinmarketcap.com/v1"
|
||||
|
||||
@classmethod
|
||||
def get_public_calls(cls):
|
||||
"""Get public API calls.
|
||||
"""
|
||||
return {
|
||||
"ticker": "GET",
|
||||
"global": "GET"
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def get_private_calls(cls):
|
||||
"""Get private API calls.
|
||||
"""
|
||||
return {}
|
||||
|
||||
@classmethod
|
||||
def translate_call_name(cls, name):
|
||||
"""Translate API call name.
|
||||
|
||||
The class method name is always underscored (aligned with Python
|
||||
standard.) This method is to translate underscored name to exchange
|
||||
API call name.
|
||||
|
||||
:param name: Method name (underscored).
|
||||
"""
|
||||
return name
|
||||
|
||||
def _request_public(self, name, http_method, **kwargs):
|
||||
"""Request public API call.
|
||||
|
||||
:param name: Method name.
|
||||
:param http_method: HTTP method (POST, GET, DELETE).
|
||||
"""
|
||||
name_list = [name]
|
||||
|
||||
if name == "ticker" and "id" in kwargs.keys():
|
||||
name_list.append(kwargs["id"])
|
||||
del kwargs["id"]
|
||||
|
||||
name_list.append("")
|
||||
|
||||
return self._send_request(
|
||||
command='/'.join(name_list), http_method=http_method,
|
||||
public_method=True, params=kwargs)
|
||||
|
||||
def _request_private(self, name, http_method, **kwargs):
|
||||
"""Request private API call.
|
||||
|
||||
:param name: Method name.
|
||||
:param http_method: HTTP method (POST, GET, DELETE).
|
||||
"""
|
||||
raise RuntimeError("No private method provided")
|
||||
@@ -1,227 +0,0 @@
|
||||
#!/bin/python
|
||||
import requests
|
||||
# import hmac
|
||||
# import hashlib
|
||||
# import urllib
|
||||
import json
|
||||
from functools import partial
|
||||
from time import time
|
||||
|
||||
|
||||
class ExchangeApi:
|
||||
"""Exchange API connector.
|
||||
"""
|
||||
|
||||
def __init__(self, public_key, private_key, logger=None):
|
||||
"""Constructor.
|
||||
|
||||
:param public_key: Public key.
|
||||
:param private_key: Private key.
|
||||
:param logger: Logger.
|
||||
"""
|
||||
self._public_key = public_key
|
||||
self._private_key = private_key
|
||||
self._logger = logger
|
||||
|
||||
@classmethod
|
||||
def get_url(cls):
|
||||
"""Get API url.
|
||||
"""
|
||||
raise NotImplementedError("Url getter not implemented")
|
||||
|
||||
@classmethod
|
||||
def get_public_url(cls):
|
||||
"""Get public API url.
|
||||
"""
|
||||
raise NotImplementedError("Public url getter not implemented")
|
||||
|
||||
@classmethod
|
||||
def get_private_url(cls):
|
||||
"""Get private API url.
|
||||
"""
|
||||
raise NotImplementedError("Private url getter not implemented")
|
||||
|
||||
@classmethod
|
||||
def get_public_calls(cls):
|
||||
"""Get public API calls.
|
||||
"""
|
||||
return {}
|
||||
|
||||
@classmethod
|
||||
def get_private_calls(cls):
|
||||
"""Get private API calls.
|
||||
"""
|
||||
return {}
|
||||
|
||||
@classmethod
|
||||
def translate_call_name(cls, name):
|
||||
"""Translate API call name.
|
||||
|
||||
The class method name is always underscored (aligned with Python
|
||||
standard.) This method is to translate underscored name to exchange
|
||||
API call name.
|
||||
|
||||
:param name: Method name (underscored).
|
||||
"""
|
||||
return name
|
||||
|
||||
def __getattr__(self, name):
|
||||
"""Get attribute.
|
||||
"""
|
||||
name = self.translate_call_name(name)
|
||||
|
||||
if name in self.get_public_calls().keys():
|
||||
return partial(self._request_public, name=name,
|
||||
http_method=self.get_public_calls()[name])
|
||||
elif name in self.get_private_calls().keys():
|
||||
return partial(self._request_private, name=name,
|
||||
http_method=self.get_private_calls()[name])
|
||||
else:
|
||||
raise AttributeError("Trading method ({0}) ".format(name) +
|
||||
"is not defined.")
|
||||
|
||||
def _request_public(self, name, http_method, **kwargs):
|
||||
"""Request public API call.
|
||||
|
||||
:param name: Method name.
|
||||
:param http_method: HTTP method (POST, GET, DELETE).
|
||||
"""
|
||||
raise NotImplementedError("Public API request not implemented")
|
||||
|
||||
def _request_private(self, name, http_method, *kwargs):
|
||||
"""Request private API call.
|
||||
|
||||
:param name: Method name.
|
||||
:param http_method: HTTP method (POST, GET, DELETE).
|
||||
"""
|
||||
raise NotImplementedError("Private API request not implemented")
|
||||
|
||||
# ======================================================================
|
||||
# Requests
|
||||
# ======================================================================
|
||||
|
||||
@classmethod
|
||||
def _generate_nonce(cls):
|
||||
"""Generate an increasing unique number.
|
||||
"""
|
||||
return int(round(time() * 1000))
|
||||
|
||||
@classmethod
|
||||
def _generate_headers(cls, command, http_method, params, data,
|
||||
public_key, private_key):
|
||||
"""Generate headers.
|
||||
|
||||
:param command: Command.
|
||||
:param http_method: HTTP method, for example GET.
|
||||
:param params: Parameters.
|
||||
:param data: Data.
|
||||
:param public_key: Public key.
|
||||
:param private_key: Private key.
|
||||
"""
|
||||
raise NotImplementedError("Not yet implemented.")
|
||||
|
||||
@classmethod
|
||||
def _generate_auth(cls, public_key, private_key):
|
||||
"""Generate authentication.
|
||||
|
||||
:param public_key: Public key.
|
||||
:param private_key: Private key.
|
||||
"""
|
||||
raise NotImplementedError("Not yet implemented.")
|
||||
|
||||
@classmethod
|
||||
def _generate_data(cls, data):
|
||||
"""Generate data.
|
||||
|
||||
:param data: Dict containing data information.
|
||||
"""
|
||||
raise NotImplementedError("Not yet implemented.")
|
||||
|
||||
def log_info(self, msg, *args):
|
||||
"""Log in INFO.
|
||||
"""
|
||||
if self._logger is not None:
|
||||
self._logger.info(msg, *args)
|
||||
|
||||
def log_debug(self, msg, *args):
|
||||
"""Log in DEBUG.
|
||||
"""
|
||||
if self._logger is not None:
|
||||
self._logger.debug(msg, *args)
|
||||
|
||||
def _send_request(self, command, http_method, params=None, data=None,
|
||||
public_method=False):
|
||||
"""Send request.
|
||||
|
||||
:param command: API command.
|
||||
:param http_method: Http method.
|
||||
:param api_key: API key.
|
||||
:param params: Input parameters, which will be parsed
|
||||
as "?key1=value1...".
|
||||
:param data: Data.
|
||||
:param public_method: Indicate if the request is a public method.
|
||||
:return: JSON object.
|
||||
"""
|
||||
http_method = http_method.upper()
|
||||
if http_method == "DELETE":
|
||||
R = requests.delete
|
||||
elif http_method == "GET":
|
||||
R = requests.get
|
||||
elif http_method == "POST":
|
||||
R = requests.post
|
||||
else:
|
||||
raise ValueError("Http method must be either DELETE, GET or "
|
||||
"POST.")
|
||||
|
||||
# Get url
|
||||
url = self.get_url()
|
||||
|
||||
if url is None:
|
||||
if public_method:
|
||||
url = self.get_public_url()
|
||||
else:
|
||||
url = self.get_private_url()
|
||||
|
||||
if url is None:
|
||||
raise NotImplementedError("Url cannot be None.")
|
||||
|
||||
if command is not None or command == "":
|
||||
url = '/'.join([url, command])
|
||||
|
||||
# Get data
|
||||
if data is not None:
|
||||
data = self._format_data(data)
|
||||
else:
|
||||
data = ""
|
||||
|
||||
# Get headers and auth
|
||||
if (self._public_key is not None and self._private_key is not None and
|
||||
not public_method):
|
||||
headers = self._generate_headers(command, http_method, params,
|
||||
data, self._public_key,
|
||||
self._private_key)
|
||||
auth = self._generate_auth(self._public_key, self._private_key)
|
||||
else:
|
||||
headers = None
|
||||
auth = None
|
||||
|
||||
self.log_debug(">>> OUT:\n%s" % json.dumps({
|
||||
"Method": http_method,
|
||||
"Url": url,
|
||||
"Params": params,
|
||||
"Data": data,
|
||||
"Headers": headers
|
||||
}))
|
||||
|
||||
if auth is None:
|
||||
response = R(url, params=params, data=data, headers=headers)
|
||||
else:
|
||||
response = R(url, params=params, data=data, headers=headers,
|
||||
auth=auth)
|
||||
|
||||
self.log_debug("<<< IN:\n%s" % json.dumps({
|
||||
"Status code": response.status_code,
|
||||
"Text": response.text
|
||||
}))
|
||||
|
||||
return response
|
||||
@@ -1,124 +0,0 @@
|
||||
#!/bin/python
|
||||
# import requests
|
||||
# import hmac
|
||||
# import hashlib
|
||||
# import urllib
|
||||
# from functools import partial
|
||||
|
||||
from libcryptomarket.api.exchange_api import ExchangeApi
|
||||
|
||||
|
||||
class GdaxApi(ExchangeApi):
|
||||
"""GDAX API.
|
||||
"""
|
||||
|
||||
def __init__(self, public_key=None, private_key=None, logger=None):
|
||||
"""Constructor.
|
||||
|
||||
:param public_key: Public key.
|
||||
:param private_key: Private key.
|
||||
:param logger: Logger.
|
||||
"""
|
||||
ExchangeApi.__init__(self, public_key, private_key, logger)
|
||||
|
||||
@classmethod
|
||||
def get_url(cls):
|
||||
"""Get API url.
|
||||
"""
|
||||
return "https://api.gdax.com"
|
||||
|
||||
@classmethod
|
||||
def get_public_calls(cls):
|
||||
"""Get public API calls.
|
||||
"""
|
||||
return {
|
||||
"products": "GET",
|
||||
"products/book": "GET",
|
||||
"products/trades": "GET",
|
||||
"products/candles": "GET",
|
||||
"currencies": "GET",
|
||||
"time": "GET",
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def get_private_calls(cls):
|
||||
"""Get public API calls.
|
||||
"""
|
||||
return {}
|
||||
|
||||
@classmethod
|
||||
def translate_call_name(cls, name):
|
||||
"""Translate API call name.
|
||||
|
||||
The class method name is always underscored (aligned with Python
|
||||
standard.) This method is to translate underscored name to exchange
|
||||
API call name.
|
||||
|
||||
:param name: Method name (underscored).
|
||||
"""
|
||||
return name.replace("_", "/")
|
||||
|
||||
def _request_public(self, name, http_method, **kwargs):
|
||||
"""Request public API call.
|
||||
|
||||
:param name: Method name.
|
||||
:param http_method: HTTP method (POST, GET, DELETE).
|
||||
"""
|
||||
if 'product_id' in kwargs.keys():
|
||||
name = name.split('/')
|
||||
name = [name[0]] + [kwargs['product_id']] + name[1:]
|
||||
del kwargs['product_id']
|
||||
return self._send_request(
|
||||
command='/'.join(name), http_method=http_method,
|
||||
public_method=True, params=kwargs)
|
||||
else:
|
||||
return self._send_request(
|
||||
command=name, http_method=http_method,
|
||||
public_method=True, params=kwargs)
|
||||
|
||||
def _request_private(self, name, http_method, **kwargs):
|
||||
"""Request private API call.
|
||||
|
||||
:param name: Method name.
|
||||
:param http_method: HTTP method (POST, GET, DELETE).
|
||||
"""
|
||||
raise NotImplementedError("request private is not implemented.")
|
||||
|
||||
# @classmethod
|
||||
# def _generate_auth(cls, public_key, private_key):
|
||||
# """Generate authentication.
|
||||
|
||||
# :param public_key: Public key.
|
||||
# :param private_key: Private key.
|
||||
# """
|
||||
# return None
|
||||
|
||||
# @classmethod
|
||||
# def _generate_headers(cls, command, http_method, params, data,
|
||||
# public_key, private_key):
|
||||
# """Generate headers.
|
||||
|
||||
# :param command: Command.
|
||||
# :param http_method: HTTP method, for example GET.
|
||||
# :param params: Parameters.
|
||||
# :param data: Data.
|
||||
# :param public_key: Public key.
|
||||
# :param private_key: Private key.
|
||||
# """
|
||||
# signature = hmac.new(private_key.encode(),
|
||||
# urllib.parse.urlencode(data).encode(),
|
||||
# digestmod=hashlib.sha512).hexdigest()
|
||||
# header = {
|
||||
# 'Key': public_key,
|
||||
# 'Sign': signature
|
||||
# }
|
||||
|
||||
# return header
|
||||
|
||||
# @classmethod
|
||||
# def _format_data(cls, data):
|
||||
# """Format the data to exchange desirable format.
|
||||
|
||||
# :param data: Data.
|
||||
# """
|
||||
# return data
|
||||
@@ -1,168 +0,0 @@
|
||||
#!/bin/python
|
||||
import hmac
|
||||
import hashlib
|
||||
import urllib
|
||||
|
||||
from libcryptomarket.api.exchange_api import ExchangeApi
|
||||
|
||||
|
||||
class PoloniexApi(ExchangeApi):
|
||||
"""Poloniex API.
|
||||
"""
|
||||
|
||||
def __init__(self, public_key=None, private_key=None, logger=None):
|
||||
"""Constructor.
|
||||
|
||||
:param public_key: Public key.
|
||||
:param private_key: Private key.
|
||||
:param logger: Logger.
|
||||
"""
|
||||
ExchangeApi.__init__(self, public_key, private_key, logger)
|
||||
|
||||
@classmethod
|
||||
def get_url(cls):
|
||||
"""Get API url.
|
||||
"""
|
||||
return None
|
||||
|
||||
@classmethod
|
||||
def get_public_url(cls):
|
||||
"""Get public API url.
|
||||
"""
|
||||
return 'https://poloniex.com/public'
|
||||
|
||||
@classmethod
|
||||
def get_private_url(cls):
|
||||
"""Get private API url.
|
||||
"""
|
||||
return 'https://poloniex.com/tradingApi'
|
||||
|
||||
@classmethod
|
||||
def get_public_calls(cls):
|
||||
"""Get public API calls.
|
||||
"""
|
||||
return {
|
||||
"returnTicker": "GET",
|
||||
"return24Volume": "GET",
|
||||
"returnOrderBook": "GET",
|
||||
"returnTradeHistory": "GET",
|
||||
"returnChartData": "GET",
|
||||
"returnCurrencies": "GET",
|
||||
"returnTicker": "GET",
|
||||
"returnLoanOrders": "GET",
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def get_private_calls(cls):
|
||||
"""Get public API calls.
|
||||
"""
|
||||
return {
|
||||
"returnBalances": "POST",
|
||||
"returnCompleteBalances": "POST",
|
||||
"returnDepositAddresses": "POST",
|
||||
"generateNewAddress": "POST",
|
||||
"returnDepositsWithdrawals": "POST",
|
||||
"returnOpenOrders": "POST",
|
||||
"returnTradeHistory": "POST",
|
||||
"returnOrderTrades": "POST",
|
||||
"buy": "POST",
|
||||
"sell": "POST",
|
||||
"cancelOrder": "POST",
|
||||
"moveOrder": "POST",
|
||||
"withdraw": "POST",
|
||||
"returnFeeInfo": "POST",
|
||||
"returnAvailableAccountBalances": "POST",
|
||||
"returnTradableBalances": "POST",
|
||||
"transferBalance": "POST",
|
||||
"returnMarginAccountSummary": "POST",
|
||||
"marginBuy": "POST",
|
||||
"marginSell": "POST",
|
||||
"getMarginPosition": "POST",
|
||||
"closeMarginPosition": "POST",
|
||||
"createLoanOffer": "POST",
|
||||
"cancelLoanOffer": "POST",
|
||||
"returnOpenLoanOffers": "POST",
|
||||
"returnActiveLoans": "POST",
|
||||
"returnLendingHistory": "POST",
|
||||
"toggleAutoRenew": "POST"
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def translate_call_name(cls, name):
|
||||
"""Translate API call name.
|
||||
|
||||
The class method name is always underscored (aligned with Python
|
||||
standard.) This method is to translate underscored name to exchange
|
||||
API call name.
|
||||
|
||||
:param name: Method name (underscored).
|
||||
"""
|
||||
splitted_names = name.split('_')
|
||||
if len(splitted_names) > 1:
|
||||
splitted_names = ([splitted_names[0]] +
|
||||
[n.title() for n in splitted_names[1:]])
|
||||
|
||||
return ''.join(splitted_names)
|
||||
|
||||
def _request_public(self, name, http_method, **kwargs):
|
||||
"""Request public API call.
|
||||
|
||||
:param name: Method name.
|
||||
:param http_method: HTTP method (POST, GET, DELETE).
|
||||
"""
|
||||
kwargs['command'] = name
|
||||
|
||||
return self._send_request(
|
||||
command=None, http_method=http_method, params=kwargs,
|
||||
public_method=True)
|
||||
|
||||
def _request_private(self, name, http_method, **kwargs):
|
||||
"""Request private API call.
|
||||
|
||||
:param name: Method name.
|
||||
:param http_method: HTTP method (POST, GET, DELETE).
|
||||
"""
|
||||
kwargs['command'] = name
|
||||
kwargs['nonce'] = self._generate_nonce()
|
||||
|
||||
return self._send_request(
|
||||
command=None, http_method=http_method, params=None, data=kwargs)
|
||||
|
||||
@classmethod
|
||||
def _generate_auth(cls, public_key, private_key):
|
||||
"""Generate authentication.
|
||||
|
||||
:param public_key: Public key.
|
||||
:param private_key: Private key.
|
||||
"""
|
||||
return None
|
||||
|
||||
@classmethod
|
||||
def _generate_headers(cls, command, http_method, params, data,
|
||||
public_key, private_key):
|
||||
"""Generate headers.
|
||||
|
||||
:param command: Command.
|
||||
:param http_method: HTTP method, for example GET.
|
||||
:param params: Parameters.
|
||||
:param data: Data.
|
||||
:param public_key: Public key.
|
||||
:param private_key: Private key.
|
||||
"""
|
||||
signature = hmac.new(private_key.encode(),
|
||||
urllib.parse.urlencode(data).encode(),
|
||||
digestmod=hashlib.sha512).hexdigest()
|
||||
header = {
|
||||
'Key': public_key,
|
||||
'Sign': signature
|
||||
}
|
||||
|
||||
return header
|
||||
|
||||
@classmethod
|
||||
def _format_data(cls, data):
|
||||
"""Format the data to exchange desirable format.
|
||||
|
||||
:param data: Data.
|
||||
"""
|
||||
return data
|
||||
@@ -3,7 +3,9 @@ import logging
|
||||
|
||||
import pandas as pd
|
||||
|
||||
from libcryptomarket import *
|
||||
# pylint: disable-msg=W0401
|
||||
from libcryptomarket.exchange import * # flake8: noqa
|
||||
from libcryptomarket import FREQUENCY_TO_SEC_DICT
|
||||
|
||||
LOG_FORMAT = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
||||
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
import pandas as pd
|
||||
|
||||
|
||||
def instruments(source):
|
||||
"""Return instruments.
|
||||
|
||||
:param source: Source, an Exchange API object.
|
||||
"""
|
||||
# Source object name
|
||||
source_name = source.__class__.__name__.lower().replace("api", "")
|
||||
|
||||
if source_name == "coinmarketcap":
|
||||
response = source.ticker()
|
||||
response.raise_for_status()
|
||||
return pd.DataFrame(response.json())
|
||||
else:
|
||||
raise ValueError("Source (%s [%s]) does not support instruments"
|
||||
% (source, source_name))
|
||||
@@ -1,16 +0,0 @@
|
||||
import ccxt
|
||||
|
||||
|
||||
def order_book(source, symbol, depth=None, params=None):
|
||||
"""Return the order book.
|
||||
|
||||
:param source: Source, an Exchange API object.
|
||||
:param symbol: Symbol.
|
||||
:param depth: Depth of the order book.
|
||||
"""
|
||||
exchange = getattr(ccxt, source.lower())()
|
||||
|
||||
if depth is not None:
|
||||
raise ValueError("Sorry that currently depth is not supported.")
|
||||
|
||||
return exchange.fetch_order_book(symbol=symbol, params=params or {})
|
||||
@@ -1,177 +0,0 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {
|
||||
"ExecuteTime": {
|
||||
"end_time": "2018-02-19T00:53:44.009602Z",
|
||||
"start_time": "2018-02-19T00:53:43.270385Z"
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"%load_ext autoreload\n",
|
||||
"%autoreload 2\n",
|
||||
"\n",
|
||||
"import pandas as pd\n",
|
||||
"\n",
|
||||
"from libcryptomarket.core import order_book"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"metadata": {
|
||||
"ExecuteTime": {
|
||||
"end_time": "2018-02-19T01:16:10.084818Z",
|
||||
"start_time": "2018-02-19T01:16:08.636986Z"
|
||||
}
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"{'asks': [[0.08806319, 74.903],\n",
|
||||
" [0.0880632, 8.0],\n",
|
||||
" [0.08806385, 69.565],\n",
|
||||
" [0.08806386, 3.46965772],\n",
|
||||
" [0.08806389, 66.78351839],\n",
|
||||
" [0.08806458, 8.88892227],\n",
|
||||
" [0.08806789, 0.12091826],\n",
|
||||
" [0.0882145, 2.25359],\n",
|
||||
" [0.08821516, 54.61112212],\n",
|
||||
" [0.08821517, 227.75833027],\n",
|
||||
" [0.08821816, 0.3],\n",
|
||||
" [0.08823728, 0.00590621],\n",
|
||||
" [0.0882497, 0.0021669],\n",
|
||||
" [0.08828145, 0.11410314],\n",
|
||||
" [0.08831127, 0.11398609],\n",
|
||||
" [0.08834685, 5.68478826],\n",
|
||||
" [0.08836529, 34.0632],\n",
|
||||
" [0.08840216, 4.05],\n",
|
||||
" [0.08840839, 0.45244575],\n",
|
||||
" [0.08846982, 0.22753128],\n",
|
||||
" [0.08849, 0.05],\n",
|
||||
" [0.08849985, 0.06866587],\n",
|
||||
" [0.0885, 0.09051559],\n",
|
||||
" [0.08851171, 5.78455985],\n",
|
||||
" [0.0885299, 0.1],\n",
|
||||
" [0.08852991, 52.4109342],\n",
|
||||
" [0.08852993, 4.3],\n",
|
||||
" [0.08855261, 0.3094033],\n",
|
||||
" [0.0885753, 0.00682119],\n",
|
||||
" [0.08867609, 81.73041587],\n",
|
||||
" [0.0886761, 0.0232845],\n",
|
||||
" [0.0886787, 0.0524102],\n",
|
||||
" [0.0886876, 0.0244448],\n",
|
||||
" [0.08868846, 0.02322458],\n",
|
||||
" [0.08869742, 0.11345586],\n",
|
||||
" [0.0887, 1.009013],\n",
|
||||
" [0.088777, 0.05025],\n",
|
||||
" [0.08881776, 0.0124816],\n",
|
||||
" [0.08883, 0.158],\n",
|
||||
" [0.08883927, 12.497],\n",
|
||||
" [0.08883928, 340.632],\n",
|
||||
" [0.0888515, 0.00217467],\n",
|
||||
" [0.08885441, 0.11282501],\n",
|
||||
" [0.08885442, 0.0022565],\n",
|
||||
" [0.08887284, 0.00498195],\n",
|
||||
" [0.08888873, 0.001183],\n",
|
||||
" [0.08891778, 0.0124816],\n",
|
||||
" [0.0889555, 0.006],\n",
|
||||
" [0.08897037, 2.5],\n",
|
||||
" [0.089, 0.01997]],\n",
|
||||
" 'bids': [[0.08792195, 0.02713255],\n",
|
||||
" [0.08790009, 5.22382779],\n",
|
||||
" [0.08790006, 0.00248732],\n",
|
||||
" [0.08790005, 7.4],\n",
|
||||
" [0.08787895, 1.14258325],\n",
|
||||
" [0.0878521, 0.00218435],\n",
|
||||
" [0.08782086, 0.00569341],\n",
|
||||
" [0.0878, 2.9925],\n",
|
||||
" [0.08779018, 2.05152775],\n",
|
||||
" [0.08777526, 0.05696365],\n",
|
||||
" [0.08777524, 0.00287234],\n",
|
||||
" [0.08777328, 1.21637425],\n",
|
||||
" [0.08776902, 0.2278708],\n",
|
||||
" [0.08775456, 0.0048503],\n",
|
||||
" [0.08773207, 0.006],\n",
|
||||
" [0.0877, 0.84925887],\n",
|
||||
" [0.08765172, 5.67135807],\n",
|
||||
" [0.08760894, 0.006854],\n",
|
||||
" [0.0876063, 0.00683824],\n",
|
||||
" [0.08756815, 0.02975783],\n",
|
||||
" [0.087559, 15.7857],\n",
|
||||
" [0.0875584, 0.00218142],\n",
|
||||
" [0.08755837, 0.49874994],\n",
|
||||
" [0.0875497, 4.0],\n",
|
||||
" [0.087544, 56.44903468],\n",
|
||||
" [0.08754399, 10.0],\n",
|
||||
" [0.08752531, 0.028909],\n",
|
||||
" [0.08742471, 5.4347113],\n",
|
||||
" [0.08738839, 0.31325678],\n",
|
||||
" [0.08738086, 0.0379],\n",
|
||||
" [0.08737386, 0.02837897],\n",
|
||||
" [0.0873598, 0.00569341],\n",
|
||||
" [0.08735837, 0.49874994],\n",
|
||||
" [0.08735423, 157.93071263],\n",
|
||||
" [0.08735421, 0.00854971],\n",
|
||||
" [0.0873542, 5.2],\n",
|
||||
" [0.08735, 0.05],\n",
|
||||
" [0.0873391, 34.0632],\n",
|
||||
" [0.08733193, 0.0031275],\n",
|
||||
" [0.08730518, 2.54749794],\n",
|
||||
" [0.08728562, 0.04791179],\n",
|
||||
" [0.08728529, 1.44332751],\n",
|
||||
" [0.08724949, 0.03252064],\n",
|
||||
" [0.08723311, 5.0],\n",
|
||||
" [0.08722376, 0.065117],\n",
|
||||
" [0.08721564, 0.04661645],\n",
|
||||
" [0.08720485, 0.229],\n",
|
||||
" [0.0871709, 0.00219089],\n",
|
||||
" [0.08716448, 5.73065903],\n",
|
||||
" [0.08714296, 15.9665893]],\n",
|
||||
" 'datetime': '2018-02-19T01:16:10.720Z',\n",
|
||||
" 'timestamp': 1519002970072}"
|
||||
]
|
||||
},
|
||||
"execution_count": 11,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"order_book(source=\"poloniex\", symbol=\"ETH/BTC\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.5.2"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
Reference in New Issue
Block a user