Skip to content

About

Forex LSTM Predictor using AI, Machine Learning, Python

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Forex LSTM Predictor - Multi-pair (EUR/USD, etc.)

Fetches real historical Forex data using yfinance, trains an LSTM model (TensorFlow) for next-step close prediction, supports walk-forward backtesting with RMSE/MAPE metrics, and includes a Streamlit dashboard for visualization and interactive predictions. All code is local — run scripts to download data and train models. No credentials required for yfinance (uses Yahoo Finance symbols like 'EURUSD=X', 'GBPUSD=X', etc.).

Quick start

  1. Create and activate a virtual environment (recommended):
python -m venv venv
# Linux / macOS
source venv/bin/activate
# Windows (PowerShell)
venv\Scripts\Activate.ps1
  1. Install dependencies:
pip install -r requirements.txt
  1. Fetch data for a currency pair (example: EURUSD):
python backend/data/fetch_data.py --pair EURUSD --period 5y --interval 1d
# This saves CSV to backend/data/EURUSD_1d.csv
  1. Train LSTM model for a pair:
python backend/model/train_lstm.py --pair EURUSD --epochs 20 --window 30
# saves model to backend/model/<pair>_lstm/
  1. Run backtest notebook:
jupyter notebook backend/notebooks/forex_backtest.ipynb
  1. Run Streamlit dashboard:
streamlit run frontend/streamlit_app.py

Technical Description

Data Pipeline

  • Historical Forex data is downloaded using yfinance, supporting daily (1d) and hourly (1h) intervals.
  • Data stored as CSV in backend/data/PAIR_INTERVAL.csv format (e.g., EURUSD_1d.csv).

Model Architecture (LSTM)

  • Uses a Single-Layer LSTM neural network with:
    • Input shape: (window_size, 1)
    • Hidden units: 64
    • Dropout: 0.2
    • Output: 1 (next-day close)
  • Trained with Mean Squared Error (MSE) loss and Adam optimizer.
  • Scaler (MinMaxScaler) stored for consistent feature scaling during inference.

Walk-Forward Backtesting (Notebook)

  • Implements stepwise walk-forward validation.
  • Re-trains the model incrementally with expanding window.
  • Evaluates RMSE and MAPE on out-of-sample predictions.

Backtrader Integration

  • Implements a trading simulation using Backtrader framework (backend/model/backtest_backtrader.py).
  • Strategy logic:
    • Predict next close each day using trained LSTM.
    • If predicted close > current close → enter long position
    • If predicted close < current close → enter short position
    • Close position when prediction reverses.
  • Plots equity curve and computes final portfolio value.

Visualization (Streamlit Dashboard)

  • Displays recent price charts, last model predictions, and basic analytics.
  • Supports user input for currency pair and window size.
  • Provides a real-time prediction button for quick inference.

Performance Metrics

  • RMSE (Root Mean Square Error): Measures average prediction error.
  • MAPE (Mean Absolute Percentage Error): Relative accuracy metric for forecast magnitude.

Future Enhancements

  • Add ensemble forecasting (ARIMA + LSTM hybrid).
  • Integrate sentiment data (Twitter/Reddit) for macro signals.
  • Deploy on web with FastAPI backend for automated predictions.

Automated Performance Reports

  • Run python backend/model/backtest_report.py --pair EURUSD --cash 10000 --window 30
  • Outputs saved under reports/ as HTML (with charts), CSV (trades), and metrics CSV/JSON.

About

Forex LSTM Predictor using AI, Machine Learning, Python

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages