Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
168 commits
Select commit Hold shift + click to select a range
3313cb2
Update WsPrivateTest.py
okxapi Dec 15, 2022
3b79325
Update WsPublicTest.py
okxapi Dec 15, 2022
2830a6e
Update WsPrivateTest.py
okxapi Dec 15, 2022
ed9d820
Update WsClientFactory.py
okxapi Dec 15, 2022
7122e14
Update WsConnectManager.py
okxapi Dec 15, 2022
48373d9
Update WsPrivate.py
okxapi Dec 15, 2022
59f2b56
Update WsPublic.py
okxapi Dec 15, 2022
0fbcb4f
Update README.md
okxapi Dec 16, 2022
41b1950
Update README.md
okxapi Dec 16, 2022
24efd1f
enriched README.md
Feb 15, 2023
ccfc453
modified readme
okxapi Feb 16, 2023
7fdd902
modified readme
okxapi Feb 16, 2023
e472ac4
modified readme
okxapi Feb 16, 2023
93ef09f
modified readme
okxapi Feb 16, 2023
617081b
update README
PerryZhu Feb 16, 2023
48fd15e
update requirements.txt
PerryZhu Feb 16, 2023
3df5503
update version
PerryZhu Feb 16, 2023
ee870fe
add setup.py
PerryZhu Feb 16, 2023
19dc436
Update trade_derivatives_en.ipynb
okxapi Mar 9, 2023
6867bfe
Update trade_derivatives_en.ipynb
okxapi Mar 9, 2023
a0a946b
Update get_started_en.ipynb
okxapi Mar 9, 2023
48d29f1
Update trade_derivatives_en.ipynb
okxapi Mar 9, 2023
cdb0a21
Update get_started_en.ipynb
okxapi Mar 9, 2023
317fb09
Update get_started_en.ipynb
okxapi Mar 9, 2023
709947b
Update get_started_en.ipynb
okxapi Mar 9, 2023
5b9acb6
httpx client not varify and no timeout
Mar 16, 2023
e4b66a4
Merge pull request #3 from Hearty07/master
okxapi Mar 16, 2023
7f9a76d
fix request path of server timestamp
Mar 17, 2023
317ccc9
retry while http request failed
Mar 20, 2023
1c8b51a
show traceback msg while exception
Mar 20, 2023
3084294
re connect while reaching the max request times
Mar 20, 2023
b47f519
retry in 1 second but not raise exception
Mar 21, 2023
b414a60
Merge pull request #8 from Hearty07/master
okxapi Mar 23, 2023
e3fa532
update version
PerryZhu Mar 23, 2023
bb36351
Update get_started_en.ipynb
okxapi Mar 27, 2023
522c7b6
Update trade_derivatives_en.ipynb
okxapi Mar 27, 2023
7b19463
Update get_started_en.ipynb
okxapi Mar 27, 2023
f5e0b6d
Update get_started_en.ipynb
okxapi Mar 28, 2023
f6338c1
Update trade_derivatives_en.ipynb
okxapi Mar 28, 2023
806924e
update dependence and version
PerryZhu Mar 28, 2023
c9e9de6
Merge remote-tracking branch 'origin/master'
PerryZhu Mar 28, 2023
4594cda
delete retry logic
PerryZhu Mar 28, 2023
c8496e5
fixed typo
okxapi Mar 28, 2023
cf81854
Add the wdId parameter to the get_withdrawal_history interface.
Sharp-Lee Apr 9, 2023
8adde22
Merge pull request #14 from Sharp-Lee/master
okxapi Apr 11, 2023
0afee92
update version
PerryZhu Apr 12, 2023
fcd7f52
delete unnecessary log
PerryZhu May 11, 2023
ceadad7
jzh
Jun 16, 2023
9f95fbb
update
Jun 19, 2023
ed77fed
upgrade
Jun 19, 2023
42ea0c3
upgrade
Jun 19, 2023
55f44a3
Delete .DS_Store
okxapi Jun 19, 2023
872d924
Delete test/__pycache__ directory
okxapi Jun 19, 2023
8bd3c5a
Delete .DS_Store
okxapi Jun 19, 2023
f989505
delete unusefull doc
PerryZhu Jun 19, 2023
ec58e88
Delete okx/websocket/__pycache__ directory
okxapi Jun 19, 2023
3137cfe
Merge branch 'master' of github.com:okxapi/python-okx
PerryZhu Jun 19, 2023
a09940e
add git ignore
PerryZhu Jun 19, 2023
9266813
add git ignore
PerryZhu Jun 19, 2023
b136361
add spread
Aug 14, 2023
37782e0
Update README.md
okxapi Oct 8, 2023
1e7bdfa
update version
Oct 9, 2023
7cf0ca5
fix
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
Oct 17, 2024
6cd392d
upgrade
Dec 9, 2024
9313397
upgrade
Dec 26, 2024
1711d62
upgrade
Mar 31, 2025
a3e7a8b
upgrade
Mar 31, 2025
472a5ed
upgrade
Apr 29, 2025
564208c
upgrade
May 12, 2025
53f5582
Update README.md
okxapi May 29, 2025
6d4bcf7
upgrade
Jul 22, 2025
c5d834d
add sol-staking and update endpoint params
alex-okx Jul 28, 2025
e0be0b1
Initialize websocket in constructor to avoid AttributeError
BlazeStorm001 Jun 4, 2025
a5af471
fix bug, func amend_algo_order lost params
Worldworld Oct 31, 2024
46974ba
Update Trade.py add place order param tradeQuoteCcy
lishoujun Oct 19, 2025
91fee83
add set auto earn endpoint
Dec 15, 2025
7a4fa53
feat: add idxVol and fix wrong params for posBuilder
Dec 16, 2025
f8847a9
feat: add unit-testing for the test_account
Dec 16, 2025
0234ece
add set auto earn endpoint
Dec 16, 2025
31fd401
add id parameter to all websocket subscription
Dec 16, 2025
14c3a94
websocket enhancement
Dec 17, 2025
12ebac7
创建0.4.1版本分支
Dec 18, 2025
f78c2fe
add set auto earn endpoint
Dec 18, 2025
e82ee55
feat: add idxVol and fix wrong params for posBuilder
Dec 16, 2025
3f739c2
feat: add unit-testing for the test_account
Dec 16, 2025
94c2070
websocket enhancement
Dec 18, 2025
1f0a83d
Merge remote-tracking branch 'origin/release/version_0.4.1' into dev/…
Dec 18, 2025
a392b53
Merge remote-tracking branch 'origin/release/version_0.4.1' into dev/…
Dec 18, 2025
0f4d44c
websocket enhancement
Dec 18, 2025
2839e7f
Merge remote-tracking branch 'origin/release/version_0.4.1' into dev/…
Dec 18, 2025
69a416a
add /api/v5/account/set-auto-earn endpoint
Dec 18, 2025
68fa15c
add /api/v5/account/set-auto-earn endpoint
Dec 19, 2025
3aebd72
add toAddrType to the 3 withdrawal endpoints
Dec 16, 2025
7398c55
add toAddrType to the 3 withdrawal endpoints
Dec 18, 2025
df68d90
add toAddrType to the 3 withdrawal endpoints
Dec 19, 2025
19f7393
add toAddrType to the 3 withdrawal endpoints
Dec 19, 2025
759c054
add market-data-history endpoint
Dec 17, 2025
46817fe
add market-data-history endpoint
Dec 18, 2025
4b165d0
add market-data-history endpoint
Dec 19, 2025
4391f0e
add /api/v5/account/set-auto-earn endpoint
Dec 19, 2025
2f9fea9
http version compatibility issue
Dec 17, 2025
ed85a8c
confirm if it's version compatibility issue
Dec 18, 2025
0023ce6
confirm if it's version compatibility issue
Dec 19, 2025
e02411c
Merge remote-tracking branch 'origin/release/version_0.4.1' into dev/…
Dec 19, 2025
6746db7
Merge remote-tracking branch 'origin/release/version_0.4.1' into dev/…
Dec 19, 2025
500baa6
add id parameter to all websocket subscription
Dec 19, 2025
69af821
add id parameter to all websocket subscription
Dec 19, 2025
240216f
Merge pull request #125 from okxapi/dev/BROK-741
okxapi Dec 19, 2025
892bc6b
Merge pull request #128 from okxapi/dev/BROK-1214
okxapi Dec 19, 2025
e808427
feat: add api changes
Dec 19, 2025
61562c4
websocket enhancement
Dec 19, 2025
32706d9
Merge remote-tracking branch 'origin/release/version_0.4.1' into dev/…
Dec 19, 2025
11efc37
feat: add ci cd
Dec 19, 2025
b6c3b83
websocket enhancement
Dec 19, 2025
81c269c
feat: add ci cd and remove wrong commit for gitlab
Dec 19, 2025
89e8e25
feat: add cicd
Dec 19, 2025
2d47171
fix: missing import
Dec 19, 2025
ee04e6b
fix: broken testing
Dec 19, 2025
34cf7c4
Merge pull request #130 from okxapi/dev-add-cicd
okxapi Dec 19, 2025
40e7629
Merge branch 'dev-add-cicd' to dev-add-px-amend-type-and-trade-quote-…
Dec 19, 2025
75a20fc
feat: add .env
Dec 19, 2025
6cee7dc
feat: rmv changes
Dec 19, 2025
128da2d
fix: add testing files
Dec 19, 2025
ee3c263
websocket enhancement
Dec 22, 2025
a2b7fb0
Merge pull request #133 from okxapi/dev-add-px-amend-type-and-trade-q…
okxapi Dec 22, 2025
3f53945
Merge remote-tracking branch 'origin/release/version_0.4.1' into dev/…
Dec 22, 2025
4813e7e
websocket enhancement
Dec 22, 2025
8d0e78e
Merge remote-tracking branch 'origin/release/version_0.4.1' into dev/…
Dec 22, 2025
9dfb5b6
websocket enhancement
Dec 22, 2025
fb237f6
websocket enhancement
Dec 22, 2025
2dfafef
config update
Dec 22, 2025
1fb02f4
Merge pull request #134 from okxapi/dev/config_update
okxapi Dec 22, 2025
883901f
Merge remote-tracking branch 'origin/release/version_0.4.1' into dev/…
Dec 22, 2025
7172e8e
Merge pull request #124 from okxapi/dev/BROK-653
okxapi Dec 22, 2025
c35c355
feat: add dependency file and cicd
Dec 22, 2025
a278d23
feat: rmv requirements-dev.txt
Dec 22, 2025
299acc4
fix: ci build failure
Dec 22, 2025
2b2936a
fix: ci build failure
Dec 22, 2025
b3973c7
Merge pull request #135 from okxapi/dev-add-requirements-files
okxapi Dec 22, 2025
7e40b52
add tradeQuoteCcy request param to the trade-related endpoints
Dec 22, 2025
93672c1
Merge pull request #136 from okxapi/dev/BROK-1216
okxapi Dec 22, 2025
149d3f4
add tradeQuoteCcy request param to the trade-related endpoints
Jan 5, 2026
1bb68a1
Merge pull request #137 from okxapi/dev/BROK-1217
okxapi Jan 6, 2026
bbafebf
add back get_open_interest_history() function
Jan 6, 2026
bd8cd34
add back get_open_interest_history() function
Jan 6, 2026
9473ac0
add back get_open_interest_history() function
Jan 6, 2026
41b3bda
重复字段常量类提取
Jan 8, 2026
eeb851e
Merge pull request #131 from okxapi/release/version_0.4.1
okxapi Jan 8, 2026
199d6ff
Skip dev dependencies in setup.py so pip install only pulls runtime deps
aisk Jun 9, 2026
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
Prev Previous commit
Next Next commit
add tradeQuoteCcy request param to the trade-related endpoints
  • Loading branch information
