Sample code for 30+ languages & platforms
SQL Server Requires Chilkat v11.0.0+

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.

Schwab API specification

Chilkat SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @sTmp1 nvarchar(4000)
    DECLARE @sTmp2 nvarchar(4000)
    DECLARE @sTmp3 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    --  Assumes Chilkat is unlocked. See Global Unlock Sample.
    SELECT @success = 0

    DECLARE @vault int
    EXEC @hr = sp_OACreate 'Chilkat.Secrets', @vault OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OASetProperty @vault, 'Location', 'local_manager'
    EXEC sp_OASetProperty @vault, 'AutoSplit', 1
    DECLARE @entry int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @entry OUT

    EXEC sp_OAMethod @entry, 'UpdateString', @success OUT, 'appName', 'SchwabTrader'
    EXEC sp_OAMethod @entry, 'UpdateString', @success OUT, 'service', 'OAuth2'
    EXEC sp_OAMethod @entry, 'UpdateString', @success OUT, 'username', 'personal-desktop'
    DECLARE @tokens int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @tokens OUT

    EXEC sp_OAMethod @vault, 'GetSecretJson', @success OUT, @entry, @tokens
    IF @success <> 1
      BEGIN

        PRINT 'Could not read the token record. Check the store and complete desktop authorization first.'
        EXEC @hr = sp_OADestroy @vault
        EXEC @hr = sp_OADestroy @entry
        EXEC @hr = sp_OADestroy @tokens
        RETURN
      END
    DECLARE @accessToken nvarchar(4000)
    EXEC sp_OAMethod @tokens, 'StringOf', @accessToken OUT, 'access_token'
    IF @accessToken = ''
      BEGIN

        PRINT 'Run the Schwab authorization or refresh example to save an access token.'
        EXEC @hr = sp_OADestroy @vault
        EXEC @hr = sp_OADestroy @entry
        EXEC @hr = sp_OADestroy @tokens
        RETURN
      END
    DECLARE @http int
    EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT

    EXEC sp_OASetProperty @http, 'RequireSslCertVerify', 1
    EXEC sp_OASetProperty @http, 'AuthToken', @accessToken

    --  Read the hash saved by the authorized-account selection example.
    EXEC sp_OAMethod @entry, 'UpdateString', @success OUT, 'service', 'SelectedAccount'
    DECLARE @selection int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @selection OUT

    EXEC sp_OAMethod @vault, 'GetSecretJson', @success OUT, @entry, @selection
    DECLARE @accountHash nvarchar(4000)
    EXEC sp_OAMethod @selection, 'StringOf', @accountHash OUT, 'hashValue'
    IF (@success <> 1) or (@accountHash = '')
      BEGIN

        PRINT 'Select an authorized account first.'
        EXEC @hr = sp_OADestroy @vault
        EXEC @hr = sp_OADestroy @entry
        EXEC @hr = sp_OADestroy @tokens
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @selection
        RETURN
      END
    EXEC sp_OAMethod @http, 'SetUrlVar', @success OUT, 'account', @accountHash

    --  Supply the ID as text so large numeric identifiers keep all their digits.
    DECLARE @orderId nvarchar(4000)
    SELECT @orderId = 'ORDER_ID_FROM_LIST'
    EXEC sp_OAMethod @http, 'SetUrlVar', @success OUT, 'orderId', @orderId
    DECLARE @resp int
    EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT

    EXEC sp_OAMethod @http, 'HttpNoBody', @success OUT, 'GET', 'https://api.schwabapi.com/trader/v1/accounts/{$account}/orders/{$orderId}', @resp
    EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
    IF (@success <> 1) or (@iTmp0 <> 200)
      BEGIN
        EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
        IF @iTmp0 = 0
          BEGIN

            PRINT 'No HTTP response. Check connectivity.'
          END
        ELSE
          BEGIN

            EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
            PRINT 'Schwab returned HTTP ' + @iTmp0
            EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
            IF @iTmp0 = 401
              BEGIN

                PRINT 'Refresh the access token or repeat desktop authorization.'
              END
          END
        EXEC @hr = sp_OADestroy @vault
        EXEC @hr = sp_OADestroy @entry
        EXEC @hr = sp_OADestroy @tokens
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @selection
        EXEC @hr = sp_OADestroy @resp
        RETURN
      END
    DECLARE @order int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @order OUT

    EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
    EXEC sp_OAMethod @order, 'Load', @success OUT, @sTmp0
    IF @success <> 1
      BEGIN

        PRINT 'Schwab returned unexpected JSON.'
        EXEC @hr = sp_OADestroy @vault
        EXEC @hr = sp_OADestroy @entry
        EXEC @hr = sp_OADestroy @tokens
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @selection
        EXEC @hr = sp_OADestroy @resp
        EXEC @hr = sp_OADestroy @order
        RETURN
      END


    EXEC sp_OAMethod @order, 'StringOf', @sTmp0 OUT, 'orderId'

    EXEC sp_OAMethod @order, 'StringOf', @sTmp1 OUT, 'status'
    PRINT 'Order ' + @sTmp0 + '  ' + @sTmp1
    DECLARE @symbols int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @symbols OUT

    DECLARE @i int
    SELECT @i = 0
    EXEC sp_OAMethod @order, 'SizeOfArray', @iTmp0 OUT, 'orderLegCollection'
    WHILE (@i < @iTmp0)
      BEGIN
        EXEC sp_OASetProperty @order, 'I', @i
        DECLARE @legId nvarchar(4000)
        EXEC sp_OAMethod @order, 'StringOf', @legId OUT, 'orderLegCollection[i].legId'
        DECLARE @symbol nvarchar(4000)
        EXEC sp_OAMethod @order, 'StringOf', @symbol OUT, 'orderLegCollection[i].instrument.symbol'
        EXEC sp_OAMethod @symbols, 'UpdateString', @success OUT, @legId, @symbol


        EXEC sp_OAMethod @order, 'StringOf', @sTmp0 OUT, 'orderLegCollection[i].instruction'

        EXEC sp_OAMethod @order, 'StringOf', @sTmp1 OUT, 'orderLegCollection[i].quantity'

        PRINT 'Leg ' + @legId + '  ' + @sTmp0 + ' ' + @sTmp1 + ' ' + @symbol
        SELECT @i = @i + 1
      END
    DECLARE @fills int
    SELECT @fills = 0
    SELECT @i = 0
    EXEC sp_OAMethod @order, 'SizeOfArray', @iTmp0 OUT, 'orderActivityCollection'
    WHILE (@i < @iTmp0)
      BEGIN
        EXEC sp_OASetProperty @order, 'I', @i
        EXEC sp_OAMethod @order, 'StringOfEquals', @iTmp0 OUT, 'orderActivityCollection[i].executionType', 'FILL', 1
        IF @iTmp0 = 1
          BEGIN
            DECLARE @j int
            SELECT @j = 0
            EXEC sp_OAMethod @order, 'SizeOfArray', @iTmp0 OUT, 'orderActivityCollection[i].executionLegs'
            WHILE (@j < @iTmp0)
              BEGIN
                EXEC sp_OASetProperty @order, 'J', @j
                DECLARE @filledLeg nvarchar(4000)
                EXEC sp_OAMethod @order, 'StringOf', @filledLeg OUT, 'orderActivityCollection[i].executionLegs[j].legId'


                EXEC sp_OAMethod @symbols, 'StringOf', @sTmp0 OUT, @filledLeg

                EXEC sp_OAMethod @order, 'StringOf', @sTmp1 OUT, 'orderActivityCollection[i].executionLegs[j].quantity'

                EXEC sp_OAMethod @order, 'StringOf', @sTmp2 OUT, 'orderActivityCollection[i].executionLegs[j].price'

                EXEC sp_OAMethod @order, 'StringOf', @sTmp3 OUT, 'orderActivityCollection[i].executionLegs[j].time'
                PRINT 'Fill on leg ' + @filledLeg + '  ' + @sTmp0 + '  quantity ' + @sTmp1 + '  price ' + @sTmp2 + '  time ' + @sTmp3
                SELECT @fills = @fills + 1
                SELECT @j = @j + 1
              END
          END
        SELECT @i = @i + 1
      END
    IF @fills = 0
      BEGIN

        PRINT 'No fills reported for this order.'
      END

    EXEC @hr = sp_OADestroy @vault
    EXEC @hr = sp_OADestroy @entry
    EXEC @hr = sp_OADestroy @tokens
    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @selection
    EXEC @hr = sp_OADestroy @resp
    EXEC @hr = sp_OADestroy @order
    EXEC @hr = sp_OADestroy @symbols


END
GO