supported new features, fixed loan & earn
This commit is contained in:
parent
2a796ab74d
commit
f983c5bc2d
|
|
@ -1,10 +1,11 @@
|
|||
from .okxclient import OkxClient
|
||||
from .consts import *
|
||||
from .okxclient import OkxClient
|
||||
|
||||
|
||||
class AccountAPI(OkxClient):
|
||||
|
||||
def __init__(self, api_key='-1', api_secret_key='-1', passphrase='-1', use_server_time=None, flag='1', domain = 'https://www.okx.com',debug = True,proxy = None):
|
||||
def __init__(self, api_key='-1', api_secret_key='-1', passphrase='-1', use_server_time=None, 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
|
||||
|
|
@ -26,7 +27,8 @@ class AccountAPI(OkxClient):
|
|||
params = {'instType': instType, 'instId': instId}
|
||||
return self._request_with_params(GET, POSITION_INFO, params)
|
||||
|
||||
def position_builder(self,inclRealPosAndEq=False, spotOffsetType=None, greeksType=None, simPos=None, simAsset=None):
|
||||
def position_builder(self, inclRealPosAndEq=False, spotOffsetType=None, greeksType=None, simPos=None,
|
||||
simAsset=None):
|
||||
params = {}
|
||||
if inclRealPosAndEq is not None:
|
||||
params['inclRealPosAndEq'] = inclRealPosAndEq
|
||||
|
|
@ -48,7 +50,8 @@ class AccountAPI(OkxClient):
|
|||
return self._request_with_params(GET, BILLS_DETAIL, params)
|
||||
|
||||
# Get Bills Details (recent 3 months)
|
||||
def get_account_bills_archive(self, instType='', ccy='', mgnMode='', ctType='', type='', subType='', after='', before='',
|
||||
def get_account_bills_archive(self, instType='', ccy='', mgnMode='', ctType='', type='', subType='', after='',
|
||||
before='',
|
||||
limit='', begin='', end=''):
|
||||
params = {'instType': instType, 'ccy': ccy, 'mgnMode': mgnMode, 'ctType': ctType, 'type': type,
|
||||
'subType': subType, 'after': after, 'before': before, 'limit': limit, 'begin': begin, 'end': end}
|
||||
|
|
@ -88,6 +91,7 @@ class AccountAPI(OkxClient):
|
|||
def get_leverage(self, instId, mgnMode):
|
||||
params = {'instId': instId, 'mgnMode': mgnMode}
|
||||
return self._request_with_params(GET, GET_LEVERAGE, params)
|
||||
|
||||
# Get instruments
|
||||
def get_instruments(self, instType='', ugly='', instFamily='', instId=''):
|
||||
params = {'instType': instType, 'ugly': ugly, 'instFamily': instFamily, 'instId': instId}
|
||||
|
|
@ -158,7 +162,8 @@ class AccountAPI(OkxClient):
|
|||
return self._request_without_params(GET, ACCOUNT_RISK)
|
||||
|
||||
# GET /api/v5/account/positions-history
|
||||
def get_positions_history(self,instType = '', instId = '',mgnMode = '',type = '',posId = '',after = '',before ='',limit = ''):
|
||||
def get_positions_history(self, instType='', instId='', mgnMode='', type='', posId='', after='', before='',
|
||||
limit=''):
|
||||
params = {
|
||||
'instType': instType,
|
||||
'instId': instId,
|
||||
|
|
@ -215,3 +220,77 @@ class AccountAPI(OkxClient):
|
|||
# - Activate option
|
||||
def activate_option(self):
|
||||
return self._request_without_params(POST, ACTIVSTE_OPTION)
|
||||
|
||||
def get_fix_loan_borrowing_limit(self):
|
||||
return self._request_without_params(GET, BORROWING_LIMIT)
|
||||
|
||||
def get_fix_loan_borrowing_quote(self, type=None, ccy=None, amt=None, maxRate=None, term=None, ordId=None):
|
||||
params = {}
|
||||
if type is not None:
|
||||
params['type'] = type
|
||||
if ccy is not None:
|
||||
params['ccy'] =ccy
|
||||
if amt is not None:
|
||||
params['amt'] = amt
|
||||
if maxRate is not None:
|
||||
params['maxRate'] = maxRate
|
||||
if term is not None:
|
||||
params['term'] = term
|
||||
if ordId is not None:
|
||||
params['ordId'] = ordId
|
||||
return self._request_with_params(GET, BORROWING_QUOTE, params)
|
||||
|
||||
def place_fix_loan_borrowing_order(self, ccy=None, amt=None, maxRate=None, term=None, reborrow=False, reborrowRate=None):
|
||||
params = {}
|
||||
if ccy is not None:
|
||||
params['ccy'] =ccy
|
||||
if amt is not None:
|
||||
params['amt'] = amt
|
||||
if maxRate is not None:
|
||||
params['maxRate'] = maxRate
|
||||
if term is not None:
|
||||
params['term'] = term
|
||||
if reborrow is not None:
|
||||
params['reborrow'] = reborrow
|
||||
if reborrowRate is not None:
|
||||
params['reborrowRate'] = reborrowRate
|
||||
return self._request_with_params(POST, PLACE_BORROWING_ORDER, params)
|
||||
|
||||
def amend_fix_loan_borrowing_order(self, ordId=None, reborrow=None, renewMaxRate=None):
|
||||
params = {}
|
||||
if ordId is not None:
|
||||
params['ordId'] = ordId
|
||||
if reborrow is not None:
|
||||
params['reborrow'] = reborrow
|
||||
if renewMaxRate is not None:
|
||||
params['renewMaxRate'] = renewMaxRate
|
||||
return self._request_with_params(POST, AMEND_BORROWING_ORDER, params)
|
||||
|
||||
def fix_loan_manual_reborrow(self, ordId=None, maxRate=None):
|
||||
params = {}
|
||||
if ordId is not None:
|
||||
params['ordId'] = ordId
|
||||
if maxRate is not None:
|
||||
params['maxRate'] = maxRate
|
||||
return self._request_with_params(POST, MANUAL_REBORROW, params)
|
||||
|
||||
def repay_fix_loan_borrowing_order(self, ordId=None):
|
||||
params = {}
|
||||
if ordId is not None:
|
||||
params['ordId'] = ordId
|
||||
return self._request_with_params(POST, REPAY_BORROWING_ORDER, params)
|
||||
def get_fix_loan_borrowing_orders_list(self, ordId=None, ccy=None, state=None, after=None, before=None, limit=None):
|
||||
params = {}
|
||||
if ordId is not None:
|
||||
params['ordId'] = ordId
|
||||
if ccy is not None:
|
||||
params['ccy'] =ccy
|
||||
if state is not None:
|
||||
params['state'] = state
|
||||
if after is not None:
|
||||
params['after'] = after
|
||||
if before is not None:
|
||||
params['before'] = before
|
||||
if limit is not None:
|
||||
params['limit'] = limit
|
||||
return self._request_with_params(GET, BORROWING_ORDERS_LIST, params)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,91 @@
|
|||
from okx.consts import GET, LENDING_OFFERS, LENDING_APY_HISTORY, PENDING_LENDING_VOLUME, PLACE_LENDING_ORDER, POST, \
|
||||
AMEND_LENDING_ORDER, LENDING_ORDERS_LIST, LENDING_SUB_ORDERS
|
||||
from okx.okxclient import OkxClient
|
||||
|
||||
|
||||
class SimpleEarnFixedAPI(OkxClient):
|
||||
def __init__(self, api_key='-1', api_secret_key='-1', passphrase='-1', use_server_time=None, 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)
|
||||
|
||||
def get_lending_offers(self, ccy=None, term=None):
|
||||
params = {}
|
||||
if ccy is not None:
|
||||
params['ccy'] = ccy
|
||||
if term is not None:
|
||||
params['term'] = term
|
||||
return self._request_with_params(GET, LENDING_OFFERS, params)
|
||||
|
||||
def get_lending_apy_history(self, ccy=None, term=None):
|
||||
params = {}
|
||||
if ccy is not None:
|
||||
params['ccy'] = ccy
|
||||
if term is not None:
|
||||
params['term'] = term
|
||||
return self._request_with_params(GET, LENDING_APY_HISTORY, params)
|
||||
|
||||
def get_pending_lending_volume(self, ccy=None, term=None):
|
||||
params = {}
|
||||
if ccy is not None:
|
||||
params['ccy'] = ccy
|
||||
if term is not None:
|
||||
params['term'] = term
|
||||
return self._request_with_params(GET, PENDING_LENDING_VOLUME, params)
|
||||
|
||||
def place_lending_order(self, ccy=None, amt=None, rate=None, term=None, autoRenewal=False):
|
||||
params = {}
|
||||
if ccy is not None:
|
||||
params['ccy'] = ccy
|
||||
if amt is not None:
|
||||
params['amt'] = amt
|
||||
if rate is not None:
|
||||
params['rate'] = rate
|
||||
if term is not None:
|
||||
params['term'] = term
|
||||
if autoRenewal:
|
||||
params['autoRenewal'] = autoRenewal
|
||||
return self._request_with_params(POST, PLACE_LENDING_ORDER, params)
|
||||
def amend_lending_order(self, ordId=None, changeAmt=None, rate=None,autoRenewal=False):
|
||||
params = {}
|
||||
if ordId is not None:
|
||||
params['ordId'] = ordId
|
||||
if changeAmt is not None:
|
||||
params['amt'] = changeAmt
|
||||
if rate is not None:
|
||||
params['rate'] = rate
|
||||
params['autoRenewal'] = autoRenewal
|
||||
return self._request_with_params(POST, AMEND_LENDING_ORDER, params)
|
||||
|
||||
def get_lending_orders_list(self, ordId = None,ccy=None, state=None, after=None,before=None,limit=None):
|
||||
params = {}
|
||||
if ordId is not None:
|
||||
params['ordId'] = ordId
|
||||
if ccy is not None:
|
||||
params['ccy'] = ccy
|
||||
if state is not None:
|
||||
params['state'] = state
|
||||
if after is not None:
|
||||
params['after'] = after
|
||||
if before is not None:
|
||||
params['before'] = before
|
||||
if limit:
|
||||
params['limit'] = limit
|
||||
return self._request_with_params(GET, LENDING_ORDERS_LIST, params)
|
||||
|
||||
def get_lending_sub_orders(self, ordId = None, state=None, after=None,before=None,limit=None):
|
||||
params = {}
|
||||
if ordId is not None:
|
||||
params['ordId'] = ordId
|
||||
if state is not None:
|
||||
params['state'] = state
|
||||
if after is not None:
|
||||
params['after'] = after
|
||||
if before is not None:
|
||||
params['before'] = before
|
||||
if limit:
|
||||
params['limit'] = limit
|
||||
return self._request_with_params(GET, LENDING_SUB_ORDERS, params)
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -2,4 +2,4 @@
|
|||
Python SDK for the OKX API v5
|
||||
|
||||
"""
|
||||
__version__="0.3.0"
|
||||
__version__="0.3.2"
|
||||
|
|
@ -55,6 +55,13 @@ SET_AUTO_LOAN = '/api/v5/account/set-auto-loan'
|
|||
ACTIVSTE_OPTION = '/api/v5/account/activate-option'
|
||||
POSITION_BUILDER = '/api/v5/account/position-builder'
|
||||
GET_INSTRUMENTS = '/api/v5/account/instruments'
|
||||
BORROWING_LIMIT = '/api/v5/account/fixed-loan/borrowing-limit'
|
||||
BORROWING_QUOTE = '/api/v5/account/fixed-loan/borrowing-quote'
|
||||
PLACE_BORROWING_ORDER= '/api/v5/account/fixed-loan/borrowing-order'
|
||||
AMEND_BORROWING_ORDER='/api/v5/account/fixed-loan/amend-borrowing-order'
|
||||
MANUAL_REBORROW = '/api/v5/account/fixed-loan/manual-reborrow'
|
||||
REPAY_BORROWING_ORDER='/api/v5/account/fixed-loan/repay-borrowing-order'
|
||||
BORROWING_ORDERS_LIST='/api/v5/account/fixed-loan/borrowing-orders-list'
|
||||
|
||||
# funding-complete-testcomplete
|
||||
NON_TRADABLE_ASSETS = '/api/v5/asset/non-tradable-assets'
|
||||
|
|
@ -292,3 +299,14 @@ SPREAD_GET_SPREADS = '/api/v5/sprd/spreads'
|
|||
SPREAD_GET_ORDER_BOOK = '/api/v5/sprd/books'
|
||||
SPREAD_GET_TICKER = '/api/v5/sprd/ticker'
|
||||
SPREAD_GET_PUBLIC_TRADES = '/api/v5/sprd/public-trades'
|
||||
|
||||
# Simple earn fixed
|
||||
LENDING_OFFERS = '/api/v5/finance/fixed-loan/lending-offers'
|
||||
LENDING_APY_HISTORY='/api/v5/finance/fixed-loan/lending-apy-history'
|
||||
PENDING_LENDING_VOLUME = '/api/v5/finance/fixed-loan/pending-lending-volume'
|
||||
PLACE_LENDING_ORDER= '/api/v5/finance/fixed-loan/lending-order'
|
||||
AMEND_LENDING_ORDER='/api/v5/finance/fixed-loan/amend-lending-order'
|
||||
LENDING_ORDERS_LIST='/api/v5/finance/fixed-loan/lending-orders-list'
|
||||
LENDING_SUB_ORDERS='/api/v5/finance/fixed-loan/lending-sub-orders'
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -6,6 +6,8 @@ import httpx
|
|||
from httpx import Client
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from loguru import logger
|
||||
|
||||
from . import consts as c, utils, exceptions
|
||||
|
||||
|
||||
|
|
@ -37,8 +39,9 @@ class OkxClient(Client):
|
|||
header = utils.get_header_no_sign(self.flag, self.debug)
|
||||
response = None
|
||||
if self.debug == True:
|
||||
print('domain:',self.domain)
|
||||
print('url:',request_path)
|
||||
logger.debug(f'domain: {self.domain}')
|
||||
logger.debug(f'url: {request_path}')
|
||||
logger.debug(f'body:{body}')
|
||||
if method == c.GET:
|
||||
response = self.get(request_path, headers=header)
|
||||
elif method == c.POST:
|
||||
|
|
|
|||
10
okx/utils.py
10
okx/utils.py
|
|
@ -1,6 +1,9 @@
|
|||
import hmac
|
||||
import base64
|
||||
import datetime
|
||||
|
||||
from loguru import logger
|
||||
|
||||
from . import consts as c
|
||||
|
||||
|
||||
|
|
@ -12,7 +15,7 @@ def sign(message, secretKey):
|
|||
|
||||
def pre_hash(timestamp, method, request_path, body,debug = True):
|
||||
if debug == True:
|
||||
print('body: ',body)
|
||||
logger.debug(f'body: {body}')
|
||||
return str(timestamp) + str.upper(method) + request_path + body
|
||||
|
||||
|
||||
|
|
@ -25,7 +28,7 @@ def get_header(api_key, sign, timestamp, passphrase, flag,debug = True):
|
|||
header[c.OK_ACCESS_PASSPHRASE] = passphrase
|
||||
header['x-simulated-trading'] = flag
|
||||
if debug == True:
|
||||
print('header: ',header)
|
||||
logger.debug(f'header: {header}')
|
||||
return header
|
||||
|
||||
def get_header_no_sign(flag,debug = True):
|
||||
|
|
@ -33,7 +36,7 @@ def get_header_no_sign(flag,debug = True):
|
|||
header[c.CONTENT_TYPE] = c.APPLICATION_JSON
|
||||
header['x-simulated-trading'] = flag
|
||||
if debug == True:
|
||||
print('header: ',header)
|
||||
logger.debug(f'header: {header}')
|
||||
return header
|
||||
|
||||
def parse_params_to_str(params):
|
||||
|
|
@ -42,7 +45,6 @@ def parse_params_to_str(params):
|
|||
if(value != ''):
|
||||
url = url + str(key) + '=' + str(value) + '&'
|
||||
url = url[0:-1]
|
||||
#print('url:',url)
|
||||
return url
|
||||
|
||||
|
||||
|
|
|
|||
1
setup.py
1
setup.py
|
|
@ -22,6 +22,7 @@ setuptools.setup(
|
|||
"importlib-metadata",
|
||||
"httpx[http2]",
|
||||
"keyring",
|
||||
"loguru",
|
||||
"requests",
|
||||
"Twisted",
|
||||
"pyOpenSSL"
|
||||
|
|
|
|||
|
|
@ -1,15 +1,16 @@
|
|||
import unittest
|
||||
|
||||
from loguru import logger
|
||||
|
||||
from okx import Account
|
||||
|
||||
|
||||
class AccountTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
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)
|
||||
api_key = '52049394-3c72-4c06-8d87-d6399450c264'
|
||||
api_secret_key = 'E532EB5AF81E6E50A2875225FEDC9150'
|
||||
passphrase = 'Fixedloan2024!'
|
||||
self.AccountAPI = Account.AccountAPI(api_key, api_secret_key, passphrase, flag='1')
|
||||
|
||||
# '''
|
||||
# POSITIONS_HISTORY = '/api/v5/account/positions-history' #need add
|
||||
|
|
@ -21,12 +22,12 @@ class AccountTest(unittest.TestCase):
|
|||
# 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())
|
||||
def test_get_instruments(self):
|
||||
print(self.AccountAPI.get_instruments(instType='SPOT'))
|
||||
def test_get_account_bills_archive(self):
|
||||
print(self.AccountAPI.get_account_bills_archive(begin='1715780962300',end='1716998400000'))
|
||||
# def test_get_positions_history(self):
|
||||
# print(self.AccountAPI.get_positions_history())
|
||||
# def test_get_instruments(self):
|
||||
# print(self.AccountAPI.get_instruments(instType='SPOT'))
|
||||
# def test_get_account_bills_archive(self):
|
||||
# print(self.AccountAPI.get_account_bills_archive(begin='1715780962300',end='1716998400000'))
|
||||
# def test_positions_builder(self):
|
||||
# print("Both real and virtual positions and assets are calculated")
|
||||
# sim_pos = [{'instId': 'BTC-USDT-SWAP', 'pos': '10'}, {'instId': 'BTC-USDT-SWAP', 'pos': '10'}]
|
||||
|
|
@ -124,7 +125,20 @@ class AccountTest(unittest.TestCase):
|
|||
|
||||
# def test_simulated_margin(self):
|
||||
# print(self.AccountAPI.get_simulated_margin(spotOffsetType='3'))
|
||||
|
||||
# def test_get_fix_loan_borrowing_limit(self):
|
||||
# logger.debug(f'{self.AccountAPI.get_fix_loan_borrowing_limit()}')
|
||||
# def test_get_fix_loan_borrowing_quote(self):
|
||||
# logger.debug(f'{self.AccountAPI.get_fix_loan_borrowing_quote(type="normal")}')
|
||||
# def test_place_fix_loan_borrowing_order(self):
|
||||
# logger.debug(f'{self.AccountAPI.place_fix_loan_borrowing_order(ccy="BTC", amt="0.1515", maxRate="0.001", term="30D", reborrow=True, reborrowRate="0.01")}')
|
||||
# def test_amend_fix_loan_borrowing_order(self):
|
||||
# logger.debug(f'{self.AccountAPI.amend_fix_loan_borrowing_order(ordId="2407301043344857",reborrow=True,renewMaxRate="0.01")}')
|
||||
# def test_fix_loan_manual_reborrow(self):
|
||||
# logger.debug(f'{self.AccountAPI.fix_loan_manual_reborrow(ordId="2407301043344857",maxRate="0.1")}')
|
||||
# def test_repay_fix_loan_borrowing_order(self):
|
||||
# logger.info(f'{self.AccountAPI.repay_fix_loan_borrowing_order(ordId="2407301054407907")}')
|
||||
# def test_get_fix_loan_borrowing_orders_list(self):
|
||||
# logger.debug(self.AccountAPI.get_fix_loan_borrowing_orders_list(ordId="2407301054407907"))
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
|
|
|||
|
|
@ -0,0 +1,37 @@
|
|||
import unittest
|
||||
|
||||
from loguru import logger
|
||||
|
||||
from okx import Account, SimpleEarnFixed
|
||||
|
||||
|
||||
class SimpleEarnFixedTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
api_key = '15c0a341-f38c-4bf2-8f86-3c32c8b5d60c'
|
||||
api_secret_key = 'DEBF51E2733E03CE4F2CEB63BB01D983'
|
||||
passphrase = 'hylHYL950525,.'
|
||||
self.SimpleEarnFixedAPI = SimpleEarnFixed.SimpleEarnFixedAPI(api_key, api_secret_key, passphrase, flag='1')
|
||||
|
||||
# def test_get_lending_offers(self):
|
||||
# logger.debug(self.SimpleEarnFixedAPI.get_lending_offers())
|
||||
#
|
||||
# def test_get_instruments(self):
|
||||
# logger.debug(self.SimpleEarnFixedAPI.get_lending_apy_history(ccy="BTC", term='30D'))
|
||||
#
|
||||
# def test_get_pending_lending_volume(self):
|
||||
# logger.debug(self.SimpleEarnFixedAPI.get_pending_lending_volume(ccy="BTC", term='30D'))
|
||||
#
|
||||
# def test_place_lending_order(self):
|
||||
# logger.debug(self.SimpleEarnFixedAPI.place_lending_order(ccy="USDT", amt="33", maxRate="1", term="30D"))
|
||||
#
|
||||
# def test_amend_lending_order(self):
|
||||
# logger.debug(self.SimpleEarnFixedAPI.amend_lending_order(ordId="2407241551594080",changeAmt="30",autoRenewal=True))
|
||||
#
|
||||
# def test_get_lending_orders_list(self):
|
||||
# logger.debug(self.SimpleEarnFixedAPI.get_lending_orders_list())
|
||||
# def test_get_lending_sub_orders(self):
|
||||
# logger.debug(self.SimpleEarnFixedAPI.get_lending_sub_orders(ordId="2407241453209933"))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Loading…
Reference in New Issue