Skip to content
⚠️ Not financial advice. For educational use only.
WikiTrading Environment

Trading Environment

The TradingEnvironment is the middle layer that connects the BacktesterEngine (or live API) with Bots and the UI.
It ensures bots interact with a consistent interface, indicators are kept up-to-date, and no lookahead bias leaks into strategy execution.


1. Role in the System

The TradingEnvironment:

  • Acts as the hub between the data source (BacktesterEngine or live API), the strategy bots, and the UI
  • Computes and updates technical indicators (ATR, RSI, EMA, Bollinger Bands, etc.)
  • Provides safe, no-lookahead snapshots for bots at the OPEN phase
  • Proxies calls from bots to the underlying API (place orders, cancel, modify SL/TP, query portfolio/positions)
  • Emits signals so bots and UIs run in sync with the bar cycle

2. Indicator Pipeline

The environment maintains an indicator registry and automatically updates indicators during backtests or live runs.

  • Default indicators:

    • ATR(14)
    • RSI(14)
    • EMA(21), EMA(50)
    • Bollinger Bands (20, ±2σ)
  • Indicator updates:

    • At OPEN: indicators from the previous bar are carried forward, so bots can trade without lookahead bias
    • At CLOSE: indicators are recomputed for the just-closed bar
  • Customization: users can extend the registry (INDICATOR_REGISTRY) to add their own indicator functions.


3. Bot Lifecycle

At each bar:

  1. The BacktesterEngine emits a snapshot (OPEN only).
  2. The TradingEnvironment computes/forwards indicators.
  3. The bot’s on_bar(env) method is called.
    • If flat, the bot may decide to enter (decide_side).
    • If in position, the bot’s exit_strategy may update the stop-loss.
  4. At CLOSE, the environment recomputes indicators for the bar that just finished.

This ensures a clean lifecycle:

  • Bots never see future data
  • Exits update consistently across bots

4. Proxy Methods

The environment forwards API calls so bots interact with a clean, stable interface:

  • get_portfolio() — returns cash, positions, open orders, equity
  • get_positions() — returns position details (qty, entry price, PnL, SL/TP)
  • get_total_pnl() — realized, unrealized, and total PnL
  • get_asset_list() — list of tradable symbols
  • get_asset_data(symbol) — full historical DataFrame for a symbol
  • get_latest_data(symbol) — current snapshot for trading
  • get_orders(symbol) — trade log (all orders or filtered by symbol)
  • place_order(order_dict) — submit a market/limit order
  • cancel_order(order_id) — cancel an order
  • modify_stop_loss(symbol, new_value) — adjust stop level
  • modify_take_profit(symbol, new_value) — adjust take-profit

5. Signals

The TradingEnvironment hooks into signals from the API/engine and forwards them to bots/UI:

  • bar_advanced — emitted each bar (engine advanced)
  • bar_updated(df) — snapshot at OPEN (bot runs here)
  • bar_closed(df) — full bar closed (indicators recomputed here)
  • backtest_finished — backtest completed

6. Example Usage

Using TradingEnvironment in a backtest

from classes.Backtester_Engine import BacktesterEngine
from classes.Trading_Environment import TradingEnvironment
from bots.coin_flip_bot.coin_flip_bot import CoinFlipBot
 
# Load config
engine = BacktesterEngine(config_path="backtest_configs/backtest_config.yaml")
env = TradingEnvironment()
env.set_api(engine)
 
# Attach bot
bot = CoinFlipBot()
env.set_bot(bot)
 
# Run
engine.start_backtest(interval_ms=10)

7. How It Fits with the System

  • BacktesterEngine (or live API) → drives the data
  • TradingEnvironment → computes indicators, proxies API calls, orchestrates bar lifecycle
  • Bots → implement entry/exit logic, risk management, and sizing
  • Exits → manage stop-loss/take-profit logic consistently across bots
  • UI → listens to environment signals to display charts, equity, positions, and stats

8. Summary

The TradingEnvironment provides:

  • A clean sandbox for bots to trade in, without touching raw engine internals
  • Automatic indicator computation with no lookahead bias
  • Proxy methods so bots have a stable, broker-like interface
  • Signals that keep bots and UI synchronized with the bar loop

It is the bridge that makes Algo Trading Stack modular, extensible, and safe for research.