Trading System Example Setup
Relevant source files
The following files were used as context for generating this wiki page:
The Trading System Example Setup in AlphaPy provides a framework for developing and testing predictive models for financial markets. It utilizes the MarketFlow pipeline to integrate data acquisition, feature engineering, machine learning model training, and backtesting within a single environment.
This setup is primarily configured through YAML files that define market parameters, technical indicators, and model hyperparameters. The system can run in either training mode to build models or prediction mode to generate signals on new data, ultimately producing trade lists and portfolio performance metrics.
Sources: alphapy/market_flow.py:168-232, alphapy/examples/Trading%20Model/config/market.yml, README.rst:43-52
System Architecture
The trading system is built on a modular architecture where market-specific configurations and machine learning specifications are separated. The market_pipeline coordinates the flow from raw market data to a completed portfolio.
The diagram shows the top-down flow of the MarketFlow pipeline from configuration to final analysis.
Sources: alphapy/market_flow.py:168-232, README.rst:43-52
Key Components
| Component | Responsibility | Source File |
|---|---|---|
| MarketFlow | Manages the end-to-end workflow for market data and system execution. | alphapy/market_flow.py |
| System | Evaluates long/short entry and exit signals based on features or model probabilities. | alphapy/system.py |
| Model | Handles algorithm selection, training, and prediction logic. | alphapy/model.py |
| Portfolio | Manages positions, trade execution simulation, and capital allocation. | alphapy/portfolio.py |
Market Configuration (market.yml)
The market configuration defines the scope of the trading universe and the technical environment for feature generation.
Market Parameters
These settings control data frequency and history lookbacks:
data_fractal: The frequency of the data (e.g.,1dfor daily).data_history: Number of historical data points to fetch for training.predict_history: Number of points to fetch for prediction mode.target_group: The group of symbols (e.g.,['aapl', 'amzn', 'goog']) to process.
Sources: alphapy/market_flow.py:65-95, alphapy/examples/Trading%20Model/config/market.yml:1-15
Technical Features and Aliases
The system allows for extensive feature engineering by defining technical indicators and aliases for complex calculations.
- Features: List of indicators such as
rsi_14,volatility, andma_close. - Aliases: Human-readable names for underlying functions, e.g.,
atr: 'ma_truerange'. - Variables: Logical expressions for signal generation, such as
bigup: 'rrover & sephigh & netup'.
Sources: alphapy/examples/Trading%20Model/config/market.yml:45-125
Model Configuration (model.yml)
The model configuration specifies how AlphaPy should handle the machine learning aspect of the trading system.
Pipeline Settings
The example configuration typically uses a classification approach to predict market movements.
- Algorithms: Common choices include Random Forest (
RF) or XGBoost (XGB). - Target: The specific feature to predict, often a return-based indicator like
rrover. - Scoring: Functions like
roc_aucare used to evaluate model performance.
Sources: alphapy/examples/Trading%20Model/config/model.yml:25-50, alphapy/model.py:136-235
Data Treatment
Data is pre-processed using scaling and variance thresholds:
scaling: Typically set tostandardto normalize feature inputs.variance: A threshold (e.g.,0.1) to remove low-variance features that provide little predictive power.
Sources: alphapy/examples/Trading%20Model/config/model.yml:75-115
System Execution and Backtesting
Once a model is trained or predictions are made, the System class evaluates signals to simulate trading.
The sequence diagram illustrates the interaction between MarketFlow, the trading system, and the portfolio manager during backtesting.
Sources: alphapy/system.py:228-285, alphapy/portfolio.py:660-720
Order Types
The system supports several order types for managing market positions:
le: Long Entryse: Short Entrylx/sx: Long/Short Exit (Signal-based)lh/sh: Long/Short Exit (Hold period-based)
Sources: alphapy/globals.py:183-205, alphapy/system.py:168-195
Portfolio Valuation
The portfolio is valuated based on current positions and cash:
- Starting Capital: Initial funds (default
$100,000). - Cost Basis: Calculated as the total value of all trades divided by the total number of shares.
- Position Sizing: Can be fixed or determined by a specific column in the data (e.g.,
close).
Sources: alphapy/portfolio.py:90-140, alphapy/portfolio.py:260-310
Conclusion
The Trading System Example Setup facilitates a structured approach to quantitative trading. By leveraging YAML-driven configurations, developers can rapidly iterate on feature sets and machine learning algorithms. The resulting integration with portfolio management tools allows for a comprehensive assessment of strategy risk and return before live deployment.
Sources: README.rst:43-52, alphapy/market_flow.py:230-245