SQL Server Requires Chilkat v11.0.0+
SQL Server
Schwab: List recent orders for one account
See more Schwab Examples
Lists orders entered in the last seven days, with an optional status filter such as WORKING. Shows the order ID, entry time, status, filled quantity, and remaining quantity. Select an ID from the results for the order detail and fills example. An older order can still be working outside this entry-time window.
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 SQL Server Downloads
-- 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
-- Use UTC endpoints for a rolling seven-day window.
DECLARE @clock int
EXEC @hr = sp_OACreate 'Chilkat.CkDateTime', @clock OUT
EXEC sp_OAMethod @clock, 'SetFromCurrentSystemTime', @success OUT
DECLARE @endTime int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @endTime OUT
EXEC sp_OAMethod @clock, 'GetAsTimestamp', @sTmp0 OUT, 0
EXEC sp_OAMethod @endTime, 'Append', @success OUT, @sTmp0
DECLARE @replacements int
EXEC sp_OAMethod @endTime, 'Replace', @replacements OUT, 'Z', '.000Z'
EXEC sp_OAMethod @clock, 'AddDays', @success OUT, -7
DECLARE @startTime int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @startTime OUT
EXEC sp_OAMethod @clock, 'GetAsTimestamp', @sTmp0 OUT, 0
EXEC sp_OAMethod @startTime, 'Append', @success OUT, @sTmp0
EXEC sp_OAMethod @startTime, 'Replace', @replacements OUT, 'Z', '.000Z'
DECLARE @statusFilter nvarchar(4000)
SELECT @statusFilter = ''
DECLARE @resp int
EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT
DECLARE @query int
EXEC @hr = sp_OACreate 'Chilkat.HttpRequest', @query OUT
EXEC sp_OASetProperty @query, 'HttpVerb', 'GET'
EXEC sp_OAMethod @startTime, 'GetAsString', @sTmp0 OUT
EXEC sp_OAMethod @query, 'AddParam', NULL, 'fromEnteredTime', @sTmp0
EXEC sp_OAMethod @endTime, 'GetAsString', @sTmp0 OUT
EXEC sp_OAMethod @query, 'AddParam', NULL, 'toEnteredTime', @sTmp0
EXEC sp_OAMethod @query, 'AddParam', NULL, 'maxResults', '100'
IF @statusFilter <> ''
BEGIN
EXEC sp_OAMethod @query, 'AddParam', NULL, 'status', @statusFilter
END
EXEC sp_OAMethod @http, 'HttpReq', @success OUT, 'https://api.schwabapi.com/trader/v1/accounts/{$account}/orders', @query, @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 @clock
EXEC @hr = sp_OADestroy @endTime
EXEC @hr = sp_OADestroy @startTime
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @query
RETURN
END
DECLARE @orders int
EXEC @hr = sp_OACreate 'Chilkat.JsonArray', @orders OUT
EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
EXEC sp_OAMethod @orders, '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 @clock
EXEC @hr = sp_OADestroy @endTime
EXEC @hr = sp_OADestroy @startTime
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @query
EXEC @hr = sp_OADestroy @orders
RETURN
END
EXEC sp_OAGetProperty @orders, 'Size', @iTmp0 OUT
IF @iTmp0 = 0
BEGIN
PRINT 'No matching orders entered during this interval.'
END
EXEC sp_OAGetProperty @orders, 'Size', @iTmp0 OUT
IF @iTmp0 = 100
BEGIN
PRINT 'Reached maxResults. Narrow the interval to check for additional orders.'
END
DECLARE @order int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @order OUT
DECLARE @i int
SELECT @i = 0
EXEC sp_OAGetProperty @orders, 'Size', @iTmp0 OUT
WHILE (@i < @iTmp0)
BEGIN
EXEC sp_OAMethod @orders, 'ObjectAt2', @success OUT, @i, @order
EXEC sp_OAMethod @order, 'StringOf', @sTmp0 OUT, 'orderId'
EXEC sp_OAMethod @order, 'StringOf', @sTmp1 OUT, 'enteredTime'
EXEC sp_OAMethod @order, 'StringOf', @sTmp2 OUT, 'orderType'
EXEC sp_OAMethod @order, 'StringOf', @sTmp3 OUT, 'status'
PRINT 'Order ' + @sTmp0 + ' ' + @sTmp1 + ' ' + @sTmp2 + ' ' + @sTmp3
EXEC sp_OAMethod @order, 'StringOf', @sTmp0 OUT, 'filledQuantity'
EXEC sp_OAMethod @order, 'StringOf', @sTmp1 OUT, 'remainingQuantity'
PRINT ' filled ' + @sTmp0 + ' remaining ' + @sTmp1
SELECT @i = @i + 1
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 @clock
EXEC @hr = sp_OADestroy @endTime
EXEC @hr = sp_OADestroy @startTime
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @query
EXEC @hr = sp_OADestroy @orders
EXEC @hr = sp_OADestroy @order
END
GO