Skip to content

rebase to fork origin #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 29 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
37782e0
Update README.md
okxapi Oct 8, 2023
1e7bdfa
update version
mumu212 Oct 9, 2023
7cf0ca5
fix
mumu212 Oct 18, 2023
12e983d
fix: throws errors when we use the URLs for the demo trading environment
007live Dec 26, 2023
cc2d1f7
fix: client parameter exception
007live Dec 27, 2023
9006434
Update getting ts from _get_timestamp() (#33)
kkathena Jan 8, 2024
7fd4543
fix: Python install python-okx --upgrade error && Error in file name
007live Jan 8, 2024
71504d3
Merge remote-tracking branch 'origin/master'
007live Jan 8, 2024
db0bbb5
Update WsPrivateAsyncTest.py
okxapi Jan 18, 2024
068c63d
feat: upgrade for proxy
007live Jan 25, 2024
422ee7e
feat:Support multiply trigger TP
007live Jan 28, 2024
7d43159
feat: supported position builder
007live Mar 7, 2024
d95d1ae
update Funding non_tradable_assets
Nort007 May 23, 2023
c4e7a98
Rename ORDERS_ALGO_OENDING to ORDERS_ALGO_PENDING
WoLeo-Z Aug 25, 2023
a10a7f1
Rename ORDERS_ALGO_OENDING to ORDERS_ALGO_PENDING
WoLeo-Z Aug 25, 2023
e2e7b4f
updated typo in constant and reference
dalton-cole Nov 15, 2023
26956b9
fix: #55 incorrect timestamp format in 'OK-ACCESS-TIMESTAMP' header w…
grzhan Mar 17, 2024
0df4058
remove use_server_time param
007live May 9, 2024
ef7e4f3
Merge remote-tracking branch 'origin/master'
007live May 9, 2024
2a796ab
add new api and extend new apram
007live May 31, 2024
f983c5b
supported new features, fixed loan & earn
007live Aug 7, 2024
7420e2a
fix get_max_loan
mumu212 Oct 17, 2024
6cd392d
upgrade
mumu212 Dec 9, 2024
9313397
upgrade
mumu212 Dec 26, 2024
1711d62
upgrade
mumu212 Mar 31, 2025
a3e7a8b
upgrade
mumu212 Mar 31, 2025
472a5ed
upgrade
mumu212 Apr 29, 2025
564208c
upgrade
mumu212 May 12, 2025
53f5582
Update README.md
okxapi May 29, 2025
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
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ If you came here looking to purchase cryptocurrencies from the OKX exchange, ple

#### Source code
https://github.com/okxapi/python-okx
#### OKX API Telegram
https://t.me/OKXAPI
#### API trading tutorials
- Spot trading: https://www.okx.com/learn/spot-trading-with-jupyter-notebook
- Derivative trading: https://www.okx.com/learn/derivatives-trading-with-jupyter-notebook
- Spot trading: https://www.okx.com/help/how-can-i-do-spot-trading-with-the-jupyter-notebook
- Derivative trading: https://www.okx.com/help/how-can-i-do-derivatives-trading-with-the-jupyter-notebook

Make sure you update often and check the [Changelog](https://www.okx.com/docs-v5/log_en/) for new features and bug fixes.

Expand Down
216 changes: 171 additions & 45 deletions okx/Account.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from .client import Client
from .consts import *
from .okxclient import OkxClient


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):
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=None, flag='1',
domain='https://www.okx.com', debug=False, 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=''):
Expand All @@ -26,18 +27,36 @@ def get_positions(self, instType='', instId=''):
params = {'instType': instType, 'instId': instId}
return self._request_with_params(GET, POSITION_INFO, params)

def position_builder(self, acctLv=None,inclRealPosAndEq=False, lever=None, greeksType=None, simPos=None,
simAsset=None):
params = {}
if acctLv is not None:
params['acctLv'] = acctLv
if inclRealPosAndEq is not None:
params['inclRealPosAndEq'] = inclRealPosAndEq
if lever is not None:
params['spotOffsetType'] = lever
if greeksType is not None:
params['greksType'] = greeksType
if simPos is not None:
params['simPos'] = simPos
if simAsset is not None:
params['simAsset'] = simAsset
return self._request_with_params(POST, POSITION_BUILDER, params)

# Get Bills Details (recent 7 days)
def get_account_bills(self, instType='', ccy='', mgnMode='', ctType='', type='', subType='', after='', before='',
limit=''):
limit=''):
params = {'instType': instType, 'ccy': ccy, 'mgnMode': mgnMode, 'ctType': ctType, 'type': type,
'subType': subType, 'after': after, 'before': before, 'limit': limit}
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='',
limit=''):
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}
'subType': subType, 'after': after, 'before': before, 'limit': limit, 'begin': begin, 'end': end}
return self._request_with_params(GET, BILLS_ARCHIVE, params)

