forked from veox/python3-krakenex
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerate-trades-history-csv.py
More file actions
executable file
·59 lines (48 loc) · 1.56 KB
/
generate-trades-history-csv.py
File metadata and controls
executable file
·59 lines (48 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python
# This file is part of krakenex.
# Licensed under the Simplified BSD license. See `examples/LICENSE.txt`.
# Saves trade history to CSV file.
#
# WARNING: submits a lot of queries in rapid succession!
# Maintainer: Austin.Deric@gmail.com (@AustinDeric on github)
import pandas as pd
import krakenex
import datetime
import calendar
import time
# takes date and returns nix time
def date_nix(str_date):
return calendar.timegm(str_date.timetuple())
# takes nix time and returns date
def date_str(nix_time):
return datetime.datetime.fromtimestamp(nix_time).strftime('%m, %d, %Y')
# return formatted TradesHistory request data
def date(start, end, ofs):
req_data = {'type': 'all',
'trades': 'true',
'start': str(date_nix(start)),
'end': str(date_nix(end)),
'ofs': str(ofs)
}
return req_data
k = krakenex.API()
k.load_key('kraken.key')
data = []
count = 0
for i in range(1,11):
start_date = datetime.datetime(2016, i+1, 1)
end_date = datetime.datetime(2016, i+2, 29)
th = k.query_private('TradesHistory', date(start_date, end_date, 1))
time.sleep(.25)
print(th)
th_error = th['error']
try:
if int(th['result']['count'])>0:
count += th['result']['count']
data.append(pd.DataFrame.from_dict(th['result']['trades']).transpose())
except Exception as e:
print(e)
trades = pd.DataFrame
trades = pd.concat(data, axis = 0)
trades = trades.sort_values(columns='time', ascending=True)
trades.to_csv('data.csv')