Sample code for 30+ languages & platforms
Delphi DLL Requires Chilkat v11.0.0+

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.

Schwab API specification

Chilkat Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
vault: HCkSecrets;
entry: HCkJsonObject;
tokens: HCkJsonObject;
accessToken: PWideChar;
http: HCkHttp;
underlying: PWideChar;
expiration: PWideChar;
strike: PWideChar;
side: PWideChar;
resp: HCkHttpResponse;
query: HCkHttpRequest;
reply: HCkJsonObject;
mapName: PWideChar;
expirations: HCkJsonObject;
strikes: HCkJsonObject;
contracts: HCkJsonArray;
contract: HCkJsonObject;
found: Integer;
i: Integer;
j: Integer;
k: Integer;

begin
success := False;

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
success := False;

vault := CkSecrets_Create();
CkSecrets_putLocation(vault,'local_manager');
CkSecrets_putAutoSplit(vault,True);
entry := CkJsonObject_Create();
CkJsonObject_UpdateString(entry,'appName','SchwabTrader');
CkJsonObject_UpdateString(entry,'service','OAuth2');
CkJsonObject_UpdateString(entry,'username','personal-desktop');
tokens := CkJsonObject_Create();
success := CkSecrets_GetSecretJson(vault,entry,tokens);
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not read the token record. Check the store and complete desktop authorization first.');
    Exit;
  end;
accessToken := CkJsonObject__stringOf(tokens,'access_token');
if (accessToken = '') then
  begin
    Memo1.Lines.Add('Run the Schwab authorization or refresh example to save an access token.');
    Exit;
  end;
http := CkHttp_Create();
CkHttp_putRequireSslCertVerify(http,True);
CkHttp_putAuthToken(http,accessToken);

underlying := 'AAPL';
expiration := '2027-01-15';
strike := '250';
side := 'CALL';
resp := CkHttpResponse_Create();
query := CkHttpRequest_Create();
CkHttpRequest_putHttpVerb(query,'GET');
CkHttpRequest_AddParam(query,'symbol',underlying);
CkHttpRequest_AddParam(query,'contractType',side);
CkHttpRequest_AddParam(query,'strike',strike);
CkHttpRequest_AddParam(query,'fromDate',expiration);
CkHttpRequest_AddParam(query,'toDate',expiration);
success := CkHttp_HttpReq(http,'https://api.schwabapi.com/marketdata/v1/chains',query,resp);
if ((success <> True) or (CkHttpResponse_getStatusCode(resp) <> 200)) then
  begin
    if (CkHttpResponse_getStatusCode(resp) = 0) then
      begin
        Memo1.Lines.Add('No HTTP response. Check connectivity.');
      end
    else
      begin
        Memo1.Lines.Add('Schwab returned HTTP ' + IntToStr(CkHttpResponse_getStatusCode(resp)));
        if (CkHttpResponse_getStatusCode(resp) = 401) then
          begin
            Memo1.Lines.Add('Refresh the access token or repeat desktop authorization.');
          end;
      end;
    Exit;
  end;
reply := CkJsonObject_Create();
success := CkJsonObject_Load(reply,CkHttpResponse__bodyStr(resp));
if (success <> True) then
  begin
    Memo1.Lines.Add('Schwab returned unexpected JSON.');
    Exit;
  end;

mapName := 'callExpDateMap';
if (side = 'PUT') then
  begin
    mapName := 'putExpDateMap';
  end;
expirations := CkJsonObject_Create();
success := CkJsonObject_ObjectOf2(reply,mapName,expirations);
if (success <> True) then
  begin
    Memo1.Lines.Add('No option map returned. Chain status: ' + CkJsonObject__stringOf(reply,'status'));
    Exit;
  end;
//  Expiration keys contain a day count. Strike keys may contain a decimal point.
strikes := CkJsonObject_Create();
contracts := CkJsonArray_Create();
contract := CkJsonObject_Create();
found := 0;
i := 0;
while (i < CkJsonObject_getSize(expirations)) do
  begin
    CkJsonObject_ObjectAt2(expirations,i,strikes);
    j := 0;
    while (j < CkJsonObject_getSize(strikes)) do
      begin
        CkJsonObject_ArrayAt2(strikes,j,contracts);
        k := 0;
        while (k < CkJsonArray_getSize(contracts)) do
          begin
            CkJsonArray_ObjectAt2(contracts,k,contract);
            Memo1.Lines.Add(CkJsonObject__stringOf(contract,'description') + '  strike ' + CkJsonObject__stringOf(contract,'strikePrice'));
            Memo1.Lines.Add('Contract symbol: [' + CkJsonObject__stringOf(contract,'symbol') + ']');
            found := found + 1;
            k := k + 1;
          end;

        j := j + 1;
      end;

    i := i + 1;
  end;

if (found = 0) then
  begin
    Memo1.Lines.Add('No contracts matched the requested expiration, side and strike.');
  end;

CkSecrets_Dispose(vault);
CkJsonObject_Dispose(entry);
CkJsonObject_Dispose(tokens);
CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
CkHttpRequest_Dispose(query);
CkJsonObject_Dispose(reply);
CkJsonObject_Dispose(expirations);
CkJsonObject_Dispose(strikes);
CkJsonArray_Dispose(contracts);
CkJsonObject_Dispose(contract);