Skip to content

gkeiel/market_forecaster

Repository files navigation

Market Forecaster

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

📊 Features

  • 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 .env for private environment variables, .json for tickers list, .jsonfor indicators list, and .csv for strategies list.

📈 Available Strategies

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

🧩 Structure

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  

⚙️ How to Use

  1. 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
  2. Configure tickers and indicators

    • In config.json add the various configuration parameters.
    • In tickers.json add the stock codes to analyze.
    • In indicators.json add the indicators to generate.
    • In strategies.csv list the stocks to generate trading signals, each with its corresponding best strategy.
  3. 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 .env file to be read by market_forecaster.py.
  4. 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 TOKEN and CHAT_ID for the preconfigured workflow.

🖼️ Output Examples

  • Backtest chart with Random Forest

    After running market_forecaster.py it generates strategy charts, spreadsheets for each ticker, and a summary with results sorted by best. The generated figures follow the example below:

    Optimization Optimization

    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:

🧩 Project Structure

  • 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.

📌 Notes

⚠️ We are not responsible for any financial losses resulting from the use of the strategies or signals generated by this code.

  • 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.

🚀 Premium Version

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.

About

A Python tool for trading signals on global markets based on machine learning

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages