-
Notifications
You must be signed in to change notification settings - Fork 54
/
ModExchBittrex.bas
218 lines (175 loc) · 9.99 KB
/
ModExchBittrex.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
Attribute VB_Name = "ModExchBittrex"
Sub TestBittrex()
'Source: https://github.com/krijnsent/crypto_vba
'Documentation: https://bittrex.com/home/api
'v3 - https://bittrex.github.io/api/v3
'Remember to create a new API key for excel/VBA
Dim Apikey As String
Dim secretKey As String
Apikey = "your api key here"
secretKey = "your secret key here"
'Remove these 2 lines, unless you define 2 constants somewhere ( Public Const secretkey_btce = "the key to use everywhere" etc )
Apikey = apikey_bittrex
secretKey = secretkey_bittrex
'Put the credentials in a dictionary
Dim Cred As New Dictionary
Cred.Add "apiKey", Apikey
Cred.Add "secretKey", secretKey
' Create a new test suite
Dim Suite As New TestSuite
Suite.Description = "ModExchBittrex"
' Create reporter and attach it to these specs
Dim Reporter As New ImmediateReporter
Reporter.ListenTo Suite
' Create a new test
Dim Test As TestCase
Set Test = Suite.Test("TestBittrexPublic")
'Error, unknown/wrong command
TestResult = PublicBittrex("AnUnknownCommand", "GET")
'{"error_nr":404,"error_txt":"HTTP-Not Found","response_txt":{"code":"NOT_FOUND"}}
Test.IsOk InStr(TestResult, "error") > 0, "test error 1 failed, result: ${1}"
Set JsonResult = JsonConverter.ParseJson(TestResult)
Test.IsEqual JsonResult("error_nr"), 404, "test error 2 failed, result: ${1}"
'Request without parameters
TestResult = PublicBittrex("markets", "GET")
'[{"symbol":"4ART-BTC","baseCurrencySymbol":"4ART","quoteCurrencySymbol":"BTC","minTradeSize":"10.00000000","precision":8,"status":"ONLINE","createdAt":"2020-06-10T15:05:29.833Z","notice":"","prohibitedIn":["US"],"associatedTermsOfService":[]},{"symbol":"4ART-USDT","baseCurrencySymbol":"4ART","quoteCurrencySymbol":"USDT","minTradeSize":"10.00000000","precision":5,"status":"ONLINE","createdAt":"2020-06-10T15:05:40.98Z", etc.
Test.IsOk InStr(TestResult, "quoteCurrencySymbol") > 0, "test markets 1 failed, result: ${1}"
Set JsonResult = JsonConverter.ParseJson(TestResult)
Test.IsEqual JsonResult(1)("quoteCurrencySymbol"), "BTC", "test markets 2 failed, result: ${1}"
Test.IsOk JsonResult(1)("precision") > 0, "test markets 3 failed, result: ${1}"
'Put parameters/options in a dictionary for a summary of one coin, wrong input
Dim Params As New Dictionary
Params.Add "market", "BTC-DOGE"
TestResult = PublicBittrex("markets", "GET", Params)
'{"error_nr":404,"error_txt":"HTTP-Not Found","response_txt":{"code":"MARKET_NAME_REVERSED","detail":"The provided market symbol appears to be reversed. Please retry with the market symbol provided in data.NewMarketSymbol.","data":{"newMarketSymbol":"DOGE-BTC"}}}
Test.IsOk InStr(TestResult, "error") > 0, "test error2 1 failed, result: ${1}"
Set JsonResult = JsonConverter.ParseJson(TestResult)
Test.IsEqual JsonResult("error_nr"), 404, "test error2 2 failed, result: ${1}"
'Parameter in a dictionary
Dim Params2 As New Dictionary
Params2.Add "market", "DOGE-BTC"
TestResult = PublicBittrex("markets", "GET", Params2)
'{"symbol":"DOGE-BTC","baseCurrencySymbol":"DOGE","quoteCurrencySymbol":"BTC","minTradeSize":"1000.00000000","precision":8,"status":"ONLINE","createdAt":"2014-02-13T00:00:00Z","prohibitedIn":[],"associatedTermsOfService":[]}
Test.IsOk InStr(TestResult, "baseCurrencySymbol") > 0, "test markets detail 1 failed, result: ${1}"
Set JsonResult = JsonConverter.ParseJson(TestResult)
Test.IsEqual JsonResult("quoteCurrencySymbol"), "BTC", "test markets detail 2 failed, result: ${1}"
Test.IsEqual JsonResult("precision"), 8, "test markets detail 3 failed, result: ${1}"
Test.IsEqual JsonResult("baseCurrencySymbol"), "DOGE", "test markets detail 4 failed, result: ${1}"
'Parameters in a dictionary get '/markets/{marketSymbol}/candles/{candleInterval}/recent
Dim Params3 As New Dictionary
Params3.Add "market", "ETH-BTC"
Params3.Add "type1", "candles"
Params3.Add "candleInterval", "HOUR_1"
Params3.Add "type2", "recent"
TestResult = PublicBittrex("markets", "GET", Params3)
'[{"startsAt":"2020-08-13T15:00:00Z","open":"0.03405607","high":"0.03412946","low":"0.03393712","close":"0.03411082","volume":"224.62409851","quoteVolume":"7.64110651"},{"startsAt":"2020-08-13T16:00:00Z","open":"0.03411095","high":"0.03418634","low":"0.03387446","close":"0.03402789","volume":"303.55027355","quoteVolume":"10.33201616"},{"startsAt":"2020-08-13T17:00:00Z","open":"0.03403607","high":"0.03407806","low":"0.03389236","close":"0.03403147","volume":"487.61617145","quoteVolume":"16.57089220"},{"startsAt":"2020-08-13T18:00:00Z","open":"0.03403252","high":"0.03413220","low":"0.03403252","close":"0.03410964","volume":"388.13757692","quoteVolume":"13.22881730"},{"startsAt":"2020-08-13T19:00:00Z","open":"0.03408765","high":"0.03425485","low":"0.03408765","close":"0.03422712","volume":"312.75229144","quoteVolume":"10.69620756"}, etc...
Test.IsOk InStr(TestResult, "startsAt") > 0, "test candles 1 failed, result: ${1}"
Set JsonResult = JsonConverter.ParseJson(TestResult)
Test.IsOk JsonResult(1)("open") > 0, "test candles 2 failed, result: ${1}"
Test.IsOk JsonResult(1)("high") > 0, "test candles 3 failed, result: ${1}"
Test.IsOk JsonResult(1)("low") > 0, "test candles 4 failed, result: ${1}"
'Get bittrex time from ping
Set Test = Suite.Test("TestBittrexTime")
TestResult = GetBittrexTime()
Test.IsOk TestResult > 0, "test time 2 failed, result: ${1}"
'Test private API
Set Test = Suite.Test("TestBittrexPrivate")
TestResult = PrivateBittrex("balances", "GET", Cred)
'[{"currencySymbol":"BCH","total":"0.00001733","available":"0.00001733","updatedAt":"2001-01-01T00:00:00Z"},{"currencySymbol":"BTC","total":"0.01500039","available":"0.01500039","updatedAt":"2001-01-01T00:00:00Z"},{"currencySymbol":"BTXCRD","total":"0.00000000","available":"0.00000000","updatedAt":"2019-10-23T04:16:31.1Z"},{"currencySymbol":"XLM","total":"0","available":"0","updatedAt":"2020-09-13T16:02:42.84307Z"}], etc...
Test.IsOk InStr(TestResult, "currencySymbol") > 0
Set JsonResult = JsonConverter.ParseJson(TestResult)
Test.IsOk Len(JsonResult(1)("currencySymbol")) >= 3
Test.IsOk JsonResult(1)("Balance") >= 0
Dim Params4 As New Dictionary
Params4.Add "marketSymbol", "XRP-BTC"
TestResult = PrivateBittrex("orders/open", "GET", Cred, Params4)
'[] (assuming no open orders DOGE-BTC, why would you have any...?
Test.IsEqual TestResult, "[]"
'Buy 1 BTC for a crazy low price of 0.1 USD :-)
Dim Params5 As New Dictionary
Params5.Add "marketSymbol", "BTC-USD"
Params5.Add "direction", "BUY"
Params5.Add "type", "LIMIT"
Params5.Add "timeInForce", "FILL_OR_KILL"
Params5.Add "quantity", 1
Params5.Add "limit", 0.1
TestResult = PrivateBittrex("orders", "POST", Cred, Params5)
Debug.Print TestResult
If InStr(TestResult, "error_nr") Then
'e.g. {"error_nr":400,"error_txt":"HTTP-Bad Request","response_txt":{"code":"SOURCE_OF_FUNDS_REQUIRED"}}
Set JsonResult = JsonConverter.ParseJson(TestResult)
Test.IsOk JsonResult("error_nr") >= 400
Else
'or OK: {"id": "string (uuid)","marketSymbol": "string","direction": "string","type": "string","quantity": "number (double)","limit": "number (double)","ceiling": "number (double)","timeInForce": "string","clientOrderId": "string (uuid)","fillQuantity": "number (double)","commission": "number (double)","proceeds": "number (double)","status": "string","createdAt": "string (date-time)","updatedAt": "string (date-time)","closedAt": "string (date-time)","orderToCancel": { "type": "string", "id": "string (uuid)"}}
Test.IsOk InStr(TestResult, "marketSymbol") > 0
Set JsonResult = JsonConverter.ParseJson(TestResult)
Test.IsEqual JsonResult("direction"), "BUY"
End If
'Cancel order with id
Dim Params6 As New Dictionary
Params6.Add "uuid", "orderid-bla"
TestResult = PrivateBittrex("orders", "DELETE", Cred, Params6)
'{"error_nr":404,"error_txt":"HTTP-Not Found","response_txt":{"code":"NOT_FOUND"}}
Test.IsOk InStr(TestResult, "error_nr") > 0
Test.IsOk InStr(TestResult, "NOT_FOUND") > 0
End Sub
Function PublicBittrex(Method As String, ReqType As String, Optional ParamDict As Dictionary) As String
Dim url As String
PublicApiSite = "https://api.bittrex.com"
MethodParams = ""
If Not ParamDict Is Nothing Then
For Each itm In ParamDict
MethodParams = MethodParams & ParamDict(itm) & "/"
Next itm
End If
urlPath = "/v3/" & Method & "/" & MethodParams
url = PublicApiSite & urlPath
PublicBittrex = WebRequestURL(url, ReqType)
End Function
Function PrivateBittrex(Method As String, ReqType As String, Credentials As Dictionary, Optional ParamDict As Dictionary) As String
Dim NonceUnique As String
Dim postdata As String
Dim url As String
Dim Uri As String
'Get a 13-digit Nonce from the server time
NonceUnique = GetBittrexTime
TradeApiSite = "https://api.bittrex.com/v3/"
url = TradeApiSite & Method
postdata = ""
If ReqType = "DELETE" Then
For Each itm In ParamDict
url = url & "/" & ParamDict(itm)
Next itm
ElseIf ReqType = "GET" And Not ParamDict Is Nothing Then
url = url & "?" & DictToString(ParamDict, "URLENC")
ElseIf ReqType = "POST" Then
postdata = JsonConverter.ConvertToJson(ParamDict)
End If
contentHash = ComputeHash_C("SHA512", postdata, "", "STRHEX")
preSign = NonceUnique & url & ReqType & contentHash
APIsign = ComputeHash_C("SHA512", preSign, Credentials("secretKey"), "STRHEX")
Dim headerDict As New Dictionary
headerDict.Add "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
headerDict.Add "Content-Type", "application/json"
headerDict.Add "Api-Key", Credentials("apiKey")
headerDict.Add "Api-Timestamp", NonceUnique
headerDict.Add "Api-Content-Hash", contentHash
headerDict.Add "Api-Signature", APIsign
'Debug.Print url, postdata
PrivateBittrex = WebRequestURL(url, ReqType, headerDict, postdata)
End Function
Function GetBittrexTime() As Double
Dim JsonResponse As String
Dim Json As Object
'GetBittrexTime time from ping
JsonResponse = PublicBittrex("ping", "GET")
Set Json = JsonConverter.ParseJson(JsonResponse)
GetBittrexTime = Json("serverTime")
NonceUnique = CreateNonce(13)
If GetBittrexTime = 0 Then
TimeCorrection = -3600
GetBittrexTime = DateDiff("s", "1/1/1970", Now)
GetBittrexTime = Trim(Str((Val(GetBittrexTime) + TimeCorrection)) & Right(Int(Timer * 100), 2) & "0")
End If
Set Json = Nothing
End Function