trading-bot-template

Minimal event-driven trading-bot skeleton in pure Python — strategy, risk manager, and broker as pluggable components.

Active Pure Nim score 65/100 · last commit 2026-09-08 · 1 stars · tests present · no docs generated

Summary

Latest Version Unknown
License Unknown
CI Status Failing
Stars 1
Forks 0
Open Issues 0
Last Commit 2026-09-08
Downloads 0
Last Indexed 2026-11-23 09:07

Installation

nimble install trading-bot-template
choosenim install trading-bot-template
git clone https://gitlab.com/djstrategyllc/trading-bot-template

OS Compatibility

Platform Linux macOS Windows FreeBSD OpenBSD NetBSD Android iOS WASM Embedded
trading-bot-template ✓ ✓ ✓ - - - - - - -

README

trading-bot-template

A minimal, event-driven trading-bot skeleton in pure Python. Strategy, risk manager, and broker are pluggable components wired around a single event loop — swap any one out without touching the others.

Python License: MIT pipeline status coverage report

A clean-room architecture template — generic patterns only, no proprietary strategy logic. It's the scaffolding you build a real bot on top of.

The idea

Everything is an event moving through one FIFO queue. Each component handles the event type it cares about and emits the next one:

flowchart LR
    DF[Data feed] -->|MarketEvent| S[Strategy]
    S -->|SignalEvent| R[Risk manager]
    R -->|OrderEvent| B[Broker]
    B -->|FillEvent| R
    B -->|FillEvent| L[(Fills / P&L)]

That single rule — data → signal → order → fill — is the whole architecture. Because components talk only through events, each one stays ignorant of the others and is trivial to test or replace.

Components

Contract (Protocol) Responsibility Example impl
Strategy market data → signals strategies/SmaCrossover
RiskManager signals → sized/vetoed orders; tracks P&L BasicRiskManager
Broker orders → fills brokers/PaperBroker
Engine owns the queue and routes events —

Contracts are typing.Protocol classes, so your own strategy or a live broker adapter just needs the right methods — no base class, no imports from the engine.

Run the demo

git clone https://gitlab.com/djstrategyllc/trading-bot-template.git
cd trading-bot-template
pip install -e ".[dev]"

python -m examples.run_backtest

You'll see the SMA crossover fire, the risk layer size each order, and the paper broker fill it — over a synthetic price path, no network or credentials.

Plug in your own strategy

from trading_bot import Engine, BasicRiskManager, MarketEvent, Side, SignalEvent
from trading_bot.brokers import PaperBroker

class MyStrategy:
    def on_market(self, event: MarketEvent) -> list[SignalEvent]:
        # your logic here → return zero or more SignalEvents
        return []

engine = Engine(
    strategy=MyStrategy(),
    risk=BasicRiskManager(order_size=10, max_position=30, max_loss=1_000),
    broker=PaperBroker(slippage=0.01, commission_per_share=0.005),
)
engine.run(market_events)

Swap PaperBroker for a live broker adapter that implements the same execute() method and the rest of the system is unchanged.

Risk layer

BasicRiskManager is intentionally simple but demonstrates the key idea: the risk layer is the single chokepoint where every signal becomes an order, so it's the one place to enforce safety rules. This one ships with:

  • fixed position sizing scaled by signal strength,
  • a maximum absolute position cap, and
  • a realized-loss circuit breaker that vetoes all new orders once tripped.

Testing

pytest

The engine is tested with tiny stub components, and the risk manager's sizing, caps, circuit breaker, and P&L accounting are each pinned by tests.

License

MIT © Dan Jharahy / DJ Strategy LLC