I can obtain AccessToken; however, I can’t move forward from here. For Websocket, I can logged in, and it return back o(Open Frame) . then the socket closed (even I sent out “authorize\n5\n\n{AccessToken}” . For REST API, I placed a BUY order, it it returned 400 Bad Request. Please see attachment.
Please HELP! HELP!
My MessageReceived event
Private Sub webSocketTrado_MessageReceived(s As Object, e As WebSocket4Net.MessageReceivedEventArgs)
'Dim retJsonObj As Object = clstd.StrJson2Obj(e.Message)
Debug.WriteLine(" Main message rec = " + e.Message)
Dim sOption As String = e.Message(0)
Debug.WriteLine("first character = " + sOption)
If sOption = "o" Then
webSocketTrado.Send("authorize\n5\n\n" + txtAccessToken.Text)
'RequestAuthorize()
RequestExecutionReport()
ElseIf sOption = "h" Then
ElseIf sOption = "a" Then
ElseIf sOption = "c" Then
Debug.Write("Close Frame .. Closed Connection ...Don't ask")
End If
RequestExecutionReport()
End Sub 'ws message received
Private Function BuyMarket(ByVal sSymbol As String, ByVal sAction As String) As Boolean
Dim urlOrder = CurServer + “/v1/order/placeorder”
Debug.WriteLine(" Buy from Server " + urlOrder)
Dim wc As System.Net.WebClient = New System.Net.WebClient
wc.Headers.Add("Authorization", "Bearer " + txtAccessToken.Text)
wc.Headers.Add("Content-Type", "application/json")
wc.Headers.Add("Method", "POST")
wc.Headers.Add("Accept", "application/json")
Order.accountSpec = txtName.Text
Order.accountId = Val(txtuserId.Text)
Order.orderType = "Market" '"Limit"
Order.timeInForce = "Day"
Order.isAutomated = True
Order.orderQty = 1
'Order.price = 4165.25
sAction = UCase(sAction)
If sAction = "BUY" Then
Order.action = "Buy"
ElseIf sAction = "SELL" Then
Order.action = "Sell"
Else
Return False
End If
Order.symbol = UCase(sSymbol)
Dim sJson As String = clstd.Obj2StrJson(Order)
' Dim data As Byte() = System.Text.Encoding.Unicode.GetBytes(sJson)
Dim data As Byte() = System.Text.Encoding.Unicode.GetBytes(sJson)
wc.Headers.Add("ContentLength", data.Length.ToString)
Debug.WriteLine(" BUY JSONstr = " + sJson)
Try
'Dim sRet As String = wc.UploadString(urlOrder, sJson)
Dim sRet As String = wc.UploadString(urlOrder, sJson) 'System.Text.Encoding.Unicode.GetString(wc.UploadData(urlOrder, data))
Debug.WriteLine(" Return from MarketOrder = " + sRet)
Return (True)
Catch ex As Exception
Debug.WriteLine("ERROR!!! Fail to submit order " + ex.Message)
Return (False)
End Try
End Function