Sample code for 30+ languages & platforms
Xbase++ 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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oVault
LOCAL oEntry
LOCAL oTokens
LOCAL cAccessToken
LOCAL oHttp
LOCAL oResp
LOCAL oQuery
LOCAL oReply
LOCAL oInstrument
LOCAL i
LOCAL cKey
LOCAL j

nSuccess := 0

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
nSuccess := 0

oVault := CreateObject("Chilkat.Secrets")
oVault:Location := "local_manager"
oVault:AutoSplit := 1
oEntry := CreateObject("Chilkat.JsonObject")
oEntry:UpdateString("appName", "SchwabTrader")
oEntry:UpdateString("service", "OAuth2")
oEntry:UpdateString("username", "personal-desktop")
oTokens := CreateObject("Chilkat.JsonObject")
nSuccess := oVault:GetSecretJson(oEntry, oTokens)
IF (nSuccess != 1)
    ? "Could not read the token record. Check the store and complete desktop authorization first."
    oVault:destroy()
    oEntry:destroy()
    oTokens:destroy()
    RETURN
ENDIF

cAccessToken := oTokens:StringOf("access_token")
IF (cAccessToken == "")
    ? "Run the Schwab authorization or refresh example to save an access token."
    oVault:destroy()
    oEntry:destroy()
    oTokens:destroy()
    RETURN
ENDIF

oHttp := CreateObject("Chilkat.Http")
oHttp:RequireSslCertVerify := 1
oHttp:AuthToken := cAccessToken

oResp := CreateObject("Chilkat.HttpResponse")
oQuery := CreateObject("Chilkat.HttpRequest")
oQuery:HttpVerb := "GET"
oQuery:AddParam("symbols", "AAPL,MSFT,BRK/B")
oQuery:AddParam("fields", "quote")
nSuccess := oHttp:HttpReq("https://api.schwabapi.com/marketdata/v1/quotes", oQuery, oResp)
IF ((nSuccess != 1) .OR. (oResp:StatusCode != 200))
    IF (oResp:StatusCode == 0)
        ? "No HTTP response. Check connectivity."
    ELSE
        ? "Schwab returned HTTP " + Str(oResp:StatusCode)
        IF (oResp:StatusCode == 401)
            ? "Refresh the access token or repeat desktop authorization."
        ENDIF

    ENDIF

    oVault:destroy()
    oEntry:destroy()
    oTokens:destroy()
    oHttp:destroy()
    oResp:destroy()
    oQuery:destroy()
    RETURN
ENDIF

oReply := CreateObject("Chilkat.JsonObject")
nSuccess := oReply:Load(oResp:BodyStr)
IF (nSuccess != 1)
    ? "Schwab returned unexpected JSON."
    oVault:destroy()
    oEntry:destroy()
    oTokens:destroy()
    oHttp:destroy()
    oResp:destroy()
    oQuery:destroy()
    oReply:destroy()
    RETURN
ENDIF

//  Read members by index. A symbol may contain punctuation meaningful in a JSON path.
oInstrument := CreateObject("Chilkat.JsonObject")
i := 0
DO WHILE (i < oReply:Size)
    cKey := oReply:NameAt(i)
    IF (cKey != "errors")
        nSuccess := oReply:ObjectAt2(i, oInstrument)
        IF (nSuccess == 1)
            ? cKey + "  bid " + oInstrument:StringOf("quote.bidPrice") + "  ask " + oInstrument:StringOf("quote.askPrice") + "  last " + oInstrument:StringOf("quote.lastPrice")
            ? "  quote time (Unix ms): " + oInstrument:StringOf("quote.quoteTime") + "  real-time: " + oInstrument:StringOf("realtime")
        ENDIF

    ENDIF

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

oVault:destroy()
oEntry:destroy()
oTokens:destroy()
oHttp:destroy()
oResp:destroy()
oQuery:destroy()
oReply:destroy()
oInstrument:destroy()