Skip to content

rahulpatil0001/forex_newstrading_bot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 

Repository files navigation

forex_newstrading_bot

Forex NewsTrading Bot (MetaTrader 5 + Python)

Forex NewsTrading Bot (MetaTrader 5 + Python)

Python MetaTrader5 License Status


Overview

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.


Features

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


How It Works — Step by Step

Here’s a walkthrough of what happens inside the code:

1. Initialize Connection

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 order

and a scheduled execution time:

set = datetime(2025,3,10,19,0,0,100000)
timestamp_set = datetime.timestamp(set)
  1. Wait Until News Time The bot continuously checks the current time until it matches your news event timestamp:
if timestamp_now >= timestamp_set:
  1. 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)
  1. 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))
  1. Monitor Active Positions Once one of the trades is triggered: The opposite order is deleted automatically. The bot calculates the new trailing stop level.

  2. Trailing Stop Mechanism As price moves favorably, it adjusts the SL dynamically:

trail(symbol, initial_distance, new_sl, tp)
  1. Close and Reset When all trades are closed, the bot exits gracefully.

  1. 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.

  2. Install Python Dependencies Run the following in your terminal:

pip install MetaTrader5 pandas numpy python-dateutil pytz

Or use the included requirements.txt:

pip install -r requirements.txt
  1. Connect the Bot Open the script and update:
mt5.initialize(login=YOUR_LOGIN, server="YOUR_SERVER", password="YOUR_PASSWORD")

How to Run

  1. Ensure MetaTrader 5 is open and connected.
  2. Enable Algorithmic Trading in MT5.
  3. Run the bot:
python forex_newstrading_bot.py
  1. 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

Releases

No releases published

Packages

 
 
 

Contributors

Languages