Download MetaTrader 5

Ordermodify - BreakEven

To add comments, please log in or register
We have launched Spanish and Portuguese versions of the website. Try them!
suikka
77
suikka 2016.04.06 14:08 

Hi

Im coding EA and I want it to have feature to set SL to BE if x% of TP is reached. This is what I have so far. 

void BreakEven()
{
   for (int i = OrdersTotal()-1 ; i >= 0; i--)
   {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         if(OrderMagicNumber()==Magic)
            if(OrderType()==OP_BUY)
               if(iClose(OrderSymbol(), Period(),0) >= BE*OrderTakeProfit() && OrderStopLoss() < OrderOpenPrice())
               {
                  Print(BE + " hit " + OrderSymbol());
                  bool res=OrderModify(OrderTicket(),OrderOpenPrice(), 25 ,0 ,clrBlue);
                  if(!res)
                     Print("Error in OrderModify (BreakEven). CurrencyPair = " + OrderSymbol() + "  Error code=",GetLastError());          
               }   
            if(OrderType()==OP_SELL)
               if(iClose(OrderSymbol(), Period(),0) <= BE*OrderTakeProfit() && OrderStopLoss() > OrderOpenPrice())
               {
                  Print(BE + " hit " + OrderSymbol());
                  bool res=OrderModify(OrderTicket(),OrderOpenPrice(), 25 ,0 ,clrBlue);
                  if(!res)
                     Print("Error in OrderModify (BreakEven). CurrencyPair = " + OrderSymbol() + "  Error code=",GetLastError());          
               }
   }

}


EDIT: BE is global variable and it is 0.7.

Problem is in this line:

bool res=OrderModify(OrderTicket(),OrderOpenPrice(), 25 ,0 ,clrBlue);

I tried to set SL to 25 point over/under OrderOpenPrice (I think this means open price of the trade?).

Now this code just removes SL and is not able to set up the new SL value.

Can someone help me with this one.. Thank you.

Alain Verleyen
Moderator
30745
Alain Verleyen 2016.04.06 14:25  
suikka:

Hi

Im coding EA and I want it to have feature to set SL to BE if x% of TP is reached. This is what I have so far. 

EDIT: BE is global variable and it is 0.7.

Problem is in this line:

I tried to set SL to 25 point over/under OrderOpenPrice (I think this means open price of the trade?).

Now this code just removes SL and is not able to set up the new SL value.

Can someone help me with this one.. Thank you.

Of course, OrderModify() is needing a price for SL and TP, not a pip/point value (25).
suikka
77
suikka 2016.04.06 14:34  
Alain Verleyen:
Of course, OrderModify() is waiting a price for SL and TP, not a pip/point value (25).
Thank you Alain for the fast reply.. What is the best way to add 25 pips to OrderOpenPrice ?


Can I do it something like this ?

bool res=OrderModify(OrderTicket(),OrderOpenPrice(), OrderOpenPrice() + OrderCommission() + OrderSwap(),0 ,clrBlue);

To add comments, please log in or register