Skip to main content

A Python wrapper around the TDAmeritrade API

Project description

Tradebot

A Python wrapper around the TDAmeritrade API

The only thing that needs to be manually configured by the user is the config file. As of right now, this needs to be a json file with the following format:

{
  "access_token": "oMLcWUb4GFRnSRCztRGy...",
  "refresh_token": "7GWdTz2AdBAz1IEtsErLM...",
  "scope": "PlaceTrades AccountAccess MoveMoney",
  "expires_in": 1800,
  "refresh_token_expires_in": 7776000,
  "token_type": "Bearer",
  "consumer_key": "5SMAF9JAZYKKPIPL5E6IZUQCAKZOV5D7",
  "account_number": "495909744"
}

You must make a developer account with TDAmeritrade to get your consumer/api key.

Follow the official TDAmeritrade API documentation for instructions on generating the refresh token. Following those instructions will provide you with the initial json you need; copy this into a json file in your source code directory. This is the central point of truth for your api configurations.

The access_token is essential for all requests and must be kept up-to-date. This expires every 30 minutes, but the user does not have to manually create a new one. Use the following snippet as an example of how to update the access token with the update_access_token utility function. This will automatically update the source json file.

from pathlib import Path
from tradebot.configs.utils import update_access_token

config_file = Path(__file__).parent / "configs.json"
configs = TradebotConfigs(config_file)
update_access_token(configs)

The refresh_token is what is used to get a new access token. The user must follow the same process as before to get a new refresh token. Thankfully, this only expires every 90 days. Work is in progress to automate this with Chromium drivers.

The account_number refers to the account number of your TDAmeritrade brokerage account and is only useful if you wish to see account info (positions, balances, etc). It is not necessary to perform the other stock related functions of Tradebot.

Here are some code snippets to get started.

Requesting fundamental data for a given ticker:

from pathlib import Path
from tradebot.configs import TradebotConfigs
from tradebot.backend import Tradebot
from tradebot.requests import TradebotRequests

file = Path(__file__).parent / "configs.json"
configs = TradebotConfigs(file)
requests = TradebotRequests(configs)
bot = Tradebot(configs, requests)

msft_fundamentals = bot.fundamentals("MSFT")

Requesting price history:

from tradebot.backend.enums import CandlePeriodType, CandleFrequencyType

file = Path(__file__).parent / "configs.json"
configs = TradebotConfigs(file)
requests = TradebotRequests(configs)
bot = Tradebot(configs, requests)

aapl_price_history = bot.price_history(
    "AAPL",
    period_type=CandlePeriodType.DAY,
    period=2,
    frequency_type=CandleFrequencyType.MINUTE,
    frequency=1
)

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

tradebot-0.2.0.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

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

tradebot-0.2.0-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file tradebot-0.2.0.tar.gz.

File metadata

  • Download URL: tradebot-0.2.0.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.5

File hashes

Hashes for tradebot-0.2.0.tar.gz
Algorithm Hash digest
SHA256 d09dcef35c684f4de9ccff0d8a7f4d368a6daa12f7c09866ea54b88555f91208
MD5 8832add814fb60425bbc529b0d4e19eb
BLAKE2b-256 e6616e43a11eb7fd0edf5a11f6bf109be358e261487005cc83939f91396f6432

See more details on using hashes here.

File details

Details for the file tradebot-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: tradebot-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 9.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.5

File hashes

Hashes for tradebot-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fed0d95e6b389ec1fa72650e294258eb8c9f6065e8ea1c9e2db8439674687e4f
MD5 db2cc7a7613bd4a29b5b658513eab1ea
BLAKE2b-256 3b5cf0c2ec2787f0e1e4dc0311fe2e00d5a89817d2b5e95f61104fb51a825a6f

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