I am trying to automate extracting from the Crypto.com exchange API, but I’m stuck on the Order, Trade, and Transaction History API. The connection works, but my dataset is empty. I emailed crypto.com support, but I could not get it resolved. I have a list of transactions in my history, but I am not able to retrieve them with my code below. I can get the methods for account balances to work, but not for transaction and order history.
Can anyone please suggests what I may be missing or what is wrong with my codes?
https://exchange-docs.crypto.com/exchange/v1/rest-ws/index.html#order-trade-transaction-history-api
import requests
import time
import hmac
import hashlib
import json
import datetime
import time
from datetime import datetime
def load_api_credentials(file_path):
#"""Load API credentials from a JSON file."""
with open(file_path, 'r') as file:
credentials = json.load(file)
return credentials['API_KEY'], credentials['SECRET_KEY']
def get_subaccount_balances(api_key, secret_key):
#"""Retrieve the get-order-history from Crypto.com Exchange API."""
url = 'https://api.crypto.com/exchange/v1/private/get-order-history'
nonce = str(int(time.time() * 1000))
fromDate = int(datetime.strptime('2023-11-15', '%Y-%m-%d').timestamp() * 1000)
toDate = int(datetime.strptime('2024-07-08', '%Y-%m-%d').timestamp() * 1000)
print(fromDate)
print(toDate)
req = {
'id': 1,
'method': 'private/get-order-history',
'api_key': api_key,
'params': { "instrument_name": "BTCUSD-PERP",
"start_time": fromDate,
"end_time": toDate,
"limit": 20},
'nonce': nonce
}
print(nonce)
param_str = ""
MAX_LEVEL = 3
def params_to_str(obj, level):
if level >= MAX_LEVEL:
return str(obj)
return_str = ""
for key in sorted(obj):
return_str += key
if obj[key] is None:
return_str += 'null'
elif isinstance(obj[key], list):
return_str += ''.join([params_to_str(subObj, level + 1) for subObj in obj[key]])
else:
return_str += str(obj[key])
return return_str
if "params" in req:
param_str = params_to_str(req['params'], 0)
payload_str = req['method'] + str(req['id']) + req['api_key'] + param_str + str(req['nonce'])
req['sig'] = hmac.new(bytes(str(secret_key), 'utf-8'), msg=bytes(payload_str, 'utf-8'), digestmod=hashlib.sha256).hexdigest()
headers = {'Content-Type': 'application/json'}
response = requests.post(url, headers=headers, json=req)
return response.json()
# Load API credentials from JSON file
API_KEY, SECRET_KEY = load_api_credentials('config.json')
# Get the Order Trade History
balances = get_subaccount_balances(API_KEY, SECRET_KEY)
if balances:
print(json.dumps(balances, indent=4))
Can you please review my codes, and if there’s anyone who has a working Python program to connect to the crypto.com API, your help is greatly appreciated to help me overcome this issue.
Taroun Napaul is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.