DEV Community

Bot and Bull
Bot and Bull

Posted on AI-assisted

The One Flag That Kept My Trading Bot From Spending Real Money Too Early

Bot and Bull card: From an empty account to a trading bot that runs itself. Part 2 of the build, the setup step by step. botandbull.com.
When I set up my first trading bot, the scariest moment wasn't the code. It was the first time the code could place a real order.

So before any buying logic existed, every order went through one function with one switch in front of it:

import os

DRY_RUN = os.environ.get("DRY_RUN", "true").lower() == "true"

def place_order(client, symbol, qty, side):
    if DRY_RUN:
        print(f"[DRY RUN] would {side} {qty} {symbol}")
        return None
    return client.submit_order(symbol=symbol, qty=qty, side=side)
Enter fullscreen mode Exit fullscreen mode

Three things I like about this:

  1. It defaults to safe. If I forget to set the variable, nothing real happens.
  2. There's one door. No other part of the bot talks to the broker's order endpoint directly, so there's one place to check.
  3. Keys and switches live in the environment, not the code. Nothing sensitive ends up in a repo.

I ran in dry-run mode, then on a paper account, for weeks before flipping it. Boring was the goal.

That's one step of nine. I wrote up the full setup, from an empty account to a bot that runs on a schedule, here: How to set up a trading bot from scratch, step by step.

If you're starting from zero like I did, the story of how it began is in Part 1 of the build.

Educational only, not financial advice.

Top comments (0)