Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 58 additions & 86 deletions bincentive_trader/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ class TraderClient(object):
def __init__(self, email, password, testing=True):
if testing:
self.LOGIN_ENDPOINT = 'https://fs-sitapi.bincentive.com/member/api/member/login'
self.TRADER_ENDPOINT = 'https://qdapps-sitapi.bincentive.com/'
self.TRADER_ENDPOINT = 'https://bi-gateway-go-rd.bincentive.com'

else:
self.LOGIN_ENDPOINT = 'https://fs-api.bincentive.com/member/api/member/login'
self.TRADER_ENDPOINT = 'https://qdapps-proapi.bincentive.com/'
self.TRADER_ENDPOINT = 'https://bi-gateway-go-prod.bincentive.com'

self.session = requests.Session()
self.session.headers = {
Expand Down Expand Up @@ -69,148 +70,119 @@ def _request(self, method, endpoint, timeout, *args, **kwargs):

def _post(self, endpoint, json=None, timeout=None):
return self._request('POST', endpoint, timeout, json=json)

def _get(self, endpoint, timeout=None):
return self._request('GET', endpoint, timeout)

def _delete(self, endpoint, timeout=None):
return self._request('DELETE', endpoint, timeout)

def _put(self, endpoint, json=None, timeout=None):
return self._request('PUT', endpoint, timeout, json=json)

def get_strategy_list(self, timeout=None):
"""Gets the list of approved strategies."""
endpoint = self.TRADER_ENDPOINT + 'api/trader/getApprovedStrategyList'
r = self._post(endpoint, timeout=timeout)
endpoint = self.TRADER_ENDPOINT + '/api/v1/user/StrategyNameList'
r = self._get(endpoint, timeout=timeout)
return r.json()['data']

def get_exchange_list(self, timeout=None):
"""Gets the list of exchanges currently active."""
endpoint = self.TRADER_ENDPOINT + 'api/common/getActiveExchangeList'
r = self._post(endpoint, timeout=timeout)
endpoint = self.TRADER_ENDPOINT + '/api/v1/common/ExchangeList'
r = self._get(endpoint, timeout=timeout)
return r.json()['data']

def add_market_order(self, strategy_id, exchange_id, base_currency, quote_currency, side, amount,
leverage=1, timeout=None):
"""Adds a market order for a specific strategy.
:param strategy_id: int
:param exchange_id: int
:param base_currency: e.g., 'BTC'
:param quote_currency: e.g., 'USDT'
:param side: 'BUY' or 'SELL'
:param amount: the amount to sell or buy
:param leverage: Bitmex exchange leverage. This parameter is used only on Bitmex exchange and can be set
to `None` if using other exchanges.
:param timeout: request timeout
:return: Signal id or None if no order was created.
"""
endpoint = self.TRADER_ENDPOINT + 'api/order/addOrder'
payload = {
'strategyId': strategy_id,
'exchangeId': exchange_id,
'baseCurrency': base_currency,
'quoteCurrency': quote_currency,
'orderSide': side,
'unit': amount,
'leverage': leverage,
'orderType': 'MARKET',
}
r = self._post(endpoint, json=payload, timeout=timeout)
if r.status_code == 200:
return r.json()['data']['signalId']
else:
return None

def get_history_list(self, strategy_id, begin_time, end_time, account_type='real', timeout=None):
def get_history_list(self, strategy_id, startDate, endDate, timeout=None):
"""Gets the historical data of all transactions.
:param strategy_id: int
:param begin_time: datetime object. If no tzinfo is provided, defaults to local timezone.
:param end_time: datetime object. If no tzinfo is provided, defaults to local timezone.
:param account_type: 'real' or 'virtual'
:param startDate: str, YYYY-MM-DD
:param endDate: str, YYYY-MM-DD
:param timeout: request timeout
:return: history list
"""
local_tz = tzlocal.get_localzone()
if begin_time.tzinfo is None:
begin_time = begin_time.astimezone(local_tz)
if end_time.tzinfo is None:
end_time = end_time.astimezone(local_tz)

convert_start_time = begin_time.isoformat()
convert_end_time = end_time.isoformat()
endpoint = self.TRADER_ENDPOINT + 'api/order/getHistoryList'
payload = {
'strategyId': strategy_id,
'beginTime': convert_start_time,
'endTime': convert_end_time,
'accountType': account_type,
}
r = self._post(endpoint, json=payload, timeout=timeout)
endpoint = self.TRADER_ENDPOINT + '/api/v1/user/OrderList?strategyId={}&startDate={}&endDate={}'.format(strategy_id, startDate, endDate)
r = self._get(endpoint, timeout=timeout)
return r.json()['data']

def add_api_key(self, api_key, api_secret_key, exchange_id, timeout=None):
def add_api_key(self, api_key, api_secret_key, strategyId, timeout=None):
"""Adds all the keys of each transaction."""
endpoint = self.TRADER_ENDPOINT + 'api/member/addApiKey'
endpoint = self.TRADER_ENDPOINT + '/api/v1/user/ApiKey'
payload = {
'apiKey': api_key,
'secretKey': str(self.subkey.encrypt(pgpy.PGPMessage.new(api_secret_key))),
'exchangeId': exchange_id,
'apiNickname': '',
'fixApiAssign': True,
'strategyId': strategyId,
}
self._post(endpoint, json=payload, timeout=timeout)
return True
r = self._post(endpoint, json=payload, timeout=timeout)
return r.json()['data']

def get_api_key_list(self, timeout=None):
"""Gets API key list.
"""
endpoint = self.TRADER_ENDPOINT + 'api/member/getApiKeyList'
r = self._post(endpoint, timeout=timeout)
endpoint = self.TRADER_ENDPOINT + '/api/v1/user/ApiKey'
r = self._get(endpoint, timeout=timeout)
return r.json()['data']

def delete_api_key(self, exchange_id, timeout=None):
def delete_api_key(self, strategyId, timeout=None):
"""Deletes all the keys of a transaction.
:param exchange_id: int
:param strategyId: int
:param timeout: request timeout
:return: True if api key is deleted.
"""
endpoint = self.TRADER_ENDPOINT + 'api/member/deleteApiKey'
payload = {
'exchangeId': exchange_id
}
self._post(endpoint, json=payload, timeout=timeout)
return True

endpoint = self.TRADER_ENDPOINT + '/api/v1/user/ApiKey?strategyId={}'.format(strategyId)
r = self._delete(endpoint, timeout=timeout)
return r.json()['data']
'''
def get_account_asset(self, strategy_id, account_type='virtual', timeout=None):
"""Get account asset.
:param strategy_id: int
:param account_type: 'real' or 'virtual'
:param timeout: request timeout
:return: account asset
"""

endpoint = self.TRADER_ENDPOINT + 'api/trader/getAccountAsset'
payload = {
'strategyId': strategy_id,
'accountType': account_type,
}
r = self._post(endpoint, json=payload, timeout=timeout)
return r.json()['data']

'''
def get_exchange_symbol_list(self, exchange_id, timeout=None):
"""Get exchange symbol list.
:param exchange_id: int
:param timeout: request timeout
:return: exchange symbol list
"""

endpoint = self.TRADER_ENDPOINT + 'api/common/getExchangeSymbolList'
payload = {
"exchangeId": exchange_id,
}
r = self._post(endpoint, json=payload, timeout=timeout)
endpoint = self.TRADER_ENDPOINT + '/api/v1/common/SymbolList?exchangeId={}'.format(exchange_id)
r = self._get(endpoint, timeout=timeout)
return r.json()['data']

'''
def get_currency_list(self, timeout=None):
"""Get exchange symbol list.
:param exchange_id: int
:param timeout: request timeout
:return: exchange symbol list
"""

endpoint = self.TRADER_ENDPOINT + 'api/common/getCurrencyList'
r = self._post(endpoint, timeout=timeout)
return r.json()['data']

'''
def set_position(self, strategy_id, Ratio, LimitPrice, timeout=None):
"""Adds a market order for a specific strategy.
:param strategy_id: int
:param Ratio: float
:param LimitPrice: float
:return: Signal id or None if no order was created.
"""
endpoint = self.TRADER_ENDPOINT + '/api/v1/strategy/SetPosition'
payload = {
'strategyId': strategy_id,
'Ratio': Ratio,
'LimitPrice': LimitPrice
}
r = self._post(endpoint, json=payload, timeout=timeout)
if r.status_code == 200:
return r.json()['data']['id']
else:
return None