-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbybitutc-csv.py
119 lines (112 loc) · 6.41 KB
/
bybitutc-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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import requests
import time, datetime
import random
import sys
import csv
import os
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 getsavedcsvfiles():
try:
csvfilelist = [x[:-4] for x in os.listdir() if x.endswith(".csv") ] # select all file names with extension .csv, remove .csv part
timeendings = ("1", "3", "5", "15", "30", "60", "120", "240","360","720","D", "M")
coinlist,intervalist = zip(*[(x[:x.find("int")],x[x.find("int")+3:]) for x in csvfilelist if x.endswith(timeendings) ]) #select those csv files which ends with interval names
except:
coinlist, intervalist = [],[]
return coinlist, intervalist # returns list of coins, intervals from saved file names in the working directory
def getlastunix(filename):
try:
r = csv.reader(open(filename))
print("existing file ", filename,"read success...")
return int(float(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
def main(runoncenopromts = False):
# to read more API documentation here https://bybit-exchange.github.io/docs/
interval = "60" # change for other interval choices: 1, 3, 5, 15, 30, 60, 120, 240, 360, 720, D, M (string)
print(">> ",(("run forver","run once and quit")[runoncenopromts == True])," <<")
listofcollections = []
listofintervals = []
try:
firstpromt = sys.argv
gg = [x.upper() for x in firstpromt]
listofcollections = gg[1:] # get user input if he or she wants to add new coins
listofintervals = [interval for x in listofcollections] # add default time interval for coresponding user input
if listofcollections == []:
raise Exception
print("whole collection:", *listofcollections, "time interval is:", interval)
savedcoins, savedint = getsavedcsvfiles() # check if listed coins can be updated in csv files
addcoins, addint = [],[]
for c1, int1 in zip(savedcoins,savedint):
for c2, int2 in zip(listofcollections,listofintervals):
if (c1 == c2 and int1 != int2):
addcoins.append(c1)
addint.append(int1)
listofcollections.extend(addcoins)
listofintervals.extend(addint)
if len(listofcollections) > 0:
print("Found more csv files to update: ", ", ".join("{} {}".format(z1,z2) for z1,z2 in zip(addcoins,addint)))
except:
print("no coin pair given. Usage is follows: python3 bybitutc-csv.py BTCUSDT EHTUSDT")
listofcollections, listofintervals = getsavedcsvfiles()
if len(listofcollections) == 0: # no values given by user, no files in directory - use deault coin and interval
print("could not fine any saved files, using default values of BTCUSDT with interval of 1hour")
listofcollections = ["BTCUSDT"] # some default values if no csv files given
listofintervals = [interval]
n = 0
while True: # forever loop
for api_symbol, interval in zip(listofcollections, listofintervals):
collection = [] #empy collection before start
print('\033[1;34m', "updating coin: ", api_symbol, '\033[0;0m')
filename = api_symbol + "int" + interval + ".csv" # file name
startunix = getlastunix(filename)
# https://api-testnet.bybit.com/v5/market/kline?category=spot&symbol=BTCUSDT&interval=1&start=0
URL = "https://api-testnet.bybit.com/v5/market/kline?category=spot&symbol=" + api_symbol + "&interval=" + interval + "&start=" + str(startunix * 1000)
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"]["list"][::-1][:-1] #get data section, reverse, remove last column (waiting for the final data of the time period)
if len(data)<1: # no new data, skip to the next coin
continue
for gg in data:
if startunix < int(gg[0])/1000: # time.asctime(time.gmtime(now)) include only new entries, reduce number of zeros in unix time
collection.append(insertcoinprice(int(gg[0])/1000,time.asctime(time.gmtime(int(gg[0])/1000)),gg[4],gg[6])) #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 = 2 + random.randint(1, 10)
print("round: ",n,". sleeping for: ", sleeptime, " sec. press Ctrl-C to exit"," file name: " ,filename, " interval: ", interval ,sep="")
time.sleep(sleeptime)
if runoncenopromts:
break
sleeptime = 999 + random.randint(1, 1100)
print("===== sleeping for: ", sleeptime, "sec. press Ctrl-C to exit.")
time.sleep(sleeptime)
if __name__ == "__main__":
main(runoncenopromts=True) # set value True if running only once and quit, good for scheduling; False if to make this script to run forever loop