zihao.jiang
zihao.jiang committed Jan 5, 2026
commit 149d3f492fef1522a2c16a9247c375687bf679ea
25 changes: 25 additions & 0 deletions okx/TradingData.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,31 @@ def get_taker_block_volume(self, ccy, period=''):
params = {'ccy': ccy, 'period': period}
return self._request_with_params(GET, TAKER_FLOW, params)

def get_contracts_open_interest_history(self, instId, period=None, begin=None, end=None, limit=None):
"""
Get contract open interest history
Retrieve the contract open interest statistics of futures and perp.
Rate limit: 10 requests per 2 seconds
Rate limit rule: IP + Instrument ID

:param instId: Instrument ID, e.g. BTC-USDT-SWAP. Only applicable to FUTURES, SWAP
:param period: Bar size, the default is 5m, e.g. [5m/15m/30m/1H/2H/4H]
:param begin: Return records newer than the requested ts
:param end: Pagination of data to return records earlier than the requested ts
:param limit: Number of results per request. The maximum is 100. The default is 100
:return: API response
"""
params = {'instId': instId}
if period is not None:
params['period'] = period
if begin is not None:
params['begin'] = begin
if end is not None:
params['end'] = end
if limit is not None:
params['limit'] = limit
return self._request_with_params(GET, CONTRACTS_OPEN_INTEREST_HISTORY, params)




