PureBasic Requires Chilkat v11.0.0+
PureBasic
Schwab: Inspect an order, its legs, and fills
See more Schwab Examples
Use an order ID from the selected account’s recent orders. The example reads a single order and matches executions to order legs by legId. Each execution can have its own quantity, price, and timestamp. Working orders can have zero or partial fills. The loops cover the returned order’s own legs and activities.
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 PureBasic Downloads
IncludeFile "CkHttpResponse.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkSecrets.pb"
Procedure ChilkatExample()
success.i = 0
; Assumes Chilkat is unlocked. See Global Unlock Sample.
success = 0
vault.i = CkSecrets::ckCreate()
If vault.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkSecrets::setCkLocation(vault, "local_manager")
CkSecrets::setCkAutoSplit(vault, 1)
entry.i = CkJsonObject::ckCreate()
If entry.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::ckUpdateString(entry,"appName","SchwabTrader")
CkJsonObject::ckUpdateString(entry,"service","OAuth2")
CkJsonObject::ckUpdateString(entry,"username","personal-desktop")
tokens.i = CkJsonObject::ckCreate()
If tokens.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkSecrets::ckGetSecretJson(vault,entry,tokens)
If success <> 1
Debug "Could not read the token record. Check the store and complete desktop authorization first."
CkSecrets::ckDispose(vault)
CkJsonObject::ckDispose(entry)
CkJsonObject::ckDispose(tokens)
ProcedureReturn
EndIf
accessToken.s = CkJsonObject::ckStringOf(tokens,"access_token")
If accessToken = ""
Debug "Run the Schwab authorization or refresh example to save an access token."
CkSecrets::ckDispose(vault)
CkJsonObject::ckDispose(entry)
CkJsonObject::ckDispose(tokens)
ProcedureReturn
EndIf
http.i = CkHttp::ckCreate()
If http.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkHttp::setCkRequireSslCertVerify(http, 1)
CkHttp::setCkAuthToken(http, accessToken)
; Read the hash saved by the authorized-account selection example.
CkJsonObject::ckUpdateString(entry,"service","SelectedAccount")
selection.i = CkJsonObject::ckCreate()
If selection.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkSecrets::ckGetSecretJson(vault,entry,selection)
accountHash.s = CkJsonObject::ckStringOf(selection,"hashValue")
If (success <> 1) OR (accountHash = "")
Debug "Select an authorized account first."
CkSecrets::ckDispose(vault)
CkJsonObject::ckDispose(entry)
CkJsonObject::ckDispose(tokens)
CkHttp::ckDispose(http)
CkJsonObject::ckDispose(selection)
ProcedureReturn
EndIf
CkHttp::ckSetUrlVar(http,"account",accountHash)
; Supply the ID as text so large numeric identifiers keep all their digits.
orderId.s = "ORDER_ID_FROM_LIST"
CkHttp::ckSetUrlVar(http,"orderId",orderId)
resp.i = CkHttpResponse::ckCreate()
If resp.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkHttp::ckHttpNoBody(http,"GET","https://api.schwabapi.com/trader/v1/accounts/{$account}/orders/{$orderId}",resp)
If (success <> 1) OR (CkHttpResponse::ckStatusCode(resp) <> 200)
If CkHttpResponse::ckStatusCode(resp) = 0
Debug "No HTTP response. Check connectivity."
Else
Debug "Schwab returned HTTP " + Str(CkHttpResponse::ckStatusCode(resp))
If CkHttpResponse::ckStatusCode(resp) = 401
Debug "Refresh the access token or repeat desktop authorization."
EndIf
EndIf
CkSecrets::ckDispose(vault)
CkJsonObject::ckDispose(entry)
CkJsonObject::ckDispose(tokens)
CkHttp::ckDispose(http)
CkJsonObject::ckDispose(selection)
CkHttpResponse::ckDispose(resp)
ProcedureReturn
EndIf
order.i = CkJsonObject::ckCreate()
If order.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkJsonObject::ckLoad(order,CkHttpResponse::ckBodyStr(resp))
If success <> 1
Debug "Schwab returned unexpected JSON."
CkSecrets::ckDispose(vault)
CkJsonObject::ckDispose(entry)
CkJsonObject::ckDispose(tokens)
CkHttp::ckDispose(http)
CkJsonObject::ckDispose(selection)
CkHttpResponse::ckDispose(resp)
CkJsonObject::ckDispose(order)
ProcedureReturn
EndIf
Debug "Order " + CkJsonObject::ckStringOf(order,"orderId") + " " + CkJsonObject::ckStringOf(order,"status")
symbols.i = CkJsonObject::ckCreate()
If symbols.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
i.i = 0
While (i < CkJsonObject::ckSizeOfArray(order,"orderLegCollection"))
CkJsonObject::setCkI(order, i)
legId.s = CkJsonObject::ckStringOf(order,"orderLegCollection[i].legId")
symbol.s = CkJsonObject::ckStringOf(order,"orderLegCollection[i].instrument.symbol")
CkJsonObject::ckUpdateString(symbols,legId,symbol)
Debug "Leg " + legId + " " + CkJsonObject::ckStringOf(order,"orderLegCollection[i].instruction") + " " + CkJsonObject::ckStringOf(order,"orderLegCollection[i].quantity") + " " + symbol
i = i + 1
Wend
fills.i = 0
i = 0
While (i < CkJsonObject::ckSizeOfArray(order,"orderActivityCollection"))
CkJsonObject::setCkI(order, i)
If CkJsonObject::ckStringOfEquals(order,"orderActivityCollection[i].executionType","FILL",1) = 1
j.i = 0
While (j < CkJsonObject::ckSizeOfArray(order,"orderActivityCollection[i].executionLegs"))
CkJsonObject::setCkJ(order, j)
filledLeg.s = CkJsonObject::ckStringOf(order,"orderActivityCollection[i].executionLegs[j].legId")
Debug "Fill on leg " + filledLeg + " " + CkJsonObject::ckStringOf(symbols,filledLeg) + " quantity " + CkJsonObject::ckStringOf(order,"orderActivityCollection[i].executionLegs[j].quantity") + " price " + CkJsonObject::ckStringOf(order,"orderActivityCollection[i].executionLegs[j].price") + " time " + CkJsonObject::ckStringOf(order,"orderActivityCollection[i].executionLegs[j].time")
fills = fills + 1
j = j + 1
Wend
EndIf
i = i + 1
Wend
If fills = 0
Debug "No fills reported for this order."
EndIf
CkSecrets::ckDispose(vault)
CkJsonObject::ckDispose(entry)
CkJsonObject::ckDispose(tokens)
CkHttp::ckDispose(http)
CkJsonObject::ckDispose(selection)
CkHttpResponse::ckDispose(resp)
CkJsonObject::ckDispose(order)
CkJsonObject::ckDispose(symbols)
ProcedureReturn
EndProcedure