← all repositories
AminHP/gym-mtsim

Your RL bot can now blow up a demo account realistically

A Gym environment that wraps MetaTrader 5's actual mechanics—margin calls, hedging, and all—so your trading agent learns on broker-grade simulation instead of toy data.

519 stars Python Domain AppsAgents
gym-mtsim
Velocity · 7d
+0.3
★ / day
Trend
steady
star history

What it does

gym-mtsim is a simulator for MetaTrader 5 paired with an OpenAI Gym environment. It lets reinforcement learning agents trade Forex, stocks, or crypto using the same concepts real brokers use: leverage, margin levels, stop-outs, and hedged or unhedged positions. The simulator can run standalone for backtesting, or inside the Gym loop for RL training.

The interesting bit

Most trading environments abstract away the ugly parts of real markets. This one doesn’t. It models the bid-ask spread as a configurable fee, tracks free margin and margin level per tick, and will liquidate your agent’s positions automatically if equity drops too far. The action space is deliberately warped—using logit transforms—to squeeze multi-symbol, multi-order control into the flat vectors that stable-baselines3 expects.

Key highlights

  • Downloads historical data directly from MetaTrader 5 (Windows only; the MT5 Python package has no Linux build)
  • Supports hedged accounts with multiple open positions per symbol
  • Observation window includes balance, equity, margin, price features, and live order state
  • Three render modes: human-readable, simple figure, and advanced figure
  • Simulator is decoupled from the Gym wrapper, so you can backtest without RL baggage

Caveats

  • Data download requires Windows and a live MetaTrader 5 install with a demo account
  • The README notes the fee model is “tricky”—bid/ask history isn’t available for distant pasts, so you approximate

Verdict

Worth a look if you’re building RL agents that need to respect real position risk, not just predict price direction. Skip it if you want something that runs headless on a Linux server out of the box.

heatdrop uses Google Analytics to see which pages get read — nothing else. Your call. How we handle data.