Skip to content

An example quant trading system for beginners to reference and learn from.

Notifications You must be signed in to change notification settings

aeri-rt/beginnerQuant

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🚀 Quant Trading System

A beginner-friendly quantitative trading system implementing an RSI (Relative Strength Index) mean-reversion strategy.

📋 Overview

This project is designed to teach the fundamentals of quantitative trading through a hands-on implementation. It includes:

  • Data Fetching: Download historical market data from Yahoo Finance
  • Technical Indicators: Calculate RSI and generate trading signals
  • Backtesting Engine: Simulate trading on historical data
  • Performance Metrics: Calculate Sharpe ratio, drawdown, and more
  • Visualization: Generate comprehensive charts

🏗️ Project Structure

testQuant/
├── config.py                    # Configuration parameters
├── main.py                      # Main entry point
├── requirements.txt             # Python dependencies
├── README.md                    # This file
├── data/
│   ├── raw/                     # Cached raw data
│   └── processed/               # Processed data
├── src/
│   ├── data/
│   │   ├── fetcher.py          # Data downloading
│   │   └── processor.py        # Data cleaning
│   ├── strategy/
│   │   ├── indicators.py       # Technical indicators (RSI, SMA, etc.)
│   │   └── rsi_strategy.py     # RSI trading strategy
│   ├── backtest/
│   │   ├── engine.py           # Backtesting simulation
│   │   └── metrics.py          # Performance calculations
│   └── visualization/
│       └── plots.py            # Chart generation
├── notebooks/                   # Jupyter notebooks for exploration
├── results/                     # Saved charts and reports
└── tests/                       # Unit tests

🚀 Quick Start

1. Install Dependencies

pip install -r requirements.txt

2. Run the Backtest

python main.py

3. Customize Parameters

# Test on different stock
python main.py --symbol AAPL --start 2022-01-01 --end 2023-12-31

# Adjust RSI parameters
python main.py --rsi-period 14 --oversold 25 --overbought 75

# Change capital and commission
python main.py --capital 50000 --commission 0.002

📊 The RSI Strategy

What is RSI?

RSI (Relative Strength Index) measures the speed and magnitude of price changes on a scale of 0-100.

  • RSI < 30: Oversold condition → Potential buying opportunity
  • RSI > 70: Overbought condition → Potential selling opportunity

Strategy Logic

  1. Buy Signal: When RSI crosses above 30 (leaving oversold zone)
  2. Sell Signal: When RSI crosses below 70 (leaving overbought zone)
  3. Stop-Loss: Exit if position loses more than 5% (configurable)

Mathematical Formula

RSI = 100 - (100 / (1 + RS))

Where:
RS = Average Gain / Average Loss (over N periods)

📈 Understanding the Output

Performance Metrics

Metric Description Good Value
Total Return Overall profit/loss Higher is better
Sharpe Ratio Risk-adjusted return > 1.0 is good, > 2.0 is excellent
Max Drawdown Largest peak-to-trough decline Lower is better (< 20% ideal)
Win Rate % of profitable trades > 50% is good, but not required
Profit Factor Gross profit / Gross loss > 1.5 is good

Generated Charts

  1. Backtest Results: Price chart with signals, RSI, portfolio value, drawdown
  2. RSI Signals: Zoomed view of recent signals
  3. Monthly Returns: Heatmap showing performance by month
  4. Trade Analysis: Distribution of trade P&L

⚙️ Configuration

Edit config.py to change default parameters:

# Trading symbol
SYMBOL = "SPY"

# Date range
START_DATE = "2020-01-01"
END_DATE = "2024-12-31"

# RSI parameters
RSI_PERIOD = 14
RSI_OVERSOLD = 30
RSI_OVERBOUGHT = 70

# Backtest settings
INITIAL_CAPITAL = 100_000
COMMISSION_PCT = 0.001  # 0.1%
STOP_LOSS_PCT = 0.05    # 5%

📚 Learning Path

This project teaches:

  1. Data Handling: Working with financial time series data
  2. Technical Analysis: Understanding and implementing indicators
  3. Backtesting: Simulating trading strategies historically
  4. Risk Management: Position sizing, stop-losses
  5. Performance Analysis: Evaluating strategy effectiveness
  6. Python Best Practices: Clean code, documentation, modularity

⚠️ Important Warnings

Backtesting Pitfalls

  1. Overfitting: A strategy tuned too closely to historical data will fail live
  2. Look-Ahead Bias: Never use future data in decisions
  3. Survivorship Bias: Historical data may exclude failed companies
  4. Transaction Costs: Always include commissions and slippage
  5. Market Impact: Large orders can move prices against you

Disclaimer

⚠️ This is an educational project. Past performance does not guarantee future results. Do not use this system for live trading without extensive further testing and professional advice.

🔧 Extending the System

Ideas for improvement:

  1. Add More Indicators: MACD, Bollinger Bands, Moving Averages
  2. Combine Strategies: Use multiple indicators for confirmation
  3. Parameter Optimization: Find optimal RSI periods and thresholds
  4. Walk-Forward Testing: More robust validation technique
  5. Multiple Assets: Trade a portfolio of stocks
  6. Paper Trading: Connect to broker API for simulated live trading

📖 Resources

📝 License

MIT License - Feel free to use and modify for learning purposes.


Happy Trading! 📈🤖

About

An example quant trading system for beginners to reference and learn from.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages