· 5 years ago · Aug 13, 2020, 01:52 PM
1#!/usr/bin/env python
2# coding: utf-8
3
4# In[ ]:
5
6
7import hmac
8import hashlib
9import json
10import requests
11import pandas as pd
12import os
13import time
14from datetime import datetime, timedelta
15
16
17# In[ ]:
18
19
20# You can only request 24-hour periods (this script requests 23 hour periods because ... rounding)
21periods = 15
22
23
24# In[ ]:
25
26
27base_url = "https://api.crypto.com/v2/"
28
29
30# In[ ]:
31
32
33## Add authentication details
34API_KEY = os.environ['CRYPTO_COM_API_KEY']
35SECRET_KEY = os.environ['CRYPTO_COM_API_SEC']
36
37
38# In[ ]:
39
40
41try: # check to see if orders already exists
42 orders
43except NameError:
44 print("No orders to delete")
45else:
46 del(orders)
47
48
49# In[ ]:
50
51
52for j in range (0, periods):
53
54 print("Period", j)
55
56 if j == 0:
57 end_ts_dt = datetime.now()
58 start_ts_dt = end_ts_dt - timedelta(hours = 23)
59 start_ts = round(start_ts_dt.timestamp() * 1000)
60 end_ts = round(end_ts_dt.timestamp() * 1000)
61 else:
62 end_ts_dt = start_ts_dt
63 start_ts_dt = end_ts_dt - timedelta(hours = 23)
64 start_ts = round(start_ts_dt.timestamp() * 1000)
65 end_ts = round(end_ts_dt.timestamp() * 1000)
66
67 for i in range(0,101):
68 print("Page ", i)
69 ## Request orders history
70 nonce = int(round(time.time() * 1000))
71
72 req = {
73 "id": i+1,
74 "api_key": API_KEY,
75 "method": "private/get-order-history",
76 "params": {
77 "end_ts": end_ts,
78 "page": i,
79 "page_size": 100,
80 "start_ts": start_ts
81 },
82 "nonce": nonce
83 }
84
85 # This step isn't actually necessary with an empty params entry
86 paramString = ""
87
88 if "params" in req:
89 for key in req['params']:
90 paramString += key
91 paramString += str(req['params'][key])
92
93 sigPayload = req['method'] + str(req['id']) + req['api_key'] + paramString + str(req['nonce'])
94
95 req['sig'] = hmac.new(
96 bytes(str(SECRET_KEY), 'utf-8'),
97 msg=bytes(sigPayload, 'utf-8'),
98 digestmod=hashlib.sha256
99 ).hexdigest()
100
101 response = requests.post(base_url + "private/get-order-history", json=req)
102
103 if response.json()['result']['order_list'] == []:
104 time.sleep(1)
105 break
106
107 try: # check to see if fees already exists
108 orders
109 except NameError:
110 orders = pd.DataFrame.from_dict(response.json()['result']['order_list'])
111 else:
112 orders = orders.append(pd.DataFrame.from_dict(response.json()['result']['order_list']))
113
114 time.sleep(2)
115
116
117# In[ ]:
118
119
120# Turn the millis since epoch into a datetime and format for Cointracking.info
121orders.create_time = pd.to_datetime(orders.create_time, unit='ms')
122orders.update_time = pd.to_datetime(orders.update_time, unit='ms')
123# e.g. 2020-06-23T14:42:44+00
124orders.create_time = orders.create_time.dt.strftime('%Y-%m-%dT%H:%M:%S+00')
125orders.update_time = orders.update_time.dt.strftime('%Y-%m-%dT%H:%M:%S+00')
126
127# Split the instruments into two columns
128orders['left'], orders['right'] = orders['instrument_name'].str.split('_', 1).str
129
130
131# In[ ]:
132
133
134try: # check to see if fees already exists
135 fees
136except NameError:
137 print("No fees to delete")
138else:
139 del(fees)
140
141
142# In[ ]:
143
144
145for j in range (0, periods):
146
147 print("Period", j)
148
149 if j == 0:
150 end_ts_dt = datetime.now()
151 start_ts_dt = end_ts_dt - timedelta(hours = 23)
152 start_ts = round(start_ts_dt.timestamp() * 1000)
153 end_ts = round(end_ts_dt.timestamp() * 1000)
154 else:
155 end_ts_dt = start_ts_dt
156 start_ts_dt = end_ts_dt - timedelta(hours = 23)
157 start_ts = round(start_ts_dt.timestamp() * 1000)
158 end_ts = round(end_ts_dt.timestamp() * 1000)
159
160 for i in range(0,101):
161 print("Page", i)
162 ## The fee information does not seem to be included here.
163 # Individual trades can be summed (not sure if this is right - do they add up correctly?)
164 nonce = int(round(time.time() * 1000))
165
166 req = {
167 "id": i+1,
168 "method": "private/get-trades",
169 "api_key": API_KEY,
170 "params": {
171 "end_ts": end_ts,
172 "page": i,
173 "page_size": 100,
174 "start_ts": start_ts
175 },
176 "nonce": nonce
177 }
178
179 paramString = ""
180
181 if "params" in req:
182 for key in req['params']:
183 paramString += key
184 paramString += str(req['params'][key])
185
186 sigPayload = req['method'] + str(req['id']) + req['api_key'] + paramString + str(req['nonce'])
187
188 req['sig'] = hmac.new(
189 bytes(str(SECRET_KEY), 'utf-8'),
190 msg=bytes(sigPayload, 'utf-8'),
191 digestmod=hashlib.sha256
192 ).hexdigest()
193
194 response = requests.post(base_url + "private/get-trades", json=req)
195
196 if response.json()['result']['trade_list'] == []:
197 time.sleep(1)
198 break
199
200 try: # check to see if fees already exists
201 fees
202 except NameError:
203 fees = pd.DataFrame.from_dict(response.json()['result']['trade_list'])
204 else:
205 fees = fees.append(pd.DataFrame.from_dict(response.json()['result']['trade_list']))
206
207 time.sleep(2)
208
209
210# In[ ]:
211
212
213# Aggregate by order_id and fee_currency
214fees = fees.groupby(['order_id', 'fee_currency']).agg({'fee': 'sum', 'traded_price': 'mean', "traded_quantity": 'sum'}).reset_index()
215
216
217# In[ ]:
218
219
220# Merge into the order list
221out = orders.merge(fees, on = ["order_id"], how = "inner")
222
223
224# In[ ]:
225
226
227# Write to CSV
228out.to_csv("crypto_com.csv")
229