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

Schwab: Check equity market hours for a date

See more Schwab Examples

Looks up the US equity calendar for a selected trading date. Reads each product’s open flag and the actual regular-session start and end, including their offsets. This distinguishes a holiday closure from a shortened session. The open flag describes the requested date, not whether the market is open at this instant.

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 PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Vault
oleobject loo_Entry
oleobject loo_Tokens
string ls_AccessToken
oleobject loo_Http
oleobject loo_Clock
string ls_MarketDate
oleobject loo_Resp
oleobject loo_Query
oleobject loo_Reply
oleobject loo_Products
oleobject loo_Product
long i
long j
long li_Count

li_Success = 0

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

loo_Vault = create oleobject
li_rc = loo_Vault.ConnectToNewObject("Chilkat.Secrets")
if li_rc < 0 then
    destroy loo_Vault
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_Vault.Location = "local_manager"
loo_Vault.AutoSplit = 1
loo_Entry = create oleobject
li_rc = loo_Entry.ConnectToNewObject("Chilkat.JsonObject")

loo_Entry.UpdateString("appName","SchwabTrader")
loo_Entry.UpdateString("service","OAuth2")
loo_Entry.UpdateString("username","personal-desktop")
loo_Tokens = create oleobject
li_rc = loo_Tokens.ConnectToNewObject("Chilkat.JsonObject")

li_Success = loo_Vault.GetSecretJson(loo_Entry,loo_Tokens)
if li_Success <> 1 then
    Write-Debug "Could not read the token record. Check the store and complete desktop authorization first."
    destroy loo_Vault
    destroy loo_Entry
    destroy loo_Tokens
    return
end if

ls_AccessToken = loo_Tokens.StringOf("access_token")
if ls_AccessToken = "" then
    Write-Debug "Run the Schwab authorization or refresh example to save an access token."
    destroy loo_Vault
    destroy loo_Entry
    destroy loo_Tokens
    return
end if

loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")

loo_Http.RequireSslCertVerify = 1
loo_Http.AuthToken = ls_AccessToken

//  Supply the calendar date you want to check, for example a US holiday.
loo_Clock = create oleobject
li_rc = loo_Clock.ConnectToNewObject("Chilkat.CkDateTime")

loo_Clock.SetFromCurrentSystemTime()
ls_MarketDate = loo_Clock.GetAsIso8601("YYYY-MM-DD",0)
loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")

loo_Query = create oleobject
li_rc = loo_Query.ConnectToNewObject("Chilkat.HttpRequest")

loo_Query.HttpVerb = "GET"
loo_Query.AddParam("date",ls_MarketDate)
li_Success = loo_Http.HttpReq("https://api.schwabapi.com/marketdata/v1/markets/equity",loo_Query,loo_Resp)
if (li_Success <> 1) OR (loo_Resp.StatusCode <> 200) then
    if loo_Resp.StatusCode = 0 then
        Write-Debug "No HTTP response. Check connectivity."
    else
        Write-Debug "Schwab returned HTTP " + string(loo_Resp.StatusCode)
        if loo_Resp.StatusCode = 401 then
            Write-Debug "Refresh the access token or repeat desktop authorization."
        end if

    end if

    destroy loo_Vault
    destroy loo_Entry
    destroy loo_Tokens
    destroy loo_Http
    destroy loo_Clock
    destroy loo_Resp
    destroy loo_Query
    return
end if

loo_Reply = create oleobject
li_rc = loo_Reply.ConnectToNewObject("Chilkat.JsonObject")

li_Success = loo_Reply.Load(loo_Resp.BodyStr)
if li_Success <> 1 then
    Write-Debug "Schwab returned unexpected JSON."
    destroy loo_Vault
    destroy loo_Entry
    destroy loo_Tokens
    destroy loo_Http
    destroy loo_Clock
    destroy loo_Resp
    destroy loo_Query
    destroy loo_Reply
    return
end if

loo_Products = create oleobject
li_rc = loo_Products.ConnectToNewObject("Chilkat.JsonObject")

li_Success = loo_Reply.ObjectOf2("equity",loo_Products)
if (li_Success <> 1) OR (loo_Products.Size = 0) then
    Write-Debug "No equity market schedule returned for " + ls_MarketDate
    destroy loo_Vault
    destroy loo_Entry
    destroy loo_Tokens
    destroy loo_Http
    destroy loo_Clock
    destroy loo_Resp
    destroy loo_Query
    destroy loo_Reply
    destroy loo_Products
    return
end if

loo_Product = create oleobject
li_rc = loo_Product.ConnectToNewObject("Chilkat.JsonObject")

i = 0
do while (i < loo_Products.Size)
    loo_Products.ObjectAt2(i,loo_Product)
    Write-Debug loo_Products.NameAt(i) + "  " + loo_Product.StringOf("date")
    if loo_Product.BoolOf("isOpen") = 1 then
        j = 0
        li_Count = loo_Product.SizeOfArray("sessionHours.regularMarket")
        if li_Count <= 0 then
            Write-Debug "Open date, but no regular-session times were supplied."
        end if

        do while (j < li_Count)
            loo_Product.J = j
            Write-Debug "Regular session: " + loo_Product.StringOf("sessionHours.regularMarket[j].start") + " to " + loo_Product.StringOf("sessionHours.regularMarket[j].end")
            j = j + 1
        loop
    else
        Write-Debug "Closed for this date."
    end if

    i = i + 1
loop


destroy loo_Vault
destroy loo_Entry
destroy loo_Tokens
destroy loo_Http
destroy loo_Clock
destroy loo_Resp
destroy loo_Query
destroy loo_Reply
destroy loo_Products
destroy loo_Product