bactester for strategies

This commit is contained in:
Ajasra 2025-05-29 14:22:50 +08:00
parent b0ea701020
commit df19ef32db
7 changed files with 2059 additions and 1 deletions

2
.gitignore vendored
View File

@ -172,7 +172,7 @@ cython_debug/
An introduction to trading cycles.pdf
An introduction to trading cycles.txt
README.md
.vscode/launch.json
data/*

268
IncrementalTrader/README.md Normal file
View File

@ -0,0 +1,268 @@
# IncrementalTrader
A high-performance, memory-efficient trading framework designed for real-time algorithmic trading and backtesting. Built around the principle of **incremental computation**, IncrementalTrader processes new data points efficiently without recalculating entire histories.
## 🚀 Key Features
- **Incremental Computation**: Constant memory usage and O(1) processing time per data point
- **Real-time Capable**: Designed for live trading with minimal latency
- **Modular Architecture**: Clean separation between strategies, execution, and testing
- **Built-in Strategies**: MetaTrend, BBRS, and Random strategies included
- **Comprehensive Backtesting**: Multi-threaded backtesting with parameter optimization
- **Rich Indicators**: Supertrend, Bollinger Bands, RSI, Moving Averages, and more
- **Performance Tracking**: Detailed metrics and portfolio analysis
## 📦 Installation
```bash
# Clone the repository
git clone <repository-url>
cd Cycles
# Install dependencies
pip install -r requirements.txt
# Import the module
from IncrementalTrader import *
```
## 🏃‍♂️ Quick Start
### Basic Strategy Usage
```python
from IncrementalTrader import MetaTrendStrategy, IncTrader
import pandas as pd
# Load your data
data = pd.read_csv('your_data.csv')
# Create strategy
strategy = MetaTrendStrategy("metatrend", params={
"timeframe": "15min",
"supertrend_periods": [10, 20, 30],
"supertrend_multipliers": [2.0, 3.0, 4.0]
})
# Create trader
trader = IncTrader(strategy, initial_usd=10000)
# Process data
for _, row in data.iterrows():
trader.process_data_point(
timestamp=row['timestamp'],
ohlcv=(row['open'], row['high'], row['low'], row['close'], row['volume'])
)
# Get results
results = trader.get_results()
print(f"Final Portfolio Value: ${results['final_portfolio_value']:.2f}")
print(f"Total Return: {results['total_return_pct']:.2f}%")
```
### Backtesting
```python
from IncrementalTrader import IncBacktester, BacktestConfig
# Configure backtest
config = BacktestConfig(
initial_usd=10000,
stop_loss_pct=0.03,
take_profit_pct=0.06,
start_date="2024-01-01",
end_date="2024-12-31"
)
# Run backtest
backtester = IncBacktester()
results = backtester.run_single_strategy(
strategy_class=MetaTrendStrategy,
strategy_params={"timeframe": "15min"},
config=config,
data_file="data/BTCUSDT_1m.csv"
)
# Analyze results
print(f"Sharpe Ratio: {results['performance_metrics']['sharpe_ratio']:.2f}")
print(f"Max Drawdown: {results['performance_metrics']['max_drawdown_pct']:.2f}%")
```
## 📊 Available Strategies
### MetaTrend Strategy
A sophisticated trend-following strategy that uses multiple Supertrend indicators to detect market trends.
```python
strategy = MetaTrendStrategy("metatrend", params={
"timeframe": "15min",
"supertrend_periods": [10, 20, 30],
"supertrend_multipliers": [2.0, 3.0, 4.0],
"min_trend_agreement": 0.6
})
```
### BBRS Strategy
Combines Bollinger Bands and RSI with market regime detection for adaptive trading.
```python
strategy = BBRSStrategy("bbrs", params={
"timeframe": "15min",
"bb_period": 20,
"bb_std": 2.0,
"rsi_period": 14,
"volume_ma_period": 20
})
```
### Random Strategy
A testing strategy that generates random signals for framework validation.
```python
strategy = RandomStrategy("random", params={
"timeframe": "15min",
"buy_probability": 0.1,
"sell_probability": 0.1
})
```
## 🔧 Technical Indicators
All indicators are designed for incremental computation:
```python
from IncrementalTrader.strategies.indicators import *
# Moving Averages
sma = MovingAverageState(period=20)
ema = ExponentialMovingAverageState(period=20, alpha=0.1)
# Volatility
atr = ATRState(period=14)
# Trend
supertrend = SupertrendState(period=10, multiplier=3.0)
# Oscillators
rsi = RSIState(period=14)
bb = BollingerBandsState(period=20, std_dev=2.0)
# Update with new data
for price in price_data:
sma.update(price)
current_sma = sma.get_value()
```
## 🧪 Parameter Optimization
```python
from IncrementalTrader import OptimizationConfig
# Define parameter ranges
param_ranges = {
"supertrend_periods": [[10, 20, 30], [15, 25, 35], [20, 30, 40]],
"supertrend_multipliers": [[2.0, 3.0, 4.0], [1.5, 2.5, 3.5]],
"min_trend_agreement": [0.5, 0.6, 0.7, 0.8]
}
# Configure optimization
opt_config = OptimizationConfig(
base_config=config,
param_ranges=param_ranges,
max_workers=4
)
# Run optimization
results = backtester.optimize_strategy(
strategy_class=MetaTrendStrategy,
optimization_config=opt_config,
data_file="data/BTCUSDT_1m.csv"
)
# Get best parameters
best_params = results['best_params']
best_performance = results['best_performance']
```
## 📈 Performance Analysis
```python
# Get detailed performance metrics
performance = results['performance_metrics']
print(f"Total Trades: {performance['total_trades']}")
print(f"Win Rate: {performance['win_rate']:.2f}%")
print(f"Profit Factor: {performance['profit_factor']:.2f}")
print(f"Sharpe Ratio: {performance['sharpe_ratio']:.2f}")
print(f"Max Drawdown: {performance['max_drawdown_pct']:.2f}%")
print(f"Calmar Ratio: {performance['calmar_ratio']:.2f}")
# Access trade history
trades = results['trades']
for trade in trades[-5:]: # Last 5 trades
print(f"Trade: {trade['side']} at {trade['price']} - P&L: {trade['pnl']:.2f}")
```
## 🏗️ Architecture
IncrementalTrader follows a modular architecture:
```
IncrementalTrader/
├── strategies/ # Trading strategies and indicators
│ ├── base.py # Base classes and framework
│ ├── metatrend.py # MetaTrend strategy
│ ├── bbrs.py # BBRS strategy
│ ├── random.py # Random strategy
│ └── indicators/ # Technical indicators
├── trader/ # Trade execution and position management
│ ├── trader.py # Main trader implementation
│ └── position.py # Position management
├── backtester/ # Backtesting framework
│ ├── backtester.py # Main backtesting engine
│ ├── config.py # Configuration management
│ └── utils.py # Utilities and helpers
└── docs/ # Documentation
```
## 🔍 Memory Efficiency
Traditional batch processing vs. IncrementalTrader:
| Aspect | Batch Processing | IncrementalTrader |
|--------|------------------|-------------------|
| Memory Usage | O(n) - grows with data | O(1) - constant |
| Processing Time | O(n) - recalculates all | O(1) - per data point |
| Real-time Capable | No - too slow | Yes - designed for it |
| Scalability | Poor - memory limited | Excellent - unlimited data |
## 📚 Documentation
- [Architecture Overview](docs/architecture.md) - Detailed system design
- [Strategy Development Guide](docs/strategies/strategies.md) - How to create custom strategies
- [Indicator Reference](docs/indicators/base.md) - Complete indicator documentation
- [Backtesting Guide](docs/backtesting.md) - Advanced backtesting features
- [API Reference](docs/api/api.md) - Complete API documentation
## 🤝 Contributing
1. Fork the repository
2. Create a feature branch
3. Make your changes
4. Add tests for new functionality
5. Submit a pull request
## 📄 License
This project is licensed under the MIT License - see the LICENSE file for details.
## 🆘 Support
For questions, issues, or contributions:
- Open an issue on GitHub
- Check the documentation in the `docs/` folder
- Review the examples in the `examples/` folder
---
**IncrementalTrader** - Efficient, scalable, and production-ready algorithmic trading framework.

View File

@ -0,0 +1,34 @@
{
"backtest_settings": {
"data_file": "btcusd_1-min_data.csv",
"data_dir": "data",
"start_date": "2023-01-01",
"end_date": "2023-01-02",
"initial_usd": 10000
},
"strategies": [
{
"name": "Valid_Strategy",
"type": "random",
"params": {
"signal_probability": 0.001,
"timeframe": "15min"
},
"trader_params": {
"stop_loss_pct": 0.02,
"portfolio_percent_per_trade": 0.5
}
},
{
"name": "Invalid_Strategy",
"type": "nonexistent_strategy",
"params": {
"some_param": 42
},
"trader_params": {
"stop_loss_pct": 0.02,
"portfolio_percent_per_trade": 0.5
}
}
]
}

View File

@ -0,0 +1,83 @@
{
"backtest_settings": {
"data_file": "btcusd_1-min_data.csv",
"data_dir": "data",
"start_date": "2023-01-01",
"end_date": "2023-01-31",
"initial_usd": 10000
},
"strategies": [
{
"name": "MetaTrend_Conservative",
"type": "metatrend",
"params": {
"supertrend_periods": [
12,
10,
11
],
"supertrend_multipliers": [
3.0,
1.0,
2.0
],
"min_trend_agreement": 0.8,
"timeframe": "15min"
},
"trader_params": {
"stop_loss_pct": 0.02,
"portfolio_percent_per_trade": 1.0
}
},
{
"name": "MetaTrend_Aggressive",
"type": "metatrend",
"params": {
"supertrend_periods": [
10,
8,
9
],
"supertrend_multipliers": [
2.0,
1.0,
1.5
],
"min_trend_agreement": 0.5,
"timeframe": "5min"
},
"trader_params": {
"stop_loss_pct": 0.03,
"portfolio_percent_per_trade": 1.0
}
},
{
"name": "BBRS_Default",
"type": "bbrs",
"params": {
"bb_length": 20,
"bb_std": 2.0,
"rsi_length": 14,
"rsi_overbought": 70,
"rsi_oversold": 30,
"timeframe": "15min"
},
"trader_params": {
"stop_loss_pct": 0.025,
"portfolio_percent_per_trade": 1.0
}
},
{
"name": "Random_Baseline",
"type": "random",
"params": {
"signal_probability": 0.001,
"timeframe": "15min"
},
"trader_params": {
"stop_loss_pct": 0.02,
"portfolio_percent_per_trade": 1.0
}
}
]
}

View File

@ -0,0 +1,37 @@
{
"backtest_settings": {
"data_file": "btcusd_1-min_data.csv",
"data_dir": "data",
"start_date": "2025-01-01",
"end_date": "2025-03-01",
"initial_usd": 10000
},
"strategies": [
{
"name": "MetaTrend_Quick_Test",
"type": "metatrend",
"params": {
"supertrend_periods": [12, 10, 11],
"supertrend_multipliers": [3.0, 1.0, 2.0],
"min_trend_agreement": 0.5,
"timeframe": "15min"
},
"trader_params": {
"stop_loss_pct": 0.02,
"portfolio_percent_per_trade": 1.0
}
},
{
"name": "Random_Baseline",
"type": "random",
"params": {
"signal_probability": 0.001,
"timeframe": "15min"
},
"trader_params": {
"stop_loss_pct": 0.02,
"portfolio_percent_per_trade": 1.0
}
}
]
}

333
test/backtest/README.md Normal file
View File

@ -0,0 +1,333 @@
# Strategy Backtest Runner
A comprehensive and efficient backtest runner for executing predefined trading strategies with advanced visualization and analysis capabilities.
## Overview
The Strategy Backtest Runner (`strategy_run.py`) executes specific trading strategies with predefined parameters defined in a JSON configuration file. Unlike the parameter optimization script, this runner focuses on testing and comparing specific strategy configurations with detailed market analysis and visualization.
## Features
- **JSON Configuration**: Define strategies and parameters in easy-to-edit JSON files
- **Multiple Strategy Support**: Run multiple strategies in sequence with a single command
- **All Strategy Types**: Support for MetaTrend, BBRS, and Random strategies
- **Organized Results**: Automatic folder structure creation for each run
- **Advanced Visualization**: Detailed plots showing portfolio performance and market context
- **Full Market Data Integration**: Continuous price charts with buy/sell signals overlay
- **Signal Export**: Complete buy/sell signal data exported to CSV files
- **Real-time File Saving**: Individual strategy results saved immediately upon completion
- **Comprehensive Analysis**: Multiple plot types for thorough performance analysis
- **Detailed Results**: Comprehensive result reporting with CSV and JSON export
- **Result Analysis**: Automatic summary generation and performance comparison
- **Error Handling**: Robust error handling with detailed logging
- **Flexible Configuration**: Support for different data files, date ranges, and trader parameters
## Usage
### Basic Usage
```bash
# Run strategies from a configuration file
python test/backtest/strategy_run.py --config configs/strategy/example_strategies.json
# Save results to a custom directory
python test/backtest/strategy_run.py --config configs/strategy/my_strategies.json --results-dir my_results
# Enable verbose logging
python test/backtest/strategy_run.py --config configs/strategy/example_strategies.json --verbose
```
### Enhanced Analysis Features
Each run automatically generates:
- **Organized folder structure** with timestamp for easy management
- **Real-time file saving** - results saved immediately after each strategy completes
- **Full market data visualization** - continuous price charts show complete market context
- **Signal tracking** - all buy/sell decisions exported with precise timing and pricing
- **Multi-layered analysis** - from individual trade details to portfolio-wide comparisons
- **Professional plots** - high-resolution (300 DPI) charts suitable for reports and presentations
### Create Example Configuration
```bash
# Create an example configuration file
python test/backtest/strategy_run.py --create-example configs/example_strategies.json
```
## Configuration File Format
The configuration file uses JSON format with two main sections:
### Backtest Settings
```json
{
"backtest_settings": {
"data_file": "btcusd_1-min_data.csv",
"data_dir": "data",
"start_date": "2023-01-01",
"end_date": "2023-01-31",
"initial_usd": 10000
}
}
```
### Strategy Definitions
```json
{
"strategies": [
{
"name": "MetaTrend_Conservative",
"type": "metatrend",
"params": {
"supertrend_periods": [12, 10, 11],
"supertrend_multipliers": [3.0, 1.0, 2.0],
"min_trend_agreement": 0.8,
"timeframe": "15min"
},
"trader_params": {
"stop_loss_pct": 0.02,
"portfolio_percent_per_trade": 0.5
}
}
]
}
```
## Strategy Types
### MetaTrend Strategy
Parameters:
- `supertrend_periods`: List of periods for multiple supertrend indicators
- `supertrend_multipliers`: List of multipliers for supertrend indicators
- `min_trend_agreement`: Minimum agreement threshold between indicators (0.0-1.0)
- `timeframe`: Data aggregation timeframe ("1min", "5min", "15min", "30min", "1h")
### BBRS Strategy
Parameters:
- `bb_length`: Bollinger Bands period
- `bb_std`: Bollinger Bands standard deviation multiplier
- `rsi_length`: RSI period
- `rsi_overbought`: RSI overbought threshold
- `rsi_oversold`: RSI oversold threshold
- `timeframe`: Data aggregation timeframe
### Random Strategy
Parameters:
- `signal_probability`: Probability of generating a signal (0.0-1.0)
- `timeframe`: Data aggregation timeframe
## Trader Parameters
All strategies support these trader parameters:
- `stop_loss_pct`: Stop loss percentage (e.g., 0.02 for 2%)
- `portfolio_percent_per_trade`: Percentage of portfolio to use per trade (0.0-1.0)
## Results Organization
Each run creates an organized folder structure for easy navigation and analysis:
```
results/
└── [config_name]_[timestamp]/
├── strategy_1_[strategy_name].json # Individual strategy data
├── strategy_1_[strategy_name]_plot.png # 4-panel performance plot
├── strategy_1_[strategy_name]_detailed_plot.png # 3-panel market analysis
├── strategy_1_[strategy_name]_trades.csv # Trade details
├── strategy_1_[strategy_name]_signals.csv # All buy/sell signals
├── strategy_2_[strategy_name].* # Second strategy files
├── ... # Additional strategies
├── summary.csv # Strategy comparison table
├── summary_plot.png # Multi-strategy comparison
└── summary_*.json # Comprehensive results
```
## Visualization Types
The runner generates three types of plots for comprehensive analysis:
### 1. Individual Strategy Plot (4-Panel)
- **Equity Curve**: Portfolio value over time
- **Trade P&L**: Individual trade profits/losses
- **Drawdown**: Portfolio drawdown visualization
- **Statistics**: Strategy performance summary
### 2. Detailed Market Analysis Plot (3-Panel)
- **Portfolio Signals**: Portfolio value with buy/sell signal markers
- **Market Price**: Full continuous market price with entry/exit points
- **Combined View**: Dual-axis plot showing market vs portfolio performance
### 3. Summary Comparison Plot (4-Panel)
- **Returns Comparison**: Total returns across all strategies
- **Trade Counts**: Number of trades per strategy
- **Risk vs Return**: Win rate vs maximum drawdown scatter plot
- **Statistics Table**: Comprehensive performance metrics
## Output Files
The runner generates comprehensive output files organized in dedicated folders:
### Individual Strategy Files (per strategy)
- `strategy_N_[name].json`: Complete strategy data and metadata
- `strategy_N_[name]_plot.png`: 4-panel performance analysis plot
- `strategy_N_[name]_detailed_plot.png`: 3-panel market context plot
- `strategy_N_[name]_trades.csv`: Detailed trade information
- `strategy_N_[name]_signals.csv`: All buy/sell signals with timestamps
### Summary Files (per run)
- `summary.csv`: Strategy comparison table
- `summary_plot.png`: Multi-strategy comparison visualization
- `summary_*.json`: Comprehensive results and metadata
### Signal Data Format
Each signal CSV contains:
- `signal_id`: Unique signal identifier
- `signal_type`: BUY or SELL
- `time`: Signal timestamp
- `price`: Execution price
- `trade_id`: Associated trade number
- `quantity`: Trade quantity
- `value`: Trade value (quantity × price)
- `strategy`: Strategy name
## Example Configurations
### Simple MetaTrend Test
```json
{
"backtest_settings": {
"data_file": "btcusd_1-min_data.csv",
"start_date": "2023-01-01",
"end_date": "2023-01-07",
"initial_usd": 10000
},
"strategies": [
{
"name": "MetaTrend_Test",
"type": "metatrend",
"params": {
"supertrend_periods": [12, 10],
"supertrend_multipliers": [3.0, 1.0],
"min_trend_agreement": 0.5,
"timeframe": "15min"
},
"trader_params": {
"stop_loss_pct": 0.02,
"portfolio_percent_per_trade": 0.5
}
}
]
}
```
### Multiple Strategy Comparison
```json
{
"backtest_settings": {
"data_file": "btcusd_1-min_data.csv",
"start_date": "2023-01-01",
"end_date": "2023-01-31",
"initial_usd": 10000
},
"strategies": [
{
"name": "Conservative_MetaTrend",
"type": "metatrend",
"params": {
"supertrend_periods": [12, 10, 11],
"supertrend_multipliers": [3.0, 1.0, 2.0],
"min_trend_agreement": 0.8,
"timeframe": "15min"
},
"trader_params": {
"stop_loss_pct": 0.02,
"portfolio_percent_per_trade": 0.5
}
},
{
"name": "Aggressive_MetaTrend",
"type": "metatrend",
"params": {
"supertrend_periods": [10, 8],
"supertrend_multipliers": [2.0, 1.0],
"min_trend_agreement": 0.5,
"timeframe": "5min"
},
"trader_params": {
"stop_loss_pct": 0.03,
"portfolio_percent_per_trade": 0.8
}
},
{
"name": "BBRS_Baseline",
"type": "bbrs",
"params": {
"bb_length": 20,
"bb_std": 2.0,
"rsi_length": 14,
"rsi_overbought": 70,
"rsi_oversold": 30,
"timeframe": "15min"
},
"trader_params": {
"stop_loss_pct": 0.025,
"portfolio_percent_per_trade": 0.6
}
}
]
}
```
## Command Line Options
- `--config`: Path to JSON configuration file (required)
- `--results-dir`: Directory for saving results (default: "results")
- `--create-example`: Create example config file at specified path
- `--verbose`: Enable verbose logging for debugging
## Error Handling
The runner includes comprehensive error handling:
- **Configuration Validation**: Validates JSON structure and required fields
- **Data File Verification**: Checks if data files exist before running
- **Strategy Creation**: Handles unknown strategy types gracefully
- **Backtest Execution**: Captures and logs individual strategy failures
- **Result Saving**: Ensures results are saved even if some strategies fail
## Integration
This runner integrates seamlessly with the existing IncrementalTrader framework:
- Uses the same `IncBacktester` and strategy classes
- Compatible with all existing data formats
- Leverages the same result saving utilities
- Maintains consistency with optimization scripts
## Performance
- **Sequential Execution**: Strategies run one after another for clear logging
- **Real-time Results**: Individual strategy files saved immediately upon completion
- **Efficient Data Loading**: Market data loaded once per run for all visualizations
- **Progress Tracking**: Clear progress indication for long-running backtests
- **Detailed Timing**: Individual strategy execution times are tracked
- **High-Quality Output**: Professional 300 DPI plots suitable for presentations
## Best Practices
1. **Start Small**: Test with short date ranges first
2. **Validate Data**: Ensure data files exist and cover the specified date range
3. **Monitor Resources**: Watch memory usage for very long backtests
4. **Save Configs**: Keep configuration files organized for reproducibility
5. **Use Descriptive Names**: Give strategies clear, descriptive names
6. **Test Incrementally**: Add strategies one by one when debugging
7. **Leverage Visualizations**: Use detailed plots to understand market context and strategy behavior
8. **Analyze Signals**: Review signal CSV files to understand strategy decision patterns
9. **Compare Runs**: Use organized folder structure to compare different parameter sets
10. **Monitor Execution**: Watch real-time progress as individual strategies complete

File diff suppressed because it is too large Load Diff