Unicode C Requires Chilkat v11.0.0+
Unicode C
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 Unicode C Downloads
#include <C_CkSecretsW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkHttpW.h>
#include <C_CkHttpResponseW.h>
#include <C_CkHttpRequestW.h>
#include <C_CkJsonArrayW.h>
void ChilkatSample(void)
{
BOOL success;
HCkSecretsW vault;
HCkJsonObjectW entry;
HCkJsonObjectW tokens;
const wchar_t *accessToken;
HCkHttpW http;
const wchar_t *underlying;
const wchar_t *expiration;
const wchar_t *strike;
const wchar_t *side;
HCkHttpResponseW resp;
HCkHttpRequestW query;
HCkJsonObjectW reply;
const wchar_t *mapName;
HCkJsonObjectW expirations;
HCkJsonObjectW strikes;
HCkJsonArrayW contracts;
HCkJsonObjectW contract;
int found;
int i;
int j;
int k;
success = FALSE;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success = FALSE;
vault = CkSecretsW_Create();
CkSecretsW_putLocation(vault,L"local_manager");
CkSecretsW_putAutoSplit(vault,TRUE);
entry = CkJsonObjectW_Create();
CkJsonObjectW_UpdateString(entry,L"appName",L"SchwabTrader");
CkJsonObjectW_UpdateString(entry,L"service",L"OAuth2");
CkJsonObjectW_UpdateString(entry,L"username",L"personal-desktop");
tokens = CkJsonObjectW_Create();
success = CkSecretsW_GetSecretJson(vault,entry,tokens);
if (success != TRUE) {
wprintf(L"Could not read the token record. Check the store and complete desktop authorization first.\n");
CkSecretsW_Dispose(vault);
CkJsonObjectW_Dispose(entry);
CkJsonObjectW_Dispose(tokens);
return;
}
accessToken = CkJsonObjectW_stringOf(tokens,L"access_token");
if (accessToken == L"") {
wprintf(L"Run the Schwab authorization or refresh example to save an access token.\n");
CkSecretsW_Dispose(vault);
CkJsonObjectW_Dispose(entry);
CkJsonObjectW_Dispose(tokens);
return;
}
http = CkHttpW_Create();
CkHttpW_putRequireSslCertVerify(http,TRUE);
CkHttpW_putAuthToken(http,accessToken);
underlying = L"AAPL";
expiration = L"2027-01-15";
strike = L"250";
side = L"CALL";
resp = CkHttpResponseW_Create();
query = CkHttpRequestW_Create();
CkHttpRequestW_putHttpVerb(query,L"GET");
CkHttpRequestW_AddParam(query,L"symbol",underlying);
CkHttpRequestW_AddParam(query,L"contractType",side);
CkHttpRequestW_AddParam(query,L"strike",strike);
CkHttpRequestW_AddParam(query,L"fromDate",expiration);
CkHttpRequestW_AddParam(query,L"toDate",expiration);
success = CkHttpW_HttpReq(http,L"https://api.schwabapi.com/marketdata/v1/chains",query,resp);
if ((success != TRUE) || (CkHttpResponseW_getStatusCode(resp) != 200)) {
if (CkHttpResponseW_getStatusCode(resp) == 0) {
wprintf(L"No HTTP response. Check connectivity.\n");
}
else {
wprintf(L"Schwab returned HTTP %d\n",CkHttpResponseW_getStatusCode(resp));
if (CkHttpResponseW_getStatusCode(resp) == 401) {
wprintf(L"Refresh the access token or repeat desktop authorization.\n");
}
}
CkSecretsW_Dispose(vault);
CkJsonObjectW_Dispose(entry);
CkJsonObjectW_Dispose(tokens);
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkHttpRequestW_Dispose(query);
return;
}
reply = CkJsonObjectW_Create();
success = CkJsonObjectW_Load(reply,CkHttpResponseW_bodyStr(resp));
if (success != TRUE) {
wprintf(L"Schwab returned unexpected JSON.\n");
CkSecretsW_Dispose(vault);
CkJsonObjectW_Dispose(entry);
CkJsonObjectW_Dispose(tokens);
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkHttpRequestW_Dispose(query);
CkJsonObjectW_Dispose(reply);
return;
}
mapName = L"callExpDateMap";
if (side == L"PUT") {
mapName = L"putExpDateMap";
}
expirations = CkJsonObjectW_Create();
success = CkJsonObjectW_ObjectOf2(reply,mapName,expirations);
if (success != TRUE) {
wprintf(L"No option map returned. Chain status: %s\n",CkJsonObjectW_stringOf(reply,L"status"));
CkSecretsW_Dispose(vault);
CkJsonObjectW_Dispose(entry);
CkJsonObjectW_Dispose(tokens);
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkHttpRequestW_Dispose(query);
CkJsonObjectW_Dispose(reply);
CkJsonObjectW_Dispose(expirations);
return;
}
// Expiration keys contain a day count. Strike keys may contain a decimal point.
strikes = CkJsonObjectW_Create();
contracts = CkJsonArrayW_Create();
contract = CkJsonObjectW_Create();
found = 0;
i = 0;
while ((i < CkJsonObjectW_getSize(expirations))) {
CkJsonObjectW_ObjectAt2(expirations,i,strikes);
j = 0;
while ((j < CkJsonObjectW_getSize(strikes))) {
CkJsonObjectW_ArrayAt2(strikes,j,contracts);
k = 0;
while ((k < CkJsonArrayW_getSize(contracts))) {
CkJsonArrayW_ObjectAt2(contracts,k,contract);
wprintf(L"%s strike %s\n",CkJsonObjectW_stringOf(contract,L"description"),CkJsonObjectW_stringOf(contract,L"strikePrice"));
wprintf(L"Contract symbol: [%s]\n",CkJsonObjectW_stringOf(contract,L"symbol"));
found = found + 1;
k = k + 1;
}
j = j + 1;
}
i = i + 1;
}
if (found == 0) {
wprintf(L"No contracts matched the requested expiration, side and strike.\n");
}
CkSecretsW_Dispose(vault);
CkJsonObjectW_Dispose(entry);
CkJsonObjectW_Dispose(tokens);
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkHttpRequestW_Dispose(query);
CkJsonObjectW_Dispose(reply);
CkJsonObjectW_Dispose(expirations);
CkJsonObjectW_Dispose(strikes);
CkJsonArrayW_Dispose(contracts);
CkJsonObjectW_Dispose(contract);
}