Expand Down
1 change: 1 addition & 0 deletions okx/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@
OPEN_INTEREST_VOLUME_EXPIRY = '/api/v5/rubik/stat/option/open-interest-volume-expiry'
INTEREST_VOLUME_STRIKE = '/api/v5/rubik/stat/option/open-interest-volume-strike'
TAKER_FLOW = '/api/v5/rubik/stat/option/taker-block-volume'
CONTRACTS_OPEN_INTEREST_HISTORY = '/api/v5/rubik/stat/contracts/open-interest-history'

# Trade
PLACR_ORDER = '/api/v5/trade/order'
Expand Down
12 changes: 10 additions & 2 deletions test/test_trading_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,16 @@ def test_open_interest_volume_strike(self):
print(self.TradingDataAPI.get_interest_volume_strike(ccy="BTC",expTime="20220901"))

"""
def test_taker_block_vol(self):
print(self.TradingDataAPI.get_taker_flow(ccy='BTC'))

def test_get_contracts_open_interest_history(self):
print(self.TradingDataAPI.get_contracts_open_interest_history(instId='BTC-USDT-SWAP'))

def test_get_contracts_open_interest_history_with_params(self):
print(self.TradingDataAPI.get_contracts_open_interest_history(
instId='BTC-USDT-SWAP',
period='1H',
limit='50'
))

if __name__ == "__main__":
unittest.main()
177 changes: 177 additions & 0 deletions test/unit/okx/test_trading_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
"""
Unit tests for okx.TradingData module

