From 422ee7ec8bc604c54394ab4e58838fe213c9ff06 Mon Sep 17 00:00:00 2001 From: 007live Date: Sun, 28 Jan 2024 19:50:13 +0800 Subject: [PATCH] feat:Support multiply trigger TP --- okx/Account.py | 8 +- okx/BlockTrading.py | 6 +- okx/Convert.py | 6 +- okx/CopyTrading.py | 8 +- okx/Earning.py | 6 +- okx/FDBroker.py | 6 +- okx/Funding.py | 6 +- okx/Grid.py | 6 +- okx/MarketData.py | 6 +- okx/NDBroker.py | 6 +- okx/PublicData.py | 6 +- okx/SpreadTrading.py | 6 +- okx/Status.py | 6 +- okx/SubAccount.py | 6 +- okx/Trade.py | 86 +++++----- okx/TradingData.py | 6 +- okx/__init__.py | 2 +- okx/{client.py => okxclient.py} | 4 +- test/AccountTest.py | 134 ++++++++-------- test/TradeTest.py | 272 +++++++++++++++++++------------- 20 files changed, 331 insertions(+), 261 deletions(-) rename okx/{client.py => okxclient.py} (95%) diff --git a/okx/Account.py b/okx/Account.py index 589d94a..644c3cf 100644 --- a/okx/Account.py +++ b/okx/Account.py @@ -1,11 +1,11 @@ -from .client import Client +from .okxclient import OkxClient from .consts import * -class AccountAPI(Client): +class AccountAPI(OkxClient): - def __init__(self, api_key='-1', api_secret_key='-1', passphrase='-1', use_server_time=False, flag='1', domain = 'https://www.okx.com',debug = True,proxy = None,timeout = 30): - Client.__init__(self, api_key, api_secret_key, passphrase, use_server_time, flag, domain, debug,proxy,timeout) + def __init__(self, api_key='-1', api_secret_key='-1', passphrase='-1', use_server_time=False, flag='1', domain = 'https://www.okx.com',debug = True,proxy = None): + OkxClient.__init__(self, api_key, api_secret_key, passphrase, use_server_time, flag, domain, debug, proxy) # Get Positions def get_position_risk(self, instType=''): diff --git a/okx/BlockTrading.py b/okx/BlockTrading.py index b2b8d65..49646f5 100644 --- a/okx/BlockTrading.py +++ b/okx/BlockTrading.py @@ -1,10 +1,10 @@ -from .client import Client +from .okxclient import OkxClient from .consts import * -class BlockTradingAPI(Client): +class BlockTradingAPI(OkxClient): def __init__(self, api_key='-1', api_secret_key='-1', passphrase='-1', use_server_time=False, flag='1', domain = 'https://www.okx.com',debug = True, proxy=None): - Client.__init__(self, api_key, api_secret_key, passphrase, use_server_time, flag, domain, debug,proxy) + OkxClient.__init__(self, api_key, api_secret_key, passphrase, use_server_time, flag, domain, debug, proxy) def counterparties(self): params = {} diff --git a/okx/Convert.py b/okx/Convert.py index 8e996fb..2e57252 100644 --- a/okx/Convert.py +++ b/okx/Convert.py @@ -1,10 +1,10 @@ -from .client import Client +from .okxclient import OkxClient from .consts import * -class ConvertAPI(Client): +class ConvertAPI(OkxClient): def __init__(self, api_key='-1', api_secret_key='-1', passphrase='-1', use_server_time=False, flag='1', domain = 'https://www.okx.com',debug = True,proxy = None): - Client.__init__(self, api_key, api_secret_key, passphrase, use_server_time, flag, domain, debug,proxy) + OkxClient.__init__(self, api_key, api_secret_key, passphrase, use_server_time, flag, domain, debug, proxy) def get_currencies(self): params = {} diff --git a/okx/CopyTrading.py b/okx/CopyTrading.py index d2047c4..3d6725b 100644 --- a/okx/CopyTrading.py +++ b/okx/CopyTrading.py @@ -1,12 +1,12 @@ -from .client import Client +from .okxclient import OkxClient from .consts import * -class CopyTradingAPI(Client): +class CopyTradingAPI(OkxClient): def __init__(self, api_key='-1', api_secret_key='-1', passphrase='-1', use_server_time=False, flag='1', domain='https://www.okx.com', debug=True, proxy=None): - Client.__init__(self, api_key, api_secret_key, passphrase, use_server_time, flag, domain, debug, - proxy=proxy) + OkxClient.__init__(self, api_key, api_secret_key, passphrase, use_server_time, flag, domain, debug, + proxy=proxy) # Get existing leading positions def get_existing_leading_positions(self, instId=''): diff --git a/okx/Earning.py b/okx/Earning.py index ce913a2..963e13f 100644 --- a/okx/Earning.py +++ b/okx/Earning.py @@ -1,10 +1,10 @@ -from .client import Client +from .okxclient import OkxClient from .consts import * -class EarningAPI(Client): +class EarningAPI(OkxClient): def __init__(self, api_key='-1', api_secret_key='-1', passphrase='-1', use_server_time=False, flag='1', domain = 'https://www.okx.com',debug = True, proxy=None): - Client.__init__(self, api_key, api_secret_key, passphrase, use_server_time, flag, domain, debug,proxy) + OkxClient.__init__(self, api_key, api_secret_key, passphrase, use_server_time, flag, domain, debug, proxy) def get_offers(self,productId = '',protocolType = '',ccy = ''): params = { diff --git a/okx/FDBroker.py b/okx/FDBroker.py index 3af8202..f1f220c 100644 --- a/okx/FDBroker.py +++ b/okx/FDBroker.py @@ -1,10 +1,10 @@ -from .client import Client +from .okxclient import OkxClient from .consts import * -class FDBrokerAPI(Client): +class FDBrokerAPI(OkxClient): def __init__(self, api_key='-1', api_secret_key='-1', passphrase='-1', use_server_time=False, flag='1', domain = 'https://www.okx.com',debug = True, proxy=None): - Client.__init__(self, api_key, api_secret_key, passphrase, use_server_time, flag, domain, debug,proxy) + OkxClient.__init__(self, api_key, api_secret_key, passphrase, use_server_time, flag, domain, debug, proxy) def generate_rebate_details_download_link(self, begin ='', end = ''): params = {'begin': begin, 'end': end} diff --git a/okx/Funding.py b/okx/Funding.py index 79b2a61..4f2e905 100644 --- a/okx/Funding.py +++ b/okx/Funding.py @@ -1,12 +1,12 @@ -from .client import Client +from .okxclient import OkxClient from .consts import * -class FundingAPI(Client): +class FundingAPI(OkxClient): def __init__(self, api_key='-1', api_secret_key='-1', passphrase='-1', use_server_time=False, flag='1', domain = 'https://www.okx.com',debug = True, proxy=None): - Client.__init__(self, api_key, api_secret_key, passphrase, use_server_time, flag, domain, debug,proxy) + OkxClient.__init__(self, api_key, api_secret_key, passphrase, use_server_time, flag, domain, debug, proxy) # Get Deposit Address def get_deposit_address(self, ccy): diff --git a/okx/Grid.py b/okx/Grid.py index d2ca9c5..b09e8d5 100644 --- a/okx/Grid.py +++ b/okx/Grid.py @@ -1,10 +1,10 @@ -from .client import Client +from .okxclient import OkxClient from .consts import * -class GridAPI(Client): +class GridAPI(OkxClient): def __init__(self, api_key='-1', api_secret_key='-1', passphrase='-1', use_server_time=False, flag='1', domain = 'https://www.okx.com',debug = True, proxy=None): - Client.__init__(self, api_key, api_secret_key, passphrase, use_server_time, flag, domain, debug,proxy) + OkxClient.__init__(self, api_key, api_secret_key, passphrase, use_server_time, flag, domain, debug, proxy) def grid_order_algo(self, instId='', algoOrdType='', maxPx='', minPx='', gridNum='', runType='', tpTriggerPx='', slTriggerPx='', tag='', quoteSz='', baseSz='', sz='', direction='', lever='', basePos=''): diff --git a/okx/MarketData.py b/okx/MarketData.py index e362139..72e822a 100644 --- a/okx/MarketData.py +++ b/okx/MarketData.py @@ -1,11 +1,11 @@ -from .client import Client +from .okxclient import OkxClient from .consts import * -class MarketAPI(Client): +class MarketAPI(OkxClient): def __init__(self, api_key='-1', api_secret_key='-1', passphrase='-1', use_server_time=False, flag='1', domain = 'https://www.okx.com',debug = True, proxy=None): - Client.__init__(self, api_key, api_secret_key, passphrase, use_server_time, flag, domain, debug,proxy) + OkxClient.__init__(self, api_key, api_secret_key, passphrase, use_server_time, flag, domain, debug, proxy) # Get Tickers diff --git a/okx/NDBroker.py b/okx/NDBroker.py index 2fc6f20..1df88de 100644 --- a/okx/NDBroker.py +++ b/okx/NDBroker.py @@ -1,8 +1,8 @@ -from .client import Client +from .okxclient import OkxClient from .consts import * -class NDBrokerAPI(Client): +class NDBrokerAPI(OkxClient): def __init__(self, api_key='-1', api_secret_key='-1', passphrase='-1', use_server_time=False, flag='1', domain = 'https://www.okx.com',debug = True, proxy=None): - Client.__init__(self, api_key, api_secret_key, passphrase, use_server_time, flag, domain, debug,proxy) + OkxClient.__init__(self, api_key, api_secret_key, passphrase, use_server_time, flag, domain, debug, proxy) #GET /api/v5/broker/nd/info def get_broker_info(self): diff --git a/okx/PublicData.py b/okx/PublicData.py index 80268ae..214a565 100644 --- a/okx/PublicData.py +++ b/okx/PublicData.py @@ -1,11 +1,11 @@ -from .client import Client +from .okxclient import OkxClient from .consts import * -class PublicAPI(Client): +class PublicAPI(OkxClient): def __init__(self, api_key='-1', api_secret_key='-1', passphrase='-1', use_server_time=False, flag='1', domain = 'https://www.okx.com',debug = True, proxy=None): - Client.__init__(self, api_key, api_secret_key, passphrase, use_server_time, flag, domain, debug,proxy) + OkxClient.__init__(self, api_key, api_secret_key, passphrase, use_server_time, flag, domain, debug, proxy) # Get Instruments def get_instruments(self, instType, uly='', instId='',instFamily = ''): diff --git a/okx/SpreadTrading.py b/okx/SpreadTrading.py index eb7508b..18a4033 100644 --- a/okx/SpreadTrading.py +++ b/okx/SpreadTrading.py @@ -1,11 +1,11 @@ -from .client import Client +from .okxclient import OkxClient from .consts import * -class SpreadTradingAPI(Client): +class SpreadTradingAPI(OkxClient): def __init__(self, api_key='-1', api_secret_key='-1', passphrase='-1', use_server_time=False, flag='1', domain = 'https://www.okx.com',debug = True, proxy=None): - Client.__init__(self, api_key, api_secret_key, passphrase, use_server_time, flag, domain, debug,proxy) + OkxClient.__init__(self, api_key, api_secret_key, passphrase, use_server_time, flag, domain, debug, proxy) # Place Order def place_order(self, sprdId='', clOrdId='', tag='', side='', ordType='', sz='', px=''): diff --git a/okx/Status.py b/okx/Status.py index 8e62f1e..7281de9 100644 --- a/okx/Status.py +++ b/okx/Status.py @@ -1,10 +1,10 @@ -from .client import Client +from .okxclient import OkxClient from .consts import * -class StatusAPI(Client): +class StatusAPI(OkxClient): def __init__(self, api_key='-1', api_secret_key='-1', passphrase='-1', use_server_time=False, flag='1', domain = 'https://www.okx.com',debug = True, proxy=None): - Client.__init__(self, api_key, api_secret_key, passphrase, use_server_time, flag, domain, debug,proxy) + OkxClient.__init__(self, api_key, api_secret_key, passphrase, use_server_time, flag, domain, debug, proxy) def status(self, state=''): params = {'state': state} diff --git a/okx/SubAccount.py b/okx/SubAccount.py index d152104..08aa3c0 100644 --- a/okx/SubAccount.py +++ b/okx/SubAccount.py @@ -1,10 +1,10 @@ -from .client import Client +from .okxclient import OkxClient from .consts import * -class SubAccountAPI(Client): +class SubAccountAPI(OkxClient): def __init__(self, api_key='-1', api_secret_key='-1', passphrase='-1', use_server_time=False, flag='1', domain = 'https://www.okx.com',debug = True, proxy=None): - Client.__init__(self, api_key, api_secret_key, passphrase, use_server_time, flag, domain, debug,proxy) + OkxClient.__init__(self, api_key, api_secret_key, passphrase, use_server_time, flag, domain, debug, proxy) def get_account_balance(self, subAcct): params = {"subAcct": subAcct} diff --git a/okx/Trade.py b/okx/Trade.py index 456635a..504b463 100644 --- a/okx/Trade.py +++ b/okx/Trade.py @@ -1,21 +1,26 @@ -from .client import Client +import json + +from .okxclient import OkxClient from .consts import * -class TradeAPI(Client): +class TradeAPI(OkxClient): - def __init__(self, api_key='-1', api_secret_key='-1', passphrase='-1', use_server_time=False, flag='1', domain = 'https://www.okx.com',debug = True, proxy=None): - Client.__init__(self, api_key, api_secret_key, passphrase, use_server_time, flag, domain, debug,proxy) + def __init__(self, api_key='-1', api_secret_key='-1', passphrase='-1', use_server_time=False, flag='1', + domain='https://www.okx.com', debug=True, proxy=None): + OkxClient.__init__(self, api_key, api_secret_key, passphrase, use_server_time, flag, domain, debug, proxy) # Place Order def place_order(self, instId, tdMode, side, ordType, sz, ccy='', clOrdId='', tag='', posSide='', px='', reduceOnly='', tgtCcy='', tpTriggerPx='', tpOrdPx='', slTriggerPx='', slOrdPx='', - tpTriggerPxType='', slTriggerPxType='', quickMgnType='', stpId='', stpMode=''): + tpTriggerPxType='', slTriggerPxType='', quickMgnType='', stpId='', stpMode='', + attachAlgoOrds=None): params = {'instId': instId, 'tdMode': tdMode, 'side': side, 'ordType': ordType, 'sz': sz, 'ccy': ccy, 'clOrdId': clOrdId, 'tag': tag, 'posSide': posSide, 'px': px, 'reduceOnly': reduceOnly, 'tgtCcy': tgtCcy, 'tpTriggerPx': tpTriggerPx, 'tpOrdPx': tpOrdPx, 'slTriggerPx': slTriggerPx, 'slOrdPx': slOrdPx, 'tpTriggerPxType': tpTriggerPxType, 'slTriggerPxType': slTriggerPxType, 'quickMgnType': quickMgnType, 'stpId': stpId, 'stpMode': stpMode} + params['attachAlgoOrds'] = attachAlgoOrds return self._request_with_params(POST, PLACR_ORDER, params) # Place Multiple Orders @@ -33,11 +38,13 @@ class TradeAPI(Client): # Amend Order def amend_order(self, instId, cxlOnFail='', ordId='', clOrdId='', reqId='', newSz='', newPx='', newTpTriggerPx='', - newTpOrdPx='', newSlTriggerPx='', newSlOrdPx='', newTpTriggerPxType='', newSlTriggerPxType=''): + newTpOrdPx='', newSlTriggerPx='', newSlOrdPx='', newTpTriggerPxType='', newSlTriggerPxType='', + attachAlgoOrds=''): params = {'instId': instId, 'cxlOnFailc': cxlOnFail, 'ordId': ordId, 'clOrdId': clOrdId, 'reqId': reqId, 'newSz': newSz, 'newPx': newPx, 'newTpTriggerPx': newTpTriggerPx, 'newTpOrdPx': newTpOrdPx, 'newSlTriggerPx': newSlTriggerPx, 'newSlOrdPx': newSlOrdPx, 'newTpTriggerPxType': newTpTriggerPxType, 'newSlTriggerPxType': newSlTriggerPxType} + params['attachAlgoOrds'] = attachAlgoOrds return self._request_with_params(POST, AMEND_ORDER, params) # Amend Multiple Orders @@ -56,9 +63,10 @@ class TradeAPI(Client): return self._request_with_params(GET, ORDER_INFO, params) # Get Order List - def get_order_list(self, instType='', uly='', instId='', ordType='', state='', after='', before='', limit='',instFamily = ''): + def get_order_list(self, instType='', uly='', instId='', ordType='', state='', after='', before='', limit='', + instFamily=''): params = {'instType': instType, 'uly': uly, 'instId': instId, 'ordType': ordType, 'state': state, - 'after': after, 'before': before, 'limit': limit,'instFamily':instFamily} + 'after': after, 'before': before, 'limit': limit, 'instFamily': instFamily} return self._request_with_params(GET, ORDERS_PENDING, params) # Get Order History (last 7 days) @@ -78,9 +86,9 @@ class TradeAPI(Client): return self._request_with_params(GET, ORDERS_HISTORY_ARCHIVE, params) # Get Transaction Details - def get_fills(self, instType='', uly='', instId='', ordId='', after='', before='', limit='',instFamily = ''): + def get_fills(self, instType='', uly='', instId='', ordId='', after='', before='', limit='', instFamily=''): params = {'instType': instType, 'uly': uly, 'instId': instId, 'ordId': ordId, 'after': after, 'before': before, - 'limit': limit,'instFamily':instFamily} + 'limit': limit, 'instFamily': instFamily} return self._request_with_params(GET, ORDER_FILLS, params) # Place Algo Order @@ -90,16 +98,17 @@ class TradeAPI(Client): triggerPx='', orderPx='', tgtCcy='', pxVar='', pxSpread='', szLimit='', pxLimit='', timeInterval='', tpTriggerPxType='', slTriggerPxType='', - callbackRatio='',callbackSpread='',activePx='',tag='',triggerPxType='',closeFraction='' - ,quickMgnType='',algoClOrdId=''): + callbackRatio='', callbackSpread='', activePx='', tag='', triggerPxType='', closeFraction='' + , quickMgnType='', algoClOrdId=''): params = {'instId': instId, 'tdMode': tdMode, 'side': side, 'ordType': ordType, 'sz': sz, 'ccy': ccy, 'posSide': posSide, 'reduceOnly': reduceOnly, 'tpTriggerPx': tpTriggerPx, 'tpOrdPx': tpOrdPx, 'slTriggerPx': slTriggerPx, 'slOrdPx': slOrdPx, 'triggerPx': triggerPx, 'orderPx': orderPx, 'tgtCcy': tgtCcy, 'pxVar': pxVar, 'szLimit': szLimit, 'pxLimit': pxLimit, 'timeInterval': timeInterval, 'pxSpread': pxSpread, 'tpTriggerPxType': tpTriggerPxType, 'slTriggerPxType': slTriggerPxType, - 'callbackRatio' : callbackRatio, 'callbackSpread':callbackSpread,'activePx':activePx, - 'tag':tag,'triggerPxType':triggerPxType,'closeFraction':closeFraction,'quickMgnType':quickMgnType,'algoClOrdId':algoClOrdId} + 'callbackRatio': callbackRatio, 'callbackSpread': callbackSpread, 'activePx': activePx, + 'tag': tag, 'triggerPxType': triggerPxType, 'closeFraction': closeFraction, + 'quickMgnType': quickMgnType, 'algoClOrdId': algoClOrdId} return self._request_with_params(POST, PLACE_ALGO_ORDER, params) # Cancel Algo Order @@ -107,11 +116,12 @@ class TradeAPI(Client): return self._request_with_params(POST, CANCEL_ALGOS, params) # Cancel Advance Algos - def cancel_advance_algos(self,params): + def cancel_advance_algos(self, params): return self._request_with_params(POST, Cancel_Advance_Algos, params) # Get Algo Order List - def order_algos_list(self, ordType='', algoId='', instType='', instId='', after='', before='', limit='',algoClOrdId=''): + def order_algos_list(self, ordType='', algoId='', instType='', instId='', after='', before='', limit='', + algoClOrdId=''): params = {'ordType': ordType, 'algoId': algoId, 'instType': instType, 'instId': instId, 'after': after, 'before': before, 'limit': limit, 'algoClOrdId': algoClOrdId} return self._request_with_params(GET, ORDERS_ALGO_OENDING, params) @@ -123,49 +133,49 @@ class TradeAPI(Client): return self._request_with_params(GET, ORDERS_ALGO_HISTORY, params) # Get Transaction Details History - def get_fills_history(self, instType, uly='', instId='', ordId='', after='', before='', limit='',instFamily=''): + def get_fills_history(self, instType, uly='', instId='', ordId='', after='', before='', limit='', instFamily=''): params = {'instType': instType, 'uly': uly, 'instId': instId, 'ordId': ordId, 'after': after, 'before': before, - 'limit': limit,'instFamily':instFamily} + 'limit': limit, 'instFamily': instFamily} return self._request_with_params(GET, ORDERS_FILLS_HISTORY, params) def get_easy_convert_currency_list(self): return self._request_without_params(GET, EASY_CONVERT_CURRENCY_LIST) - def easy_convert(self,fromCcy = [],toCcy = ''): + def easy_convert(self, fromCcy=[], toCcy=''): params = { - 'fromCcy':fromCcy, - 'toCcy':toCcy + 'fromCcy': fromCcy, + 'toCcy': toCcy } return self._request_with_params(POST, EASY_CONVERT, params) - def get_easy_convert_history(self,before = '',after = '',limit = ''): + def get_easy_convert_history(self, before='', after='', limit=''): params = { - 'before':before, - 'after':after, - 'limit':limit + 'before': before, + 'after': after, + 'limit': limit } - return self._request_with_params(GET,CONVERT_EASY_HISTORY,params) + return self._request_with_params(GET, CONVERT_EASY_HISTORY, params) - def get_oneclick_repay_list(self,debtType = ''): + def get_oneclick_repay_list(self, debtType=''): params = { - 'debtType':debtType + 'debtType': debtType } - return self._request_with_params(GET,ONE_CLICK_REPAY_SUPPORT,params) + return self._request_with_params(GET, ONE_CLICK_REPAY_SUPPORT, params) - def oneclick_repay(self,debtCcy = [] , repayCcy=''): + def oneclick_repay(self, debtCcy=[], repayCcy=''): params = { - 'debtCcy':debtCcy, - 'repayCcy':repayCcy + 'debtCcy': debtCcy, + 'repayCcy': repayCcy } - return self._request_with_params(POST,ONE_CLICK_REPAY,params) + return self._request_with_params(POST, ONE_CLICK_REPAY, params) - def oneclick_repay_history(self,after = '',before = '',limit = ''): + def oneclick_repay_history(self, after='', before='', limit=''): params = { - 'after':after, - 'before':before, - 'limit':limit + 'after': after, + 'before': before, + 'limit': limit } - return self._request_with_params(GET,ONE_CLICK_REPAY_HISTORY,params) + return self._request_with_params(GET, ONE_CLICK_REPAY_HISTORY, params) # Get algo order details def get_algo_order_details(self, algoId='', algoClOrdId=''): diff --git a/okx/TradingData.py b/okx/TradingData.py index f507930..bda2b4f 100644 --- a/okx/TradingData.py +++ b/okx/TradingData.py @@ -1,11 +1,11 @@ -from .client import Client +from .okxclient import OkxClient from .consts import * -class TradingDataAPI(Client): +class TradingDataAPI(OkxClient): def __init__(self, api_key='-1', api_secret_key='-1', passphrase='-1', use_server_time=False, flag='1', domain = 'https://www.okx.com',debug = True, proxy=None): - Client.__init__(self, api_key, api_secret_key, passphrase, use_server_time, flag, domain, debug,proxy) + OkxClient.__init__(self, api_key, api_secret_key, passphrase, use_server_time, flag, domain, debug, proxy) def get_support_coin(self): diff --git a/okx/__init__.py b/okx/__init__.py index 8ba55b8..ec4b7fd 100644 --- a/okx/__init__.py +++ b/okx/__init__.py @@ -2,4 +2,4 @@ Python SDK for the OKX API v5 """ -__version__="0.2.6" \ No newline at end of file +__version__="0.2.7" \ No newline at end of file diff --git a/okx/client.py b/okx/okxclient.py similarity index 95% rename from okx/client.py rename to okx/okxclient.py index 8c20307..ae8d6d9 100644 --- a/okx/client.py +++ b/okx/okxclient.py @@ -6,10 +6,10 @@ from httpx import Client from . import consts as c, utils, exceptions -class Client(Client): +class OkxClient(Client): def __init__(self, api_key='-1', api_secret_key='-1', passphrase='-1', use_server_time=False, flag='1',base_api=c.API_URL, debug='True', proxy=None): - super().__init__(base_url=base_api, http2=True, proxy= proxy) + super().__init__(base_url=base_api, http2=True, proxy=proxy) self.API_KEY = api_key self.API_SECRET_KEY = api_secret_key self.PASSPHRASE = passphrase diff --git a/test/AccountTest.py b/test/AccountTest.py index bc43605..0f75f90 100644 --- a/test/AccountTest.py +++ b/test/AccountTest.py @@ -1,78 +1,80 @@ - import unittest + from okx import Account + class AccountTest(unittest.TestCase): def setUp(self): - proxy = "http://125.106.203.126:10423"; - api_key = 'aee24256-7393-422f-90d9-47cc4028ce39' - api_secret_key = 'FF517E627F6A702E2BF50F311F245D2A' - passphrase = 'hylHYL950525,.' - self.AccountAPI = Account.AccountAPI(api_key, api_secret_key, passphrase, use_server_time=False, flag='1',proxy=proxy,timeout=30) + proxy = "http://43.152.113.72:19206" + api_key = 'your_apiKey' + api_secret_key = 'your_secretKey' + passphrase = 'your_secretKey' + self.AccountAPI = Account.AccountAPI(api_key, api_secret_key, passphrase, use_server_time=False, flag='1', proxy=proxy) + # ''' - POSITIONS_HISTORY = '/api/v5/account/positions-history' #need add - GET_PM_LIMIT = '/api/v5/account/position-tiers' #need add - ACCOUNT_RISK = '/api/v5/account/risk-state' #need add + # POSITIONS_HISTORY = '/api/v5/account/positions-history' #need add + # GET_PM_LIMIT = '/api/v5/account/position-tiers' #need add + # ACCOUNT_RISK = '/api/v5/account/risk-state' #need add # def test_account_risk(self): # print(self.AccountAPI.get_account_risk()) # # def test_get_pm_limit(self): # print(self.AccountAPI.get_pm_limit("SWAP","BTC-USDT")) - #positions-history + # positions-history def test_get_positions_history(self): print(self.AccountAPI.get_positions_history()) - def test_get_user_config(self): - print(self.AccountAPI.get_account_config()) - def test_get_positions(self): - print(self.AccountAPI.get_positions("SWAP")) - def test_get_balance(self): - print(self.AccountAPI.get_account()) - def test_get_positions_risk(self): - print(self.AccountAPI.get_position_risk("SWAP")) - def test_get_bills(self): - print(self.AccountAPI.get_bills_detail()) - - def test_get_bills_arch(self): - print(self.AccountAPI.get_bills_details()) - def test_set_position_mode(self): - print(self.AccountAPI.set_position_mode("long_short_mode")) - def test_set_leverage(self): - print(self.AccountAPI.set_leverage(instId="BTC-USDT",lever="5",mgnMode="isolated")) - def test_get_max_avaliable_size(self): - print(self.AccountAPI.get_max_avail_size(instId="BTC-USDT",tdMode="cash")) - def test_get_max_size(self): - print(self.AccountAPI.get_maximum_trade_size(instId="BTC-USDT",tdMode="cash")) - def test_get_positions(self): - print(self.AccountAPI.get_positions("MARGIN")) - def test_set_margin_balance(self): - print(self.AccountAPI.Adjustment_margin(instId="BTC-USDT",posSide="net",type="add",amt="1")) - def test_get_lev_info(self): - print(self.AccountAPI.get_leverage("BTC-USDT","cross")); - def test_get_max_loan(self): - print(self.AccountAPI.get_max_loan("BTC-USDT","cross","USDT")) - def test_get_trade_fee(self): - print(self.AccountAPI.get_fee_rates("SPOT")) - def test_get_insterested_accrued(self): - print(self.AccountAPI.get_interest_accrued()) - def test_get_interestred_rate(self): - print(self.AccountAPI.get_interest_rate()) - def test_set_greeks(self): - print(self.AccountAPI.set_greeks("BS")) - - def test_set_isolated_mode(self): - print(self.AccountAPI.set_isolated_mode("automatic","MARGIN")) - def test_set_max_withdraw(self): - print(self.AccountAPI.get_max_withdrawal("USDT")) - def test_borrow_repay(self): - print(self.AccountAPI.borrow_repay("BTC","borrow","1.0")) - def test_borrow_repay_history(self): - print(self.AccountAPI.get_borrow_repay_history()) - def test_get_interest_limits(self): - print(self.AccountAPI.get_interest_limits()) - def test_simulated_margin(self): - print(self.AccountAPI.get_simulated_margin()) - def test_get_greeks(self): - print(self.AccountAPI.get_greeks()) + # def test_get_user_config(self): + # print(self.AccountAPI.get_account_config()) + # def test_get_positions(self): + # print(self.AccountAPI.get_positions("SWAP")) + # def test_get_balance(self): + # print(self.AccountAPI.get_account()) + # def test_get_positions_risk(self): + # print(self.AccountAPI.get_position_risk("SWAP")) + # def test_get_bills(self): + # print(self.AccountAPI.get_bills_detail()) + # + # def test_get_bills_arch(self): + # print(self.AccountAPI.get_bills_details()) + # def test_set_position_mode(self): + # print(self.AccountAPI.set_position_mode("long_short_mode")) + # def test_set_leverage(self): + # print(self.AccountAPI.set_leverage(instId="BTC-USDT",lever="5",mgnMode="isolated")) + # def test_get_max_avaliable_size(self): + # print(self.AccountAPI.get_max_avail_size(instId="BTC-USDT",tdMode="cash")) + # def test_get_max_size(self): + # print(self.AccountAPI.get_maximum_trade_size(instId="BTC-USDT",tdMode="cash")) + # def test_get_positions(self): + # print(self.AccountAPI.get_positions("MARGIN")) + # def test_set_margin_balance(self): + # print(self.AccountAPI.Adjustment_margin(instId="BTC-USDT",posSide="net",type="add",amt="1")) + # def test_get_lev_info(self): + # print(self.AccountAPI.get_leverage("BTC-USDT","cross")); + # def test_get_max_loan(self): + # print(self.AccountAPI.get_max_loan("BTC-USDT","cross","USDT")) + # def test_get_trade_fee(self): + # print(self.AccountAPI.get_fee_rates("SPOT")) + # def test_get_insterested_accrued(self): + # print(self.AccountAPI.get_interest_accrued()) + # def test_get_interestred_rate(self): + # print(self.AccountAPI.get_interest_rate()) + # def test_set_greeks(self): + # print(self.AccountAPI.set_greeks("BS")) + # + # def test_set_isolated_mode(self): + # print(self.AccountAPI.set_isolated_mode("automatic","MARGIN")) + # def test_set_max_withdraw(self): + # print(self.AccountAPI.get_max_withdrawal("USDT")) + # def test_borrow_repay(self): + # print(self.AccountAPI.borrow_repay("BTC","borrow","1.0")) + # def test_borrow_repay_history(self): + # print(self.AccountAPI.get_borrow_repay_history()) + # def test_get_interest_limits(self): + # print(self.AccountAPI.get_interest_limits()) + # def test_simulated_margin(self): + # print(self.AccountAPI.get_simulated_margin()) + # def test_get_greeks(self): + # print(self.AccountAPI.get_greeks()) # ''' # def test_simulated_margin(self): # print(self.AccountAPI.get_simulated_margin()) @@ -103,7 +105,9 @@ class AccountTest(unittest.TestCase): # def test_borrow_repay(self): # print(self.AccountAPI.borrow_repay("BTC", "borrow", "1.0")) - def test_simulated_margin(self): - print(self.AccountAPI.get_simulated_margin(spotOffsetType='3')) + # def test_simulated_margin(self): + # print(self.AccountAPI.get_simulated_margin(spotOffsetType='3')) + + if __name__ == '__main__': - unittest.main() \ No newline at end of file + unittest.main() diff --git a/test/TradeTest.py b/test/TradeTest.py index 53ea252..82487d9 100644 --- a/test/TradeTest.py +++ b/test/TradeTest.py @@ -1,117 +1,171 @@ import unittest + from okx import Trade + + class TradeTest(unittest.TestCase): def setUp(self): api_key = 'your_apiKey' api_secret_key = 'your_secretKey' passphrase = 'your_secretKey' self.tradeApi = Trade.TradeAPI(api_key, api_secret_key, passphrase, False, '1') - """ - def test_place_order(self): - print(self.tradeApi.place_order("BTC-USDT",tdMode="cross",clOrdId="asCai1",side="buy",ordType="limit",sz="0.01",px="18000")) - def test_cancel_order(self): - print(self.tradeApi.cancel_order(instId="ETH-USDT",ordId="480702180748558336")) - def test_batch_order(self): - orderData = [ { - "instId":"ETH-USDT", - "tdMode":"cross", - "clOrdId":"b151121", - "side":"buy", - "ordType":"limit", - "px":"2.15", - "sz":"2" - }, - { - "instId":"BTC-USDT", - "tdMode":"cross", - "clOrdId":"b152233", - "side":"buy", - "ordType":"limit", - "px":"2.15", - "sz":"2" - }] - print(self.tradeApi.place_multiple_orders(orderData)) - #480702180748558336 - def test_cancel_batch_orders(self): - data=[ - { - 'instId':"ETH-USDT", - 'ordId':"480702885353881600" - }, - { - 'instId':"BTC-USDT", - 'ordId':'480702885353881601' - } - ] - print(self.tradeApi.cancel_multiple_orders(data)) - def test_amend_order(self): - print(self.tradeApi.amend_order("BTC-USDT",ordId="480706017781743616",newSz="0.03")) - def test_amend_order_batch(self): - orderData = [ - { - 'instId':'ETH-USDT', - 'ordId':'480707205436669952', - 'newSz':'0.02' - }, - { - 'instId':'BTC-USDT', - 'ordId':'480707205436669953', - 'newPx':'3.0' - } - ] - print(self.tradeApi.amend_multiple_orders(orderData)) - def test_close_all_positions(self): - print(self.tradeApi.close_positions("BTC-USDT",mgnMode="cross")) - def test_get_order_info(self): - print(self.tradeApi.get_orders("ETH-USDT","480707205436669952")) - def test_get_order_pending(self): - print(self.tradeApi.get_order_list("SPOT")) - def test_get_order_history(self): - print(self.tradeApi.get_orders_history("SPOT")) - def test_get_order_histry_archive(self): - print(self.tradeApi.orders_history_archive("SPOT")) - def test_get_fills(self): - print(self.tradeApi.get_fills("SPOT")) - def test_get_fills_history(self): - print(self.tradeApi.get_fills_history("SPOT")) - def test_get_order_algo_pending(self): - print(self.tradeApi.order_algos_list('oco')) - def test_order_algo(self): - print(self.tradeApi.place_algo_order('BTC-USDT-SWAP', 'cross', side='buy', ordType='trigger', posSide='long', - sz='100', triggerPx='22000', triggerPxType ='index', orderPx='-1')) - def test_cancel_algos(self): - params = [{ - 'algoId': '485903392536264704', - 'instId': 'BTC-USDT-SWAP' - }] + # # """ + # def test_place_order(self): + # attachAlgoOrds = [{'tpTriggerPx': '49000.0', 'tpOrdPx': '-1', 'sz': '1', 'tpTriggerPxType': 'last'}]; + # print(self.tradeApi.place_order( + # "BTC-USDT-SWAP", tdMode="isolated", clOrdId="asCai1234", side='buy', posSide="long", ordType="limit", + # sz="1", + # px="30000.0", + # attachAlgoOrds=attachAlgoOrds) + # ); + # + # attachAlgoOrds = [{'slTriggerPx': '25000.0', 'slOrdPx': '-1', 'sz': '1', 'slTriggerPxType': 'last'}]; + # print(self.tradeApi.place_order( + # "BTC-USDT-SWAP", tdMode="isolated", clOrdId="asCai1234", side='buy', posSide="long", ordType="limit", + # sz="1", + # px="30000.0", + # attachAlgoOrds=attachAlgoOrds) + # ); + # + # attachAlgoOrds = [ + # { + # "tpTriggerPxType": "last", + # "tpOrdPx": "-1", + # "tpTriggerPx": "34000", + # "sz": "1" + # }, + # { + # "tpTriggerPxType": "last", + # "tpOrdPx": "-1", + # "tpTriggerPx": "35000", + # "sz": "1" + # }, + # { + # "slTriggerPxType": "last", + # "slOrdPx": "-1", + # "slTriggerPx": "20000", + # "sz": "3" + # } + # ] + # print(self.tradeApi.place_order( + # "BTC-USDT-SWAP", tdMode="isolated", clOrdId="asCai1234", side='buy', posSide="long", ordType="limit", + # sz="1", + # px="30000.0", + # attachAlgoOrds=attachAlgoOrds) + # ); + # def test_cancel_order(self): + # print(self.tradeApi.cancel_order(instId="ETH-USDT",ordId="480702180748558336")) - print(self.tradeApi.cancel_algo_order(params)) - def test_cancel_adv_algos(self): - params = [{ - 'algoId': '485936482235191296', - 'instId': 'BTC-USDT-SWAP' - }] + # def test_batch_order(self): + # orderData = [{ + # "instId": "BTC-USDT-SWAP", + # "tdMode": "isolated", + # "clOrdId": "b15112122", + # "side": "buy", + # "posSide": "long", + # "ordType": "limit", + # "px": "30000.0", + # "sz": "2", + # "attachAlgoOrds": [{'tpTriggerPx': '50000.0', 'tpOrdPx': '-1', 'sz': '1', 'tpTriggerPxType': 'last'}] + # }, + # { + # "instId": "BTC-USDT-SWAP", + # "tdMode": "isolated", + # "clOrdId": "b15112111", + # "side": "buy", + # "posSide": "long", + # "ordType": "limit", + # "px": "31000.0", + # "sz": "2", + # "attachAlgoOrds": [{'tpTriggerPx': '51000.0', 'tpOrdPx': '-1', 'sz': '1', 'tpTriggerPxType': 'last'}] + # } + # ] + # + # print(self.tradeApi.place_multiple_orders(orderData)) - print(self.tradeApi.cancel_advance_algos(params))) - def test_orders_algo_pending(self): - print(self.tradeApi.order_algos_list(ordType='iceberg')) - def test_algo_order_history(self): - print(self.tradeApi.order_algos_history(algoId='485903392536264704',ordType='conditional')) - def test_get_easy_convert_list(self): - print(self.tradeApi.get_easy_convert_currency_list()) - def test_easy_convert(self): - print(self.tradeApi.easy_convert(fromCcy=['BTC'],toCcy='OKB')) - def test_get_convert_history(self): - print(self.tradeApi.get_easy_convert_history()) - def test_get_oneclick_repay_support_list(self): - print(self.tradeApi.get_oneclick_repay_list('cross')) - def test_oneclick_repay(self): - print(self.tradeApi.oneclick_repay(['BTC'],'USDT')) -""" -#485903392536264704 - #485936482235191296 + # 480702180748558336 + # def test_cancel_batch_orders(self): + # data=[ + # { + # 'instId':"ETH-USDT", + # 'ordId':"480702885353881600" + # }, + # { + # 'instId':"BTC-USDT", + # 'ordId':'480702885353881601' + # } + # ] + # print(self.tradeApi.cancel_multiple_orders(data)) + # def test_amend_order(self): + # attachAlgoOrds = [{'attachAlgoId': '672081789170569217', 'newTpTriggerPx': '55000.0'}]; + # print(self.tradeApi.amend_order("BTC-USDT-SWAP", ordId="672081789170569216", newSz="1", + # attachAlgoOrds=attachAlgoOrds)) + + # def test_amend_order_batch(self): + # orderData = [ + # { + # 'instId': 'BTC-USDT-SWAP', + # 'ordId': '672081789170569216', + # 'newSz': '1', + # "attachAlgoOrds": [{'attachAlgoId': '672081789170569217', 'newTpTriggerPx': '53000.0'}] + # } + # ] + # + # print(self.tradeApi.amend_multiple_orders(orderData)) + + # def test_close_all_positions(self): + # print(self.tradeApi.close_positions("BTC-USDT",mgnMode="cross")) + # def test_get_order_info(self): + # print(self.tradeApi.get_orders("ETH-USDT","480707205436669952")) + # def test_get_order_pending(self): + # print(self.tradeApi.get_order_list("SPOT")) + # def test_get_order_history(self): + # print(self.tradeApi.get_orders_history("SPOT")) + # def test_get_order_histry_archive(self): + # print(self.tradeApi.orders_history_archive("SPOT")) + # def test_get_fills(self): + # print(self.tradeApi.get_fills("SPOT")) + # def test_get_fills_history(self): + # print(self.tradeApi.get_fills_history("SPOT")) + # def test_get_order_algo_pending(self): + # print(self.tradeApi.order_algos_list('oco')) + # def test_order_algo(self): + # print(self.tradeApi.place_algo_order('BTC-USDT-SWAP', 'cross', side='buy', ordType='trigger', posSide='long', + # sz='100', triggerPx='22000', triggerPxType ='index', orderPx='-1')) + # def test_cancel_algos(self): + # params = [{ + # 'algoId': '485903392536264704', + # 'instId': 'BTC-USDT-SWAP' + # }] + # + # + # print(self.tradeApi.cancel_algo_order(params)) + # def test_cancel_adv_algos(self): + # params = [{ + # 'algoId': '485936482235191296', + # 'instId': 'BTC-USDT-SWAP' + # }] + # + # print(self.tradeApi.cancel_advance_algos(params))) + # def test_orders_algo_pending(self): + # print(self.tradeApi.order_algos_list(ordType='iceberg')) + # def test_algo_order_history(self): + # print(self.tradeApi.order_algos_history(algoId='485903392536264704',ordType='conditional')) + # def test_get_easy_convert_list(self): + # print(self.tradeApi.get_easy_convert_currency_list()) + # def test_easy_convert(self): + # print(self.tradeApi.easy_convert(fromCcy=['BTC'],toCcy='OKB')) + # def test_get_convert_history(self): + # print(self.tradeApi.get_easy_convert_history()) + # def test_get_oneclick_repay_support_list(self): + # print(self.tradeApi.get_oneclick_repay_list('cross')) + # def test_oneclick_repay(self): + # print(self.tradeApi.oneclick_repay(['BTC'],'USDT')) + # """ + # 485903392536264704 + # 485936482235191296 # def test_oneclick_repay_history(self): # print(self.tradeApi.oneclick_repay_history()) # def test_order_algo(self): @@ -122,7 +176,7 @@ class TradeTest(unittest.TestCase): # def test_get_algo_order_details(self): # print(self.tradeApi.get_algo_order_details(algoId='581628185981308928')) - #581628185981308928 + # 581628185981308928 # def test_amend_algo_order(self): # print(self.tradeApi.amend_algo_order(instId='BTC-USDT-SWAP', algoId='581628185981308928',newSz='3')) @@ -155,8 +209,8 @@ class TradeTest(unittest.TestCase): # }] # print(self.tradeApi.place_multiple_orders(orderData)) - #581616258865516544 - #581616258865516545 + # 581616258865516544 + # 581616258865516545 # def test_amend_order(self): # print(self.tradeApi.amend_order("BTC-USDT", ordId="581616258865516544", newSz="0.03")) # def test_amend_order_batch(self): @@ -179,13 +233,15 @@ class TradeTest(unittest.TestCase): # print(self.tradeApi.place_algo_order(instId='BTC-USDT-SWAP', tdMode='cross', side='buy', ordType='conditional', \ # tpTriggerPx='15', tpOrdPx='18', sz='2',algoClOrdId='7678687',quickMgnType='manual')) - def test_order_algos_list(self): - print(self.tradeApi.order_algos_list(ordType='conditional')) + # def test_order_algos_list(self): + # print(self.tradeApi.order_algos_list(ordType='conditional')) # def test_order_algo(self): # print(self.tradeApi.place_order(instId='BTC-USDT-SWAP', tdMode='cross', side='buy',px='121',sz='2', # clOrdId='234234565535',ordType='market')) # def test_close_all_positions(self): # print(self.tradeApi.close_positions(instId="BTC-USDT-SWAP", mgnMode="cross",clOrdId='1213124')) -if __name__=='__main__': + + +if __name__ == '__main__': unittest.main()