Lazarus Pascal Requires Chilkat v11.0.0+
Lazarus Pascal
Schwab: Find an option contract by expiration and strike
See more Schwab Examples
Filters a chain by underlying, call or put, exact expiration, and strike. Choose a currently listed expiration and strike. Schwab groups contracts by expiration and strike and may return multiple deliverables. Compare the descriptions and use the exact returned contract symbol, including its spaces, for a later quote request.
Use the access token saved by desktop authorization. Enable the Market Data API for the Schwab app. Use the same Chilkat Secrets store as the OAuth example.
Chilkat Lazarus Pascal Downloads
program ChilkatDemo;
// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
SysUtils,
CkDllLoader,
Chilkat.JsonArray,
Chilkat.HttpResponse,
Chilkat.HttpRequest,
Chilkat.JsonObject,
Chilkat.Secrets,
Chilkat.Http;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
vault: TSecrets;
entry: TJsonObject;
tokens: TJsonObject;
accessToken: string;
http: THttp;
underlying: string;
expiration: string;
strike: string;
side: string;
resp: THttpResponse;
query: THttpRequest;
reply: TJsonObject;
mapName: string;
expirations: TJsonObject;
strikes: TJsonObject;
contracts: TJsonArray;
contract: TJsonObject;
found: Integer;
i: Integer;
j: Integer;
k: Integer;
begin
success := False;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success := False;
vault := TSecrets.Create;
vault.Location := 'local_manager';
vault.AutoSplit := True;
entry := TJsonObject.Create;
entry.UpdateString('appName','SchwabTrader');
entry.UpdateString('service','OAuth2');
entry.UpdateString('username','personal-desktop');
tokens := TJsonObject.Create;
success := vault.GetSecretJson(entry,tokens);
if (success <> True) then
begin
WriteLn('Could not read the token record. Check the store and complete desktop authorization first.');
Exit;
end;
accessToken := tokens.StringOf('access_token');
if (accessToken = '') then
begin
WriteLn('Run the Schwab authorization or refresh example to save an access token.');
Exit;
end;
http := THttp.Create;
http.RequireSslCertVerify := True;
http.AuthToken := accessToken;
underlying := 'AAPL';
expiration := '2027-01-15';
strike := '250';
side := 'CALL';
resp := THttpResponse.Create;
query := THttpRequest.Create;
query.HttpVerb := 'GET';
query.AddParam('symbol',underlying);
query.AddParam('contractType',side);
query.AddParam('strike',strike);
query.AddParam('fromDate',expiration);
query.AddParam('toDate',expiration);
success := http.HttpReq('https://api.schwabapi.com/marketdata/v1/chains',query,resp);
if ((success <> True) or (resp.StatusCode <> 200)) then
begin
if (resp.StatusCode = 0) then
begin
WriteLn('No HTTP response. Check connectivity.');
end
else
begin
WriteLn('Schwab returned HTTP ' + resp.StatusCode);
if (resp.StatusCode = 401) then
begin
WriteLn('Refresh the access token or repeat desktop authorization.');
end;
end;
Exit;
end;
reply := TJsonObject.Create;
success := reply.Load(resp.BodyStr);
if (success <> True) then
begin
WriteLn('Schwab returned unexpected JSON.');
Exit;
end;
mapName := 'callExpDateMap';
if (side = 'PUT') then
begin
mapName := 'putExpDateMap';
end;
expirations := TJsonObject.Create;
success := reply.ObjectOf2(mapName,expirations);
if (success <> True) then
begin
WriteLn('No option map returned. Chain status: ' + reply.StringOf('status'));
Exit;
end;
// Expiration keys contain a day count. Strike keys may contain a decimal point.
strikes := TJsonObject.Create;
contracts := TJsonArray.Create;
contract := TJsonObject.Create;
found := 0;
i := 0;
while (i < expirations.Size) do
begin
expirations.ObjectAt2(i,strikes);
j := 0;
while (j < strikes.Size) do
begin
strikes.ArrayAt2(j,contracts);
k := 0;
while (k < contracts.Size) do
begin
contracts.ObjectAt2(k,contract);
WriteLn(contract.StringOf('description') + ' strike ' + contract.StringOf('strikePrice'));
WriteLn('Contract symbol: [' + contract.StringOf('symbol') + ']');
found := found + 1;
k := k + 1;
end;
j := j + 1;
end;
i := i + 1;
end;
if (found = 0) then
begin
WriteLn('No contracts matched the requested expiration, side and strike.');
end;
vault.Free;
entry.Free;
tokens.Free;
http.Free;
resp.Free;
query.Free;
reply.Free;
expirations.Free;
strikes.Free;
contracts.Free;
contract.Free;
end;
// ---------------------------------------------------------------------------
begin
try
RunDemo;
except
on E: Exception do
WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
end;
WriteLn;
{$IFDEF MSWINDOWS}
WriteLn('Press Enter to exit...');
ReadLn;
{$ENDIF}
end.