An automated trading system that implements technical analysis and machine learning strategies on NIFTY 50 stocks. The system features real-time data processing, backtesting, automated trade execution, and integration with Google Sheets for reporting.
algo-trading-assignment/
β
βββ π src/ # Main source code
β βββ π data/ # Data handling modules
β β βββ __init__.py
β β βββ data_fetcher.py # API connections & data fetching
β β βββ data_processor.py # Data cleaning & preprocessing
β β βββ indicators.py # Technical indicators (RSI, MA, MACD)
β β
β βββ π strategy/ # Trading strategy logic
β β βββ __init__.py
β β βββ base_strategy.py # Abstract base strategy class
β β βββ rsi_ma_strategy.py # RSI + Moving Average strategy
β β βββ backtester.py # Backtesting engine
β β
β βββ π ml/ # Machine Learning components
β β βββ __init__.py
β β βββ feature_engineer.py # Feature creation & selection
β β βββ model_trainer.py # ML model training
β β βββ predictor.py # Prediction engine
β β
β βββ π automation/ # Automation & integration
β β βββ __init__.py
β β βββ sheets_manager.py # Google Sheets operations
β β βββ telegram_bot.py # Telegram notifications (bonus)
β β βββ scheduler.py # Task scheduling
β β
β βββ π utils/ # Utility functions
β βββ __init__.py
β βββ logger.py # Logging configuration
β βββ config.py # Configuration management
β βββ helpers.py # Common helper functions
β
βββ π config/ # Configuration files
β βββ config.yaml # Main configuration
β βββ api_keys.env.example # Environment variables template
β βββ stocks_list.json # NIFTY 50 stocks configuration
β
βββ π data/ # Data storage
β βββ raw/ # Raw data from APIs
β βββ processed/ # Cleaned/processed data
β βββ backtest_results/ # Backtesting outputs
β
βββ π models/ # Saved ML models
β βββ decision_tree_model.pkl
β βββ model_metrics.json
β
βββ π logs/ # Log files
β βββ trading.log
β βββ errors.log
β βββ api_calls.log
β
βββ π notebooks/ # Jupyter notebooks (optional)
β βββ data_exploration.ipynb
β βββ strategy_analysis.ipynb
β βββ ml_model_development.ipynb
β
βββ π tests/ # Unit tests
β βββ __init__.py
β βββ test_data_fetcher.py
β βββ test_strategy.py
β βββ test_ml_model.py
β
βββ main.py # Main execution script
βββ requirements.txt # Python dependencies
βββ README.md
βββ .gitignore # Git ignore rules
- Multi-source Data Integration: Real-time and historical data from Alpha Vantage and Yahoo Finance
- Technical Indicators: RSI, Moving Averages, MACD, Bollinger Bands
- Strategy Engine: Customizable trading strategies with rule-based logic
- Backtesting Framework: Historical strategy performance evaluation
- Machine Learning Integration: Predictive models for trade signal enhancement
- Automation: Scheduled execution and reporting
- Google Sheets Integration: Real-time trade logging and performance tracking
- Telegram Notifications: Instant alerts for trade signals and system events
- Python 3.9+
- 8GB RAM (recommended)
- 1GB free disk space
- Google Cloud Platform account (for Sheets API)
- Alpha Vantage API key (free tier available)
# Clone repository
git clone https://github.com/fa-code2/algo-trading-ml-assignment
cd algo-trading-assignment
# Create virtual environment
python -m venv trading-env
source trading-env/bin/activate # Linux/Mac
trading-env\Scripts\activate # Windows
# Install dependencies
pip install -r requirements.txt-
Set up configuration files:
cp config/config.yaml.example config/config.yaml cp config/api_keys.env.example config/api_keys.env
-
Edit configuration files:
config/config.yaml: Add your Google Sheet URL and trading parametersconfig/api_keys.env: Add your Alpha Vantage API key and Telegram bot token
-
Google Sheets API Setup:
- Enable Google Sheets API in Google Cloud Console
- Download credentials JSON file and save as
config/credentials.json
=============================================================
TRADING SYSTEM EXECUTION SUMMARY
=============================================================
Data Fetched for 3 stocks:
β’ RELIANCE.NS
β’ TCS.NS
β’ INFY.NS
Backtest Results:
β’ RELIANCE.NS: 12.34% return, 65.2% win rate
β’ TCS.NS: 8.76% return, 58.1% win rate
β’ INFY.NS: 15.23% return, 71.4% win rate
ML Predictions (Accuracy):
β’ RELIANCE.NS: 67.8%
β’ TCS.NS: 62.3%
β’ INFY.NS: 69.1%
Current Signals:
β’ RELIANCE.NS: BUY at βΉ2,456.75
β’ TCS.NS: No active signals
Google Sheets updated with all results
=================================
python main.py --mode backtest --strategy rsi_ma --symbols TCS,INFY --from 2023-01-01 --to 2023-12-31python main.py --mode live --strategy ml_enhanced --symbols NIFTY50python main.py --mode signals --strategy rsi_ma --symbols RELIANCE| Option | Description | Default |
|---|---|---|
--mode |
Operation mode (backtest, live, etc.) | backtest |
--strategy |
Trading strategy to use | rsi_ma |
--symbols |
Comma-separated stock symbols | NIFTY50 |
--from |
Start date (YYYY-MM-DD) | 30 days ago |
--to |
End date (YYYY-MM-DD) | Current date |
--output |
Output format (console, gsheets) | console |
- Feature Engineering:
- Technical indicators (RSI, MACD, Bollinger Bands)
- Price derivatives
- Volume analysis
- Statistical features
The system automatically logs all trades to Google Sheets with the following structure:
| Timestamp | Symbol | Action | Price | Quantity | Strategy | Confidence | Indicators |
|---|---|---|---|---|---|---|---|
| 2023-08-04 10:25 | TCS.NS | BUY | 3450 | 10 | ML | 0.87 | RSI=28, MA_CROSS |
| 2023-08-04 14:30 | RELIANCE.NS | SELL | 1450 | 15 | RSI_MA | - | RSI=72, MA_CROSS |
Run the test suite with:
#Run Test to check Full System
python test_system.py
Test Coverage:
- Data fetching and processing
- Indicator calculations
- Strategy logic
- Backtesting engine
- Google Sheets integration
- Sensitive configuration excluded via
.gitignore - API keys stored in environment variables
- Encrypted credentials handling
- Pre-commit hooks for secret detection
- Limited permission Google Cloud credentials
- Fork the repository
- Create a feature branch (
git checkout -b feature/your-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin feature/your-feature) - Open a pull request
This algorithmic trading system was developed as part of a 2nd round internship test assignment by H TO H. We acknowledge their comprehensive requirements and evaluation criteria that guided the development of this full-featured trading platform. Special thanks to:
H TO H for providing the internship opportunity and detailed project specifications The evaluation team for setting challenging requirements that pushed for a robust solution The open-source community for the excellent libraries that made this system possible Financial data providers for making market data accessible for testing and development
This software is for educational and research purposes only. Trading stocks involves risk, and past performance does not guarantee future results. The authors are not responsible for any financial losses incurred through the use of this software. Always consult with a financial advisor before making investment decisions.
This project is licensed under the MIT License
For inquiries or support:
- Email: faaa367810@gmail.com
- LinkedIn: linkedin.com/in/fa-aslam/