-
Notifications
You must be signed in to change notification settings - Fork 54
/
ModExchPoloniex.bas
174 lines (141 loc) · 6.66 KB
/
ModExchPoloniex.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
Attribute VB_Name = "ModExchPoloniex"
Sub TestPoloniex()
'Source: https://github.com/krijnsent/crypto_vba
'Remember to create a new API key for excel/VBA
'https://docs.poloniex.com/#http-api
'Poloniex will require ever increasing values/nonces for the private API and the nonces created in VBA might mismatch that of other sources
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_poloniex = "the key to use everywhere" etc )
Apikey = apikey_poloniex
secretKey = secretkey_poloniex
'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 = "ModExchPoloniex"
' 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("TestPoloniexPublic")
'Error, unknown command
TestResult = PublicPoloniex("returnUnknownCommand", "GET")
'{"error":"Invalid command."}
Test.IsOk InStr(TestResult, "error") > 0, "test error 1 failed, result: ${1}"
Set JsonResult = JsonConverter.ParseJson(TestResult)
Test.IsEqual JsonResult("error"), "Invalid command.", "test error 2 failed, result: ${1}"
'Error, missing parameters
TestResult = PublicPoloniex("returnOrderBook", "GET")
'{"error":"Please specify a currency pair."}
Test.IsOk InStr(TestResult, "error") > 0, "test error 3 failed, result: ${1}"
Set JsonResult = JsonConverter.ParseJson(TestResult)
Test.IsEqual JsonResult("error"), "Please specify a currency pair.", "test error 4 failed, result: ${1}"
'Testing error catching and replies
TestResult = PublicPoloniex("returnTicker", "GET")
'{"BTC_BCN":{"id":7,"last":"0.00000120","lowestAsk":"0.00000120","highestBid":"0.00000119","percentChange":"1.00000000","baseVolume":"21570.44763887","quoteVolume":"21082615430.89178085", etc...
Test.IsOk InStr(TestResult, "lowestAsk") > 0
Test.IsOk InStr(TestResult, "BTC_ETH"":") > 0
Set JsonResult = JsonConverter.ParseJson(TestResult)
Test.IsEqual JsonResult("error"), ""
Test.IsEqual JsonResult("BTC_ETH")("id"), 148
Test.IsOk JsonResult("BTC_ETH")("highestBid") > 0
'Put the parameters in a dictionary
Dim Params As New Dictionary
Params.Add "currencyPair", "BTC_ETH"
Params.Add "depth", 10
TestResult = PublicPoloniex("returnOrderBook", "GET", Params)
'{"asks":[["0.03530499",1.18647302],["0.03530500",110.78279995],["0.03531880",0.70796807],["0.03534095",2.12187844],["0.03534099",0.11553593],["0.03534767",29.95566069],["0.03534768",3.99999999],["0.03535000",0.99900001],["0.03535497",14.16571992],["0.03535498",0.6221801]],"bids":[["0.03528822",0.0031],["0.03528813",0.06749181],["0.03528730",0.0674917],["0.03528711",0.0674917],["0.03528638",0.0673596],["0.03528531",0.01],["0.03528303",0.01417112],["0.03527231",16.12158867],["0.03527000",110.5868],["0.03526147",33.74922032]],"isFrozen":"0","seq":644421713}
Test.IsOk InStr(TestResult, "],[") > 0
Set JsonResult = JsonConverter.ParseJson(TestResult)
Test.IsEqual JsonResult("asks").Count, 10
Test.IsEqual JsonResult("bids").Count, 10
Test.IsOk JsonResult("asks")(1)(2) > 0
'Unix time period:
Set Test = Suite.Test("TestPoloniexPrivate")
t1 = DateToUnixTime("1/1/2016")
t2 = DateToUnixTime("1/1/2019")
TestResult = PrivatePoloniex("returnBalances", "POST", Cred)
'{"1CR":"0.00000000","ABY":"0.00000000","AC":"0.00000000","ACH":"0.00000000","ADN":"0.00000000","AEON":"0.00000000" etc...
Test.IsOk InStr(TestResult, "BTC") > 0
Set JsonResult = JsonConverter.ParseJson(TestResult)
Test.IsOk JsonResult.Count >= 10
Test.IsOk JsonResult("ETH") >= 0
'Put the parameters in a dictionary
Dim Params2 As New Dictionary
Params2.Add "currencyPair", "all"
Params2.Add "start", t1
Params2.Add "end", t2
TestResult = PrivatePoloniex("returnTradeHistory", "POST", Cred, Params2)
If InStr(TestResult, "globalTradeID") > 0 Then
'has some results
'e.g.: {"BTC_ETH":[{"globalTradeID":108848981,"tradeID":"22880801","date":"2017-04-19 23:26:55","rate":"0.03900000","amount":"65.35644222","total":"2.54890124", etc...
Test.IsOk InStr(TestResult, "amount") > 0
Set JsonResult = JsonConverter.ParseJson(TestResult)
For Each k In JsonResult.Keys()
Test.IsOk JsonResult(k).Count >= 1
Next k
Else
'no results
'Empty: []
Test.IsEqual TestResult, "[]"
End If
'Put the parameters in a dictionary
Dim Params3 As New Dictionary
Params3.Add "currencyPair", "BTC_ETH"
Params3.Add "rate", 0.001
Params3.Add "amount", 3
Params3.Add "fillOrKill", 1
TestResult = PrivatePoloniex("buy", "POST", Cred, Params3)
'{"error":"This API key does not have permission to trade."}
'{orderNumber: '514845991795',resultingTrades:[{amount: '3.0',Date: '2018-10-25 23:03:21',rate:'0.0002',total:'0.0006',tradeID:'251834',type:'buy'}]}
'{"error":"Not enough BTC.","fee":"0.00125000","currencyPair":"BTC_ETH"}
If InStr(TestResult, "error") > 0 Then
Test.IsOk InStr(TestResult, "currencyPair") > 0
Set JsonResult = JsonConverter.ParseJson(TestResult)
Test.IsEqual JsonResult("error"), "Not enough BTC."
Else
Test.IsOk InStr(TestResult, "resultingTrades") > 0
Set JsonResult = JsonConverter.ParseJson(TestResult)
Test.IsOk JsonResult("orderNumber") >= 0
End If
End Sub
Function PublicPoloniex(Method As String, ReqType As String, Optional ParamDict As Dictionary) As String
Dim url As String
PublicApiSite = "https://poloniex.com"
MethodParams = DictToString(ParamDict, "URLENC")
If MethodParams <> "" Then MethodParams = "&" & MethodParams
urlPath = "/public?command=" & Method & MethodParams
url = PublicApiSite & urlPath
PublicPoloniex = WebRequestURL(url, ReqType)
End Function
Function PrivatePoloniex(Method As String, ReqType As String, Credentials As Dictionary, Optional ParamDict As Dictionary) As String
Dim NonceUnique As String
Dim postdata As String
Dim PayloadDict As Dictionary
Dim url As String
'Poloniex nonce
NonceUnique = CreateNonce(16)
url = "https://poloniex.com/tradingApi"
Set PayloadDict = New Dictionary
PayloadDict("command") = Method
If Not ParamDict Is Nothing Then
For Each key In ParamDict.Keys
PayloadDict(key) = ParamDict(key)
Next key
End If
PayloadDict("&nonce") = NonceUnique
postdata = DictToString(PayloadDict, "URLENC")
APIsign = ComputeHash_C("SHA512", postdata, 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/x-www-form-urlencoded"
headerDict.Add "Key", Credentials("apiKey")
headerDict.Add "Sign", APIsign
PrivatePoloniex = WebRequestURL(url, ReqType, headerDict, postdata)
End Function