EA that moves Stop to BE + 1 after price moves a certain amount of pips into profit

 

Hi guys,


I've been trying to code an EA that moves the Stop to BE + 1 after price moves a certain amount of pips/ticks into profit, but I have been unable to get it to work. I searched through the forum and found similar examples of trailing stops and moving stops, but was unable to get them to work as I want them to. Basically, I want the Stop to be moved for an order that is open to BE + a certain amount of pips (a variable I call SL) after price moves a certain amount of pips/ticks in my direction (a variable I call Ticks).


Here is what I have so far. Any help is appreciated.


//+------------------------------------------------------------------+
//|                                             Trade Protection.mq4 |
//|                                            Copyright © 2010, JCC |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, JCC"
#property link      ""
#property show_inputs

extern int Ticks = 4;
extern int SL = 1;


//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----These I tried to use to see what the EA was calculating but without any luck
   ObjectCreate("B",OBJ_LABEL,0,0,0,0,0);
   ObjectSet("B",OBJPROP_CORNER,2);
   ObjectSet("B",OBJPROP_YDISTANCE,50);
   ObjectSet("B",OBJPROP_XDISTANCE,50);
   
   ObjectCreate("S",OBJ_LABEL,0,0,0,0,0);
   ObjectSet("S",OBJPROP_CORNER,2);
   ObjectSet("S",OBJPROP_YDISTANCE,75);
   ObjectSet("S",OBJPROP_XDISTANCE,50);
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
   bool   result;
   double stop_loss,point,take_profit;
   int cmd,total,error,B,S;
   static bool moved = false;
   
   total=OrdersTotal();
   point=MarketInfo(Symbol(),MODE_POINT);
//----
   if(OrdersTotal()!=0){
   if(OrderSelect(0,SELECT_BY_POS,MODE_TRADES)){
   double op=OrderOpenPrice(); double tp=OrderTakeProfit(); double sl=OrderStopLoss();double pr=OrderProfit();double com= OrderCommission();datetime cl=OrderCloseTime(); int tck = OrderTicket();}
   
   
   if (moved == false)
   {
    int diff;
   
   if (OrderType() == OP_BUY)
    {
      diff = (Close[0] - op)/point;
      if (diff == Ticks)
      { stop_loss=OrderOpenPrice()+SL*point; OrderModify(tck,0,stop_loss,OrderTakeProfit(),0,CLR_NONE); moved = true;} 
      ObjectSetText("B","B= "+diff,10,"Arial",Black);

    }
   
   if (OrderType() == OP_SELL)
    {
      diff = (op - Close[0])/point;
      if (diff == Ticks)
      { stop_loss=OrderOpenPrice()-SL*point; OrderModify(tck,0,stop_loss,OrderTakeProfit(),0,CLR_NONE); moved = true;} 
      ObjectSetText("S","S= "+diff,10,"Arial",Black);

    }
   
   }
  
  }
   
//----This is code I tried previously but did not work   
/*      cmd=OrderType();
      B=(Close[0] - op)/point; S=(op - Close[0])/point;
      ObjectSetText("B","B: "+B,10,"Arial Bold",Black); ObjectSetText("S","S: "+S,10,"Arial Bold",Black);
      
      
         //---- buy or sell orders are considered
         if(cmd==OP_BUY || cmd==OP_SELL)
           {
            //---- modify first market order
            while(true)
              {
               if(cmd==OP_BUY && (B == Ticks)) {stop_loss=OrderOpenPrice()+SL*point; take_profit=OrderOpenPrice()+8*point; moved = true;} else continue;
               if (cmd==OP_SELL && (S == Ticks)) {stop_loss=OrderOpenPrice()-SL*point; take_profit=OrderOpenPrice()+8*point; moved = true;} else continue;
               result=OrderModify(OrderTicket(),0,stop_loss,OrderTakeProfit(),0,CLR_NONE);
               if(result!=TRUE) { error=GetLastError(); Print("LastError = ",error); }
               else error=0;
               if(error==135) RefreshRates();
               else break;
              }
               //---- print modified order (it still selected after modify)
             OrderPrint();
             //break;
           }
    }
    }
*/
//----
   return(0);
  }
//+------------------------------------------------------------------+
 

1)

if (OrderType() == OP_BUY)
if (OrderType() == OP_SELL)
order must be selected by OrderSelect() function.

2)

OrderModify(tck,0,

supposed to be OrderOpenPrice()

3)

diff = (Close[0] - op)/point;

u should use NormalizeDouble