Skip to content

fortunatoman/Trading-AI-Bot

Repository files navigation

Trading AI Bot

Python trading bot that combines technical analysis, fundamental analysis, and sentiment analysis with AI/ML models, and can trade via MetaTrader 5. It includes a web dashboard for backtest results and trading history, and supports training models, generating history, and running in demo or real mode.

Features

  • Technical analysis — Multiple timeframes, 20+ indicators (ta), pattern and volume analysis
  • Fundamental analysis — Macro indicators, rates, GDP, inflation, employment, trade balance (optional APIs)
  • Sentiment analysis — News/text sentiment (e.g. TextBlob)
  • AI/ML models — Technical, fundamental, sentiment, and neural models (scikit-learn, TensorFlow, PyTorch); model manager for train/validate/load
  • Risk management — Position sizing, stop-loss, take-profit, max daily loss, correlation limits
  • MetaTrader 5 — Broker integration for live/demo trading (MT5 terminal required on host)
  • Web dashboard — aiohttp server: main page, backtest page, API for trading history and backtest results
  • Backtesting — Backtester using historical data and model predictions; equity curve and monthly returns

Tech Stack

  • Python 3.8+
  • Data/ML: numpy, pandas, scikit-learn, TensorFlow, Keras, PyTorch, joblib, ta, yfinance
  • Trading: MetaTrader5
  • Web: aiohttp
  • Other: python-dotenv, loguru, plotly, matplotlib, beautifulsoup4, requests, textblob, tqdm

Project Structure

Trading-AI-Bot/
├── main.py                    # Entrypoint: learn | history | web
├── trading_history_generator.py
├── requirements.txt
├── config/
│   ├── settings.py            # Config from env (.env)
│   └── logging_config.py
├── models/                    # AI models
│   ├── model_manager.py
│   ├── technical_model.py
│   ├── fundamental_model.py
│   ├── sentiment_model.py
│   └── neural_model.py
├── analysis/
│   ├── analyzer.py
│   ├── technical.py
│   ├── fundamental.py
│   └── sentiment.py
├── trading/
│   ├── broker.py              # MT5 broker
│   ├── strategy.py
│   ├── risk_manager.py
│   └── backtest.py
├── utils/
│   ├── data_loader.py         # MT5 + data loading
│   └── preprocessing.py
├── frontend/
│   ├── index.html
│   ├── backtest.html
│   └── static/
├── data/
│   ├── history/               # trading_history.json
│   └── models/                # saved .joblib models
└── logs/

Configuration

Settings are read from environment variables (and .env via python-dotenv). Do not commit real credentials.

Variable Description Example
MT5_LOGIN MetaTrader 5 account ID 12345678
MT5_PASSWORD MT5 password
MT5_SERVER MT5 server name XMGlobal-MT5 7
MODE demo or real demo
SYMBOL Instrument EURUSD
TIMEFRAME M1, M5, M15, M30, H1, H4, D1, W1, MN1 H1
LOT_SIZE Default lot size 0.01
MAX_DAILY_LOSS Max daily loss (e.g. -2.0 for -2%) -2.0
MAX_POSITION_SIZE Max position size 0.1
ECONOMIC_API_KEY Optional economic data API
NEWS_API_KEY Optional news API
WEB_HOST Web server bind address (Docker: use 0.0.0.0) localhost
WEB_PORT Web server port 8080

Copy .env.example to .env and fill in your values.

Running locally (without Docker)

Requirements

  • Python 3.8+
  • MetaTrader 5 terminal (for live data and trading)
  • 8GB+ RAM recommended

Setup

git clone <repo-url>
cd Trading-AI-Bot
python -m venv venv
# Windows: venv\Scripts\activate
# Linux/macOS: source venv/bin/activate
pip install -r requirements.txt
cp .env.example .env
# Edit .env with your MT5 and API keys

Commands

Command Description
python main.py learn Train and validate all models (uses MT5 for data)
python main.py history Generate trading history JSON (data/history/trading_history.json)
python main.py web Start web server (dashboard + backtest UI + APIs)

Web server:

  • Main page: http://localhost:8080/
  • Backtest: http://localhost:8080/backtest
  • APIs: GET /api/trading-history, GET /api/backtest-results

For real trading mode, set MODE=real in .env (or export before running). Training and history generation require MT5 to be running and connected.


Running with Docker

You can run the bot in Docker. The web server is the main use case in a container; MT5 does not run inside the image (it is a desktop app), so learn and history need MT5 data when run locally or with MT5 available elsewhere.

Build and run (web only)

# Build
docker build -t trading-ai-bot .

# Run web server (listens on 0.0.0.0:8080 inside container)
docker run -p 8080:8080 --env-file .env trading-ai-bot

Then open http://localhost:8080 in your browser.

Using docker-compose

# Start (builds if needed, uses .env)
docker compose up -d

# Logs
docker compose logs -f

# Stop
docker compose down

The compose file runs python main.py web with:

  • Port 8080 mapped to the host
  • WEB_HOST=0.0.0.0 so the server is reachable from the host
  • Env vars from .env
  • Optional volume for logs/ to persist logs

Run other commands in Docker

# Train models (requires MT5; usually run on host or with MT5 data mounted)
docker run --rm --env-file .env trading-ai-bot python main.py learn

# Generate history (same note as above)
docker run --rm --env-file .env trading-ai-bot python main.py history

For training or history inside Docker you would need to provide data or an MT5 gateway; typically learn and history are run on the host where MT5 is installed.

Environment file

Create .env from the example (do not commit real secrets):

cp .env.example .env

Edit .env with your MT5 credentials, optional API keys, and any overrides (e.g. MODE, SYMBOL, WEB_HOST for Docker).

Tests

Run tests (if present):

pytest tests/

Manual checks: test_connection.py, test_data_loader.py, test_mt5.py in the project root.

Disclaimer

Trading involves substantial risk. This bot is for educational and research purposes. Use at your own risk; the authors are not responsible for any financial losses.

License

See LICENSE.


Last updated: 2026-03-18

About

R-DEV Trading AI is an advanced automated trading bot integrated with MetaTrader 5. It combines technical, fundamental, and sentiment analysis, uses deep learning and ML for signal generation, updates in real time, and applies advanced risk management for intelligent trade execution.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors