-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathpoloniex.py
93 lines (77 loc) · 2.91 KB
/
poloniex.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
u"""
fetch data from Poloniex Exchange
Created on 14/07/17
by fccoelho
license: GPL V3 or Later
"""
import pandas as pd
import datetime
def get_full_table(pair, start, end):
"""
Gets at most 300000 raw trades
Trade history records look like this:
[{"date":"2014-02-10 04:23:23","type":"buy","rate":"0.00007600","amount":"140","total":"0.01064"},{"date":"2014-02-10 01:19:37","type":"buy","rate":"0.00007600","amount":"655","total":"0.04978"}, ... ]
"""
df = pd.read_json(
'https://poloniex.com/public?command=returnTradeHistory¤cyPair={}&start={}&end={}'.format(pair, int(
start.timestamp()), int(end.timestamp())))
df.set_index(['date'], inplace=True)
print('fetched {} {} trades.'.format(df.size, pair))
return df
def get_price_table(pair, start, end):
"""
Poloniex API only returns maximum of 300000 trades or 1 year for each pair.
:returns:
dictionary with one dataframe per pair
"""
print('Downloading {} from {} to {}.'.format(pair, start, end))
df = get_full_table(pair, start, end)
df = df.resample('1T').mean() # resample in windows of 1 minute
df[pair] = df.rate
for cname in df.columns:
if cname != pair:
del df[cname]
return df
def concatenate_series(rates):
"""
:parameters:
- rates: dictionary with the pairs dataframes
"""
for k, df in rates.items(): # Solve non-unique indices
rates[k] = df.loc[~df.index.duplicated(keep='first')]
data = pd.concat(rates, axis=1)
data.columns = data.columns.droplevel(0)
print(data.columns)
data.columns = [name.lower() for name in data.columns] # convenient to save to PGSQL
return data
def extend_history(pair, df):
"""
Extends a dataframe with data on a pair with older data.
:param pair:
:param df:
:return:
"""
End = df.index.min()
Start = end - datetime.timedelta(days=364)
dfextra = get_price_table(pair, Start, End)
df = df.append(dfextra) # pd.concat([df,dfextra], axis=0)
return df
def get_ohlc(pair, start, end, save=False):
"""
Gets OHLC historical data aggregated in 5-minute candlesticks
:param pair: Currency pair, e.g. USDT_ETH
:param start: unix timestamp
:param end: unix timestamp
:return: dataframe
"""
print('Downloading {} from {} to {}.'.format(pair, start, end))
url = 'https://poloniex.com/public?command=returnChartData¤cyPair={}&start={}&end={}&resolution=auto'.format(pair,
int(start.timestamp()),
int(end.timestamp()))
print(url)
df = pd.read_json(url)
df.date = pd.to_datetime(df.date)
df.set_index(['date'], inplace=True)
if save:
df.to_pickle('{}.pickle')
return df