mate
is an experimental mini algorithmic trading engine. Use at your own risk.
I am writing this to learn more about algorithmic trading, and quantitative finance.
Mate supports the following account types:
Account Type | Description |
---|---|
TD Ameritrade | See TD Ameritrade developer portal and TDA SDK Crate source in use for this project |
Kraken | See Kraken API docs and TDA SDK Crate source in use for this project |
You will need access to an API with live market data. Currently only supporting TD Ameritrade
.
Configuration is passed in through environment only currently:
Env Var | Description |
---|---|
TDA_CLIENT_ID |
OAuth Client ID from your TD Ameritrade developer account, appending your oAuth domain eg: @AMER.OAUTHAP |
TDA_REFRESH_TOKEN |
Oauth refresh token to renew oauth access token, see TD Ameritrade |
KRAKEN_CLIENT_KEY |
API client key for your Kraken Account |
KRAKEN_CLIENT_SECRET |
API client secret for your Kraken Account |
To register an account and get API access, you can either see upstream documentation, or try your luck with the steps below:
-
Sign up for a developer account, even if you have a brokerage account
-
Create your application (if you plan to run this locally, use
http://localhost
orhttp://127.0.0.1
for theredirect_url
) -
go to
https://auth.tdameritrade.com/auth?response_type=code&redirect_uri=<Your url encoded callback url>&client_id=<Your Client Key>%40AMER.OAUTHAP
in a browser, replacing<Your url encoded callback url>
with theredirect_url
you created when you created your TD Ameritrade app, and replacing<Your Client Key>
with the client key from your TD Ameritrade app.- eg
https://auth.tdameritrade.com/auth?response_type=code&redirect_uri=http%3A%2F%2Flocalhost&client_id=MYKEY123%40AMER.OAUTHAP
- eg
-
This should redirect you to a log in for your TD ameritrade account (brokerage account), to perform the oauth grant to give your app access to your account
-
if you used localhost, it will redirect and be broken, but the code to fetch your access and refresh token will be in the redirected url query params (eg:
https://localhost/?code=Your%2Code%2Here
) -
take that code, and go to https://developer.tdameritrade.com/authentication/apis/post/token-0
-
enter
authorization_code
forgrant_type
-
enter
offline
foraccess_type
-
urldecode the code you copied from the redirect earlier
ipython -c "import urllib; urllib.parse.unquote(\"YOURCODEHERE\")"
-
enter the urldecoded string into
code
-
enter your application's given
client_id
intoclient_id
(should end in@AMER.OAUTHAP
) -
enter your application's given
redirect_uri
-
click
send
-
the JSON populated at the bottom of the page contains an
access_token
and arefresh_token
, be sure to save therefresh_token
, and set it in your environment (export it in your shell rc eg:~/.bashrc
or~/.zshrc
)
-
note: if you end up with a failing
grant_invalid
error, you might want to make sure you're unquoting your authorization code, otherwise see the Authentication FAQ
Data is represented with tick data, or OHLC candles, then serialized into JSON.
Equities:
[
{
"close": 39.435,
"datetime": 1546236000000,
"high": 39.84,
"low": 39.12,
"open": 39.6325,
"volume": 140013864
}
]
This is derived from the response of the pricehistory
endpoint of the TD Ameritrade API
Crypto:
{
"1641237335": {
"a": ["0.169609000", "221449", "221449.000"],
"b": ["0.169608900", "268", "268.000"],
"c": ["0.169608900", "67.14677906"]
}
}
JSON contains a list of epoch timestamps as strings, containing an object representing the ask, bid, and close of each tick.
This model is derived from the ticker
endpoint response that comes from the Kraken API
The following references may be helpful for the underlying technologies used in this project:
Name | Description | Link |
---|---|---|
actix-web | web frameworks | docs |
actix-cors | cors middleware | crates.io |
clap | cli framework | derive arg reference |
diesel | orm | getting started |
type mappings | ||
query DSL | ||
krakenrs | client api | cargo docs |
crates.io | ||
magic-crypt | encryption library | docs.rs |