Overview

This post has a new function to refresh the tokens in the background and an updated package is included in the supplement. I have now provided examples for all the important functions in the Schwab API Market Market Data Production. Based on the quote times shown in this package, I am not in a hurry to develop trading functions, but I will probably eventually get around to implementing account access functions.
​
I would welcome collaboration if anyone is interested in further development of the SchwabAPI package including Mathematica style documentation.
​
At the end of this notebook there is an index to all the previous Wolfram Community posts on this project.

automaticTokenRefresh function

In[]:=
Needs["SchwabAPI`"]
This is a new function in the package, which keeps the tokens refreshed as a background task before they expire. autoRefreshTask is a global symbol for the session.
In[]:=
(*autorefreshtokensforMathematicasession*)​​(*ThefirsttimeduringasessionthatthisisrunitwillreturnaTaskObjectwhichcanbemanagedwithtaskschedulerfunctions*)​​(*IfthetaskisalreadyrunningitreturnsRunning*)
In[]:=
apiTokens=Import[tokenFile,"RawJSON"];​​automaticTokenRefresh[appKey,appSecret,apiTokens["refresh_token"]]
Out[]=
Running
In[]:=
(*ThisistheTaskObject*)
In[]:=
autoRefreshTask
Out[]=
TaskObject
Task UUID: f2b46214-a95d-4079-8623-37c5c7b73de3
Task environment: Session
Task type: Scheduled
Evaluation expression: apiTokens=Import[tokenFile,RawJSON];1;12=1;


SchwabGetQuotes example

Ticker symbols for this example. startTime - endTime gives the time to get data into the kernel.
In[]:=
symbols={"TSLA","AAPL","MSFT","SPY"};
In[]:=
startTime=AbsoluteTime[];​​quotes=SchwabGetQuotes[symbols,apiTokens["access_token"]];​​endTime=AbsoluteTime[];
In[]:=
endTime-startTime
Out[]=
0.3488728
This can be turned into a Dataset that includes the symbols.
In[]:=
keyList=Keys@quotes
Out[]=
{TSLA,AAPL,MSFT,SPY}
In[]:=
quoteDS=(PrependTo[quotes[#]["quote"],"symbol"->#]&/@keyList)//Dataset;​​quoteDS/.bigint_Integer:>If[bigint>10^12,FromUnixTime[bigint/1000],bigint]​​
Out[]=
symbol
52WeekHigh
52WeekLow
askMICId
askPrice
askSize
askTime
bidMICId
bidPrice
bidSize
TSLA
488.54
167.41
XNAS
273.99
1
Tue 6 May
2025 09:4
4:05
EDGX
273.92
2
AAPL
260.1
169.21
EDGX
199.73
2
Tue 6 May
2025 09:4
4:05
XNAS
199.69
2
MSFT
468.35
344.79
XNAS
436.37
1
Tue 6 May
2025 09:4
4:04
XNAS
436.26
1
SPY
613.23
481.8
ARCX
560.6
7
Tue 6 May
2025 09:4
4:05
ARCX
560.57
1
columns 1–10 of 29
The function quoteReturnTime gives the time between the request and the time to the kernel. The Unix times returned by the API have millisecond resolution.
In[]:=
quoteReturnTime[symbol_,timeKey_]:=Module[{unix},​​unix=quotes[symbol]["quote"][timeKey]/1000.;​​unix-UnixTime[startTime]​​];
In[]:=
{#,quoteReturnTime[#,"bidTime"]}&/@Keys@quotes//TableForm
Out[]//TableForm=
TSLA
2.021
AAPL
1.992
MSFT
1.977
SPY
2.232
In[]:=
{#,quoteReturnTime[#,"askTime"]}&/@Keys@quotes//TableForm
Out[]//TableForm=
TSLA
2.005
AAPL
2.431
MSFT
1.977
SPY
2.232
In[]:=
{#,quoteReturnTime[#,"tradeTime"]}&/@Keys@quotes//TableForm
Out[]//TableForm=
TSLA
2.157
AAPL
2.225
MSFT
1.954
SPY
1.85
These times are long enough that I do not plan to use the Trader API for market orders.

Developer journal

This update completes showing the important functions in Schwab Market Product API . I plan to examine the functions more deeply before thinking about tackling the Trader Product. I will be happy to help anyone who has a Schwab account to get this running. The first appAuthorization[] run can be confusing. I would also welcome collaboration on this product. If there is significant interest, the package could be enhanced with documentation. It looks like AI products can write a lot of documentation and produce examples.
There is a lot of information in SchwabGetOptionChain[], including calculated option parameters that appear to be better than the example I used. The data structure is complicated and I need to research what formula is being used for the parameters.
The latest SchwabAPI package is included as a supplement to this post.

Index to previous posts on the project

◼
  • Developing an interface to the Schwab API with Wolfram
  • ◼
  • Volatility analysis using daily price data from the Schwab API
  • ◼
  • Volatility measure using minute price data from the Schwab API
  • ◼
  • Real time market monitor with notebook using Schwab API
  • ◼
  • Option pricing with Schwab API
  • For future posts I will keep the strategy of keeping “Schwab API” in the title so an internet search: Wolfram Community “Schwab API” should eventually be able to find them. Grok could find them all 15 minutes after I had made a previous post.

    CITE THIS NOTEBOOK

    Enhancements to SchwabAPI: token refresh, updated package, and comprehensive market data examples​
    by Robert Rimmer​
    Wolfram Community, STAFF PICKS, May 6, 2025
    ​https://community.wolfram.com/groups/-/m/t/3456448