Details
The alphapy
system's core trading simulation is orchestrated by the Run System
, which initiates the Trade System
. The Trade System
drives the simulation loop, triggering Execute Trade
for potential new positions. Before a new trade is fully executed, Execute Trade
delegates to Allocate Trade
for pre-trade risk assessment. Allocate Trade
is crucial for determining if a trade should proceed or if an existing position needs to be closed, in which case it triggers Close Position
. Both Execute Trade
(for new trades) and Close Position
(for exiting trades) interact with Update Portfolio
to ensure the Portfolio Manager
accurately reflects the current financial state. The Portfolio Manager
serves as the central data store for all portfolio-related information, which is also assessed by Valuate Portfolio
to determine overall market value. This architecture ensures a clear separation of concerns, with Allocate Trade
acting as the gatekeeper for both opening and closing positions based on risk rules.
Run System
Serves as the high-level entry point, initiating the entire backtesting and simulation process.
Trade System
Orchestrates the core simulation loop, iterating through historical data, applying trading strategies (which would leverage predictions from ML models), and triggering trade execution and portfolio updates.
Portfolio Manager
Manages the state of the simulated trading account. It holds Position
objects, tracks cash, equity, and overall portfolio performance. It acts as the central data structure for the backtest.
Execute Trade
Executes a simulated trade. It handles the creation of Position
and Trade
objects, applies risk management rules, and updates the portfolio state.
Allocate Trade
Applies pre-trade risk management rules (e.g., stop-loss, kick-out conditions) to determine if a trade should proceed or if an existing position should be closed.
Close Position
Manages the process of exiting an open position. It updates the portfolio, records the trade, and removes the position.
Update Portfolio
Ensures the portfolio's financial state (cash, equity, positions) is accurately reflected after any transaction, market movement, or event.
Valuate Portfolio
Calculates the current market value of the entire portfolio, aggregating the value of all individual positions.