Download MetaTrader 5

Hedge function for existing EA & Trailing stop for open positions if TP level is reached.

2016.11.07 Experts

Job finished

Execution time 4 days
Feedback from customer
My first time working with a freelancer here, Billy was a pleasure to work with. Very fast and highly skilled. My EA is greatly improved. Will work with him again and I can highly recommend him.
Feedback from employee
Happy to work with you Oscar, hope we can do it again next time.


Dear all,

I'm new to, but learning, MQL4 coding. I've created a very simple EA that I'm quite happy with. But I'd like to pay for some help with a few more complicated (at least for me) functions. I really want to improve my coding skills so I'd appreciate well-commented code. That way I can learn.

Please note that this EA is only aimed at short positions

What I'm looking for is to extend my EA with the following:

1 - Hedging short positions

  • I wish to prevent margin calls and negative liquidity/ equity fluctuations if a short position temporarily moves against me. 
  • I'd like to profit from the hedge position if my short position gets stopped out and the price keeps moving up. 


  1. I'd like to have an input variable called something like "input double HedgeCost=10.0;"
  2. Every time my EA opens a short position, I'd like a buystop order to be placed at a level of the OpenSell price + HedgeCost. In this example, the buystop order would be triggered when the bid price reaches 10.0 PIP's above the OpenSell price).
  3. When the price moves down, I'd like the hedge position to be closed at OpenSell price - HedgeCost. And again, another BuyStop order should be placed, at OpenSell price + HedgeCost. So if the price moves up again, my short position will be hedged again.
  4. When my short position is closed by Take Profit or any reason except Stop Loss, I want the BuyStop order to be canceled.
  5. If my short position is closed due to a stop loss, the hedge position should be modified and a trailing stop should be placed at the exact level of where my short position got stopped out.

I also would like this hedge function to really attach to the ticket number. Because in the future, my EA may open several positions, and all of these need to be hedged. So it won't work if the hedge order is triggered by looking at the total open orders, it has to really check any open short position that hasn't got a hedge Buystop attached to it. And then hedge those that don't. 


2 - Trailing stop when TP is reached

I think this is relatively simple. Again I'd like to learn how to do this, based on your code.

  1. I'd like an input variable called something like "input int TrailingRange=3"
  2. When my short position almost reaches it's TP level, I'd like an OrderModify to happen so that the TP is removed and a trailing stop loss is put into place at the TP - TrailingRange level.
Thank you in advance and I look forward to your offer/ reply.

Best regards,


Project information

10 - 50 USD
from 1 to 3 day(s)