tradebot

A trading bot for the Indian stock market where strategies can only ask. One gate decides what becomes an order.

Role
Solo. Design and build.
When
2026
Status
In progress
Built with
Python 3.12, pydantic, mypy strict, polars

One real day, replayed

19 October 2026. Three stocks, five-minute candles.

Replay of 19 October 2026. The test strategy raised 3 requests to buy. Every one that could be given a size went to the risk manager, which runs thirteen rules in order and stops at the first one that says no.

  1. 09:20, buy RELIANCE. Passed all thirteen rules and went on to the order manager.
  2. 14:25, buy TCS. Refused. The order needed more margin than the account could spare.
  3. 15:30, buy INFY. Refused. Too late in the day to open a new trade.

The rules, in the order they run: Kill switch (the stop file exists), Trading hours (outside 09:20 to 14:30), Daily loss (day's loss limit reached), Duplicate order (same order within 10 s), Leverage (exposure over 2× capital), Losing streak (three losses in a row), Margin (needs over 90% of funds), Drawdown (drawdown limit reached), Open positions (two already open), Order rate (over five orders a second), Price sanity (limit 3% off last price), Stale data (last tick older than 10 s), Trades per day (four trades already).

More test than source

617tests, all passing

Test code
7,427 lines
Source
5,586 lines

One path, three modes

Replays stored trading days through the whole path. Only the clock, the candles and the broker are stand-ins.

A strategy cannot reach the broker

StrategyBrokerthe gate

Tests fail the build if any code tries to go round the gate.

No equity curve, on purpose.

The one strategy in the box only tests the plumbing, so its results are not shown.

No app. A terminal and some files.

tradebot is started from a command line and answers in text. These are its own words, copied from the project: what it logs, the files it reads, the report it writes. Money results are blanked on purpose.

Starting a backtest (run log, trimmed)

uv run python run.py --mode backtest --from 2026-10-01 --to 2026-10-23

02:09:50.975 INFO tradebot.app.bootstrap: backtest wired: 1 strategy(ies), 13 risk rule(s)

02:09:50.986 INFO tradebot.app.cli: tradebot 0.1.0 started: mode=backtest capital=₹5,000 strategies enabled=1

02:09:50.996 INFO tradebot.backtest.driver: backtest 2026-10-01 to 2026-10-23: 15 trading day(s), timeframes 5m

02:09:50.997 INFO tradebot.scanners.runner: scanner all_symbols selected 3 symbols

02:09:51.022 INFO tradebot.risk.manager: approved demo BUY INFY x7 @ 1009.50

02:09:51.022 INFO tradebot.execution.brokers.sim: placed ord-b7c0ab5a3b94 BUY INFY x7 LIMIT limit=1011.55 trigger=None reason=entry

02:09:51.024 INFO tradebot.execution.brokers.sim: filled ord-b7c0ab5a3b94 BUY INFY x7 @ 1009.80 (entry)

02:09:51.025 INFO tradebot.execution.oms: protected BUY INFY x7: stop ord-a6b5a4371b4f trigger=1003.15 limit=1001.10

02:09:51.030 WARNING tradebot.risk.manager: vetoed demo BUY TCS x2 by entry_window: 14:35:00 IST is outside the entry window 09:20–14:30

02:09:51.036 INFO tradebot.backtest.driver: 2026-10-01: replayed 218 candle(s) for 3 symbol(s)

The limits, all in one file (config/risk.yaml)

risk_per_trade_pct: 1.0 # max loss if a stop is hit (₹50)

daily_loss_limit_pct: 3.0 # stop opening trades for the day (₹150)

max_drawdown_pct: 15.0 # bot disables itself until re-enabled by hand

max_consecutive_losses: 3 # pause for the day after this many losses in a row

max_trades_per_day: 4

max_open_positions: 2

max_leverage: 2.0 # exposure ÷ capital, even if MIS allows more

max_price_deviation_pct: 3.0 # reject limit prices this far from last traded price

duplicate_window_seconds: 10 # block an identical order repeated within this time

stale_data_seconds: 10 # block entries if the last tick is older than this

kill_file: KILL # create data/KILL to stop all trading immediately

The only strategy in the box (config/strategies/demo.yaml)

# TEMPORARY — a plumbing test for the backtest pipeline, NOT a trading idea.

strategy: demo_breakout

enabled: true

stage: backtest

The report it writes (money columns blanked here)

Backtest 2026-10-01 to 2026-10-23 (15 trading day(s))

Capital       ₹5,000.00

Net P&L       

Trades        22

Max drawdown  

closedsymbolqtyentryexitnet P&Lreason
2026-10-01 14:40INFY71009.80target
2026-10-05 15:15RELIANCE41179.59square_off
2026-10-05 15:15TCS12098.45square_off
2026-10-06 11:00RELIANCE61194.80target
2026-10-07 13:15TCS22102.05stop_loss
2026-10-08 15:15INFY21008.75square_off
Asking for paper trading (it will not start)

uv run python run.py --mode paper

Config error: … strategy 'demo' is at stage 'backtest'; it cannot run in 'paper' mode until promoted (backtest → paper → live)

The gate saying no (three lines from the log)

WARNING tradebot.risk.manager: vetoed demo BUY TCS x4 by margin: estimated margin ₹4926.00 (₹9852.00 ÷ 2× leverage) exceeds ₹4657.64 (90% of available ₹5175.16)

WARNING tradebot.risk.manager: vetoed demo BUY RELIANCE x8 by leverage: leverage 2.17× (exposure ₹1136.52 + working entries ₹0.00 + this intent ₹9712.40 over capital ₹5000.00) exceeds limit 2.00×

INFO tradebot.portfolio.sizer: demo BUY TCS: sized to 0 shares, skipped (risk limit 0, leverage limit 4; capital 5000.00, risk/trade 1.00%, risk/share 62.20, entry 2412.20, share 1.00, leverage 2.0x)

The test run (one dot per passing test, 617 of them)

uv run pytest -q

Where it stands

  • Backtest runs end to end, with a report.
  • Paper and live refuse to start, on purpose.
  • No real strategy yet. The bundled one only proves the plumbing.
  • No broker adapter yet. That is the next phase.

Built with Python 3.12, pydantic, mypy strict, polars, Parquet, SQLite, uv.