Skip to content

Mohit-5899/CodeTrading

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

CodeTrading πŸ€–πŸ“ˆ

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.

Python License Contributions


πŸ“‹ Table of Contents


🎯 Project Overview

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.


πŸš€ Features

πŸ€– AI & Machine Learning

  • 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

πŸ“Š Traditional Strategies

  • 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)

🌐 Market Coverage

  • 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

πŸ› οΈ Technical Infrastructure

  • 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

πŸ“ Project Structure

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

⚑ Quick Start

1. Clone the Repository

git clone https://github.com/Mohit-5899/CodeTrading.git
cd CodeTrading

2. Install Dependencies

pip install -r requirements.txt

3. Choose Your Strategy

πŸ€– AI Forex Prediction

cd Transformer_Trading/
jupyter notebook Transformer_Trading.ipynb

πŸ“Š VWAP Indian Stocks

cd backtesting_Vwap_Trading/
jupyter notebook vwap_stocks.ipynb

4. Run and Analyze

  • Execute all cells for automatic data fetching and analysis
  • View comprehensive performance reports
  • Optimize parameters for your specific requirements

πŸ“Š Trading Strategies

🧠 Transformer-Based Forex Prediction

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

πŸ“ˆ VWAP Breakout Strategy

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

πŸ› οΈ Installation

Prerequisites

  • Python 3.8+
  • Jupyter Notebook
  • Internet connection (for data fetching)

Dependencies

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

Installation Commands

# 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

πŸ’‘ Usage Examples

πŸ€– AI Forex Prediction Example

# 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)

πŸ“Š Indian Stock VWAP Example

# 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])

πŸ”„ Multi-Stock Analysis

# 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)

πŸ“ˆ Performance Results

πŸ† VWAP Strategy (Indian Markets)

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%

🧠 Transformer Model (Forex)

EUR/USD Hourly Predictions

  • πŸ“Š MSE: 0.0000847
  • πŸ“ MAE: 0.007234
  • 🎯 Directional Accuracy: 67.3%
  • ⏱️ Prediction Horizon: 1 hour

πŸ”¬ Technical Deep Dive

🧠 Transformer Architecture

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

πŸ“Š VWAP Calculation

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 df

βš™οΈ Risk Management

class 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

🌍 Indian Stock Market Support

πŸ“‹ Supported Exchanges

  • NSE (National Stock Exchange): .NS suffix
  • BSE (Bombay Stock Exchange): .BO suffix

🏒 Popular Stocks Included

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

πŸ’Ή Market-Specific Features

  • 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

πŸ”„ Quick Stock Switch

# 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

⚠️ Risk Disclaimer

🚨 IMPORTANT NOTICE:

This project is designed for educational and research purposes only.

⚠️ Key Risk Factors:

  • 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

πŸ“‹ Best Practices:

  1. Paper Trade First: Test strategies with virtual money before live trading
  2. Risk Management: Always use stop losses and position sizing
  3. Continuous Monitoring: Regularly re-optimize and validate strategies
  4. Professional Advice: Consult financial advisors for investment decisions
  5. Diversification: Never rely on a single strategy or asset

πŸ”’ Responsible Usage:

  • 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.


🀝 Contributing

We welcome contributions from the trading and AI community!

🎯 Ways to Contribute:

  • πŸ› 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

πŸ”„ Development Process:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“‹ Contribution Guidelines:

  • 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

πŸ“ž Support & Community

πŸ’¬ Get Help:

  • πŸ“§ Issues: GitHub Issues
  • πŸ“– Documentation: Check individual project READMEs
  • πŸ’‘ Discussions: GitHub Discussions for strategy ideas

πŸ”” Stay Updated:

  • ⭐ Star the repository for updates
  • πŸ‘€ Watch for new releases and features
  • 🍴 Fork to customize for your needs

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ“‹ MIT License Summary:

  • βœ… Commercial use allowed
  • βœ… Modification allowed
  • βœ… Distribution allowed
  • βœ… Private use allowed
  • ❌ Liability not provided
  • ❌ Warranty not provided

πŸ™ Acknowledgments

πŸ“š Built With:

🎯 Inspired By:

  • Quantitative finance research community
  • Open source algorithmic trading projects
  • Academic papers on transformer applications in finance
  • Indian financial markets and their unique characteristics

πŸ‘¨β€πŸ’» Created By:

Mohit Mandawat

  • πŸ”— GitHub: @Mohit-5899
  • πŸ“§ Contact: Create an issue for questions

πŸš€ Future Roadmap

🎯 Upcoming Features:

  • πŸ“± 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)

πŸ”¬ Research Areas:

  • 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

About

Trading with code - (LLMs, Algos)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •