Forex NewsTrading Bot (MetaTrader 5 + Python)
The Forex NewsTrading Bot is an automated trading system designed to execute breakout trades around major news events such as NFP, CPI, or FOMC decisions.
It connects to MetaTrader 5 using the official Python API, places Buy Stop and Sell Stop orders before the news, and dynamically manages the trades with a trailing stop-loss once one of the orders is triggered.
This approach helps traders capture fast price movements immediately after high-impact news releases — with no manual intervention required.
Automatically places pending Buy Stop and Sell Stop orders
Activates only at a predefined timestamp (perfect for news events)
Dynamically calculates entry, TP, and SL levels from live quotes
Includes auto trailing stop management for active trades
Cancels the opposite pending order once a trade is triggered
Clean, efficient, and fully automated
Here’s a walkthrough of what happens inside the code:
The bot connects to your MetaTrader 5 terminal using account credentials:
mt5.initialize(login=XXXXX, server="XXXXXX", password="XXXXXX")1.Define Key Parameters
gap = 3 # Distance from current price to stop orders
tp = 45 # Take profit in pips
sl = 0.75 # Stop loss multiplier
symbol = "EURUSD"
volume = 0.5 # Lot size per orderand a scheduled execution time:
set = datetime(2025,3,10,19,0,0,100000)
timestamp_set = datetime.timestamp(set)- Wait Until News Time The bot continuously checks the current time until it matches your news event timestamp:
if timestamp_now >= timestamp_set:- Calculate Trading Levels
It fetches live price data and calculates: <O → midpoint (average of bid & ask)> <H → price for Buy Stop> <L → price for Sell Stop> <buy_tp & sell_tp → take profit levels>
O, H, L, sell_tp, buy_tp = cal_levels(symbol, tp, gap)- Place Pending Orders The bot places both Buy Stop and Sell Stop orders:
buy_stop(symbol, volume, H, buy_tp, O - sl*(O - L))
sell_stop(symbol, volume, L, sell_tp, O + sl*(O - L))-
Monitor Active Positions Once one of the trades is triggered: The opposite order is deleted automatically. The bot calculates the new trailing stop level.
-
Trailing Stop Mechanism As price moves favorably, it adjusts the SL dynamically:
trail(symbol, initial_distance, new_sl, tp)- Close and Reset When all trades are closed, the bot exits gracefully.
-
Install MetaTrader 5 Download and install the MetaTrader 5 terminal from: https://www.metatrader5.com Log in with your broker’s demo or live credentials.
-
Install Python Dependencies Run the following in your terminal:
pip install MetaTrader5 pandas numpy python-dateutil pytzOr use the included requirements.txt:
pip install -r requirements.txt- Connect the Bot Open the script and update:
mt5.initialize(login=YOUR_LOGIN, server="YOUR_SERVER", password="YOUR_PASSWORD")How to Run
- Ensure MetaTrader 5 is open and connected.
- Enable Algorithmic Trading in MT5.
- Run the bot:
python forex_newstrading_bot.py- The bot will wait until the defined timestamp and then place orders automatically.
Code Highlights buy_stop() and sell_stop() Functions to place pending buy/sell orders.
cal_levels() Calculates midpoint, high, low, and TP/SL targets from real-time quotes.
trail() Implements the trailing stop mechanism — adjusts SL as price moves.
close_limit() Cancels untriggered pending orders after one side is activated.
Recommended Use Case Use this bot to automate breakout trades during major news releases, such as: Non-Farm Payrolls (NFP) Consumer Price Index (CPI) Interest Rate Decisions GDP or Employment Reports