Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion okx/TradingData.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ 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):
def get_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.
Expand Down
18 changes: 9 additions & 9 deletions test/test_trading_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ def test_open_interest_volume_strike(self):

"""

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'
))
# def test_get_open_interest_history(self):
# print(self.TradingDataAPI.get_open_interest_history(instId='BTC-USDT-SWAP'))
#
# def test_get_open_interest_history_with_params(self):
# print(self.TradingDataAPI.get_open_interest_history(
# instId='BTC-USDT-SWAP',
# period='1H',
# limit='50'
# ))

if __name__ == "__main__":
unittest.main()
44 changes: 22 additions & 22 deletions test/unit/okx/test_trading_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@


class TestTradingDataAPIContractsOpenInterestHistory(unittest.TestCase):
"""Unit tests for the get_contracts_open_interest_history method"""
"""Unit tests for the get_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"""
def test_get_open_interest_history_with_required_params(self, mock_request):
"""Test ge_open_interest_history with required parameters only"""
# Arrange
mock_response = {
'code': '0',
Expand All @@ -30,7 +30,7 @@ def test_get_contracts_open_interest_history_with_required_params(self, mock_req
mock_request.return_value = mock_response

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

Expand All @@ -42,8 +42,8 @@ def test_get_contracts_open_interest_history_with_required_params(self, mock_req
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"""
def test_get_open_interest_history_with_all_params(self, mock_request):
"""Test get_open_interest_history with all parameters provided"""
# Arrange
mock_response = {
'code': '0',
Expand All @@ -55,7 +55,7 @@ def test_get_contracts_open_interest_history_with_all_params(self, mock_request)
mock_request.return_value = mock_response

# Act
result = self.trading_data_api.get_contracts_open_interest_history(
result = self.trading_data_api.get_open_interest_history(
instId='BTC-USDT-SWAP',
period='1H',
begin='1609459200000',
Expand All @@ -75,14 +75,14 @@ def test_get_contracts_open_interest_history_with_all_params(self, mock_request)
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"""
def test_get_open_interest_history_with_period(self, mock_request):
"""Test get_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(
result = self.trading_data_api.get_open_interest_history(
instId='ETH-USDT-SWAP',
period='5m'
)
Expand All @@ -95,16 +95,16 @@ def test_get_contracts_open_interest_history_with_period(self, mock_request):
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"""
def test_get_open_interest_history_different_periods(self, mock_request):
"""Test get_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(
result = self.trading_data_api.get_open_interest_history(
instId='BTC-USDT-SWAP',
period=period
)
Expand All @@ -113,31 +113,31 @@ def test_get_contracts_open_interest_history_different_periods(self, mock_reques
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"""
def test_get_open_interest_history_different_inst_ids(self, mock_request):
"""Test get_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(
result = self.trading_data_api.get_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"""
def test_get_open_interest_history_with_pagination(self, mock_request):
"""Test get_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(
result = self.trading_data_api.get_open_interest_history(
instId='BTC-USDT-SWAP',
begin='1609459200000',
end='1609545600000',
Expand All @@ -154,16 +154,16 @@ def test_get_contracts_open_interest_history_with_pagination(self, mock_request)
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"""
def test_get_open_interest_history_utc_periods(self, mock_request):
"""Test get_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(
result = self.trading_data_api.get_open_interest_history(
instId='BTC-USDT-SWAP',
period=period
)
Expand Down
Loading