Thinkscript
1 Example api
5 Day / 1 Minute, including today’s data:
https://api.tdameritrade.com/v1/marketdata/XYZ/pricehistory?periodType=day&period=5&frequencyType=minute&frequency=1&endDate=1464825600000
5 Day / 1 Minute, excluding today’s data:
https://api.tdameritrade.com/v1/marketdata/XYZ/pricehistory?periodType=day&period=5&frequencyType=minute&frequency=1
6 Months / 1 Day, including today’s data:
https://api.tdameritrade.com/v1/marketdata/XYZ/pricehistory?periodType=month&frequencyType=daily&endDate=1464825600000
Note that periodType=month is specified because the default periodType is day which is not compatible with the frequencyType daily
Daily from May 25th, 2016 to today:
https://api.tdameritrade.com/v1/marketdata/XYZ/pricehistory?periodType=month&frequencyType=daily&startDate=1464148800000&endDate=1464825600000
2 code
def
to instantiate variables- Booleans are 1 or 0 , “yes” or “no”
main default variables:
def a = open + high + low + close
default variables have implicit index of current day \(n\)
open
:= \(Open(n)\)
2.1 Index
Index is relative to the current day \(n\) in the negative direction.
volume[1]
:= \(Volume(n-1)\)
def diff = close - close[1]
#close - previous day close
2.2 Recursive
def data = data[1] + volume;
plot stuff = data;
\[ F_n = F_{n-1} + P(n) \]
- data := \(F_n\)
- data[1] := \(F_{n-1}\)
- volume := \(P(n)\)
- IH is cumulative volume at some arbitrary time
ToS assumes base case \(F_0=0\)
plot \(n \mapsto F_n\)
The above will plot cumulative Volume over time.
2.3 Flow control
if 3 > 2 {
def a = 44
}
2.4 plotting lower subgraph
declare lower;
c = 5;
plot outX = c;
2.5 allow UI dropdown input
input a = close; #default value is close
plot outX = a;
3 TDAPI
CONSUMER KEY is XXXXXXXXXXXXXXXXXXX
The only thing permanent is the CONSUMER KEY.
3.1 Price access
We have no need to get a refresh token and access token if we just need price.
Just use CONSUMER KEY.
https://api.tdameritrade.com/v1/marketdata/AAPL/pricehistory?apikey=XXXXXXXXXXXXXXXXXXX&periodType=day
3.2 Account access
- PERMANENT: CONSUMER KEY
- ONE-OFF: GET CODE FROM URL FROM URL LOGIN, LOCALHOST
- USE ONE-OFF CODE TO GET 90 day REFRESH-TOKEN
- USE 90 day REFRESH TOKEN to GET 30 min ACCESS TOKEN
- USE ONE-OFF CODE TO GET 90 day REFRESH-TOKEN
- ONE-OFF: GET CODE FROM URL FROM URL LOGIN, LOCALHOST
This means every 90 days we have to login using our browser to get a one-off code through localhost url for a new refresh token. Every 15 min if use the api we need to use this 90-day refresh token to get an access token.
- Register an App with callback url
http://localhost
- Get the CONSUMER KEY
XXXXXXXXXXXX
- Go to URL
https://auth.tdameritrade.com/auth?response_type=code&redirect_uri=http%3A%2F%2Flocalhost&client_id=XXXXXXXXXXXX%40AMER.OAUTHAP
- This will open an offical TDAmeritrade page you have to login
- Redirects you to
http://localhost/?code=Z%21ZZZZZ%23Z%25ZZZZ%21Z
- get the string after
http://localhost/?code=
which isZ%21ZZZZZ%23Z%25ZZZZ%21Z
- decode urlencoding using python
#url decoder
from urllib.parse import unquote
= "Z%21ZZZZZ%23Z%25ZZZZ%21Z"
urlcode = unquote(urlcode)
code print(code)
#output: Z!ZZZZZ#Z%ZZZZ!Z
grant_type: authorization_code (literally type "authorization_code" into the box without quotes)
refresh_token: (leave blank)
acccess_type: offline (literally type "offline" into the box without quotes)
code: Z!ZZZZZ#Z%ZZZZ!Z (output from our python urldecode)
client_id: CONSUMERKEY@AMER.OAUTHAP (yes, add "@AMER.OAUTHAP" without quotes)
redirect_uri: http://localhost
{
"access_token": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"refresh_token": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"scope": "PlaceTrades AccountAccess MoveMoney",
"expires_in": 1800,
"refresh_token_expires_in": 7776000,
"token_type": "Bearer"
}