A professional-grade C++ trading bot for backtesting and simulating trading strategies on Nifty 50 index data with comprehensive risk management and performance analytics.
- Market Data Handler: Robust CSV parsing with validation and error handling
- Advanced Trading Strategy: Moving Average Crossover with RSI filter and risk controls
- Risk Management:
- Dynamic position sizing based on account balance
- Maximum exposure limits
- Stop-loss and take-profit orders
- Broker Simulator:
- Realistic order execution
- Portfolio tracking with unrealized P/L
- Average entry price calculation for multiple buys
- Comprehensive Trade Logging:
- Detailed trade history with reasons
- Advanced performance metrics (Sharpe Ratio, Max Drawdown, Profit Factor)
- CSV export for analysis
- Dual Mode Operation:
- Backtest on historical data
- Live simulation with synthetic market data
- Total/Winning/Losing trades
- Win rate percentage
- Average win/loss amounts
- Largest win/loss
- Profit factor (gross profit / gross loss)
- Maximum drawdown percentage
- Sharpe ratio (risk-adjusted returns)
nifty50-trading-bot/
βββ bot/ # Source files
β βββ main.cpp # Main entry point with TradingEngine
β βββ Config.cpp/h # Configuration management with validation
β βββ MarketDataHandler.cpp/h # CSV data loading and live data generation
β βββ Strategy.cpp/h # Trading strategy with MA crossover and RSI
β βββ RiskManager.cpp/h # Position sizing and risk controls
β βββ BrokerSimulator.cpp/h # Order execution and portfolio management
β βββ TradeLogger.cpp/h # Trade logging and statistics
β βββ Types.h # Common data structures and enums
βββ data/
β βββ nifty50_data.csv # Historical OHLCV data
βββ logs/ # Generated log files
β βββ trades.csv # Backtest trade log
β βββ live_trades.csv # Live simulation trade log
βββ config.txt # Configuration file
βββ README.md
βββ .gitignore
- C++17 compatible compiler (g++, clang, MSVC)
- Standard C++ library with threading support
Windows (PowerShell/CMD):
cd bot
g++ -std=c++17 -O2 *.cpp -o trading_bot.exe
.\trading_bot.exe # Run backtest (default)
.\trading_bot.exe --live # Run live simulationLinux / macOS:
cd bot
g++ -std=c++17 -O2 *.cpp -o trading_bot
./trading_bot # Run backtest (default)
./trading_bot --live # Run live simulation./trading_bot # Use mode from config.txt
./trading_bot --backtest # Force backtest mode
./trading_bot --live # Force live simulation mode
./trading_bot --config custom.txt # Use custom config fileFor production use, compile with optimizations:
g++ -std=c++17 -O3 -march=native -DNDEBUG *.cpp -o trading_botEdit config.txt to customize trading parameters:
initial_balance=100000 # Starting capital in INR
risk_percentage=10.0 # Capital allocation per trade (%)
max_exposure=80.0 # Max portfolio exposure (%)
stop_loss_percentage=5.0 # Stop loss threshold (%)
take_profit_percentage=15.0 # Take profit target (%)short_ma_period=20 # Short-term MA period (days)
long_ma_period=50 # Long-term MA period (days)
rsi_period=14 # RSI calculation period (days)mode=backtest # backtest or livelive_iterations=100 # Number of ticks to simulate
live_history_size=200 # Historical data points for context
live_delay_ms=500 # Delay between ticks (ms)CSV file must have the following format:
Date,Open,High,Low,Close,Volume
2024-01-01,21727.75,21834.35,21680.85,21741.90,154000000
2024-01-02,21751.35,21755.60,21555.65,21665.80,263710000Data Requirements:
- Header row required
- Date format: YYYY-MM-DD (or any consistent format)
- Prices: Decimal numbers
- Volume: Integer
- OHLC validation: High β₯ Low, High β₯ Open, High β₯ Close, Low β€ Open, Low β€ Close
BUY Signal:
- Short MA crosses above Long MA (Golden Cross)
- RSI < 70 (not overbought)
- No existing position
SELL Signal:
- Short MA crosses below Long MA (Death Cross)
- RSI > 30 (not oversold)
- Has existing position
Risk Management Exits:
- Stop Loss: Position closed if loss exceeds configured percentage
- Take Profit: Position closed if profit reaches target percentage
HOLD:
- No crossover detected
- RSI in extreme zones
- Exposure limits reached
- β Corrected risk management calculation (was allocating 40% per trade!)
- β Fixed look-ahead bias in backtesting
- β Improved CSV parsing with proper error handling
- β Fixed position management edge cases
- β Stop-loss and take-profit orders
- β Better crossover detection (handles exact crosses)
- β Detailed trade reasons in logs
- β Unrealized P/L tracking for open positions
- β Advanced performance metrics (Sharpe, Drawdown, Profit Factor)
- β Comprehensive error handling with try-catch blocks
- β Configuration validation
- β Unified TradingEngine class (DRY principle)
- β Better data validation
- β Improved random number generation for live simulation
- β Detailed progress reporting
- β Clear trade execution logs with reasons
- β Professional summary statistics
- β Better configuration with comments
- β Command-line argument support
[2024-03-18] SELL executed: 2 shares @ INR 22055.70 | Total: INR 44111.40 |
P/L: INR 568.00 (1.30%) | Balance: INR 100568.00 |
Reason: Death Cross detected (MA20=21890.50 < MA50=21920.30, RSI=62.50)
---
**Disclaimer**: This software is for educational and research purposes only. It should not be used for actual trading without proper testing, risk assessment, and professional financial advice.