forked from Eloise1988/COINGECKO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCoinGecko.bas
414 lines (324 loc) · 14.2 KB
/
CoinGecko.bas
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
'====================================================================================================================================*
' CoinGecko Google Sheet Feed by Eloise1988
' ====================================================================================================================================
' Version: 1.0
' Project Page: https://github.com/Eloise1988/COINGECKO
' Copyright: (c) 2020 by Eloise1988
'
' License: GNU General Public License, version 3 (GPL-3.0)
' http://www.opensource.org/licenses/gpl-3.0.html
' ------------------------------------------------------------------------------------------------------------------------------------
' A library for importing CoinGecko's price, volume & market cap feeds into Google spreadsheets. Functions include:
'
' GECKOPRICE For use by end users to real-time cryptocurrency prices
' GECKOVOLUME For use by end users to real-time cryptocurrency 24h volumes
' GECKOCAP For use by end users to real-time cryptocurrency total market caps
' GECKOCHANGE For use by end users to real-time cryptocurrency % CHANGE price, volumes, market cap
' GECKOPRICEBYNAME For use by end users to real-time cryptocurrency prices with specific name
' GECKOATH For use by end users to All Time High cryptocurrency prices
'
'
'
'
' For bug reports see https://github.com/Eloise1988/COINGECKO/issues
'
' ------------------------------------------------------------------------------------------------------------------------------------
'Changelog:
'
' 1.0.0 Initial release
' *====================================================================================================================================*/
'
'
' GECKOPRICE
' * Imports CoinGecko's cryptocurrency prices into Google spreadsheets. The price feed is a ONE-dimensional array.
' * By default, data gets transformed into a number so it looks more like a normal price data import.
' * For example:
' *
' * =GECKOPRICE("BTC", "USD","$A$1")
' *
' *
' * @param {cryptocurrency} the cryptocurrency ticker you want the price from
' * @param {against fiat currency} the fiat currency ex: usd or eur
' * @param {parseOptions} an optional fixed cell for automatic refresh of the data
' * @customfunction
' *
' * @return a one-dimensional array containing the price
'
Public Function GECKOPRICE(ticker As String, currency1 As String)
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Application.StatusBar = "Loadind data. Please Wait..."
Dim httpObject As Object
Dim sGetResult As String
Set httpObject = CreateObject("Microsoft.XMLHTTP")
Dim i, lenght_array As Integer
Dim jsnstr() As String
sUrl = "https://api.coingecko.com/api/v3/search?locale=fr&img_path_only=1"
ticker = UCase(ticker)
currency1 = LCase(currency1)
sRequest = sUrl
httpObject.Open "GET", sRequest, False
httpObject.send
sGetResult = httpObject.responseText
lenght_array1 = Split(Split(sGetResult, "{""coins"":[")(1), "{""id"":""")
lenght_array = UBound(lenght_array1)
For i = 1 To lenght_array
jsnstr = Split(Split(Split(Split(sGetResult, "{""coins"":[")(1), "{""id"":")(i), ",""symbol"":""")(1), """,""market_cap_rank""")
If jsnstr(0) = ticker Then
id_coin = Split(Split(Split(sGetResult, "{""coins"":[")(1), "{""id"":""")(i), """,""name""")
id_coin = id_coin(0)
Exit For
End If
Next i
sUrl = "https://api.coingecko.com/api/v3/simple/price?ids=" + id_coin + "&vs_currencies=" + currency1
sRequest = sUrl
httpObject.Open "GET", sRequest, False
httpObject.send
sGetResult = httpObject.responseText
Price = Split(Split(sGetResult, ":")(2), "}}")
GECKOPRICE = CDbl(Price(0))
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Function
'** GECKOVOLUME
' * Imports CoinGecko's cryptocurrency 24h volumes into Google spreadsheets. The feed is a ONE-dimensional array.
' * By default, data gets transformed into a number so it looks more like a normal number data import.
' * For example:
' *
' * =GECKOVOLUME("BTC", "USD","$A$1")
' *
' *
' * @param {cryptocurrency} the cryptocurrency ticker you want the 24h volume from
' * @param {against fiat currency} the fiat currency ex: usd or eur
' * @param {parseOptions} an optional fixed cell for automatic refresh of the data
' * @customfunction
' *
' * @return a one-dimensional array containing the 24h volume
' **
Public Function GECKOVOLUME(ticker As String, currency1 As String)
Application.Calculation = xlCalculationManual
Dim httpObject As Object
Dim sGetResult As String
Set httpObject = CreateObject("Microsoft.XMLHTTP")
Dim i, lenght_array As Integer
Dim jsnstr() As String
sUrl = "https://api.coingecko.com/api/v3/search?locale=fr&img_path_only=1"
ticker = UCase(ticker)
currency1 = LCase(currency1)
sRequest = sUrl
httpObject.Open "GET", sRequest, False
httpObject.send
sGetResult = httpObject.responseText
lenght_array1 = Split(Split(sGetResult, "{""coins"":[")(1), "{""id"":""")
lenght_array = UBound(lenght_array1)
For i = 1 To lenght_array
jsnstr = Split(Split(Split(Split(sGetResult, "{""coins"":[")(1), "{""id"":")(i), ",""symbol"":""")(1), """,""market_cap_rank""")
If jsnstr(0) = ticker Then
id_coin = Split(Split(Split(sGetResult, "{""coins"":[")(1), "{""id"":""")(i), """,""name""")
id_coin = id_coin(0)
Exit For
End If
Next i
sUrl = "https://api.coingecko.com/api/v3/coins/markets?vs_currency=" + currency1 + "&ids=" + id_coin
sRequest = sUrl
httpObject.Open "GET", sRequest, False
httpObject.send
sGetResult = httpObject.responseText
volume = Split(Split(sGetResult, """total_volume"":")(1), ",")
GECKOVOLUME = CDbl(volume(0))
Application.Calculation = xlCalculationAutomatic
End Function
'/** GECKOCAP
' * Imports cryptocurrencies total market cap into Google spreadsheets. The feed is a ONE-dimensional array.
' * By default, data gets transformed into a number so it looks more like a normal number data import.
' * For example:
' *
' * =GECKOCAP("BTC", "USD","$A$1")
' *
' *
' * @param {cryptocurrency} the cryptocurrency ticker you want the total market cap from
' * @param {against fiat currency} the fiat currency ex: usd or eur
' * @param {parseOptions} an optional fixed cell for automatic refresh of the data
' * @customfunction
' *
' * @return a one-dimensional array containing the total market cap
' **/
Public Function GECKOCAP(ticker As String, currency1 As String)
Application.Calculation = xlCalculationManual
Dim httpObject As Object
Dim sGetResult As String
Set httpObject = CreateObject("Microsoft.XMLHTTP")
Dim i, lenght_array As Integer
Dim jsnstr() As String
sUrl = "https://api.coingecko.com/api/v3/search?locale=fr&img_path_only=1"
ticker = UCase(ticker)
currency1 = LCase(currency1)
sRequest = sUrl
httpObject.Open "GET", sRequest, False
httpObject.send
sGetResult = httpObject.responseText
lenght_array1 = Split(Split(sGetResult, "{""coins"":[")(1), "{""id"":""")
lenght_array = UBound(lenght_array1)
For i = 1 To lenght_array
jsnstr = Split(Split(Split(Split(sGetResult, "{""coins"":[")(1), "{""id"":")(i), ",""symbol"":""")(1), """,""market_cap_rank""")
If jsnstr(0) = ticker Then
id_coin = Split(Split(Split(sGetResult, "{""coins"":[")(1), "{""id"":""")(i), """,""name""")
id_coin = id_coin(0)
Exit For
End If
Next i
sUrl = "https://api.coingecko.com/api/v3/coins/markets?vs_currency=" + currency1 + "&ids=" + id_coin
sRequest = sUrl
httpObject.Open "GET", sRequest, False
httpObject.send
sGetResult = httpObject.responseText
mkt = Split(Split(sGetResult, """market_cap"":")(1), ",")
GECKOCAP = CDbl(mkt(0))
Application.Calculation = xlCalculationAutomatic
End Function
' GECKOPRICEBYNAME
' * Imports CoinGecko's cryptocurrency prices into Google spreadsheets. The price feed is a ONE-dimensional array.
' * By default, data gets transformed into a number so it looks more like a normal price data import.
' * For example:
' *
' * =GECKOPRICEBYNAME("BITCOIN", "USD","$A$1")
' *
' *
' * @param {cryptocurrency} the cryptocurrency NAME you want the price from? you will find it from the coingecko website name
' * @param {against fiat currency} the fiat currency ex: usd or eur
' * @param {parseOptions} an optional fixed cell for automatic refresh of the data
' * @customfunction
' *
' * @return a one-dimensional array containing the price
'
Public Function GECKOPRICEBYNAME(name As String, currency1 As String)
Application.Calculation = xlCalculationManual
Dim httpObject As Object
Dim sGetResult As String
Set httpObject = CreateObject("Microsoft.XMLHTTP")
Dim i, lenght_array As Integer
Dim jsnstr() As String
sUrl = "https://api.coingecko.com/api/v3/search?locale=fr&img_path_only=1"
currency1 = LCase(currency1)
name = LCase(name)
sUrl = "https://api.coingecko.com/api/v3/simple/price?ids=" + LCase(name) + "&vs_currencies=" + currency1
sRequest = sUrl
httpObject.Open "GET", sRequest, False
httpObject.send
sGetResult = httpObject.responseText
Price = Split(Split(sGetResult, ":")(2), "}}")
GECKOPRICEBYNAME = CDbl(Price(0))
Application.Calculation = xlCalculationAutomatic
End Function
'/** GECKOCHANGE
' * Imports CoinGecko's cryptocurrency price change, volume change and market cap change into Google spreadsheets.
' * For example:
' *
' * =GECKOCHANGE("BTC","LTC","price", 7)
' * =GECKOCHANGE("ETH","USD","volume", 1)
' * =GECKOCHANGE("YFI","EUR","marketcap",365)
' *
' *
' * @param {ticker} the cryptocurrency ticker
' * @param {ticker2} the cryptocurrency ticker against which you want the %chage
' * @param {price,volume, or marketcap} the type of change you are looking for
' * @param {nb_days} the number of days you are looking for the price change, 365days=1year price change
' * @param {parseOptions} an optional fixed cell for automatic refresh of the data
' * @customfunction
' *
' * @return a one-dimensional array containing the 7D% price change on BTC (week price % change).
' **/
Public Function GECKOCHANGE(ticker As String, ticker2 As String, type1 As String, nb_days As Integer)
Application.Calculation = xlCalculationManual
Dim httpObject As Object
Dim sGetResult As String, sGetResult2 As String
Set httpObject = CreateObject("Microsoft.XMLHTTP")
Dim i, lenght_array As Integer
Dim jsnstr() As String, data As String
sUrl = "https://api.coingecko.com/api/v3/search?locale=fr&img_path_only=1"
ticker = UCase(ticker)
ticker2 = LCase(ticker2)
type1 = LCase(type1)
nb_days2 = CStr(nb_days)
sRequest = sUrl
httpObject.Open "GET", sRequest, False
httpObject.send
sGetResult = httpObject.responseText
lenght_array1 = Split(Split(sGetResult, "{""coins"":[")(1), "{""id"":""")
lenght_array = UBound(lenght_array1)
For i = 1 To lenght_array
jsnstr = Split(Split(Split(Split(sGetResult, "{""coins"":[")(1), "{""id"":")(i), ",""symbol"":""")(1), """,""market_cap_rank""")
If jsnstr(0) = ticker Then
id_coin = Split(Split(Split(sGetResult, "{""coins"":[")(1), "{""id"":""")(i), """,""name""")
id_coin = id_coin(0)
Exit For
End If
Next i
sUrl = "https://api.coingecko.com/api/v3/coins/" + id_coin + "/market_chart?vs_currency=" + ticker2 + "&days=" + nb_days2
sRequest = sUrl
httpObject.Open "GET", sRequest, False
httpObject.send
sGetResult = httpObject.responseText
sUrl = "https://api.coingecko.com/api/v3/coins/markets?vs_currency=" + ticker2 + "&ids=" + id_coin
sRequest = sUrl
httpObject.Open "GET", sRequest, False
httpObject.send
sGetResult2 = httpObject.responseText
If type1 = "price" Then
Data1 = CDbl(Split(Split(sGetResult2, """current_price"":")(1), ",")(0)) / CDbl(Split(Split(Split(sGetResult, """prices"":[[")(1), "]")(0), ",")(1)) - 1
ElseIf type1 = "volume" Then
Data1 = CDbl(Split(Split(sGetResult2, """total_volume"":")(1), ",")(0)) / CDbl(Split(Split(Split(sGetResult, """total_volumes"":[[")(1), "]")(0), ",")(1)) - 1
ElseIf type1 = "marketcap" Then
Data1 = CDbl(Split(Split(sGetResult2, """market_cap"":")(1), ",")(0)) / CDbl(Split(Split(Split(sGetResult, """market_caps"":[[")(1), "]")(0), ",")(1)) - 1
End If
GECKOCHANGE = CDbl(Data1)
Application.Calculation = xlCalculationAutomatic
End Function
' GECKOATH
' * Imports CoinGecko's cryptocurrency All Time High Price into Google spreadsheets. The price feed is a ONE-dimensional array.
' * By default, data gets transformed into a number so it looks more like a normal price data import.
' * For example:
' *
' * =GECKOATH("BTC", "USD","$A$1")
' *
' *
' * @param {cryptocurrency} the cryptocurrency ticker you want the ATH price from
' * @param {against fiat currency} the fiat currency ex: usd or eur
' * @param {parseOptions} an optional fixed cell for automatic refresh of the data
' * @customfunction
' *
' * @return a one-dimensional array containing the ATH price
'
Public Function GECKOATH(ticker As String, currency1 As String)
Application.Calculation = xlCalculationManual
Dim httpObject As Object
Dim sGetResult As String
Set httpObject = CreateObject("Microsoft.XMLHTTP")
Dim i, lenght_array As Integer
Dim jsnstr() As String
sUrl = "https://api.coingecko.com/api/v3/search?locale=fr&img_path_only=1"
ticker = UCase(ticker)
currency1 = LCase(currency1)
sRequest = sUrl
httpObject.Open "GET", sRequest, False
httpObject.send
sGetResult = httpObject.responseText
lenght_array1 = Split(Split(sGetResult, "{""coins"":[")(1), "{""id"":""")
lenght_array = UBound(lenght_array1)
For i = 1 To lenght_array
jsnstr = Split(Split(Split(Split(sGetResult, "{""coins"":[")(1), "{""id"":")(i), ",""symbol"":""")(1), """,""market_cap_rank""")
If jsnstr(0) = ticker Then
id_coin = Split(Split(Split(sGetResult, "{""coins"":[")(1), "{""id"":""")(i), """,""name""")
id_coin = id_coin(0)
Exit For
End If
Next i
sUrl = "https://api.coingecko.com/api/v3/coins/markets?vs_currency=" + currency1 + "&ids=" + id_coin
sRequest = sUrl
httpObject.Open "GET", sRequest, False
httpObject.send
sGetResult = httpObject.responseText
ATH = Split(Split(sGetResult, """ath"":")(1), ",")
GECKOATH = CDbl(ATH(0))
Application.Calculation = xlCalculationAutomatic
End Function