Mirrors the structure: okx/TradingData.py -> test/unit/okx/test_trading_data.py
"""
import unittest
from unittest.mock import patch
from okx.TradingData import TradingDataAPI
from okx import consts as c


class TestTradingDataAPIContractsOpenInterestHistory(unittest.TestCase):
"""Unit tests for the get_contracts_open_interest_history method"""

def setUp(self):
"""Set up test fixtures"""
self.trading_data_api = TradingDataAPI(flag='0')

@patch.object(TradingDataAPI, '_request_with_params')
def test_get_contracts_open_interest_history_with_required_params(self, mock_request):
"""Test get_contracts_open_interest_history with required parameters only"""
# Arrange
mock_response = {
'code': '0',
'msg': '',
'data': [
{'ts': '1609459200000', 'oi': '100000', 'oiCcy': '10'}
]
}
mock_request.return_value = mock_response

# Act
result = self.trading_data_api.get_contracts_open_interest_history(
instId='BTC-USDT-SWAP'
)

# Assert
expected_params = {
'instId': 'BTC-USDT-SWAP'
}
mock_request.assert_called_once_with(c.GET, c.CONTRACTS_OPEN_INTEREST_HISTORY, expected_params)
self.assertEqual(result, mock_response)

@patch.object(TradingDataAPI, '_request_with_params')
def test_get_contracts_open_interest_history_with_all_params(self, mock_request):
"""Test get_contracts_open_interest_history with all parameters provided"""
# Arrange
mock_response = {
'code': '0',
'msg': '',
'data': [
{'ts': '1609459200000', 'oi': '100000', 'oiCcy': '10'}
]
}
mock_request.return_value = mock_response

# Act
result = self.trading_data_api.get_contracts_open_interest_history(
instId='BTC-USDT-SWAP',
period='1H',
begin='1609459200000',
end='1609545600000',
limit='50'
)

# Assert
expected_params = {
'instId': 'BTC-USDT-SWAP',
'period': '1H',
'begin': '1609459200000',
'end': '1609545600000',
'limit': '50'
}
mock_request.assert_called_once_with(c.GET, c.CONTRACTS_OPEN_INTEREST_HISTORY, expected_params)
self.assertEqual(result, mock_response)

@patch.object(TradingDataAPI, '_request_with_params')
def test_get_contracts_open_interest_history_with_period(self, mock_request):
"""Test get_contracts_open_interest_history with period parameter"""
# Arrange
mock_response = {'code': '0', 'msg': '', 'data': []}
mock_request.return_value = mock_response

# Act
result = self.trading_data_api.get_contracts_open_interest_history(
instId='ETH-USDT-SWAP',
period='5m'
)

# Assert
expected_params = {
'instId': 'ETH-USDT-SWAP',
'period': '5m'
}
mock_request.assert_called_once_with(c.GET, c.CONTRACTS_OPEN_INTEREST_HISTORY, expected_params)

@patch.object(TradingDataAPI, '_request_with_params')
def test_get_contracts_open_interest_history_different_periods(self, mock_request):
"""Test get_contracts_open_interest_history with different period values"""
mock_response = {'code': '0', 'msg': '', 'data': []}
mock_request.return_value = mock_response

periods = ['5m', '15m', '30m', '1H', '2H', '4H', '6H', '12H', '1D', '2D', '3D', '5D', '1W', '1M', '3M']

for period in periods:
mock_request.reset_mock()
result = self.trading_data_api.get_contracts_open_interest_history(
instId='BTC-USDT-SWAP',
period=period
)

call_args = mock_request.call_args[0][2]
self.assertEqual(call_args['period'], period)

@patch.object(TradingDataAPI, '_request_with_params')
def test_get_contracts_open_interest_history_different_inst_ids(self, mock_request):
"""Test get_contracts_open_interest_history with different instrument IDs"""
mock_response = {'code': '0', 'msg': '', 'data': []}
mock_request.return_value = mock_response

inst_ids = ['BTC-USDT-SWAP', 'ETH-USDT-SWAP', 'BTC-USD-SWAP', 'BTC-USDT-240329']

for inst_id in inst_ids:
mock_request.reset_mock()
result = self.trading_data_api.get_contracts_open_interest_history(
instId=inst_id
)

call_args = mock_request.call_args[0][2]
self.assertEqual(call_args['instId'], inst_id)

@patch.object(TradingDataAPI, '_request_with_params')
def test_get_contracts_open_interest_history_with_pagination(self, mock_request):
"""Test get_contracts_open_interest_history with pagination parameters"""
# Arrange
mock_response = {'code': '0', 'msg': '', 'data': []}
mock_request.return_value = mock_response

# Act
result = self.trading_data_api.get_contracts_open_interest_history(
instId='BTC-USDT-SWAP',
begin='1609459200000',
end='1609545600000',
limit='100'
)

# Assert
expected_params = {
'instId': 'BTC-USDT-SWAP',
'begin': '1609459200000',
'end': '1609545600000',
'limit': '100'
}
mock_request.assert_called_once_with(c.GET, c.CONTRACTS_OPEN_INTEREST_HISTORY, expected_params)

@patch.object(TradingDataAPI, '_request_with_params')
def test_get_contracts_open_interest_history_utc_periods(self, mock_request):
"""Test get_contracts_open_interest_history with UTC+0 period values"""
mock_response = {'code': '0', 'msg': '', 'data': []}
mock_request.return_value = mock_response

utc_periods = ['6Hutc', '12Hutc', '1Dutc', '2Dutc', '3Dutc', '5Dutc', '1Wutc', '1Mutc', '3Mutc']

for period in utc_periods:
mock_request.reset_mock()
result = self.trading_data_api.get_contracts_open_interest_history(
instId='BTC-USDT-SWAP',
period=period
)

call_args = mock_request.call_args[0][2]
self.assertEqual(call_args['period'], period)


if __name__ == '__main__':
unittest.main()

Loading