From 068c63d5c5a7f2491c1f2a40bc253a9e2dd168bb Mon Sep 17 00:00:00 2001 From: 007live Date: Thu, 25 Jan 2024 15:23:13 +0800 Subject: [PATCH] feat: upgrade for proxy --- okx/Account.py | 4 ++-- okx/BlockTrading.py | 4 ++-- okx/Convert.py | 4 ++-- okx/CopyTrading.py | 8 +++++--- okx/Earning.py | 4 ++-- okx/FDBroker.py | 4 ++-- okx/Funding.py | 4 ++-- okx/Grid.py | 4 ++-- okx/MarketData.py | 4 ++-- okx/NDBroker.py | 4 ++-- okx/PublicData.py | 4 ++-- okx/SpreadTrading.py | 4 ++-- okx/Status.py | 4 ++-- okx/SubAccount.py | 4 ++-- okx/Trade.py | 4 ++-- okx/TradingData.py | 4 ++-- okx/client.py | 13 +++++++------ test/AccountTest.py | 23 ++++++++++++----------- 18 files changed, 54 insertions(+), 50 deletions(-) diff --git a/okx/Account.py b/okx/Account.py index e0a6d27..589d94a 100644 --- a/okx/Account.py +++ b/okx/Account.py @@ -4,8 +4,8 @@ from .consts import * class AccountAPI(Client): - 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): - Client.__init__(self, api_key, api_secret_key, passphrase, use_server_time, flag, domain,debug) + 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) # Get Positions def get_position_risk(self, instType=''): diff --git a/okx/BlockTrading.py b/okx/BlockTrading.py index 4ff22a1..b2b8d65 100644 --- a/okx/BlockTrading.py +++ b/okx/BlockTrading.py @@ -3,8 +3,8 @@ from .consts import * class BlockTradingAPI(Client): - 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): - Client.__init__(self, api_key, api_secret_key, passphrase, use_server_time, flag, domain,debug) + 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 counterparties(self): params = {} diff --git a/okx/Convert.py b/okx/Convert.py index 8d04f44..8e996fb 100644 --- a/okx/Convert.py +++ b/okx/Convert.py @@ -3,8 +3,8 @@ from .consts import * class ConvertAPI(Client): - 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): - Client.__init__(self, api_key, api_secret_key, passphrase, use_server_time, flag, domain,debug) + 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 get_currencies(self): params = {} diff --git a/okx/CopyTrading.py b/okx/CopyTrading.py index 0f41254..d2047c4 100644 --- a/okx/CopyTrading.py +++ b/okx/CopyTrading.py @@ -4,8 +4,9 @@ from .consts import * class CopyTradingAPI(Client): 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): - Client.__init__(self, api_key, api_secret_key, passphrase, use_server_time, flag, domain, debug) + 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) # Get existing leading positions def get_existing_leading_positions(self, instId=''): @@ -25,7 +26,8 @@ class CopyTradingAPI(Client): return self._request_with_params(GET, GET_LEADING_POSITIONS_HISTORY, params) # Place leading stop order - def place_leading_stop_order(self, subPosId='', tpTriggerPx='', slTriggerPx='', tpTriggerPxType='', slTriggerPxType=''): + def place_leading_stop_order(self, subPosId='', tpTriggerPx='', slTriggerPx='', tpTriggerPxType='', + slTriggerPxType=''): params = { 'subPosId': subPosId, 'tpTriggerPx': tpTriggerPx, diff --git a/okx/Earning.py b/okx/Earning.py index 44e3363..ce913a2 100644 --- a/okx/Earning.py +++ b/okx/Earning.py @@ -3,8 +3,8 @@ from .consts import * class EarningAPI(Client): - 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): - Client.__init__(self, api_key, api_secret_key, passphrase, use_server_time, flag, domain, debug) + 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 get_offers(self,productId = '',protocolType = '',ccy = ''): params = { diff --git a/okx/FDBroker.py b/okx/FDBroker.py index d0c8f57..3af8202 100644 --- a/okx/FDBroker.py +++ b/okx/FDBroker.py @@ -3,8 +3,8 @@ from .consts import * class FDBrokerAPI(Client): - 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): - Client.__init__(self, api_key, api_secret_key, passphrase, use_server_time, flag, domain,debug) + 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 generate_rebate_details_download_link(self, begin ='', end = ''): params = {'begin': begin, 'end': end} diff --git a/okx/Funding.py b/okx/Funding.py index 31cd36d..79b2a61 100644 --- a/okx/Funding.py +++ b/okx/Funding.py @@ -5,8 +5,8 @@ from .consts import * class FundingAPI(Client): - 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): - Client.__init__(self, api_key, api_secret_key, passphrase, use_server_time, flag, domain,debug) + 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) # Get Deposit Address def get_deposit_address(self, ccy): diff --git a/okx/Grid.py b/okx/Grid.py index 0e4ea9d..d2ca9c5 100644 --- a/okx/Grid.py +++ b/okx/Grid.py @@ -3,8 +3,8 @@ from .consts import * class GridAPI(Client): - 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): - Client.__init__(self, api_key, api_secret_key, passphrase, use_server_time, flag, domain,debug) + 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 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 da4691b..e362139 100644 --- a/okx/MarketData.py +++ b/okx/MarketData.py @@ -4,8 +4,8 @@ from .consts import * class MarketAPI(Client): - 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): - Client.__init__(self, api_key, api_secret_key, passphrase, use_server_time, flag, domain,debug) + 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) # Get Tickers diff --git a/okx/NDBroker.py b/okx/NDBroker.py index 01dcb33..2fc6f20 100644 --- a/okx/NDBroker.py +++ b/okx/NDBroker.py @@ -1,8 +1,8 @@ from .client import Client from .consts import * class NDBrokerAPI(Client): - 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): - Client.__init__(self, api_key, api_secret_key, passphrase, use_server_time, flag, domain,debug) + 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) #GET /api/v5/broker/nd/info def get_broker_info(self): diff --git a/okx/PublicData.py b/okx/PublicData.py index 8f915c0..80268ae 100644 --- a/okx/PublicData.py +++ b/okx/PublicData.py @@ -4,8 +4,8 @@ from .consts import * class PublicAPI(Client): - 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): - Client.__init__(self, api_key, api_secret_key, passphrase, use_server_time, flag, domain,debug) + 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) # Get Instruments def get_instruments(self, instType, uly='', instId='',instFamily = ''): diff --git a/okx/SpreadTrading.py b/okx/SpreadTrading.py index c460ace..eb7508b 100644 --- a/okx/SpreadTrading.py +++ b/okx/SpreadTrading.py @@ -4,8 +4,8 @@ from .consts import * class SpreadTradingAPI(Client): - 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): - Client.__init__(self, api_key, api_secret_key, passphrase, use_server_time, flag, domain,debug) + 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) # Place Order def place_order(self, sprdId='', clOrdId='', tag='', side='', ordType='', sz='', px=''): diff --git a/okx/Status.py b/okx/Status.py index ab9cf80..8e62f1e 100644 --- a/okx/Status.py +++ b/okx/Status.py @@ -3,8 +3,8 @@ from .consts import * class StatusAPI(Client): - 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): - Client.__init__(self, api_key, api_secret_key, passphrase, use_server_time, flag, domain,debug) + 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 status(self, state=''): params = {'state': state} diff --git a/okx/SubAccount.py b/okx/SubAccount.py index 09fce2f..d152104 100644 --- a/okx/SubAccount.py +++ b/okx/SubAccount.py @@ -3,8 +3,8 @@ from .consts import * class SubAccountAPI(Client): - 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): - Client.__init__(self, api_key, api_secret_key, passphrase, use_server_time, flag, domain,debug) + 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 get_account_balance(self, subAcct): params = {"subAcct": subAcct} diff --git a/okx/Trade.py b/okx/Trade.py index d52d143..456635a 100644 --- a/okx/Trade.py +++ b/okx/Trade.py @@ -4,8 +4,8 @@ from .consts import * class TradeAPI(Client): - 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): - Client.__init__(self, api_key, api_secret_key, passphrase, use_server_time, flag, domain,debug) + 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) # Place Order def place_order(self, instId, tdMode, side, ordType, sz, ccy='', clOrdId='', tag='', posSide='', px='', diff --git a/okx/TradingData.py b/okx/TradingData.py index 0d8bf70..f507930 100644 --- a/okx/TradingData.py +++ b/okx/TradingData.py @@ -4,8 +4,8 @@ from .consts import * class TradingDataAPI(Client): - 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): - Client.__init__(self, api_key, api_secret_key, passphrase, use_server_time, flag, domain,debug) + 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 get_support_coin(self): diff --git a/okx/client.py b/okx/client.py index d8dc956..8c20307 100644 --- a/okx/client.py +++ b/okx/client.py @@ -1,13 +1,15 @@ import json import httpx +from httpx import Client from . import consts as c, utils, exceptions -class Client(object): +class Client(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'): + 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) self.API_KEY = api_key self.API_SECRET_KEY = api_secret_key self.PASSPHRASE = passphrase @@ -15,7 +17,6 @@ class Client(object): self.flag = flag self.domain = base_api self.debug = debug - self.client = httpx.Client(base_url=base_api, http2=True) def _request(self, method, request_path, params): if method == c.GET: @@ -34,9 +35,9 @@ class Client(object): print('domain:',self.domain) print('url:',request_path) if method == c.GET: - response = self.client.get(request_path, headers=header) + response = self.get(request_path, headers=header) elif method == c.POST: - response = self.client.post(request_path, data=body, headers=header) + response = self.post(request_path, data=body, headers=header) return response.json() def _request_without_params(self, method, request_path): @@ -47,7 +48,7 @@ class Client(object): def _get_timestamp(self): request_path = c.API_URL + c.SERVER_TIMESTAMP_URL - response = self.client.get(request_path) + response = self.get(request_path) if response.status_code == 200: return response.json()['data'][0]['ts'] else: diff --git a/test/AccountTest.py b/test/AccountTest.py index 65003ac..bc43605 100644 --- a/test/AccountTest.py +++ b/test/AccountTest.py @@ -4,19 +4,20 @@ from okx import Account class AccountTest(unittest.TestCase): def setUp(self): - 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 = "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) + # ''' 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")) + # 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 def test_get_positions_history(self): print(self.AccountAPI.get_positions_history()) @@ -72,7 +73,7 @@ class AccountTest(unittest.TestCase): 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())