A fully automated Python bot that instantly creates reverse (hedge) positions in MetaTrader5 whenever a new trade opens. Designed for risk hedging, strategy testing, and automated counter-trading across all market types.
- Risk Management - Hedge original positions with automatic reverse trades
- Strategy Testing - Test counter-strategies simultaneously with live trades
- Automated Hedging - No manual intervention needed
- Loss Prevention - Quick position mirroring for protection
- Detects every new trade instantly
- Opens exactly one reverse position per original trade
- No spamming or duplicate reverses (tracked by ticket)
- Reverse Stop Loss = Original Take Profit
- Reverse Take Profit = Original Stop Loss
- Auto-synchronizes when original SL/TP changes
- Real-time SL/TP updates without manual adjustment
- Reverse volume = 2 × original volume
- Customizable multiplier (change
REVERSE_VOLUME_MULTIPLIER) - Works with partial position modifications
- When original trade closes → reverse position closes instantly
- Maintains 1:1 position lifecycle
- Prevents orphaned reverse positions
- Works on all markets supported by MetaTrader5:
- 💱 Forex (EUR/USD, GBP/USD, etc.)
- 🏆 Indices (EUROSTOXX, DAX, etc.)
- 💰 Precious Metals (Gold, Silver)
- 🪙 Cryptocurrencies (BTC/USD, ETH/USD, etc.)
- 📈 Stocks & CFDs
- 🌾 Commodities (Oil, Natural Gas, etc.)
- Survives bot restarts
- Recovers position mappings from trade comments
- Tracks original→reverse relationships
- Python 3.8+
- MetaTrader5 installed on Windows
- MT5 account with trading permissions
- Admin rights to run Python scripts
# Clone the repository
git clone https://github.com/YassineM123/MT5-Reverse-Position-Bot-Automatic-Reverse-Hedging-EA-Python.git
cd MT5-Reverse-Position-Bot-Automatic-Reverse-Hedging-EA-Python
# Install dependencies
pip install -r requirements.txtEdit the constants at the top of mt5_reverse_bot.py:
# User Configuration (optional - leave as None to use terminal login)
LOGIN = None # Leave None or set to your MT5 account number
PASSWORD = None # Leave None or set your password
SERVER = None # Leave None or set to broker server name
# Bot Configuration
MAGIC = 987654321 # Magic number for reverse positions
DEVIATION = 20 # Max allowed slippage in points
POLL_SECONDS = 1.0 # How often to check for new positions (seconds)
COMMENT_PREFIX = "REV of " # Comment prefix for reverse position# Start the bot
python mt5_reverse_bot.py
# Expected output:
# [*] Reverse bot started.
# [OK] Sent reverse EURUSD BUY vol=0.2 @ 1.0856 (order=123456789, deal=987654321)
# [OK] Linked original#456789 -> reverse#123456The bot runs infinitely. Press Ctrl+C to stop.
┌─────────────────────────────────────┐
│ MT5 Terminal - Open Positions │
├─────────────────────────────────────┤
│ Original: BUY EURUSD 0.1 │ ← Detected
│ SL: 1.0500, TP: 1.0900 │
└─────────────────────────────────────┘
┌──────────────────────────────────────────┐
│ Bot Action │
├──────────────────────────────────────────┤
│ 1. Detected original BUY │
│ 2. Calculate reverse: SELL │
│ 3. Volume: 0.1 × 2 = 0.2 │
│ 4. Reverse SL = original TP = 1.0900 │
│ 5. Reverse TP = original SL = 1.0500 │
│ 6. Send SELL order with magic #987... │
│ 7. Link positions by comment │
└──────────────────────────────────────────┘
Original Position Changed:
SL: 1.0500 → 1.0400
TP: 1.0900 → 1.1000
Reverse Position Updated (Instantly):
TP: 1.0500 → 1.0400 (now mirrors new SL)
SL: 1.0900 → 1.1000 (now mirrors new TP)
Original Closes:
↓
Reverse Closes (Instantly)
↓
Position Mappings Cleared
| Setting | Default | Description |
|---|---|---|
LOGIN |
None |
MT5 account number (None = use terminal login) |
PASSWORD |
None |
Account password (None = use terminal) |
SERVER |
None |
Broker server name (None = use terminal) |
MAGIC |
987654321 |
Magic number to identify reverse positions |
DEVIATION |
20 |
Max slippage in points |
POLL_SECONDS |
1.0 |
Check interval in seconds |
COMMENT_PREFIX |
"REV of " |
Text prefix for position comments |
Change reverse volume multiplier:
reverse_vol = round(op.volume * 3.0, 2) # 3x instead of 2xChange polling speed (faster detection):
POLL_SECONDS = 0.5 # Check every 500ms (more CPU usage)Change magic number (to avoid conflicts):
MAGIC = 111222333 # Unique identifierThe bot maintains original_ticket → reverse_ticket mappings:
# Stored in memory during runtime
orig_to_rev: Dict[int, int] = {
456789: 123456, # Original ticket 456789 → Reverse ticket 123456
789012: 654321, # Original ticket 789012 → Reverse ticket 654321
}
# Survives restarts via trade comments
# Comment format: "REV of 456789" → extracted on next run- Bot reads all current positions
- Scans comments for format
"REV of <ticket>" - Rebuilds original→reverse mappings
- Resumes monitoring and syncing
Account: 12345678
Symbol: EURUSD
Type: BUY
Volume: 0.1 lot
Entry: 1.0856
SL: 1.0500
TP: 1.0900
Ticket: 456789
[OK] Sent reverse EURUSD SELL vol=0.2 @ 1.0856
[OK] Linked original#456789 -> reverse#123456
Position Created:
Symbol: EURUSD
Type: SELL
Volume: 0.2 lot
Entry: 1.0856
SL: 1.0900 ← Mirrored from TP
TP: 1.0500 ← Mirrored from SL
Ticket: 123456
Magic: 987654321
Comment: "REV of 456789"
Original SL: 1.0500 → 1.0400
Original TP: 1.0900 → 1.0900
[OK] Modifying reverse SL/TP
Reverse TP: 1.0500 → 1.0400 ← Updated to match new SL
Reverse SL: 1.0900 → 1.0900 ← No change (already matches)
Original Position: CLOSED
[OK] Closed reverse pos#123456
Reverse Position: CLOSED
Mapping Cleared
# Failed order? Bot retries automatically
if res.retcode != mt5.TRADE_RETCODE_DONE:
print(f"[WARN] SL/TP modify failed: {res.retcode}")
# Continues trying on next poll cycle# Symbol not available? Tries to select it
if not ensure_symbol(symbol):
print(f"[WARN] Can't select symbol {symbol}")
# Skips and tries again next cycle[*] Reverse bot started.
[OK] Sent reverse EURUSD BUY vol=0.2 @ 1.0856 (order=123456789, deal=987654321)
[OK] Linked original#456789 -> reverse#123456
[OK] Modifying reverse SL/TP
[WARN] SL/TP modify failed for pos#123456: retcode=10005
[OK] Closed reverse pos#123456
[OK] Linked original#789012 -> reverse#654321
Terminal → View → Positions
Magic: 987654321 ← Filter by magic to see bot's reverses
- ✅ Netting margin account required
- ✅ Must allow reverse positions
- ✅ Account must have sufficient margin
- ❌ Hedging margin accounts NOT supported (different PT model)
- 💰 Margin Usage: Reverse positions consume margin (2x volume = more margin needed)
- 📊 Slippage: Real-world entry may differ from original
- ⚡ Speed: Network delays may affect sync precision
- 🔧 Customization: Unsuitable brokers may have different order validation
- Test First - Test on demo account before live trading
- Monitor Margin - Ensure sufficient balance for reverses
- Adjust Deviation - Increase if orders frequently fail
- Log Review - Check console output regularly
- Broker Check - Verify your broker allows reverse positions
# Solution: Ensure MT5 is running
# 1. Open MetaTrader5 application
# 2. Log in to your account
# 3. Try running bot againError: Invalid volume
Solution:
- Check minimum lot size for symbol
- Verify account has sufficient margin
- Increase DEVIATION for slippage
Error: Invalid price
Solution:
- Ensure symbol has current tick
- Verify price within bid-ask spread
Error: Symbol not available
Solution:
- Add symbol to Watchlist in MT5
- Use exact symbol name (e.g., "EURUSD", not "EUR/USD")
Checklist:
✓ MT5 terminal is open and logged in
✓ You've opened at least one original position
✓ Bot is running (no errors in console)
✓ Account has sufficient margin
✓ Symbol is in your Watchlist
.
├── mt5_reverse_bot.py # Main bot script (300+ lines)
│ ├── User Configuration
│ ├── Helper Functions
│ ├── Order Management
│ └── Main Loop
├── requirements.txt # Dependencies
└── README.md # This file
MetaTrader5==5.0.39 # MT5 Python APIInstall with:
pip install -r requirements.txt- Latency: ~100-500ms per cycle (depends on POLL_SECONDS)
- CPU Usage: Minimal (<1% typically)
- Memory: ~50-100MB
- Network: Minimal (only MT5 terminal communication)
- MT5 Documentation: https://www.metatrader5.com/en/docs/python
- MT5 Python API: https://www.mql5.com/en/docs/integration
- Reverse Trading: https://en.wikipedia.org/wiki/Pair_trading
- Hedging Guide: https://www.investopedia.com/terms/h/hedge.asp
Found a bug or want to improve? Contributions welcome!
git clone https://github.com/YassineM123/MT5-Reverse-Position-Bot.git
git checkout -b feature/improvement
git commit -am 'Your improvement'
git push origin feature/improvementMIT License - See LICENSE file for details.
This bot is provided as-is for educational and automated trading purposes. Users are responsible for:
- Trading strategy validation
- Risk management and position sizing
- Broker compliance and account rules
- Monitoring and system maintenance
Trade at your own risk. Past performance ≠ future results.
- 🐛 Issues: Open a GitHub issue
- 💬 Questions: Start a discussion
- 📖 Docs: Read this README thoroughly
- 🆘 Emergency: Stop the bot immediately (
Ctrl+C)
Made with ❤️ for traders by YassineM123
Happy hedging! 📈