SQL Server Requires Chilkat v11.0.0+
SQL Server
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 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 @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
-- These are illustrative preview inputs. Set the symbol, quantity and limit price.
DECLARE @order int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @order OUT
-- The PreviewOrder schema puts orderLegs inside orderStrategy.
EXEC sp_OAMethod @order, 'UpdateString', @success OUT, 'orderStrategy.session', 'NORMAL'
EXEC sp_OAMethod @order, 'UpdateString', @success OUT, 'orderStrategy.duration', 'DAY'
EXEC sp_OAMethod @order, 'UpdateString', @success OUT, 'orderStrategy.orderType', 'LIMIT'
EXEC sp_OAMethod @order, 'UpdateString', @success OUT, 'orderStrategy.orderStrategyType', 'SINGLE'
EXEC sp_OAMethod @order, 'UpdateNumber', @success OUT, 'orderStrategy.price', '100.00'
EXEC sp_OAMethod @order, 'UpdateInt', @success OUT, 'orderStrategy.quantity', 1
EXEC sp_OAMethod @order, 'UpdateString', @success OUT, 'orderStrategy.orderLegs[0].instruction', 'BUY'
EXEC sp_OAMethod @order, 'UpdateInt', @success OUT, 'orderStrategy.orderLegs[0].quantity', 1
EXEC sp_OAMethod @order, 'UpdateString', @success OUT, 'orderStrategy.orderLegs[0].finalSymbol', 'AAPL'
EXEC sp_OAMethod @order, 'UpdateString', @success OUT, 'orderStrategy.orderLegs[0].assetType', 'EQUITY'
DECLARE @resp int
EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT
EXEC sp_OAMethod @http, 'HttpJson', @success OUT, 'POST', 'https://api.schwabapi.com/trader/v1/accounts/{$account}/previewOrder', @order, 'application/json', @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 @order
EXEC @hr = sp_OADestroy @resp
RETURN
END
DECLARE @reply int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @reply OUT
EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
EXEC sp_OAMethod @reply, '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 @order
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @reply
RETURN
END
PRINT 'Preview only. No order was submitted.'
EXEC sp_OAMethod @reply, 'HasMember', @iTmp0 OUT, 'orderStrategy.orderType'
IF @iTmp0 = 1
BEGIN
EXEC sp_OAMethod @reply, 'StringOf', @sTmp0 OUT, 'orderStrategy.orderType'
EXEC sp_OAMethod @reply, 'StringOf', @sTmp1 OUT, 'orderStrategy.price'
PRINT 'Projected order: ' + @sTmp0 + ' price ' + @sTmp1
END
EXEC sp_OAMethod @reply, 'HasMember', @iTmp0 OUT, 'orderStrategy.orderBalance.projectedBuyingPower'
IF @iTmp0 = 1
BEGIN
EXEC sp_OAMethod @reply, 'StringOf', @sTmp0 OUT, 'orderStrategy.orderBalance.projectedBuyingPower'
PRINT 'Projected buying power: ' + @sTmp0
END
DECLARE @validation int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @validation OUT
EXEC sp_OAMethod @reply, 'ObjectOf2', @success OUT, 'orderValidationResult', @validation
IF @success <> 1
BEGIN
PRINT 'Schwab supplied no validation result.'
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 @order
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @reply
EXEC @hr = sp_OADestroy @validation
RETURN
END
-- Each category (accepts, alerts, warns, reviews, rejects) contains rule messages.
DECLARE @rules int
EXEC @hr = sp_OACreate 'Chilkat.JsonArray', @rules OUT
DECLARE @detail int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @detail OUT
DECLARE @messages int
SELECT @messages = 0
DECLARE @i int
SELECT @i = 0
EXEC sp_OAGetProperty @validation, 'Size', @iTmp0 OUT
WHILE (@i < @iTmp0)
BEGIN
DECLARE @category nvarchar(4000)
EXEC sp_OAMethod @validation, 'NameAt', @category OUT, @i
EXEC sp_OAMethod @validation, 'ArrayAt2', @success OUT, @i, @rules
IF @success = 1
BEGIN
DECLARE @j int
SELECT @j = 0
EXEC sp_OAGetProperty @rules, 'Size', @iTmp0 OUT
WHILE (@j < @iTmp0)
BEGIN
EXEC sp_OAMethod @rules, 'ObjectAt2', @success OUT, @j, @detail
EXEC sp_OAMethod @detail, 'StringOf', @sTmp0 OUT, 'validationRuleName'
EXEC sp_OAMethod @detail, 'StringOf', @sTmp1 OUT, 'message'
EXEC sp_OAMethod @detail, 'StringOf', @sTmp2 OUT, 'activityMessage'
PRINT @category + ' ' + @sTmp0 + ' ' + @sTmp1 + ' ' + @sTmp2
SELECT @messages = @messages + 1
SELECT @j = @j + 1
END
END
SELECT @i = @i + 1
END
IF @messages = 0
BEGIN
PRINT 'No validation messages returned.'
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 @order
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @reply
EXEC @hr = sp_OADestroy @validation
EXEC @hr = sp_OADestroy @rules
EXEC @hr = sp_OADestroy @detail
END
GO