DataFlex Requires Chilkat v11.0.0+
DataFlex
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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoVault
Variant vEntry
Handle hoEntry
Variant vTokens
Handle hoTokens
String sAccessToken
Handle hoHttp
Variant vSelection
Handle hoSelection
String sAccountHash
String sOrderId
Variant vResp
Handle hoResp
Handle hoOrder
Handle hoSymbols
Integer i
String sLegId
String sSymbol
Integer iFills
Integer j
String sFilledLeg
String sTemp1
String sTemp2
String sTemp3
String sTemp4
Integer iTemp1
Boolean bTemp1
Move False To iSuccess
// Assumes Chilkat is unlocked. See Global Unlock Sample.
Move False To iSuccess
Get Create (RefClass(cComChilkatSecrets)) To hoVault
If (Not(IsComObjectCreated(hoVault))) Begin
Send CreateComObject of hoVault
End
Set ComLocation Of hoVault To "local_manager"
Set ComAutoSplit Of hoVault To True
Get Create (RefClass(cComChilkatJsonObject)) To hoEntry
If (Not(IsComObjectCreated(hoEntry))) Begin
Send CreateComObject of hoEntry
End
Get ComUpdateString Of hoEntry "appName" "SchwabTrader" To iSuccess
Get ComUpdateString Of hoEntry "service" "OAuth2" To iSuccess
Get ComUpdateString Of hoEntry "username" "personal-desktop" To iSuccess
Get Create (RefClass(cComChilkatJsonObject)) To hoTokens
If (Not(IsComObjectCreated(hoTokens))) Begin
Send CreateComObject of hoTokens
End
Get pvComObject of hoEntry to vEntry
Get pvComObject of hoTokens to vTokens
Get ComGetSecretJson Of hoVault vEntry vTokens To iSuccess
If (iSuccess <> True) Begin
Showln "Could not read the token record. Check the store and complete desktop authorization first."
Procedure_Return
End
Get ComStringOf Of hoTokens "access_token" To sAccessToken
If (sAccessToken = "") Begin
Showln "Run the Schwab authorization or refresh example to save an access token."
Procedure_Return
End
Get Create (RefClass(cComChilkatHttp)) To hoHttp
If (Not(IsComObjectCreated(hoHttp))) Begin
Send CreateComObject of hoHttp
End
Set ComRequireSslCertVerify Of hoHttp To True
Set ComAuthToken Of hoHttp To sAccessToken
// Read the hash saved by the authorized-account selection example.
Get ComUpdateString Of hoEntry "service" "SelectedAccount" To iSuccess
Get Create (RefClass(cComChilkatJsonObject)) To hoSelection
If (Not(IsComObjectCreated(hoSelection))) Begin
Send CreateComObject of hoSelection
End
Get pvComObject of hoEntry to vEntry
Get pvComObject of hoSelection to vSelection
Get ComGetSecretJson Of hoVault vEntry vSelection To iSuccess
Get ComStringOf Of hoSelection "hashValue" To sAccountHash
If ((iSuccess <> True) Or (sAccountHash = "")) Begin
Showln "Select an authorized account first."
Procedure_Return
End
Get ComSetUrlVar Of hoHttp "account" sAccountHash To iSuccess
// Supply the ID as text so large numeric identifiers keep all their digits.
Move "ORDER_ID_FROM_LIST" To sOrderId
Get ComSetUrlVar Of hoHttp "orderId" sOrderId To iSuccess
Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
If (Not(IsComObjectCreated(hoResp))) Begin
Send CreateComObject of hoResp
End
Get pvComObject of hoResp to vResp
Get ComHttpNoBody Of hoHttp "GET" "https://api.schwabapi.com/trader/v1/accounts/{$account}/orders/{$orderId}" vResp To iSuccess
Get ComStatusCode Of hoResp To iTemp1
If ((iSuccess <> True) Or (iTemp1 <> 200)) Begin
Get ComStatusCode Of hoResp To iTemp1
If (iTemp1 = 0) Begin
Showln "No HTTP response. Check connectivity."
End
Else Begin
Get ComStatusCode Of hoResp To iTemp1
Showln "Schwab returned HTTP " iTemp1
Get ComStatusCode Of hoResp To iTemp1
If (iTemp1 = 401) Begin
Showln "Refresh the access token or repeat desktop authorization."
End
End
Procedure_Return
End
Get Create (RefClass(cComChilkatJsonObject)) To hoOrder
If (Not(IsComObjectCreated(hoOrder))) Begin
Send CreateComObject of hoOrder
End
Get ComBodyStr Of hoResp To sTemp1
Get ComLoad Of hoOrder sTemp1 To iSuccess
If (iSuccess <> True) Begin
Showln "Schwab returned unexpected JSON."
Procedure_Return
End
Get ComStringOf Of hoOrder "orderId" To sTemp1
Get ComStringOf Of hoOrder "status" To sTemp2
Showln "Order " sTemp1 " " sTemp2
Get Create (RefClass(cComChilkatJsonObject)) To hoSymbols
If (Not(IsComObjectCreated(hoSymbols))) Begin
Send CreateComObject of hoSymbols
End
Move 0 To i
While (i < (ComSizeOfArray(hoOrder, "orderLegCollection")))
Set ComI Of hoOrder To i
Get ComStringOf Of hoOrder "orderLegCollection[i].legId" To sLegId
Get ComStringOf Of hoOrder "orderLegCollection[i].instrument.symbol" To sSymbol
Get ComUpdateString Of hoSymbols sLegId sSymbol To iSuccess
Get ComStringOf Of hoOrder "orderLegCollection[i].instruction" To sTemp1
Get ComStringOf Of hoOrder "orderLegCollection[i].quantity" To sTemp2
Showln "Leg " sLegId " " sTemp1 " " sTemp2 " " sSymbol
Move (i + 1) To i
Loop
Move 0 To iFills
Move 0 To i
While (i < (ComSizeOfArray(hoOrder, "orderActivityCollection")))
Set ComI Of hoOrder To i
Get ComStringOfEquals Of hoOrder "orderActivityCollection[i].executionType" "FILL" True To bTemp1
If (bTemp1 = True) Begin
Move 0 To j
While (j < (ComSizeOfArray(hoOrder, "orderActivityCollection[i].executionLegs")))
Set ComJ Of hoOrder To j
Get ComStringOf Of hoOrder "orderActivityCollection[i].executionLegs[j].legId" To sFilledLeg
Get ComStringOf Of hoSymbols sFilledLeg To sTemp1
Get ComStringOf Of hoOrder "orderActivityCollection[i].executionLegs[j].quantity" To sTemp2
Get ComStringOf Of hoOrder "orderActivityCollection[i].executionLegs[j].price" To sTemp3
Get ComStringOf Of hoOrder "orderActivityCollection[i].executionLegs[j].time" To sTemp4
Showln "Fill on leg " sFilledLeg " " sTemp1 " quantity " sTemp2 " price " sTemp3 " time " sTemp4
Move (iFills + 1) To iFills
Move (j + 1) To j
Loop
End
Move (i + 1) To i
Loop
If (iFills = 0) Begin
Showln "No fills reported for this order."
End
End_Procedure