How to correctly hedge?

 

Hello everyone!

I'm trying to code a hedge position when the price goes against me certain amount of pips.

I've tried two different ways, but I'm not achieving it in a right way.

The first of them is opening a pending order when a position is open:

void openTrade(int typeOfTrade)
  {
   ZeroMemory(lastTick);
   ZeroMemory(mrequest);
   ZeroMemory(mresult);
   ZeroMemory(mcheck);
   SymbolInfoTick(Symbol(),lastTick);


   CopyBuffer(atrHandle,0,0,1,atrBuff);
   double PipValue=(((SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_VALUE))*Point())/(SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_SIZE)));
   double stopLossPips=(1.5*atrBuff[0]+(double)SymbolInfoInteger(Symbol(),SYMBOL_SPREAD)*Point())/Point();
   double totalLots=0.005*AccountInfoDouble(ACCOUNT_BALANCE)/(PipValue*stopLossPips);
   totalLots=floor(totalLots*100)/100;

   mrequest.volume=MathMax(SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MIN),MathMin(NormalizeDouble(totalLots,2),SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MAX)));

   lotValue=MathMax(SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MIN),MathMin(NormalizeDouble(totalLots,2),SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MAX)))*PipValue;

   priceForHedgeSell=(typeOfTrade==1)? NormalizeDouble(lastTick.ask-stopLossPips*Point(),_Digits):NormalizeDouble(lastTick.bid+stopLossPips*Point(),_Digits);

   mrequest.magic=Magic;
   mrequest.action=TRADE_ACTION_DEAL;
   mrequest.tp=(typeOfTrade==1)? NormalizeDouble(lastTick.ask+2*stopLossPips*Point(),_Digits):NormalizeDouble(lastTick.bid-2*stopLossPips*Point(),_Digits);
   mrequest.price=(typeOfTrade==1)?NormalizeDouble(lastTick.ask,_Digits):NormalizeDouble(lastTick.bid,_Digits);
   mrequest.symbol=Symbol();
   mrequest.type=(typeOfTrade==1)?ORDER_TYPE_BUY:ORDER_TYPE_SELL;
   mrequest.type_filling=ORDER_FILLING_FOK;


   if(OrderSend(mrequest,mresult))
     {
      
      lastOpenPrice=(typeOfTrade==1)?NormalizeDouble(lastTick.ask,_Digits):NormalizeDouble(lastTick.bid,_Digits);

     }

   ZeroMemory(mrequest);
   ZeroMemory(mresult);
   ZeroMemory(mcheck);

   stopLossPips=MathAbs(lastOpenPrice-((typeOfTrade==1?lastTick.bid:lastTick.ask)))/Point();/*(atrStopLoss*atrTrades[0])/Point()*/;
   totalLots=0.01*AccountInfoDouble(ACCOUNT_BALANCE)/(PipValue*stopLossPips);
   totalLots=floor(totalLots*100)/100;

   lotValueHedge=MathMax(SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MIN),MathMin(NormalizeDouble(totalLots,2),SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MAX)))*PipValue;
   double takeProfit=NormalizeDouble(lotValue*stopLossPips*Point()/(lotValueHedge-lotValue),_Digits);

   mrequest.volume=MathMax(SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MIN),MathMin(NormalizeDouble(totalLots,_Digits),SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MAX)));
   mrequest.sl=lastOpenPrice;
   mrequest.magic=magicHedge;
   mrequest.tp=(typeOfTrade==1)? NormalizeDouble(lastOpenPrice-takeProfit,_Digits):NormalizeDouble(lastOpenPrice+takeProfit,_Digits);
   mrequest.action=TRADE_ACTION_PENDING;
   mrequest.price=priceForHedgeSell;
   mrequest.symbol=Symbol();
   mrequest.type=(typeOfTrade==1)?ORDER_TYPE_SELL_STOP:ORDER_TYPE_BUY_STOP;
   mrequest.type_filling=ORDER_FILLING_FOK;
   
   OrderSend(mrequest,mresult);
  }

So, the idea is that the open price of the pending order is the "stoploss" of the first order. The stoploss of the pending order is the open price of the first order and I wanted to calculate the take profit using:

double takeProfit=NormalizeDouble(lotValue*stopLossPips*Point()/(lotValueHedge-lotValue),_Digits);

The idea behind that is this:

lotValue and lotValueHedge can be seen as "how many USD I win/lose with each pip". So, I thought that if I calculate lotValueHedge*X-lotValue*X-lotValue*stopLossPips = 0, then X would be the amount of pips that the price needs to move in the direction of the hedge position in order to both of them have the same USD value, right?. Welp, it does not happens that way. The first order is slightly more negative than the second one. Also, using this I get frequently "invalid stops" because of the take profit level and I don't know why its bigger than the open price of the second order.

Any thoughts?


The second of them is the same idea, but instead of opening a pending order, it just opens a second order when the price moves against the initial position X amounts of pips. The code is mostly the same, the only difference is that there is no pending order part.


How can I achieve this in a better way? How its correctly done? Is my approach good?

Thank you in advance!

PS

 
Hello Pedro do you find any break through in hedging 
 
I hedge on manually trading. I have many different techniques. There are several ways of hedging, so I don't know which is the right way you mean.
 
"Hedging" in Forex trading -Why do it? - Hedge is a method to limit risk
"Hedging" in Forex trading -Why do it? - Hedge is a method to limit risk
  • 2017.01.19
  • www.mql5.com
Hedging is a method to limit risk. Of course you could achieve it without hedging but it will be much more complicate to manage. It is hedging in the meaning of openning orders in differnet directions. It is not hedging as interpreted in your post to limit your risk by openning the same order on other direction, this we all agree is useless
Reason: