-
Notifications
You must be signed in to change notification settings - Fork 805
/
Copy pathplaceorder.vbs
72 lines (58 loc) · 2.11 KB
/
placeorder.vbs
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
' BitMEX Order Placement Example
' Written by BitMEX user `cengel`
' Note:
' Requires JSONConverter.bas and hexhash.vbs from this folder
' Usage:
' In VBA go to Tools -> References -> and check "Microsoft Scripting Runtime" and "Microsoft VB Reg Expressions"
' In "H" Column put in: Row 1: Symbol, Row 2: Price, Row 3: Qty
' For example: XBTUSD , 590.10 , 10
' Then run placeorder()
Sub placeorder()
Dim Json, httpObject As Object
Dim expires As Double
Dim verb, apiKey, apiSecret, Signature, symbol, price, qty, url, postdata, replytext, expiresStr As String
Dim jsoncount As Long
' Set expiration time 5 minutes from now
expires = DateDiff("s", "1/1/1970", DateAdd("n", 5, Now))
' Set api key and secret
apiKey = "key"
apiSecret = "secret"
' Build query
symbol = Cells(1, "H").Value
price = Cells(2, "H").Value
qty = Cells(3, "H").Value
verb = "POST"
url = "/api/v1/order"
postdata = "symbol=" & symbol & "&price=" & price & "&quantity=" & qty
' Stringize expires
expiresStr = expires
' Compute signature using hexhash script
Signature = HexHash(verb + url + expiresStr + postdata, apiSecret, "SHA256")
' Set up HTTP req with headers
Set httpObject = CreateObject("MSXML2.XMLHTTP")
httpObject.Open "POST", "https://testnet.bitmex.com" & url, False
httpObject.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
httpObject.setRequestHeader "api-expires", expiresStr
httpObject.setRequestHeader "api-key", apiKey
httpObject.setRequestHeader "api-signature", Signature
httpObject.Send (postdata)
' Catch response
replytext = httpObject.ResponseText
' Parse JSON response
Set Json = JsonConverter.ParseJson(replytext)
' This is useful for grabbing dimensions of the response for loops
' When doing this, get Json parms by doing Json(i)("key") like Json(2)("symbol")
jsoncount = Json.Count
If Json("ordStatus") = "Rejected" Then
MsgBox ("Order rejected")
Exit Sub
Else
'And here just outputting some elements of response
Cells(1, "A") = Json("symbol")
Cells(1, "B") = Json("timestamp")
Cells(1, "C") = Json("price")
Cells(1, "D") = Json("orderQty")
Cells(1, "E") = Json("orderID")
MsgBox ("Order placed.")
End If
End Sub