Sample code for 30+ languages & platforms
Visual FoxPro Requires Chilkat v11.0.0+

Schwab: Get quotes for a watchlist

See more Schwab Examples

Requests several symbols in one call and prints the bid, ask, last price, quote timestamp in Unix milliseconds, and the real-time indicator when available. Missing quote fields can occur for individual instruments. The response uses symbol names as JSON keys and can report invalid symbols alongside valid quotes.

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 Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loVault
LOCAL loEntry
LOCAL loTokens
LOCAL lcAccessToken
LOCAL loHttp
LOCAL loResp
LOCAL loQuery
LOCAL loReply
LOCAL loInstrument
LOCAL i
LOCAL lcKey
LOCAL j

lnSuccess = 0

*  Assumes Chilkat is unlocked. See Global Unlock Sample.
lnSuccess = 0

loVault = CreateObject('Chilkat.Secrets')
loVault.Location = "local_manager"
loVault.AutoSplit = 1
loEntry = CreateObject('Chilkat.JsonObject')
loEntry.UpdateString("appName","SchwabTrader")
loEntry.UpdateString("service","OAuth2")
loEntry.UpdateString("username","personal-desktop")
loTokens = CreateObject('Chilkat.JsonObject')
lnSuccess = loVault.GetSecretJson(loEntry,loTokens)
IF (lnSuccess <> 1) THEN
    ? "Could not read the token record. Check the store and complete desktop authorization first."
    RELEASE loVault
    RELEASE loEntry
    RELEASE loTokens
    CANCEL
ENDIF

lcAccessToken = loTokens.StringOf("access_token")
IF (lcAccessToken = "") THEN
    ? "Run the Schwab authorization or refresh example to save an access token."
    RELEASE loVault
    RELEASE loEntry
    RELEASE loTokens
    CANCEL
ENDIF

loHttp = CreateObject('Chilkat.Http')
loHttp.RequireSslCertVerify = 1
loHttp.AuthToken = lcAccessToken

loResp = CreateObject('Chilkat.HttpResponse')
loQuery = CreateObject('Chilkat.HttpRequest')
loQuery.HttpVerb = "GET"
loQuery.AddParam("symbols","AAPL,MSFT,BRK/B")
loQuery.AddParam("fields","quote")
lnSuccess = loHttp.HttpReq("https://api.schwabapi.com/marketdata/v1/quotes",loQuery,loResp)
IF ((lnSuccess <> 1) OR (loResp.StatusCode <> 200)) THEN
    IF (loResp.StatusCode = 0) THEN
        ? "No HTTP response. Check connectivity."
    ELSE
        ? "Schwab returned HTTP " + STR(loResp.StatusCode)
        IF (loResp.StatusCode = 401) THEN
            ? "Refresh the access token or repeat desktop authorization."
        ENDIF

    ENDIF

    RELEASE loVault
    RELEASE loEntry
    RELEASE loTokens
    RELEASE loHttp
    RELEASE loResp
    RELEASE loQuery
    CANCEL
ENDIF

loReply = CreateObject('Chilkat.JsonObject')
lnSuccess = loReply.Load(loResp.BodyStr)
IF (lnSuccess <> 1) THEN
    ? "Schwab returned unexpected JSON."
    RELEASE loVault
    RELEASE loEntry
    RELEASE loTokens
    RELEASE loHttp
    RELEASE loResp
    RELEASE loQuery
    RELEASE loReply
    CANCEL
ENDIF

*  Read members by index. A symbol may contain punctuation meaningful in a JSON path.
loInstrument = CreateObject('Chilkat.JsonObject')
i = 0
DO WHILE (i < loReply.Size)
    lcKey = loReply.NameAt(i)
    IF (lcKey <> "errors") THEN
        lnSuccess = loReply.ObjectAt2(i,loInstrument)
        IF (lnSuccess = 1) THEN
            ? lcKey + "  bid " + loInstrument.StringOf("quote.bidPrice") + "  ask " + loInstrument.StringOf("quote.askPrice") + "  last " + loInstrument.StringOf("quote.lastPrice")
            ? "  quote time (Unix ms): " + loInstrument.StringOf("quote.quoteTime") + "  real-time: " + loInstrument.StringOf("realtime")
        ENDIF

    ENDIF

    i = i + 1
ENDDO
j = 0
DO WHILE (j < loReply.SizeOfArray("errors.invalidSymbols"))
    loReply.J = j
    ? "Invalid symbol: " + loReply.StringOf("errors.invalidSymbols[j]")
    j = j + 1
ENDDO

RELEASE loVault
RELEASE loEntry
RELEASE loTokens
RELEASE loHttp
RELEASE loResp
RELEASE loQuery
RELEASE loReply
RELEASE loInstrument