-
Notifications
You must be signed in to change notification settings - Fork 0
/
ftx01utc-csv.py
90 lines (84 loc) · 4.16 KB
/
ftx01utc-csv.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
import requests
import time, datetime
import random
import sys
import csv
def savetocsv(datatosave, filename, doyouwantcolumntitles = True):
try:
f = csv.writer(open(filename,"a+"))
firstrow = list(datatosave[0])
print("saving columns: ", firstrow)
if doyouwantcolumntitles:
f.writerow(firstrow)
for item in datatosave:
f.writerow([item[gg] for gg in firstrow])
except:
sys.exit("error with saving csv file. quiting")
def getlastunix(filename):
try:
r = csv.reader(open(filename))
print("existing file ", filename,"read success...")
return int(list(r)[-1][0]) # read first column of the last row where is unix timestamp
except:
print("did not find file with filename ", filename)
return 0
def insertcoinprice(unixtime, pricedate, price, volume):
pricedate = datetime.datetime.strptime(str(pricedate),"%a %b %d %H:%M:%S %Y") # string pricedate to date format
first = {"unix": unixtime , "date": pricedate ,"price": float(price),"vol": float(volume)} # compose JSON datarow
return first
# API request URL example
# URL = "https://ftx.com/api/markets/BTC/USD/candles?resolution=3600&start_time=1559881511"
def main():
interval = 60 # 60 (minute) #300 #900 # 3600 (hourly) change here interval
collection = []
try:
firstpromt = sys.argv
gg = [x.upper() for x in firstpromt]
listofcollections = gg[1:]
if listofcollections == []:
raise Exception
print("whole collection:", *listofcollections, "time interval is:", interval)
except:
print("no coin pair given. Usage is follows: python3 ftxutc-csv.py BTC/USD EHT/USD")
listofcollections = ["BTC/USD"]
n = 0
while True: # forever loop
for api_symbol in listofcollections:
collection = [] #empy collection before start
print('\033[1;34m', "updating coin: ", api_symbol, '\033[0;0m')
filename = api_symbol.replace("/","-") + "int" + str(interval) + ".csv" # sanitize file name string replacing unacceptable symbols, add interval indicator
startunix = getlastunix(filename)
URL = "https://ftx.com/api/markets/" + api_symbol + "/candles?resolution=" + str(interval) + "&start_time=" + str(startunix)
print("API url: ", URL, sep="")
print("unix time to start: ",startunix)
try:
r = requests.get(url = URL)
except:
print("some error when getting API data")
print("api returned status code: ",r.status_code)
if r.status_code != 200:
print("some error while accesing api URL.")
continue
data = r.json()
data = data["result"]
data = data[:-1]
if len(data)<1: # no new data
continue
for gg in data:
if str(startunix) < str(int(gg["time"]/1000)): # time.asctime(time.gmtime(now)) include only new entries, reduce number of zeros in unix time
collection.append(insertcoinprice(int(gg["time"]/1000),time.asctime(time.gmtime(gg["time"]/1000)),gg["close"],gg["volume"])) #add more if needed after time gg["high"]
else:
print('\033[1;33m',"waiting for new data...",'\033[0;0m'," last unixtime: ", str(startunix), " now is: ", int(time.time()), sep="")
if collection != []:
print(*collection, sep = "\n")
savetocsv(collection, filename,((False,True)[startunix == 0])) # update database with JSON collection
print("csv file updated with ", len(collection), " entries of ", '\033[1;33m', api_symbol, '\033[0;0m', sep="")
n += 1
sleeptime = 5 + random.randint(1, 20)
print("round: ",n,". sleeping for: ", sleeptime, " sec. press Ctrl-C to exit"," file name: " ,filename, sep="")
time.sleep(sleeptime)
sleeptime = 999 + random.randint(1, 1100)
print("===== sleeping for: ", sleeptime, "sec. press Ctrl-C to exit.")
time.sleep(sleeptime)
if __name__ == "__main__":
main()