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:
- uses 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. 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 applying statistical models or non-parametric algorithmic models.
- 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
These options allow the user to compare the performance of different approaches within the selected market.
-
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 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 zero return. On the other hand, strictly following the machine learning strategy with Random Forest Regressor would produce above 160% return over the same period, excluding any trasactions fees. Furthermore, short selling operations are being considered by default in calculations, though they can easily be disabled in the backtest.
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:
- more machine learning methods; ✅
- statistical methods (ARIMA); ✅
- improve objective function with new weights and presets;
- use optimizer for the objective function.
This repository is independently maintained, only in free time. If you find the code useful and wish to support its continued development, consider donating:
Your support helps keep the project alive and evolving, by adding new indicators, improvements, and documentation.

