Skip to main content

Simple Python Auth0 login for Tradestation API v3

Project description

ts_auth0

Simple Python Auth0 login for Tradestation API v3

This package is purely for experimental purposes and is not supported by Tradestation https://www.tradestation.com/. Use at your own risk - trading is risky!

I found the Tradestation authentication process to be complicated, so once I figured out the correct procedure and was able to authenicate and get an access token, I wrapped it all into this easy to use Python class for all to enjoy!

ts_auth0 provides a simple Python class which simplifies the Tradestation authentication process. See https://api.tradestation.com/docs/fundamentals/authentication/auth-overview/ for details of the authentication process ts_auth0 facilitates.

Overview

This package provides a simple way for your Python scripts to log into Tradestation for API access. The goal is to complete authentication and get an access token used in every API call.

To start, you provide your API Key and Secret API Key. A simple function call is all that is required to log in and obtain an access token. The access token is then used for all API calls to get market updates, account information, etc.

Please note that this package only addresses authentication. Making API calls for market data and trading is a seperate task and not addresses in this package. But, see the example code below for an example of making an API once you pass authentication and are able to obtain the access token needed for all Tradestation API calls.

  • Works with Python >= 3.8
  • Tested on MacOS
  • Tested on Windows 11

To start, you will need to request API access from Tradestation. See https://api.tradestation.com/docs/faq#how-do-i-get-an-api-key. Baically you need to do the following:

  • Deposit at least $10,000 into Tradestation (!). It may take over a week to get confirmation that the money was deposited and available. Without this deposit your requests for API access will be declined.
  • After requesting API access you will receive via email a PDF file that requres a password to view. This file contains your API Key and Secret API Key.

Getting Started

Install ts_auth0 with:

  • pip install ts-auth0

Create a simple Ptyhon script with the following code:

from ts_auth0 import TS_Auth
import requests
import json
import time
from config import *

ts = TS_Auth(API_KEY, API_SECRET_KEY)
                
ts.start_auth0()

try:
    
    count = 0
    while True:
        access_token = ts.get_access_token()

        print(ts)

        url = "https://sim-api.tradestation.com/v3/marketdata/barcharts/AAPL?interval=1&unit=minute&barsback=1"
        
        headers = {'Authorization': f'Bearer {access_token}' }

        response = requests.request("GET", url, headers=headers)
        json_data = response.json()
        print(json.dumps(json_data, indent=4, sort_keys=False))

        print(f"Access Token: {access_token[:8]}... {count}")
        time.sleep(60)
        count += 1
    
except TS_Auth.Auth_Error as e:
    print(e)

FYI: To run this simple sample code your config.py file should look like this:

API_KEY = "Your API key",  
API_SECRET_KEY = "Your secret key"

Running this script (python ./test.py) will cause the following:

  • A webbrowser should appear with the Tradestation login page. Enter you user name and password.
  • If login was successful, the webbrowser should be redirected to http://localhost:3000. You will see this in the webbroswer. The ts_auth0 is listening on port 3000 and will handle the reply.
  • Back to your script, when ts.start_auth0() completes you can call ts.get_access_token() and use this access token to call the API. The example script show an eternal while loop that gets AAPL market data every minute.

See the Tradestation documentation to make API calls:

Customization

If you want to get fancy and change some of the parameters used in the authentication process, there are parameters you can modify.

Refer to the sample scripts above. The instantiation of the TS_Auth0 class can contain an optional list of paramters. Here is an example of how to specify the extra parameters:

...
parameters = {
    'REDIRECT_URI': 'http://localhost',
    'REDIRECT_PORT': 8080,
    'SHOW_KEYS_IN_STR': True
}
ts = TS_Auth(API_KEY, API_SECRET_KEY, **parameters)
...

Here is a list of the extra parametersw you can modify:

  • BASE_URL (str): The base URL for the auth server
  • AUDIENCE (str): The audience for the auth server
  • REDIRECT_PORT (int): The port for the auth server
    • Default: 3000
  • REDIRECT_URI (str): The URI for the auth server
    • Default: http://localhost
  • SCOPE (str): The scope for the auth server
    • Default: "openid profile MarketData ReadAccount Trade Matrix OptionSpreads offline_access"
  • TOKEN_REQUEST_URL (str): The URL for the token request
  • SHOW_KEYS_IN_STR (bool): Show the API keys in the string representation of the class
    • Default: False
  • REFRESH_TOKEN_CACHE_FILE (str): The file to cache the refresh token.
    • Default: refresh_token_cache.txt

Headless

I tried implementing headless login, where the user name and password would be used automatically to log in without requiring interaction with a browser. I ran into difficulty with the two-factor authentication, you still need to enter the code received in email or on your phone. So for now I have not implemented headless mode. Maybe I will in the future if I move my trading projects over to a server in the cloud. I could automate everything except the two-factor authenication. TBD.

Refresh Token

The refresh token is assigned as a result of the authentication process. This token string is stored in a text file. The next time start_auth0() is called, the authentication is skipped and the previous refresh token is used to obtain an access token needed for your first API call. Via the Tradestation Forum one of the engineers said that the refresh token remains valid indefinately. So in theory this code should only need to perform the login and two-factor authentication once, and the refresh token will be re-used on subsequent re-starts of the code.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ts_auth0-0.0.14.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ts_auth0-0.0.14-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file ts_auth0-0.0.14.tar.gz.

File metadata

  • Download URL: ts_auth0-0.0.14.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for ts_auth0-0.0.14.tar.gz
Algorithm Hash digest
SHA256 3db7f9ad3337855e19ce6b2cd8d910fdb0aaf0d1b380c11ad5563803bbf99cbb
MD5 64e58b5c7499d4cff6a04c78ee75a36a
BLAKE2b-256 550a003cc835ddb7e0f26fb1a6345ce42c306d698711c9594897104e7d534108

See more details on using hashes here.

File details

Details for the file ts_auth0-0.0.14-py3-none-any.whl.

File metadata

  • Download URL: ts_auth0-0.0.14-py3-none-any.whl
  • Upload date:
  • Size: 8.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for ts_auth0-0.0.14-py3-none-any.whl
Algorithm Hash digest
SHA256 1c5468cd84c0e9f11434cab2e69039a3a5f65b84d037bae700fc0513aa295e97
MD5 6642888b06e7d4e021a5a351a6d34942
BLAKE2b-256 c7067a1e8bbbca208ccabfe0a918036383af5cfb0d24a2138ff7387d9af0f681

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page