-
Notifications
You must be signed in to change notification settings - Fork 9
/
getdata.py
60 lines (47 loc) · 1.57 KB
/
getdata.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
import os
import time
import pandas as pd
FETCH_URL = "https://poloniex.com/public?command=returnChartData¤cyPair=%s&start=%d&end=%d&period=300"
#PAIR_LIST = ["BTC_ETH"]
DATA_DIR = "data"
COLUMNS = ["date","high","low","open","close","volume","quoteVolume","weightedAverage"]
def get_data(pair):
datafile = os.path.join(DATA_DIR, pair+".csv")
timefile = os.path.join(DATA_DIR, pair)
if os.path.exists(datafile):
newfile = False
start_time = int(open(timefile).readline()) + 1
else:
newfile = True
start_time = 1388534400 # 2014.01.01
end_time = 9999999999#start_time + 86400*30
url = FETCH_URL % (pair, start_time, end_time)
print("Get %s from %d to %d" % (pair, start_time, end_time))
df = pd.read_json(url, convert_dates=False)
#import pdb;pdb.set_trace()
if df["date"].iloc[-1] == 0:
print("No data.")
return
end_time = df["date"].iloc[-1]
ft = open(timefile,"w")
ft.write("%d\n" % end_time)
ft.close()
outf = open(datafile, "a")
if newfile:
df.to_csv(outf, index=False, cols=COLUMNS)
else:
df.to_csv(outf, index=False, cols=COLUMNS, header=False)
outf.close()
print("Finish.")
time.sleep(30)
def main():
if not os.path.exists(DATA_DIR):
os.mkdir(DATA_DIR)
df = pd.read_json("https://poloniex.com/public?command=return24hVolume")
pairs = [pair for pair in df.columns if pair.startswith('BTC')]
print(pairs)
for pair in pairs:
get_data(pair)
time.sleep(2)
if __name__ == '__main__':
main()