-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeckoFuncz.py
252 lines (164 loc) · 5.88 KB
/
geckoFuncz.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
from time import *
from datetime import *
import time
import json
import dateparser
import math
import numpy as np
def createJsonFunc(jsonOutAddr, jsonData):
try:
with open(jsonOutAddr, 'w') as fp1: json.dump(jsonData, fp1)
functionOutput = ("\nSuccess Creating JSON at: " + str(jsonOutAddr) + "\n")
except Exception as e:
functionOutput = "\nFailed to create JSON. Error msg:\n" + str(e)
return functionOutput
def readJsonFunc(jsonInAddr):
with open(jsonInAddr, 'r') as r:
jsonOutputDict = json.load(r)
return jsonOutputDict
# ---
# for api request part of fetchGecko
# ---
# convert a unix epoch value to YYYY-MM-DD
def epochToDatetime(epochTime):
localTime = time.strftime('%Y-%m-%d', time.localtime(epochTime))
return localTime
# get data from the coingecko API using a coin's symbol and a base fiat currency
def getCoinDict(coin, baseCurrency):
from pycoingecko import CoinGeckoAPI
cg = CoinGeckoAPI()
coinApiRez = cg.get_coin_market_chart_by_id(id=coin,vs_currency=baseCurrency,days='365')
coinRezPrices = coinApiRez['prices']
coinRezVolumes = coinApiRez['total_volumes']
volumeDict, priceDict = {}, {}
for price in coinRezPrices:
priceIndex = coinRezPrices.index(price)
unixTime = price[0]
volume = coinRezVolumes[priceIndex][1]
unixTime = int(str(unixTime)[:-3])
price = price[1]
localDT = epochToDatetime(unixTime)
priceDict.update({localDT: price})
volumeDict.update({localDT: volume})
returnDict = {"base": baseCurrency, "quote": coin, "data": priceDict, "volumeData": volumeDict}
return returnDict
# potentially unneccessary function to call the previous function
# was useful for when this supported multiple base currencies
def fetchTokenData(tokenName):
#tokenName = symbolNameFunc(tokenSymbol)
tokenName = tokenName.lower()
tokenUsd = getCoinDict(tokenName, 'usd')
tokenUsd = [tokenUsd]
return tokenUsd
# get all the tokens in the data/symbolNames.json file
def getAllTokens(symbolNameDict):
tokenDataDict = {}
symbols = list(symbolNameDict.keys())
for symbol in symbols:
symbolName = symbolNameDict[symbol]
symbolDataList = fetchTokenData(symbolName)
for symbolData in symbolDataList:
symbolBase = symbolData['base']
pair = str(symbol).capitalize() + str(symbolBase).capitalize()
tokenDataDict.update({pair: symbolData})
return tokenDataDict
# ---
# for sample stats part of fetchGecko
# ---
# return the percent change between two numbers
def percentChange(fromNum, toNum):
pChange = ((toNum - fromNum)/fromNum)*100
return pChange
# function that returns 52W-high
def listMaxFunc(targetDict):
firstKey = (list(targetDict.keys()))[0]
maxItem = {firstKey: targetDict[firstKey]}
targetKeys = targetDict.keys()
for targetKey in targetKeys:
maxDate = list(maxItem.keys())[0]
if targetDict[targetKey] > maxItem[maxDate]:
maxItem = {targetKey: targetDict[targetKey]}
return maxItem
# function that returns 52W-low
def listMinFunc(targetDict):
firstKey = (list(targetDict.keys()))[0]
minItem = {firstKey: targetDict[firstKey]}
targetKeys = targetDict.keys()
for targetKey in targetKeys:
minDate = list(minItem.keys())[0]
if targetDict[targetKey] < minItem[minDate]:
minItem = {targetKey: targetDict[targetKey]}
return minItem
# function that returns 52W avg price
def listAvgFunc(targetDict):
targetList = list(targetDict.values())
sumOfRates = 0
for rate in targetList:
sumOfRates += rate
avgRate = sumOfRates / len(targetList)
return avgRate
def stdDevFunc(currentGeckoDict):
currentPriceData = currentGeckoDict['data']
currentAvg = currentGeckoDict['avg']
currentMeanDevSquaredSum = 0
for currentDate in currentPriceData:
currentPrice = currentPriceData[currentDate]
currentMeanDeviation = currentPrice - currentAvg
currentMeanDevSquaredSum += currentMeanDeviation * currentMeanDeviation
currentVariance = currentMeanDevSquaredSum / len(currentPriceData)
currentStdDev = math.sqrt(currentVariance)
return currentStdDev
# function that returns avg, 52W-low, and 52W-high
def analyzeTokenFunc(targetDict):
#targetDict = geckoData[targetBase.lower()][targetPair]['data']
targetQuote, targetBase = targetDict['quote'], targetDict['base']
targetDictList = targetDict['data']
targetPair = str(targetQuote).capitalize() + str(targetBase).capitalize()
listAvgPrice = listAvgFunc(targetDictList)
listMinPrice = listMinFunc(targetDictList)
listMaxPrice = listMaxFunc(targetDictList)
listAnalysis = {'pair': targetPair, 'avg': listAvgPrice, 'max': listMaxPrice, 'min': listMinPrice}
return listAnalysis
# function to run analysis function on all pairs in gecko dictionary
def analyzeAllTokens(geckoData):
analysisList = []
quoteKeys = list(geckoData.keys())
for quoteKey in quoteKeys:
pairDict = geckoData[quoteKey]
analyzePair = analyzeTokenFunc(pairDict)
analysisList.append(analyzePair)
return analysisList
# ---
# for finding simple moving avgs in movingAvgs.py
# ---
def nDayFunc(ogDt, n):
nDayList = []
n = n - 1
nDayList.append(ogDt)
halfN = n / 2
for dayCounter in np.arange(halfN):
dtDt = dateparser.parse(ogDt, settings={'TIMEZONE': 'UTC'})
dayCounter = dayCounter+1
addDaysFull = dtDt + timedelta(days = dayCounter)
subtractDaysFull = dtDt - timedelta(days = dayCounter)
addDaysSplit = str(addDaysFull).split(" ")
subtractDaysSplit = str(subtractDaysFull).split(" ")
addDays = addDaysSplit[0]
subtractDays = subtractDaysSplit[0]
nDayList.append(addDays)
nDayList.append(subtractDays)
nDayList.sort(reverse=False)
nDayList.pop()
return nDayList
def movingAvgFunc(dataSet, centerDate, n):
nDayList = nDayFunc(centerDate, n)
analysisDict = {}
dateKeys = list(dataSet.keys())
for dateKey in dateKeys:
if dateKey in nDayList:
currentPrice = dataSet[dateKey]
currentPriceDict = {dateKey: currentPrice}
analysisDict.update(currentPriceDict)
dataAvgPrice = listAvgFunc(analysisDict)
movingAvgDict = {centerDate: dataAvgPrice}
return movingAvgDict