# Get Account Configuration
Expand Down Expand Up @@ -66,23 +85,28 @@ def get_max_avail_size(self, instId, tdMode, ccy='', reduceOnly='', unSpotOffset
return self._request_with_params(GET, MAX_AVAIL_SIZE, params)

# Increase / Decrease margin
def adjustment_margin(self, instId, posSide, type, amt,loanTrans=''):
params = {'instId': instId, 'posSide': posSide, 'type': type, 'amt': amt,'loanTrans':loanTrans}
def adjustment_margin(self, instId, posSide, type, amt, loanTrans=''):
params = {'instId': instId, 'posSide': posSide, 'type': type, 'amt': amt, 'loanTrans': loanTrans}
return self._request_with_params(POST, ADJUSTMENT_MARGIN, params)

# Get Leverage
def get_leverage(self, instId, mgnMode):
params = {'instId': instId, 'mgnMode': mgnMode}
def get_leverage(self, mgnMode, ccy='', instId=''):
params = {'instId': instId, 'mgnMode': mgnMode, 'ccy': ccy}
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}
return self._request_with_params(GET, GET_INSTRUMENTS, params)

# Get the maximum loan of isolated MARGIN
def get_max_loan(self, instId, mgnMode, mgnCcy):
def get_max_loan(self, instId, mgnMode, mgnCcy=''):
params = {'instId': instId, 'mgnMode': mgnMode, 'mgnCcy': mgnCcy}
return self._request_with_params(GET, MAX_LOAN, params)

# Get Fee Rates
def get_fee_rates(self, instType, instId='', uly='', category='',instFamily = ''):
params = {'instType': instType, 'instId': instId, 'uly': uly, 'category': category,'instFamily':instFamily}
def get_fee_rates(self, instType, instId='', uly='', category='', instFamily=''):
params = {'instType': instType, 'instId': instId, 'uly': uly, 'category': category, 'instFamily': instFamily}
return self._request_with_params(GET, FEE_RATES, params)

# Get interest-accrued
Expand All @@ -101,8 +125,8 @@ def set_greeks(self, greeksType):
return self._request_with_params(POST, SET_GREEKS, params)

# Set Isolated Mode
def set_isolated_mode(self, isoMode,type):
params = {'isoMode': isoMode, 'type':type}
def set_isolated_mode(self, isoMode, type):
params = {'isoMode': isoMode, 'type': type}
return self._request_with_params(POST, ISOLATED_MODE, params)

# Get Maximum Withdrawals
Expand All @@ -117,11 +141,11 @@ def borrow_repay(self, ccy='', side='', amt='', ordId=''):

# Get borrow repay history
def get_borrow_repay_history(self, ccy='', after='', before='', limit=''):
params = {'ccy': ccy, 'after': after, 'before': before, 'limit':limit}
params = {'ccy': ccy, 'after': after, 'before': before, 'limit': limit}
return self._request_with_params(GET, BORROW_REPAY_HISTORY, params)

# Get Obtain borrowing rate and limit
def get_interest_limits(self, type='',ccy=''):
def get_interest_limits(self, type='', ccy=''):
params = {'type': type, 'ccy': ccy}
return self._request_with_params(GET, INTEREST_LIMITS, params)

Expand All @@ -135,57 +159,58 @@ def get_greeks(self, ccy=''):
params = {'ccy': ccy}
return self._request_with_params(GET, GREEKS, params)

#GET /api/v5/account/risk-state
# GET /api/v5/account/risk-state
def get_account_position_risk(self):
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 = ''):
# GET /api/v5/account/positions-history
def get_positions_history(self, instType='', instId='', mgnMode='', type='', posId='', after='', before='',
limit=''):
params = {
'instType':instType,
'instId':instId,
'mgnMode':mgnMode,
'type':type,
'posId':posId,
'after':after,
'before':before,
'limit':limit
'instType': instType,
'instId': instId,
'mgnMode': mgnMode,
'type': type,
'posId': posId,
'after': after,
'before': before,
'limit': limit
}
return self._request_with_params(GET,POSITIONS_HISTORY,params)
return self._request_with_params(GET, POSITIONS_HISTORY, params)

#GET /api/v5/account/position-tiers
def get_account_position_tiers(self,instType = '', uly = '',instFamily = ''):
# GET /api/v5/account/position-tiers
def get_account_position_tiers(self, instType='', uly='', instFamily=''):
params = {
'instType':instType,
'uly':uly,
'instFamily':instFamily
'instType': instType,
'uly': uly,
'instFamily': instFamily
}
return self._request_with_params(GET,GET_PM_LIMIT,params)
return self._request_with_params(GET, GET_PM_LIMIT, params)

