Overview
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.
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
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
SchwabGetQuotes example
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[]=
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
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
Index to previous posts on the project
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
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
by Robert Rimmer
Wolfram Community, STAFF PICKS, May 6, 2025
https://community.wolfram.com/groups/-/m/t/3456448