Skip to content

Bot for auto purchasing securities on TD Ameritrade

Notifications You must be signed in to change notification settings

Apollorion/tdbot

Repository files navigation

TDBot

ci

This is a terraform module that will create a bot in aws that will purchase defined securities based on weights provided to the module automatically.

Use this bot at your own risk, I do not assume any financial responsibility for your use of this bot.
The bot is for educational purposes only
BE SURE YOU READ OVER THE CODE AND UNDERSTAND COMPLETELY HOW THIS BOT WORKS BEFORE USE

Note: The bot ONLY creates orders, it will not rebalance or sell.

Why

I created this bot because I know what securities I want to purchase and I do so everytime I get paid.
I dont have the funds to use some auto investing features TD offers, so I use this to auto buy securities Im interested in.

How it works

Every night at mightnight EST the bot will:

  1. login to your TD Ameritrade account
  2. check how much money you have available for trading
  3. create DAY orders for the next trading windows
    • purchases as many securities that it can with your available funds
    • purchases securities from heightes weights to lowest weights
    • buys with a LIMIT of either the ask price or the low price (whichever is cheaper)

How to install

  1. Create the required infrastructure
module "my_td_bot" {
  source = "github.com/apollorion/tdbot"
  
  weights = {
    "XLK" : 30,
    "SPHD" : 30,
    "AMD" : 30,
    "VTI" : 20,
    "NVDA" : 20,
    "AAL" : 10,
    "AAPL" : 10,
    "O" : 10,
  }

  account_id      = "444444444" # TD ameritrade account id
  fargate_arn     = "arn:aws:ecs:us-east-1:44444444:cluster/FARGATE" # Fargate cluster to run the task in
  subnets         = ["subnet-4444444"] # Subnets for the task
  security_groups = ["sg-4444444444"] # SG for the task
}
  1. Terraform will output a secret arn, export it as an environment variable locally
    • export SECRET_ARN="{secret_arn}"
  2. Export aws keys locally
    • export AWS_DEFAULT_REGION="{region}" as the region the previous steps secret is in
    • export AWS_SECRET_ACCESS_KEY="{secret_access_key}" as the secret access key with access to the previous steps secret
    • export AWS_ACCESS_KEY_ID="{access_key}" as the access key with access to the previous steps secret
  3. Export your TD Ameritrade account id
    • export ACCOUNT_ID="{your_td_account_id}"
  4. Run the set_token.sh script and follow the onscreen prompts
    • This will configure the secret the task will need to authenticate to td ameritrade
    • ./scripts/set_token.sh run from the root of the project.

Once the above 5 steps are complete, you will be good to go. You can change weights at any time and the bot will auto purchase the securities when you have funds.

Requirements

Name Version
aws >= 4
local 2.1.0

Providers

Name Version
aws 4.3.0

Resources

Name Type
aws_cloudwatch_event_rule.event_rule resource
aws_cloudwatch_event_target.ecs_scheduled_task resource
aws_cloudwatch_log_group.tdbot resource
aws_ecs_task_definition.main resource
aws_iam_role.tdbot resource
aws_iam_role_policy.tdbot resource
aws_secretsmanager_secret.tdbot resource
aws_iam_policy_document.tdbot data source
aws_iam_policy_document.tdbot_assume_role data source

Inputs

Name Description Type Default Required
account_id TD Ameritrade account ID string n/a yes
fargate_arn Fargate cluster ARN string n/a yes
security_groups security groups to assign to task list(string) n/a yes
subnets subnets to launch the task into list(string) n/a yes
weights securities weights, key = security name, value = weight map(number) {} no
secret_arn secrets manager arn to use instead of creating a new one (if used, set var.create_secret to false) string "" no
create_secret create the secret (should be false if you provide a secret_arn) bool true no
name_prefix name prefix for resources string "" no

Outputs

Name Description
secret_arn secret_arn for generating token