DEV Community

Bot and Bull
Bot and Bull

Posted on AI-assisted

The Hidden Assumption My Trading Bot Made About Market Hours

Bot and Bull card: Same bot, a market that never sleeps. Part 3 of the build, what changed when I pointed my trading bot at crypto and why the safety net had to be rethought.

My trading bot worked fine on stocks. Then I pointed it at crypto and found an assumption I didn't know I'd written: that the market closes.

A lot of stock logic quietly depends on that. Daily opens and closes, overnight gaps, "run this after the bell." None of it exists in a market that trades all day, every day.

The first fix was making the assumption explicit instead of buried:

from datetime import datetime, time
from zoneinfo import ZoneInfo

ET = ZoneInfo("America/New_York")
OPEN, CLOSE = time(9, 30), time(16, 0)

def market_is_open(asset_class: str, now: datetime | None = None) -> bool:
    if asset_class == "crypto":
        return True  # no bell, no weekends off
    now = now or datetime.now(ET)
    if now.weekday() >= 5:  # Saturday, Sunday
        return False
    return OPEN <= now.time() < CLOSE  # holidays need their own check
Enter fullscreen mode Exit fullscreen mode

Small function, big consequences:

  1. Every scheduled job asks this first, so stock-only logic can't accidentally run against a 24/7 market.
  2. It forced a harder question: if crypto never closes, who's watching at 3 AM? The answer had to be the bot itself, so every protective rule became automatic.
  3. Testing changed too. A crypto change isn't tested until it has run on paper through a full weekend, because weekends are where the differences show up.

The rest of what changed (the safety net, the signals, position sizing) is in the full write-up: How I added crypto to my trading bot.

If you're setting up your first bot, the step-by-step setup is in Part 2 of the build.

Educational only, not financial advice.

Top comments (0)