-
Notifications
You must be signed in to change notification settings - Fork 0
/
CryptoStats
144 lines (115 loc) · 6.93 KB
/
CryptoStats
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import time
import requests
import os
#dictionary that contains all the symbols for the binance API calls
priceSymbols = {'bitcoin': 'BTCUSDT', 'ripple': "XRPBTC",
'ethereum': 'ETHBTC', 'BCC': 'BCCBTC',
'LTC': 'LTCBTC', 'Dash': 'DASHBTC',
'Monero': 'XMRBTC', 'Qtum': 'QTUMBTC', 'ETC': 'ETCBTC',
'Zcash': 'ZECBTC', 'ADA': 'ADABTC', 'ADX': 'ADXBTC', 'AION' : 'AIONBTC', 'AMB': 'AMBBTC', 'APPC': 'APPCBTC', 'ARK': 'ARKBTC', 'ARN': 'ARNBTC', 'AST': 'ASTBTC', 'BAT': 'BATBTC', 'BCD': 'BCDBTC', 'BCPT': 'BCPTBTC', 'BNB': 'BNBBTC', 'BNT': 'BNTBTC', 'BQX': 'BQXBTC', 'BRD': 'BRDBTC', 'BTS': 'BTSBTC', 'CDT': 'CDTBTC', 'CMT': 'CMTBTC', 'CND': 'CNDBTC', 'CTR':'CTRBTC', 'DGD': 'DGDBTC', 'DLT': 'DLTBTC', 'DNT': 'DNTBTC', 'EDO': 'EDOBTC', 'ELF': 'ELFBTC', 'ENG': 'ENGBTC', 'ENJ': 'ENJBTC', 'EOS': 'EOSBTC', 'EVX': 'EVXBTC', 'FUEL': 'FUELBTC', 'FUN': 'FUNBTC', 'GAS': 'GASBTC', 'GTO': 'GTOBTC', 'GVT': 'GVTBTC', 'GXS': 'GXSBTC', 'HSR': 'HSRBTC', 'ICN': 'ICNBTC', 'ICX': 'ICXBTC', 'IOTA': "IOTABTC", 'KMD': 'KMDBTC', 'KNC': 'KNCBTC', 'LEND': 'LENDBTC', 'LINK':'LINKBTC', 'LRC':'LRCBTC', 'LSK':'LSKBTC', 'LUN': 'LUNBTC', 'MANA': 'MANABTC', 'MCO': 'MCOBTC', 'MDA': 'MDABTC', 'MOD': 'MODBTC', 'MTH': 'MTHBTC', 'MTL': 'MTLBTC', 'NAV': 'NAVBTC', 'NEBL': 'NEBLBTC', 'NEO': 'NEOBTC', 'NULS': 'NULSBTC', 'OAX': 'OAXBTC', 'OMG': 'OMGBTC', 'OST': 'OSTBTC', 'POE': 'POEBTC', 'POWR': 'POWRBTC', 'PPT': 'PPTBTC', 'QSP': 'QSPBTC', 'RCN': 'RCNBTC', 'RDN': 'RDNBTC', 'REQ': 'REQBTC', 'SALT': 'SALTBTC', 'SNGLS': 'SNGLSBTC', 'SNM': 'SNMBTC', 'SNT': 'SNTBTC', 'STORJ': 'STORJBTC', 'STRAT': 'STRATBTC', 'SUB': 'SUBBTC', 'TNB': 'TNBBTC', 'TNT': 'TNTBTC', 'TRIG': 'TRIGBTC', 'TRX': 'TRXBTC', 'VEN': 'VENBTC', 'VIB': 'VIBBTC', 'VIBE': 'VIBEBTC', 'WABI': 'WABIBTC', 'WAVES': 'WAVESBTC', 'WINGS': 'WINGSBTC', 'WTC': 'WTCBTC', 'XVG': 'XVGBTC', 'XZC': 'XZCBTC', 'YOYO': 'YOYOBTC', 'ZRX': 'ZRXBTC'}
#dictionarys to store the data after its read in from a text file.
cryptoOpenPriceData = {}
cryptoClosePriceData = {}
cryptoVolumeData = {}
#path to save the different text files in
cryptoPaths = r'C:\Users\DrewG\Documents\GitHub\Crypto\CryptoData'
logPath = r'C:\Users\DrewG\Documents\GitHub\Crypto\Logs\dlog.txt'
#one day in ms
ONE_DAY = 86400000
ONE_THIRD_DAY = 28800000
def getData(numDays):
#code for writing the values into three text files for each crypto: an open price, close price, and volume file.
for key, value in priceSymbols.items():
#creating the file path lengths and opening them
openPriceCryptoPath = os.path.join(cryptoPaths, value + "OpenPrice" + ".txt")
closePriceCryptoPath = os.path.join(cryptoPaths, value + "ClosePrice" + ".txt")
volumeCryptoPath = os.path.join(cryptoPaths, value + "Volume" + ".txt")
oprice = open(openPriceCryptoPath, "w")
cprice = open(closePriceCryptoPath, "w")
volume = open(volumeCryptoPath, "w")
#while loop with a counter to make sure that the start and endtime stay one day apart but go backwards in time, numdays amount of days
timeBackwards = 0
while(timeBackwards < ONE_DAY*numDays):
endTime = requests.get("https://api.binance.com/api/v1/time")
endTime = endTime.json()
endTime = endTime['serverTime'] - timeBackwards
startTime = endTime - ONE_THIRD_DAY
parameters = {'symbol': value, 'startTime': startTime, 'endTime': endTime, 'interval': '1m'}
data = requests.get("https://api.binance.com/api/v1/klines", params=parameters)
data = data.json()
print("Length of data set: {} coin associated with data set: {} data set: {}".format(len(data), value, data))
for i in reversed(data):
oprice.write("{},".format(i[1]))
cprice.write("{},".format(i[4]))
volume.write("{},".format(i[5]))
timeBackwards += ONE_THIRD_DAY
#closing all the files once we're done
oprice.close()
cprice.close()
volume.close()
def getOpenPrice():
#iterating through all the crypto symbols
for key, value in priceSymbols.items():
# creating the path lengths and opening the openprice file with read permissions
openPriceCryptoPath = os.path.join(cryptoPaths, value + "OpenPrice" + ".txt")
oprice = open(openPriceCryptoPath, "r")
#reading through the file
odata = oprice.readlines()
# iterating through each file and adding the correct open prices to the dictionary
for line in odata:
words = line.split(",")
# if there is not already a dictionary created for the value create one and put the first value in it
if (cryptoOpenPriceData == {} or value not in cryptoOpenPriceData):
temp = {value: words}
cryptoOpenPriceData.update(temp)
# otherwise append the price to the list that is already there
else:
cryptoOpenPriceData[value].append(words)
return cryptoOpenPriceData
def getClosePrice():
#iterating through all the crypto symbols
for key, value in priceSymbols.items():
#creating the path lengths and opening the close price file with read permissions
closePriceCryptoPath = os.path.join(cryptoPaths, value + "ClosePrice" + ".txt")
cprice = open(closePriceCryptoPath, "r")
#reading through the file
cdata = cprice.readlines()
#iterating through each file and adding the correct close price to the dictionary
for line in cdata:
words = line.split(",")
if (cryptoClosePriceData == {} or value not in cryptoClosePriceData):
temp = {value: words}
cryptoClosePriceData.update(temp)
else:
cryptoClosePriceData[value].append(words)
return cryptoClosePriceData
def getVolume():
#iterate through all the crypto symbols
for key, value in priceSymbols.items():
# creating the path lengths and opening the files with read permissions
volumeCryptoPath = os.path.join(cryptoPaths, value + "Volume" + ".txt")
volume = open(volumeCryptoPath, "r")
# reading through the volume file of the files
vol = volume.readlines()
# iterating through each file and adding the volume data to the dictionary
for line in vol:
openprice = line.split(",")
if (cryptoVolumeData == {} or value not in cryptoVolumeData):
temp = {value: openprice}
cryptoVolumeData.update(temp)
else:
cryptoVolumeData[value].append(openprice)
return cryptoVolumeData
def main():
file = open(logPath, "w")
a = getOpenPrice()
b = getClosePrice()
c = getVolume()
file.write("Open Price Data: {} \n".format(a))
file.write("----- \n")
file.write("Close Price Data: {} \n".format(b))
file.write("------ \n")
file.write("Volume Data: {} \n".format(c))
file.close()
if __name__ == "__main__":
main()