Core Classes
Algo Trading Stack is built around a small set of core abstractions that form the backbone of both backtests and live trading. This page explains what each class does and how they fit together.
BacktesterEngine
Defined in classes/Backtester_Engine.py
.
Acts as a simulated broker for backtests:
- Bar-by-bar execution: steps through OHLCV data, with “open-phase” snapshots to prevent look-ahead bias.
- Intrabar SL/TP policies: configurable with
intrabar_tp_sl_policy
(worst_case
,best_case
,sl_first
,tp_first
). - Margin enforcement: applies initial/maintenance margin; liquidates if equity falls below requirements.
- Slippage & commissions: configurable globally or per-asset (
slippage_ticks
,slippage_pct
,commission_per_contract
,fee_per_trade
). - Live stats tracking:
- Equity curve (portfolio and per-asset)
- Running max drawdown
- Campaign-based trade stats (wins, losses, expectancy)
- Commission/fee totals
Key methods: start_backtest()
, pause_backtest()
, resume_backtest()
, reset_backtest()
, place_order(order_dict)
, get_portfolio()
, get_stats_snapshot()
.
TradingEnvironment
Defined in classes/Trading_Environment.py
.
Provides a bridge between bots and APIs (backtest or live):
- Wires signals: listens to
bar_updated
,bar_closed
,bar_advanced
,backtest_finished
. - Indicator computation: Bollinger Bands, EMA, RSI, ATR (with forward updates at bar close).
- Bot integration: calls
bot.on_bar(self)
each bar. - Pass-through API: proxies to the underlying engine/live API (
get_portfolio
,get_positions
,place_order
,cancel_order
, SL/TP modification).
By keeping the bot attached to the TradingEnvironment
, strategy code is agnostic to whether it’s running in a backtest or live session.
Order
Defined in classes/API_Interface.py
.
Represents a single order request.
Fields:
order_id
— unique identifiersymbol
— instrument being tradedside
—"buy"
or"sell"
qty
— number of contracts/sharesprice
— limit/market price;None
for marketorder_type
—"market"
(default) or"limit"
status
—"open"
,"filled"
,"cancelled"
, etc.fill_price
,fill_time
— populated on execution
Orders flow through the engine, which applies slippage, commissions, and margin checks before filling.
Position
Defined in classes/API_Interface.py
.
Tracks the current holding in a symbol.
Fields:
symbol
— identifiercontract_size
— contract multiplier (e.g., 1000 barrels for crude oil)qty
— signed integer (# long or short contracts)avg_entry_price
— average entry price across fillsrealized_pnl
— realized profit/loss from closed tradesstop_loss_price
,take_profit
— protective levels (if set)
Methods:
get_unrealized_pnl(current_price, tick_size, tick_value)
— unrealized P&Lupdate_on_fill(fill_price, fill_qty, side, tick_size, tick_value)
— update on execution (averaging entry, realizing P&L on partial/total close)
Portfolio
Returned by BacktesterEngine.get_portfolio()
— not a standalone class.
Provides a snapshot of account state:
- Cash — current account balance (post fees/commissions)
- Total equity — cash + unrealized P&L
- Used margin — margin reserved for open positions
- Available equity — equity minus used margin
- Positions — dict of per-symbol holdings
- Open orders — outstanding orders not yet filled
How They Fit Together
- Bots issue orders via the
TradingEnvironment
. - The
TradingEnvironment
forwards to the active API (BacktesterEngine
orTradeStationLiveAPI
). - Orders are processed into Positions and update the Portfolio.
- The
BacktesterEngine
enforces rules (margin, slippage, commissions) and updates stats. - The GUI/CLI queries
get_portfolio()
orget_stats_snapshot()
to display results.
Summary
- BacktesterEngine — simulated broker with margin, intrabar logic, stats
- TradingEnvironment — glue layer adding indicators and connecting bots
- Order — single trade instruction
- Position — current holding with realized/unrealized P&L tracking
- Portfolio — snapshot of the entire account state
Together, these core classes make Algo Trading Stack a modular, realistic trading sandbox.