ERROR : place a BUY order and return 400 Bad Request and websocket : socket closed after open Frame returned

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

Can you share the permissions of your API Key? It’s possible that you are missing a permission required to place trades.

As for the WebSocket, you have no heartbeat implementation it looks like (or you omitted it?). If you don’t send heartbeats the socket will close. A heartbeat is simply an empty frame, which you can send as the string "[]". You should send a heartbeat every ~2.5 seconds. Typically I will run a time check, comparing the current message time to the last message timestamp. If the difference is greater than or equal to 2.5 secs, then send the heartbeat.

Here is my API key’s permissions:

Your permissions look just fine to me.

The main issue I see with your order is that you’re supplying a price to a Market type order, which would result in the JSON schema being invalid.

The WebSocket is a different scenario however. Can you please provide me the URL you are using to connect the socket? It looks like it’s closing right away, or possibly as a response to the auth message. Can you also verify that the access token is a non-null, non-empty string, and that webSocketTrado.Send is actually being called? I’m not super familiar with VB, but showing me some of how your websocket is implemented could help me spot an error.

If there is a way for you to get more information about what’s happening on the network, that would help too. Maybe there is a ‘verbose’ logs option for WebSocket4Net, or something to that effect?

Edit: Looking at your code, wc.Headers.Add("ContentLength", data.Length.ToString) should be wc.Headers.Add("Content-Length", data.Length.ToString) as well I believe.

Here’s what happened - the number at the end of your account spec is typically not related to the account. Your account ID is 792604 based on the response, but you’re using the string of numbers at the end of the account’s name. Use the id field when asked for another entity’s ID in a request param