Search code examples
mql4

How to make Break even trigger more than one time in one entry


I now trying to make Break Even Code trigger more than one time, example EA entry is 1.28000 and stop loss 1.28500 if current price reach 1.175000(50pips), sl move to break even such as to 1.28000(5pips).

EA will not make more modify order after condition are meet.

so how to trigger break even again if price reach 1.17000(100pips), sl move to (1.175000)(50 pips)

and again price reach 1.165000(150pips),sl move to 1.17000(100pips)

I want to make

BE_B_M(sl move to(example:5)) 

and

BE_B_T(price reach(example:50)) 

as variable and every time price reach target variable change to next value so became

BE_B_M(sl move to(example:50)) and BE_B_T(price reach(example:100)) 

The entire code is as follows

extern double  BE_T_1      = 50;   
extern double  BE_M_1      = 5;  

extern double  BE_T_2      = 100;   
extern double  BE_M_2      = 50;

extern double  BE_T_3      = 150;   
extern double  BE_M_3      = 100;

double BE_S_M; 
double BE_S_T;

void MOVE_BE_1()

  {
   for(int b=OrdersTotal()-1;b>=0;b--)
     {

      if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
         if(OrderMagicNumber()!=M_Number)continue;
      if(OrderSymbol()==Symbol())
         if(OrderType()==OP_BUY)
            if(Bid-OrderOpenPrice()>BE_S_T*Pips)
               if(OrderOpenPrice()>OrderStopLoss())
                  if(!OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+(BE_S_M*Pips),OrderTakeProfit(),0,CLR_NONE))
                     Print("eror");
     }

   for(int s=OrdersTotal()-1;s>=0;s--)
     {
      if(OrderSelect(s,SELECT_BY_POS,MODE_TRADES))
         if(OrderMagicNumber()!=M_Number)continue;
      if(OrderSymbol()==Symbol())
         if(OrderType()==OP_SELL)
            if(OrderOpenPrice()-Ask>BE_S_T*Pips)
               if(OrderOpenPrice()<OrderStopLoss())
                  if(!OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-(BE_S_M*Pips),OrderTakeProfit(),0,CLR_NONE))
                     Print("eror");
     }

  }

i expect sl will move after price reach every 50pips from entry


Solution

  • Here you can put all the 3 break even levels on one function.

    Note: you can use 1 for-loop for both OP_BUY and OP_SELL

    Here is my OnInit()

    // Global variable
    double point;
    
    int OnInit()
      {
       if(Digits == 5 || Digits == 3) point=Point*10;
       else point=Point;
       return(INIT_SUCCEEDED);
      }
    

    Here is the BreakEven() function

    //+------------------------------------------------------------------+
    //| Break even the trade at 3  levels                                |
    //+------------------------------------------------------------------+
    void BreakEven()
      {
       // get the stop level for that symbol
       double stopLevel = SymbolInfoInteger(Symbol(),SYMBOL_TRADE_STOPS_LEVEL)*Point;
    
       for(int i=OrdersTotal()-1;i>=0;i--)
         {
          if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
          if(OrderMagicNumber()!=M_Number)continue;
          if(OrderSymbol()!=Symbol())continue;
    
          if(OrderType()==OP_BUY)
            {
             double profitPips=Bid-OrderOpenPrice();
             double newSL=OrderStopLoss();
    
             if(profitPips>=BE_T_1*point && OrderStopLoss()<OrderOpenPrice()) // Break Even
               {
                newSL=OrderOpenPrice()+BE_M_1*point;
               }
             else if(profitPips>=BE_T_3*point) // 150/100
               {
                newSL=OrderOpenPrice()+BE_M_3*point;
               }
             else if(profitPips>=BE_T_2*point) // 100/50
               {
                newSL=OrderOpenPrice()+BE_M_2*point;
               }
    
             if(newSL>=OrderStopLoss()+Point && newSL<Bid-stopLevel)
                if(!OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(newSL,Digits),OrderTakeProfit(),0))
                   Print("Error at BE: ",GetLastError());
            }
          else if(OrderType()==OP_SELL)
            {
             double profitPips=OrderOpenPrice()-Ask;
             double newSL=OrderStopLoss();
             if(profitPips>=BE_T_1*point && (OrderStopLoss()>OrderOpenPrice() || OrderStopLoss()==0)) // Break Even
               {
                newSL=OrderOpenPrice()-BE_M_1*point;
               }
             else if(profitPips>=BE_T_3*point) // 150/100
               {
                newSL=OrderOpenPrice()-BE_M_3*point;
               }
             else if(profitPips>=BE_T_2*point) // 100/50
               {
                newSL=OrderOpenPrice()-BE_M_2*point;
               }
    
             if(newSL<=OrderStopLoss()-Point && newSL>Ask+stopLevel)
                if(!OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(newSL,Digits),OrderTakeProfit(),0))
                   Print("Error at BE: ",GetLastError());
            }
         }
    
      }
    

    I didn't test this myself in a trade, but it should work.