Doubts - Hedging an Order with another

 

Hi,


I am trying to figure out a way to write a MQL4 a kind of Hedging to compensate for an order that wen bad.

I will detail.

  • Order 1 - Buy EURUSD at 1.09310 with TP at 1.09331 (21 pips) with LotSize = 0.1. 

This order backfires and the Virtual Stop Loss, that is not a Stop Loss in my case but a value to fire compensating order, is 22 pips from Open Price of Order 1, meaning the value of 1,0909.

  • Order 2 - Compensating order - Sell EURUSD at 1,0909 (22 pips from Open Price of Order 2), with TP at 1,0888 with a Lot Size X

My difficulty is how should I calculate X so the profits Order 2 must be equals the (Profits of Order 1 if it hd reached its TP) + (Loss of Order 1 at TP of Order 2).


So, If I call:

  • OpenPrice1 = OP1
  • OpenPrice2 = OP2
  • LotSize1 = LS1
  • LotSize2 = LS2
  • LOSS1 the loss of Order 1 when it reaches the TP2 (The TP of Order 2) I will have: LOSS1 = (OP1- TP2) * 10 * LS1
  • PROFIT1 the profit of Order 1 if it had reached TP1 I will have: PROFIT1 = (TP1 - OP1) * 10 * LS1
  • PROFIT2 the profit of hedge order: LOSS1 + PROFIT1 = (OP2 - TP2) * 10 * LS2

PROFIT2  = (OP1 - TP2) * 10 * LS1+ (TP1 - OP1) * 10 * LS1= (TP1 - TP2)* 10 * LS1

PROFIT2 = (OP2 - TP2) * 10 * LS2

LS2= LS1* (TP1 - TP2)/(OP2 - TP2)


Am I missing anything?

Will it work for any pair or instrument?

How can I make it robust to be used on any instrument?

Documentation on MQL5: Constants, Enumerations and Structures / Indicator Constants / Price Constants
Documentation on MQL5: Constants, Enumerations and Structures / Indicator Constants / Price Constants
  • www.mql5.com
Calculations of technical indicators require price values and/or values of volumes, on which calculations will be performed. There are 7 predefined identifiers from the ENUM_APPLIED_PRICE enumeration, used to specify the desired price base for calculations. If a technical indicator uses for calculations price data, type of which is set by...
Reason: