Advanced AI-Powered Trading Strategies & Backtesting Framework
A comprehensive collection of machine learning and algorithmic trading strategies with full backtesting capabilities, featuring both traditional technical analysis and cutting-edge transformer-based price prediction models.
- π― Project Overview
- π Features
- π Project Structure
- β‘ Quick Start
- π Trading Strategies
- π οΈ Installation
- π‘ Usage Examples
- π Performance Results
- π¬ Technical Deep Dive
- π Indian Stock Market Support
β οΈ Risk Disclaimer- π€ Contributing
- π License
CodeTrading is a sophisticated algorithmic trading framework that combines:
- π§ AI-Powered Predictions: Transformer neural networks for forex price forecasting
- π Technical Analysis: VWAP breakout strategies with advanced risk management
- π¦ Indian Market Integration: Full support for NSE/BSE stocks via yfinance
- π± Interactive Analysis: Jupyter notebooks with comprehensive visualizations
- βοΈ Parameter Optimization: Automated strategy tuning and backtesting
Perfect for quantitative researchers, algorithmic traders, and financial AI enthusiasts looking to explore systematic trading approaches.
- Transformer Architecture for time series prediction
- Technical Indicators Integration (RSI, Bollinger Bands, MA Slope)
- Multi-timeframe Analysis capabilities
- Real-time Data Processing with automatic feature engineering
- VWAP Breakout Strategy with ATR-based risk management
- Intraday Position Management with automatic market close handling
- Parameter Optimization using grid search and Sharpe ratio maximization
- Comprehensive Performance Metrics (Calmar, Sortino, Win Rate)
- Forex Markets: EUR/USD and major currency pairs
- Indian Equities: NSE/BSE stocks with INR formatting
- US Markets: Stocks, ETFs, and indices
- Cryptocurrency: Bitcoin and major altcoins
- Interactive Jupyter Notebooks with professional reporting
- Automated Data Fetching via yfinance and CSV import
- Visual Analytics with matplotlib and bokeh integration
- Risk Management with drawdown control and position sizing
CodeTrading/
βββ π Transformer_Trading/ # AI-based forex prediction
β βββ π Transformer_Trading.ipynb # Main implementation notebook
β βββ π CLAUDE.md # Technical documentation
β βββ π logic.md # Strategy explanation
βββ π backtesting_Vwap_Trading/ # VWAP breakout strategy
β βββ π vwap_stocks.ipynb # Main backtesting notebook
β βββ π README.md # Strategy documentation
βββ π requirements.txt # Python dependencies
βββ π README.md # This file
git clone https://github.com/Mohit-5899/CodeTrading.git
cd CodeTradingpip install -r requirements.txtcd Transformer_Trading/
jupyter notebook Transformer_Trading.ipynbcd backtesting_Vwap_Trading/
jupyter notebook vwap_stocks.ipynb- Execute all cells for automatic data fetching and analysis
- View comprehensive performance reports
- Optimize parameters for your specific requirements
Strategy Overview:
- Uses 30 hours of historical OHLC data + technical indicators
- Predicts next 1-hour closing price using attention mechanisms
- Implements advanced feature engineering and sequential processing
Key Features:
- Architecture: 2-layer Transformer encoder with 8 attention heads
- Features: 9 dimensions including RSI, Bollinger Bands, MA Slope
- Training: 80/10/10 train/val/test split with MSE optimization
- Performance: Low MAE with visual prediction accuracy analysis
Use Cases:
- Short-term forex trading (1-hour predictions)
- Pattern recognition in currency markets
- Risk assessment for forex positions
Strategy Overview:
- Long when Close > VWAP AND Close > Day's Open
- Short when Close < VWAP AND Close < Day's Open
- ATR-based trailing stop loss with intraday position closure
Key Features:
- Risk Management: Configurable ATR multipliers (1.0-3.0)
- Indian Market Optimized: 0.1% commission, INR formatting
- Auto-Optimization: Grid search with Sharpe ratio maximization
- Real-time Data: yfinance integration for live Indian stock data
Use Cases:
- Intraday trading on liquid Indian stocks
- Mean reversion strategies with momentum confirmation
- Automated backtesting and parameter optimization
- Python 3.8+
- Jupyter Notebook
- Internet connection (for data fetching)
pandas>=1.3.0 # Data manipulation
numpy>=1.20.0 # Numerical computing
yfinance>=0.1.87 # Yahoo Finance data
backtesting>=0.3.3 # Backtesting framework
matplotlib>=3.5.0 # Plotting and visualization
torch>=1.12.0 # PyTorch for AI models
scikit-learn>=1.0.0 # ML utilities
ta>=0.10.0 # Technical analysis indicators# Option 1: pip install
pip install -r requirements.txt
# Option 2: conda install
conda install pandas numpy matplotlib scikit-learn
pip install yfinance backtesting torch ta# Load and prepare data
df = load_forex_data('EURUSD_data.csv')
df = add_technical_indicators(df)
# Create sequences
X, y = create_sequences(df, seq_length=30)
# Train model
model = TimeSeriesTransformer(input_dim=9, d_model=64)
train_model(model, X_train, y_train, epochs=20)
# Make predictions
predictions = model(X_test)
evaluate_model(y_test, predictions)# Fetch Indian stock data
df = fetch_indian_stock_data("RELIANCE.NS", period="1y", interval="15m")
# Add VWAP
df = add_vwap(df, time_col="Datetime")
# Backtest strategy
bt = Backtest(df, VWAPBreakout, cash=100000, commission=0.001)
stats = bt.run()
# Optimize parameters
best_params = bt.optimize(atr_stop=[1.0, 1.25, 1.5, 1.75, 2.0])# Test multiple Indian stocks
stocks = ["RELIANCE.NS", "TCS.NS", "HDFCBANK.NS", "INFY.NS"]
results = {}
for stock in stocks:
df = fetch_indian_stock_data(stock, period="6mo", interval="15m")
df = add_vwap(df, time_col="Datetime")
bt = Backtest(df, VWAPBreakout, cash=100000)
stats = bt.run()
results[stock] = stats["Return [%]"]
print("Performance Summary:", results)RELIANCE.NS - 15min Data (Sample Results)
| ATR Stop | Return (%) | Sharpe Ratio | Max DD (%) | Win Rate (%) |
|---|---|---|---|---|
| 1.00 | 45.2 | 0.89 | 12.3 | 58.4 |
| 1.25 | 52.7 | 0.94 | 11.8 | 61.2 |
| 1.50 | 48.9 | 0.91 | 13.1 | 59.7 |
Key Metrics:
- π Average Annual Return: 47.8%
- π Best Sharpe Ratio: 0.94
- π Maximum Drawdown: 11.8%
- π― Win Rate: 61.2%
EUR/USD Hourly Predictions
- π MSE: 0.0000847
- π MAE: 0.007234
- π― Directional Accuracy: 67.3%
- β±οΈ Prediction Horizon: 1 hour
class TimeSeriesTransformer(nn.Module):
def __init__(self, input_dim=9, d_model=64, nhead=8, num_layers=2):
super().__init__()
self.input_projection = nn.Linear(input_dim, d_model)
self.positional_encoding = nn.Parameter(torch.randn(1, 1000, d_model))
encoder_layer = nn.TransformerEncoderLayer(
d_model=d_model, nhead=nhead, dim_feedforward=256
)
self.transformer = nn.TransformerEncoder(encoder_layer, num_layers)
self.output_layer = nn.Linear(d_model, 1)Key Components:
- Input Projection: 9D β 64D feature mapping
- Positional Encoding: Learnable position embeddings
- Multi-Head Attention: 8 parallel attention mechanisms
- Feed Forward: 256-dimensional intermediate layer
def add_vwap(df, time_col="Datetime", price_col="Close", vol_col="Volume"):
# Typical Price: (High + Low + Close) / 3
tp = (df["High"] + df["Low"] + df["Close"]) / 3
# Daily grouping for VWAP reset
day = df.index.normalize()
# Cumulative volume and price-volume
cum_vol = df[vol_col].groupby(day).cumsum()
cum_pv = (tp * df[vol_col]).groupby(day).cumsum()
# VWAP = Cumulative (Price Γ Volume) / Cumulative Volume
df["VWAP"] = cum_pv / cum_vol
return dfclass VWAPBreakout(Strategy):
atr_stop = 1.5 # ATR multiplier for trailing stop
def next(self):
if self.position and self.atr_stop:
price = self.data.Close[-1]
atr = self.atr[-1]
trail = self.atr_stop * atr
# Dynamic trailing stop adjustment
for trade in self.trades:
if trade.is_long:
new_sl = price - trail
if trade.sl is None or new_sl > trade.sl:
trade.sl = new_sl- NSE (National Stock Exchange):
.NSsuffix - BSE (Bombay Stock Exchange):
.BOsuffix
| Symbol | Company | Sector |
|---|---|---|
RELIANCE.NS |
Reliance Industries | Energy/Petrochemicals |
TCS.NS |
Tata Consultancy Services | Information Technology |
HDFCBANK.NS |
HDFC Bank | Banking & Financial |
INFY.NS |
Infosys | Information Technology |
HINDUNILVR.NS |
Hindustan Unilever | FMCG |
ITC.NS |
ITC Limited | FMCG/Tobacco |
BAJFINANCE.NS |
Bajaj Finance | Non-Banking Finance |
MARUTI.NS |
Maruti Suzuki | Automotive |
- INR Currency Formatting: Portfolio values in Indian Rupees
- Commission Structure: 0.1% brokerage (typical Indian brokers)
- Market Hours: Adaptable for IST timezone
- Holiday Filtering: Automatic removal of non-trading periods
# Easy stock switching
stocks_to_test = [
("RELIANCE.NS", "Reliance Industries"),
("TCS.NS", "Tata Consultancy Services"),
("HDFCBANK.NS", "HDFC Bank")
]
for symbol, name in stocks_to_test:
print(f"Testing {name}...")
df = fetch_indian_stock_data(symbol, period="1y", interval="15m")
# ... run analysisπ¨ IMPORTANT NOTICE:
This project is designed for educational and research purposes only.
- Past Performance β Future Results: Historical backtests don't guarantee future profitability
- Market Risk: All trading involves risk of loss; never risk more than you can afford to lose
- Model Limitations: AI predictions are probabilistic, not certainties
- Data Dependencies: Strategy performance depends on data quality and market conditions
- Regulatory Compliance: Ensure compliance with local financial regulations
- Paper Trade First: Test strategies with virtual money before live trading
- Risk Management: Always use stop losses and position sizing
- Continuous Monitoring: Regularly re-optimize and validate strategies
- Professional Advice: Consult financial advisors for investment decisions
- Diversification: Never rely on a single strategy or asset
- Use for learning algorithmic trading concepts
- Validate strategies across different market conditions
- Understand the underlying mathematics and assumptions
- Keep detailed records of all trading activities
This is not financial advice. Trade responsibly.
We welcome contributions from the trading and AI community!
- π Bug Reports: Found an issue? Open a GitHub issue
- π‘ Feature Requests: Have ideas? We'd love to hear them
- π Documentation: Help improve our guides and examples
- π§ͺ Testing: Test strategies on different markets and timeframes
- π¬ Research: Add new indicators, strategies, or AI models
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
- Follow existing code style and documentation patterns
- Add tests for new features when possible
- Update documentation for any new functionality
- Ensure all notebooks run successfully
- Include performance metrics for new strategies
- π§ Issues: GitHub Issues
- π Documentation: Check individual project READMEs
- π‘ Discussions: GitHub Discussions for strategy ideas
- β Star the repository for updates
- π Watch for new releases and features
- π΄ Fork to customize for your needs
This project is licensed under the MIT License - see the LICENSE file for details.
- β Commercial use allowed
- β Modification allowed
- β Distribution allowed
- β Private use allowed
- β Liability not provided
- β Warranty not provided
- PyTorch - Deep learning framework
- yfinance - Financial data access
- backtesting.py - Backtesting framework
- TA-Lib - Technical analysis indicators
- Pandas - Data manipulation
- Quantitative finance research community
- Open source algorithmic trading projects
- Academic papers on transformer applications in finance
- Indian financial markets and their unique characteristics
- π GitHub: @Mohit-5899
- π§ Contact: Create an issue for questions
- π± Real-time Trading Integration with broker APIs
- π Multi-asset Portfolio Strategies
- π Advanced Risk Management modules
- π Web Dashboard for strategy monitoring
- π Additional AI Models (LSTM, GRU, Attention mechanisms)
- π¦ More Indian Market Features (sector analysis, derivatives)
- Sentiment Analysis integration with news data
- High-frequency Trading strategies
- Options Trading algorithms
- Cryptocurrency market strategies
- ESG Investing with AI screening
β Star this repository if you found it helpful! β
π Happy Trading! π
Built with β€οΈ for the algorithmic trading community