#- Get VIP interest accrued data
# - Get VIP interest accrued data
def get_VIP_interest_accrued_data(self, ccy='', ordId='', after='', before='', limit=''):
params = {'ccy': ccy, 'ordId': ordId, 'after': after, 'before': before, 'limit': limit}
return self._request_with_params(GET, GET_VIP_INTEREST_ACCRUED_DATA, params)

#- Get VIP interest deducted data
# - Get VIP interest deducted data
def get_VIP_interest_deducted_data(self, ccy='', ordId='', after='', before='', limit=''):
params = {'ccy': ccy, 'ordId': ordId, 'after': after, 'before': before, 'limit': limit}
return self._request_with_params(GET, GET_VIP_INTEREST_DEDUCTED_DATA, params)

# - Get VIP loan order list
def get_VIP_loan_order_list(self, ordId='',state='', ccy='', after='', before='', limit=''):
params = {'ordId': ordId, 'state': state, 'ccy': ccy,'after': after, 'before': before, 'limit': limit}
def get_VIP_loan_order_list(self, ordId='', state='', ccy='', after='', before='', limit=''):
params = {'ordId': ordId, 'state': state, 'ccy': ccy, 'after': after, 'before': before, 'limit': limit}
return self._request_with_params(GET, GET_VIP_LOAN_ORDER_LIST, params)

#- Get VIP loan order detail
# - Get VIP loan order detail
def get_VIP_loan_order_detail(self, ccy='', ordId='', after='', before='', limit=''):
params = {'ccy': ccy, 'ordId': ordId, 'after': after, 'before': before, 'limit': limit}
return self._request_with_params(GET, GET_VIP_LOAN_ORDER_DETAIL, params)

#- Set risk offset type
# - Set risk offset type
def set_risk_offset_typel(self, type=''):
params = {'type': type}
return self._request_with_params(POST, SET_RISK_OFFSET_TYPE,params)
return self._request_with_params(POST, SET_RISK_OFFSET_TYPE, params)

# - Set auto loan
def set_auto_loan(self, autoLoan=''):
Expand All @@ -194,6 +219,107 @@ def set_auto_loan(self, autoLoan=''):
}
return self._request_with_params(POST, SET_AUTO_LOAN, params)

#- Activate option
# - Set auto loan
def set_account_level(self, acctLv):
params = {
'acctLv': acctLv
}
return self._request_with_params(POST, SET_ACCOUNT_LEVEL, params)

# - 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)

def spot_manual_borrow_repay(self, ccy=None, side=None, amt=None):
params = {}
if ccy is not None:
params['ccy'] = ccy
if side is not None:
params['side'] = side
if amt is not None:
params['amt'] = amt
return self._request_with_params(POST, MANUAL_REBORROW_REPAY, params)

def set_auto_repay(self, autoRepay=False):
params = {}
if autoRepay is not None:
params['autoRepay'] = autoRepay
return self._request_with_params(POST, SET_AUTO_REPAY, params)

def spot_borrow_repay_history(self, ccy='', type='', after='', before='', limit=''):
params = {'ccy': ccy, 'type': type, 'after': after, 'before': before, 'limit': limit}
return self._request_with_params(GET, GET_BORROW_REPAY_HISTORY, params)
8 changes: 4 additions & 4 deletions okx/BlockTrading.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from .client import Client
from .okxclient import OkxClient
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)
class BlockTradingAPI(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 = False, proxy=None):
OkxClient.__init__(self, api_key, api_secret_key, passphrase, use_server_time, flag, domain, debug, proxy)

def counterparties(self):
params = {}
Expand Down
8 changes: 4 additions & 4 deletions okx/Convert.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from .client import Client
from .okxclient import OkxClient
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)
class ConvertAPI(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 = False,proxy = None):
OkxClient.__init__(self, api_key, api_secret_key, passphrase, use_server_time, flag, domain, debug, proxy)

def get_currencies(self):
params = {}
Expand Down
12 changes: 7 additions & 5 deletions okx/CopyTrading.py
Original file line number Diff line number Diff line change
@@ -1,11 +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):
Client.__init__(self, api_key, api_secret_key, passphrase, use_server_time, flag, domain, debug)
domain='https://www.okx.com', debug=False, proxy=None):
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=''):
Expand All @@ -25,7 +26,8 @@ def get_leading_position_history(self, instId='', after='', before='', limit='')
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,
Expand Down
Loading