Option Chains - Fetch
Retrieve E*Trade Option Chains using SQL Server
- See SQLHTTP easy setup for E*Trade
- See API Call documentation for parameter values and other information
- Create the stored procedure documented below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
CREATE PROCEDURE usp_ETrade_Option_Chains_Fetch( @Profile nvarchar(100), @chainType varchar(10), @expirationMonth tinyint, @expirationYear smallint, @underlier varchar(20), @skipAdjusted bit = 1, @Response nvarchar(MAX) OUTPUT, @StatusCode int OUTPUT, @StatusDescription nvarchar(MAX) OUTPUT) AS DECLARE @Text_skipAdjusted varchar(5) SET @Text_skipAdjusted = CASE WHEN @skipAdjusted = 0 THEN 'false' ELSE 'true' END EXEC usp_ETrade_Auth_Refresh @Profile DECLARE @URL nvarchar(MAX) DECLARE @QueryString varchar(MAX) DECLARE @HTTPSessionID uniqueidentifier DECLARE @Sandbox bit SET @Sandbox = SQLHTTP.net.AuthParam(@Profile, 'Sandbox') IF @Sandbox = 1 BEGIN SET @URL = 'https://etwssandbox.etrade.com' EXEC SQLHTTP.net.UrlBuilder @URL OUTPUT, @Profile, 'market', 'sandbox', 'rest', 'optionchains' EXEC SQLHTTP.net.QueryStringBuilder @QueryString OUTPUT, @Profile, 'chainType', @chainType, 'expirationMonth', @expirationMonth, 'expirationYear', @expirationYear, 'underlier', @underlier, 'skipAdjusted', @Text_skipAdjusted SET @URL = @URL + @QueryString END ELSE BEGIN SET @URL = 'https://etws.etrade.com' EXEC SQLHTTP.net.UrlBuilder @URL OUTPUT, @Profile, 'market', 'rest', 'optionchains' EXEC SQLHTTP.net.QueryStringBuilder @QueryString OUTPUT, @Profile, 'chainType', @chainType, 'expirationMonth', @expirationMonth, 'expirationYear', @expirationYear, 'underlier', @underlier, 'skipAdjusted', @Text_skipAdjusted SET @URL = @URL + @QueryString END EXEC SQLHTTP.net.HTTPSession @HTTPSessionID OUTPUT EXEC usp_Etrade_Auth_Header @Profile, @HTTPSessionID, @URL, 'GET' EXEC SQLHTTP.net.HTTPRequest @HttpSessionID, @URL = @URL, @Method = 'GET', @StatusCode = @StatusCode OUTPUT, @StatusDescription = @StatusDescription OUTPUT, @Response = @Response OUTPUT GO |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
DECLARE @Response nvarchar(MAX) DECLARE @StatusCode int DECLARE @StatusDescription nvarchar(MAX) EXEC usp_ETrade_Option_Chains_Fetch @Profile = 'My Etrade Sandbox', @chainType = 'PUT', @expirationMonth = 3, @expirationYear = 2018, @underlier = 'SPY', @Response = @Response OUTPUT, @StatusCode = @StatusCode OUTPUT, @StatusDescription = @StatusDescription OUTPUT IF @StatusCode >= 400 EXEC SQLHTTP.net.RaiseHttpError @StatusCode, @StatusDescription, @Response ELSE IF SQLHTTP.net.IsXmlValid(@Response) = 0 BEGIN RAISERROR(@Response, 16, 1) RETURN END ELSE BEGIN DECLARE @X xml SET @X = CONVERT(xml, @Response) --The XPath syntax below was easily generated by executing the following commands: --EXEC SQLHTTP.net.XQueryHelper @X --and then executing: --EXEC SQLHTTP.net.XQueryHelper @X, 'OptionChainResponse/optionPairs' -- SELECT T.C.value(N'put[1]/expireDate[1]/day[1]', N'nvarchar(MAX)') AS [day] ,T.C.value(N'put[1]/expireDate[1]/month[1]', N'nvarchar(MAX)') AS [month] ,T.C.value(N'put[1]/expireDate[1]/year[1]', N'nvarchar(MAX)') AS [year] ,T.C.value(N'put[1]/product[1]/symbol[1]', N'nvarchar(MAX)') AS [symbol] ,T.C.value(N'put[1]/strikePrice[1]', N'nvarchar(MAX)') AS [strikePrice] FROM @X.nodes(N'/OptionChainResponse/optionPairs') T(C) END |
1 2 3 4 5 6 7 8 9 |
day month year symbol strikePrice ----- ------ ------- ----------------------------- --------------- 16 3 2018 SPY Mar 16 '18 $268 Put 268.000000 16 3 2018 SPY Mar 16 '18 $269 Put 269.000000 16 3 2018 SPY Mar 16 '18 $270 Put 270.000000 16 3 2018 SPY Mar 16 '18 $270.50 Put 270.500000 16 3 2018 SPY Mar 16 '18 $271 Put 271.000000 |
IMPORTANT DISCLAIMER
CODE/SQL ON THESE PAGES ARE PROVIDED AS-IS AND ARE AVAILABLE FOR ILLUSTRATIVE PURPOSES ONLY.
USERS ARE REQUIRED TO ABIDE BY THE TERMS AND CONDITIONS FOR USING REFERENCED THIRD PARTY WEBSITES AND/OR APIs FROM THEIR RESPECTIVE WEBSITES. WE DO NOT CONDONE ANY VIOLATION OF THIRD PARTY WEBSITES AND/OR APIs TERMS AND CONDITIONS USING OUR SOFTWARE.
USERS SHALL BE SOLELY RESPONSIBLE AND BE SOLELY LIABLE FOR VIOLATION OF ANY RULES SPECIFIED BY THIRD PARTIES FOR USING THEIR WEBSITES AND/OR APIs, OR INFRINGEMENT OF RIGHTS OF SUCH THIRD PARTIES.