VB.NET Requires Chilkat v11.0.0+
VB.NET
Schwab: Preview a single-leg equity limit order
See more Schwab Examples
Builds a day limit order for one share and posts it to previewOrder. Reads the projected order and the validation messages, including warnings and rejection reasons. A successful HTTP response means the preview completed. A preview does not submit an order, reserve buying power, or guarantee later acceptance.
Use the access token saved by desktop authorization. First select the authorized account whose data you want to use. Use the same Chilkat Secrets store as the OAuth example.
Chilkat VB.NET Downloads
Dim success As Boolean = False
' Assumes Chilkat is unlocked. See Global Unlock Sample.
success = False
Dim vault As New Chilkat.Secrets
vault.Location = "local_manager"
vault.AutoSplit = True
Dim entry As New Chilkat.JsonObject
entry.UpdateString("appName","SchwabTrader")
entry.UpdateString("service","OAuth2")
entry.UpdateString("username","personal-desktop")
Dim tokens As New Chilkat.JsonObject
success = vault.GetSecretJson(entry,tokens)
If (success <> True) Then
Debug.WriteLine("Could not read the token record. Check the store and complete desktop authorization first.")
Exit Sub
End If
Dim accessToken As String = tokens.StringOf("access_token")
If (accessToken = "") Then
Debug.WriteLine("Run the Schwab authorization or refresh example to save an access token.")
Exit Sub
End If
Dim http As New Chilkat.Http
http.RequireSslCertVerify = True
http.AuthToken = accessToken
' Read the hash saved by the authorized-account selection example.
entry.UpdateString("service","SelectedAccount")
Dim selection As New Chilkat.JsonObject
success = vault.GetSecretJson(entry,selection)
Dim accountHash As String = selection.StringOf("hashValue")
If ((success <> True) Or (accountHash = "")) Then
Debug.WriteLine("Select an authorized account first.")
Exit Sub
End If
http.SetUrlVar("account",accountHash)
' These are illustrative preview inputs. Set the symbol, quantity and limit price.
Dim order As New Chilkat.JsonObject
' The PreviewOrder schema puts orderLegs inside orderStrategy.
order.UpdateString("orderStrategy.session","NORMAL")
order.UpdateString("orderStrategy.duration","DAY")
order.UpdateString("orderStrategy.orderType","LIMIT")
order.UpdateString("orderStrategy.orderStrategyType","SINGLE")
order.UpdateNumber("orderStrategy.price","100.00")
order.UpdateInt("orderStrategy.quantity",1)
order.UpdateString("orderStrategy.orderLegs[0].instruction","BUY")
order.UpdateInt("orderStrategy.orderLegs[0].quantity",1)
order.UpdateString("orderStrategy.orderLegs[0].finalSymbol","AAPL")
order.UpdateString("orderStrategy.orderLegs[0].assetType","EQUITY")
Dim resp As New Chilkat.HttpResponse
success = http.HttpJson("POST","https://api.schwabapi.com/trader/v1/accounts/{$account}/previewOrder",order,"application/json",resp)
If ((success <> True) Or (resp.StatusCode <> 200)) Then
If (resp.StatusCode = 0) Then
Debug.WriteLine("No HTTP response. Check connectivity.")
Else
Debug.WriteLine("Schwab returned HTTP " & resp.StatusCode)
If (resp.StatusCode = 401) Then
Debug.WriteLine("Refresh the access token or repeat desktop authorization.")
End If
End If
Exit Sub
End If
Dim reply As New Chilkat.JsonObject
success = reply.Load(resp.BodyStr)
If (success <> True) Then
Debug.WriteLine("Schwab returned unexpected JSON.")
Exit Sub
End If
Debug.WriteLine("Preview only. No order was submitted.")
If (reply.HasMember("orderStrategy.orderType") = True) Then
Debug.WriteLine("Projected order: " & reply.StringOf("orderStrategy.orderType") & " price " & reply.StringOf("orderStrategy.price"))
End If
If (reply.HasMember("orderStrategy.orderBalance.projectedBuyingPower") = True) Then
Debug.WriteLine("Projected buying power: " & reply.StringOf("orderStrategy.orderBalance.projectedBuyingPower"))
End If
Dim validation As New Chilkat.JsonObject
success = reply.ObjectOf2("orderValidationResult",validation)
If (success <> True) Then
Debug.WriteLine("Schwab supplied no validation result.")
Exit Sub
End If
' Each category (accepts, alerts, warns, reviews, rejects) contains rule messages.
Dim rules As New Chilkat.JsonArray
Dim detail As New Chilkat.JsonObject
Dim messages As Integer = 0
Dim i As Integer = 0
While (i < validation.Size)
Dim category As String = validation.NameAt(i)
success = validation.ArrayAt2(i,rules)
If (success = True) Then
Dim j As Integer = 0
While (j < rules.Size)
rules.ObjectAt2(j,detail)
Debug.WriteLine(category & " " & detail.StringOf("validationRuleName") & " " & detail.StringOf("message") & " " & detail.StringOf("activityMessage"))
messages = messages + 1
j = j + 1
End While
End If
i = i + 1
End While
If (messages = 0) Then
Debug.WriteLine("No validation messages returned.")
End If