A comprehensive algorithmic trading system in Python that generates buy/sell signals based on market regimes and technical/volatility strategies, sending notifications directly to a Telegram bot.
- Data Fetching: Robust downloading and local caching of stock data using
yfinance. - Regime Detection: Combines Hidden Markov Models (HMM) and GARCH volatility forecasting to classify the market as
TRENDING,RANGING, orVOLATILE. - Strategies:
- Trend Following: EMA Crossover, SuperTrend, MACD, Price Breakout.
- Mean Reversion: RSI Extremes, Bollinger Bands, Z-Score Deviation.
- Volatility: GARCH Vol Forecast, ATR Expansion, Volatility Mean Reversion.
- Risk Management: Position sizing based on Kelly Criterion and fractional risk, along with ATR/trailing stop losses.
- Backtesting Engine: Vectorized backtesting for fast optimization and Walk-Forward Analysis (WFA) to ensure out-of-sample robustness.
- Signal Delivery: Asynchronous Telegram bot integration to receive daily summaries and actionable trade alerts.
-
Clone the repository (if applicable) and navigate to the directory:
cd algo-trading -
Install dependencies:
pip install -r requirements.txt
(Note:
TA-Libis required bypandas-tafor some indicators. You may need to install the TA-Lib C library on your system first. On Mac:brew install ta-lib) -
Configure Environment: Copy the example environment file and fill in your details:
cp .env.example .env
Edit
.envto add your Telegram bot token and chat ID.
- Open Telegram and search for
@BotFather. - Send
/newbotand follow the prompts to create a new bot. - Copy the bot token (e.g.,
123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11) and add it to your.envfile asTELEGRAM_BOT_TOKEN. - To get your Chat ID:
- Send any message to your new bot.
- Visit:
https://api.telegram.org/bot<YOUR_TOKEN>/getUpdates - Find
"chat":{"id": YOUR_CHAT_ID}in the JSON response. - Add it to your
.envfile asTELEGRAM_CHAT_ID.
The system is controlled via the main.py CLI.
1. Fetch Data Fetch historical data for the US market (or specify tickers):
python main.py fetch
python main.py fetch --market ID
python main.py fetch --tickers AAPL MSFT TSLA2. Detect Regime Analyze the current market regime for a specific stock:
python main.py regime --ticker AAPL3. Generate Signals Run the full pipeline (data -> regime -> strategy -> risk) and generate signals based on your risk profile:
python main.py signal --profile aggressive
python main.py signal --profile conservative --notify(Use --notify to send the generated signals to your configured Telegram bot).
A set of Jupyter notebooks is provided in the exploration/ directory to help you understand the components, visualize data, build strategies, and run detailed backtests.
config/: System settings, risk profiles, and universes.data/: Data fetching and caching layer.indicators/: Technical and fundamental indicators usingpandas-ta.regime/: Statistical tests, HMM, and GARCH models.strategies/: Implementation of all trading strategies.risk/: Position sizing, stop loss, and portfolio tracking.backtesting/: Backtest engine, WFA, and metrics.signals/: Signal orchestrator and Telegram bot delivery.exploration/: Jupyter notebooks for research.