This project provides a Python tool for backtesting and selecting the best-performing strategies to spot market time series, applying only machine learning algorithms.
As main advantages, the project provides:
- use of supervised machine learning to generate trading signals.
- recurring trading signals via Telegram channel.
- open-source code, allowing flexibility in specifying machine learning models and comparing all strategies.
Telegram open channel with daily signals run via GitHub Actions, which anyone can sign up to get a feel for what this bot can offer: t.me/market_forecasting_public
- Data download: Downloads market data through the Yahoo Finance API.
- Machine-learning-based indicators: Implements machine-learning-based strategies to identify possible trends.
- Strategy backtesting: Runs historical backtests, generating figures and summaries for decision-making.
- Performance evaluation: Assesses results using a weighted objective function and ranks the best strategies.
- Predict future prices: Make predictions based on supervised machine learning, applying decision tree algorithms.
- Telegram notifications: Sends trading signals from the selected strategy directly to user smartphone or computer.
- Configuration files: Uses
.envfor private environment variables,.jsonfor tickers list,.jsonfor indicators list, and.csvfor strategies list.
The project currently supports supervisioned machine learning strategies for generating trading signals, using the following methods:
- Linear Regression
- Decision Trees Regressor
- Random Forest Regressor
- Gradient Boosting Regressor
- Extra Trees Regressor
- K Neighbors Regressor
- Autoregressive Integrated Moving Average (ARIMA) models
The project is organized around a modular architecture, where each class has a responsibility:
- Loader manages the market configuration files.
- Indicator generates machine-learning-based indicators.
- Forecaster generates price forecasts.
- Backtester runs trading signals on historical data and calculates performance metrics.
- Strategies generates scores and ranks strategies based on a configurable objective function.
- Exporter exports results to spreadsheets.
- Notifier sends notifications via applications.
The project has the following structure:
market_forecaster/
│
├── market_forecaster.py
├── market_forecaster_bot.py
|
├── core/
│ ├── __init__.py
│ ├── loader.py
│ ├── indicator.py
│ ├── forecaster.py
│ ├── backtester.py
│ ├── strategies.py
│ ├── exporter.py
│ └── notifier.py
│
├── config/
│ ├── config.json
│ ├── tickers.json
│ └── indicators.json
│
├── data/
│ ├── debug/
│ ├── report/
│ └── results/
| ├── best_results.xlsx
│ ├── strategies.csv
│ └── backtests.png
│
├── images/
├── requirements.txt
├── README.md
└── LICENSE
-
Install dependencies:
pip install pandas pip install numpy pip install yfinance pip install requests pip install python-dotenv pip install scikit-learn pip install statsmodels
-
Configure tickers and indicators
- In
config.jsonadd the various configuration parameters. - In
tickers.jsonadd the stock codes to analyze. - In
indicators.jsonadd the indicators to generate. - In
strategies.csvlist the stocks to generate trading signals, each with its corresponding best strategy.
- In
-
Configure Telegram
- Create a Telegram bot and obtain its
TOKEN. - Create a Telegram channel and obtain its
CHAT_ID. - Add the bot as channel administrator.
- Add keys to
.envfile to be read bymarket_forecaster.py.
- Create a Telegram bot and obtain its
-
Run the script
- To run the batch of backtests, execute:
python market_forecaster.py
- To generate recurrent trading signals and notifications for each ticker, execute:
python market_forecaster_bot.py
- To automate the signal generation with GitHub Actions, create the repository secrets
TOKENandCHAT_IDfor the preconfigured workflow.
- To run the batch of backtests, execute:
-
Backtest chart with Random Forest
After running
market_forecaster.pyit generates strategy charts, spreadsheets for each ticker, and a summary with results sorted by best. The generated figures follow the example below:Notice that the asset ends the evaluated period near its initial price, so a Buy & Hold strategy would yield approximately a 105% return. On the other hand, strictly following the machine learning strategy with Gradient Boosting would produce above 160% return over the same period, excluding any transactions fees. Furthermore, short selling operations are ignored by default in calculations assuming there exist borrowing fees involved, though they can easily be enabled in the backtest.
-
Trading signals via Telegram
After running
market_forecaster_bot.py, it generates trading signals for the selected (best) strategies, as the example below:
market_forecaster.py→ Main file for backtesting and selecting the best strategies.tickers.json→ List of tickers to analyze.indicators.json→ List of machine-learning-based indicators to test.strategies.csv→ List of selected strategies for trading signals, including tickers and their indicators.
- Contributions are welcome! Open an issue or submit a pull request.
- Future improvements and new features may be added, including:
- improve objective function with new weights and presets;
- more machine learning methods; ✅
- statistical methods (ARIMA); ✅
- WhatsApp notifications (Premium); ✅
- e-mail reports (Premium); ✅
- use optimizer for the objective function.
The Premium version unlocks advanced features for professional automation and notifications. See the comparison:
| Feature | Free | Premium |
|---|---|---|
| Backtest | ✅ | ✅ |
| Strategy evaluation | ✅ | ✅ |
| Telegram notifications | ✅ | ✅ |
| WhatsApp notifications | ❌ | ✅ |
| Gmail report | ❌ | ✅ |
If this project helped you, consider purchasing the Premium version: currently unavailable.


