A modular grid trading backtesting system with interactive Streamlit interface.
This project implements a grid trading strategy with a modular architecture. The system supports multiple asset classes and provides both CLI and web interfaces for comprehensive backtesting and analysis.
- π Interactive Interface: Streamlit web application
- π Multi-Asset Support: 48 assets across 6 classes (Tech, Finance, ETFs, Crypto, Forex, Commodities)
- β‘ Performance Optimized: Smart grid generation with automatic level limiting
- π Advanced Analytics: Comprehensive performance metrics and visualizations
- π‘οΈ Risk Management: ATR-based stop losses and position sizing
- π’ Tech Stocks: 10 assets (AAPL, TSLA, META, NVDA, etc.)
- π¦ Finance: 11 assets (JPM, V, MA, PYPL, etc.)
- π ETFs: 7 assets (SPY, QQQ, XLK, XLF, etc.)
- βΏ Crypto: 6 assets (BTC, ETH, SOL, ADA, etc.)
- π± Forex: 7 pairs (EUR/USD, GBP/USD, USD/JPY, etc.)
- π₯ Commodities: 7 assets (Gold, Silver, Oil, Gas, etc.)
# Clone the repository
git clone https://github.com/yaaks7/grid-trading.git
cd grid-trading
# Create virtual environment
python -m venv .venv
.venv\Scripts\activate # Windows
# source .venv/bin/activate # Linux/Mac
# Install dependencies
pip install -r requirements.txtstreamlit run streamlit_app/app.pyβ‘οΈ Open http://localhost:8501 in your browser
# Default backtest (AAPL, 2024)
python main.py
# Custom asset and dates
python main.py --asset BTC-USD --start 2024-06-01 --end 2024-12-31
# Dynamic midprice with verbose output
python main.py --asset EURUSD=X --dynamic --verbose# Run unit tests
python -m pytest tests/ -v
# Test specific functionality
python tests/test_basic.pyThe web interface follows a logical, pedagogical approach with an intuitive two-step workflow:
Configure your trading setup and visualize the grid strategy before backtesting.
EUR/USD configuration showing data loading, grid parameters, and initial setup
- Select Asset: Choose from AAPL, BTC-USD, EURUSD=X, etc.
- Set Date Range: Pick your analysis period
- Configure Grid Parameters:
- Midprice: Center of the grid (static or dynamic MA-20)
- Grid Distance: Spacing between levels
- Grid Range: Total grid extent
- Load & Visualize: See price chart with grid levels and signals
Interactive price chart showing Google stock with grid levels and trading signals
Configure your portfolio settings and risk management, then run comprehensive backtests.
Backtesting configuration panel with portfolio settings and risk management parameters
- Portfolio Settings: Capital, position size, max concurrent trades
- Risk Management: ATR multipliers, take profit ratios
- Run Backtest: Execute full analysis
- Review Results: Performance metrics, equity curve, trade details
Complete backtest results with performance metrics, equity curve, and drawdown analysis
python main.py [OPTIONS]
Options:
--asset {AAPL,BTC-USD,EURUSD=X,...} Asset to backtest (default: AAPL)
--start YYYY-MM-DD Start date (default: 2024-01-01)
--end YYYY-MM-DD End date (default: 2024-12-31)
--dynamic Use dynamic midprice (MA-20)
--output-dir PATH Results directory (default: data/results)
--verbose, -v Enable detailed loggingfrom src.data.fetcher import DataFetcher
from src.strategy.grid_trading import GridTradingStrategy
from src.backtest.backtester import GridBacktester
# Fetch data
fetcher = DataFetcher()
data = fetcher.fetch_data("AAPL", "2024-01-01", "2024-12-31", "1d")
data_with_indicators = fetcher.add_technical_indicators(data)
# Configure strategy
strategy = GridTradingStrategy(
midprice=200, # Price center
grid_distance=5, # Distance between levels
grid_range=50 # Total grid range
)
# Prepare data and run backtest
prepared_data = strategy.prepare_data(data_with_indicators)
backtester = GridBacktester(cash=10000)
results = backtester.run_backtest(prepared_data, {
'position_size': 100,
'atr_multiplier': 1.5,
'tp_sl_ratio': 0.5,
'max_trades': 5,
'grid_distance': 5
})
# Get performance metrics
metrics = backtester.get_performance_metrics()
print(f"Return: {metrics['total_return']:.2f}%")48 assets across 6 asset classes - All parameters optimized with July 2025 market data.
| Symbol | Name | Midprice | Grid Distance | Grid Range |
|---|---|---|---|---|
| AAPL | Apple | 214 | 3.0 | 30.0 |
| MSFT | Microsoft | 514 | 8.0 | 80.0 |
| GOOGL | 193 | 3.0 | 30.0 | |
| NVDA | NVIDIA | 174 | 5.0 | 50.0 |
| META | Meta | 713 | 8.0 | 80.0 |
| AMZN | Amazon | 231 | 4.0 | 40.0 |
| NFLX | Netflix | 1,180 | 15.0 | 150.0 |
| TSLA | Tesla | 316 | 8.0 | 80.0 |
| CRM | Salesforce | 269 | 5.0 | 50.0 |
| ADBE | Adobe | 371 | 6.0 | 60.0 |
| Symbol | Name | Midprice | Grid Distance | Grid Range |
|---|---|---|---|---|
| JPM | JPMorgan Chase | 299 | 4.0 | 40.0 |
| BAC | Bank of America | 48 | 1.0 | 10.0 |
| GS | Goldman Sachs | 729 | 8.0 | 80.0 |
| MS | Morgan Stanley | 143 | 3.0 | 30.0 |
| V | Visa | 357 | 5.0 | 50.0 |
| MA | Mastercard | 568 | 8.0 | 80.0 |
| PYPL | PayPal | 78 | 2.0 | 20.0 |
| Symbol | Name | Midprice | Grid Distance | Grid Range |
|---|---|---|---|---|
| SPY | S&P 500 ETF | 637 | 5.0 | 50.0 |
| QQQ | NASDAQ ETF | 566 | 6.0 | 60.0 |
| DIA | Dow Jones ETF | 449 | 4.0 | 40.0 |
| IWM | Russell 2000 ETF | 224 | 3.0 | 30.0 |
| XLF | Financial Sector | 53 | 1.0 | 10.0 |
| XLK | Technology Sector | 262 | 3.0 | 30.0 |
| XLE | Energy Sector | 87 | 2.0 | 20.0 |
| Symbol | Name | Midprice | Grid Distance | Grid Range |
|---|---|---|---|---|
| BTC-USD | Bitcoin | 117,600 | 2,000 | 20,000 |
| ETH-USD | Ethereum | 3,738 | 80 | 800 |
| SOL-USD | Solana | 186 | 5.0 | 50.0 |
| ADA-USD | Cardano | 0.83 | 0.02 | 0.20 |
| DOT-USD | Polkadot | 4.12 | 0.15 | 1.50 |
| AVAX-USD | Avalanche | 24 | 0.80 | 8.0 |
| Symbol | Name | Midprice | Grid Distance | Grid Range |
|---|---|---|---|---|
| EURUSD=X | EUR/USD | 1.174 | 0.005 | 0.050 |
| GBPUSD=X | GBP/USD | 1.344 | 0.006 | 0.060 |
| USDJPY=X | USD/JPY | 147.6 | 0.50 | 5.0 |
| USDCHF=X | USD/CHF | 0.794 | 0.003 | 0.030 |
| AUDUSD=X | AUD/USD | 0.657 | 0.003 | 0.030 |
| USDCAD=X | USD/CAD | 1.37 | 0.005 | 0.050 |
| EURGBP=X | EUR/GBP | 0.874 | 0.003 | 0.030 |
| Symbol | Name | Midprice | Grid Distance | Grid Range |
|---|---|---|---|---|
| GC=F | Gold Futures | 3,339 | 50 | 500 |
| SI=F | Silver Futures | 38.3 | 1.0 | 10 |
| CL=F | Crude Oil | 65.1 | 2.0 | 20 |
| SLV | Silver ETF | 34.7 | 0.8 | 8.0 |
| USO | Oil ETF | 74.9 | 2.0 | 20 |
| UNG | Natural Gas ETF | 13.8 | 0.4 | 4.0 |
| DBA | Agriculture ETF | 26.1 | 0.5 | 5.0 |
All prices and parameters updated July 2025. See docs/ASSET_CONFIGURATION.md for detailed parameter guides.
- Concept: Place buy/sell orders at predetermined price levels
- Signal Generation: Execute trades when price crosses grid levels
- Risk Management: ATR-based stops, position sizing, concurrent trade limits
- Optimization: Dynamic midprice adjustment, performance-based grid sizing
- π Full Documentation: See docs/GRID_TRADING_GUIDE.md for complete strategy guide
- Python 3.8+: Core language
- pandas/numpy: Data manipulation
- yfinance: Market data
- pandas-ta: Technical indicators
- backtesting.py: Backtesting engine
- plotly: Interactive charts
- streamlit: Web interface
- pytest: Testing framework
# Run all tests
python -m pytest tests/ -v
# Test specific functionality
python tests/test_basic.py
# Performance validation
python main.py --asset AAPL --verboseEdit config/settings.py:
SUPPORTED_ASSETS = {
'YOUR_SYMBOL': {
'midprice': 100.0,
'grid_distance': 2.0,
'grid_range': 20.0,
'name': 'Your Asset'
}
}Extend src/data/fetcher.py:
def add_technical_indicators(self, data):
df = data.copy()
df['RSI'] = ta.rsi(df.Close, length=14)
df['MACD'] = ta.macd(df.Close)['MACD_12_26_9']
df['YOUR_INDICATOR'] = your_custom_function(df)
return dfAll results are saved to data/results/ with timestamps:
This project is licensed under the MIT License - see the LICENSE file for details.
Yanis Aksas
- GitHub: github.com/yaaks7
- LinkedIn: linkedin.com/in/yanisaks
- Email: yanis.aksas@gmail.com