Can anyone help me fix My EA?

 
Hi i am new to mql4 and EA building and came to know about this Stratergy and build it but there is a bug. Bug is ,it works fine for some day but after the day or two it starts to act strange i build it to take order on open trades SL so it takes lots of it like a lot. so Hoping anyone can fix it  or help me fix it. I was building Zone recovery type EA but it is acting like martingle.
//--------------------------------------------------------------------
input string  Time_start   = "01:00",
               Time_end     = "07:00";  //Order place time, if TimeSet = "00:00", the EA works on the breakdown of the previous day. 
extern int    Delta        = 6,        //Order price shift (in points) from High/Low price
              SL           = 120,      //Stop Loss (in points)
              TP           = 90,       //Take Profit (in points)
              risk         = 0,        //if 0, fixed lot is use
              trailing     = 0,        //if 0, it doesn't uses trailing
              max_spread   = 0;        //if 0, it doesn't uses spread
extern double Lot          = 0.01;     //used only if risk = 0
extern bool   OpenStop     = true;     //Place stop orders if order is opened
extern color  color_BAR    = DarkBlue; //info color
//--------------------------------------------------------------------
double        MaxPrice,MinPrice;
int           magic,tip,TimeBarbuy,TimeBarSell,LastDay,TimeStart,Cur_Time,Time_Sta,Spread;
int expiration;
float STOPLEVEL;
double PriceB,PriceS,StopB,StopS,
       TakeB,TakeS;
bool flag = false;
int buy = 0,sel = 0,error;
//--------------------------------------------------------------------
int init()
{
   string dummy,result[],T,res[],P;
   int dummmy;
   ushort u_sep;
   STOPLEVEL = MarketInfo(Symbol(),MODE_STOPLEVEL);
   Spread = MarketInfo(Symbol(),MODE_SPREAD);
   if (SL < STOPLEVEL) SL = STOPLEVEL;
   if (TP < STOPLEVEL) TP = STOPLEVEL;
   if (trailing < STOPLEVEL && trailing != 0) trailing = STOPLEVEL;
   u_sep=StringGetCharacter(":",0);
   dummy = StringSplit(Time_start,u_sep,result);
   T = result[0];
   dummy = StringSplit(Time_end,u_sep,res);
   P = res[0];
   Time_Sta = StringToInteger(P) - StringToInteger(T);
   dummmy = StringToInteger(T)*3600;
   T = result[1];
   dummmy = dummmy + StringToInteger(T)*60;
   TimeStart = dummmy;
   Cur_Time = TimeHour(TimeCurrent())*3600 + TimeMinute(TimeCurrent())*60;
   
/*   Comment(
      "Time_start"   , Time_start,         "\n",
      "Time_end   "   , Time_end,           "\n",
      "Delta       " , Delta,             "\n",
      "SL           ", SL,                "\n",
      "TP          " , TP,                "\n",
      "Lot          ", DoubleToStr(Lot,2),"\n",
      "Max Spread       ", max_spread    ,"\n",
      "risk         ", risk,              "\n",
      "trailing     ", trailing);
     
*/
 return(0);
}
//--------------------------------------------------------------------
int start()
{
   if (OpenStop) magic=TimeDay(CurTime());
   //----------------------------------------------------------------
   double TrPr,StLo;
   sel = 0;
   buy =0;
   bool BUY=false,SEL=false;
   for (int i=0; i<OrdersTotal(); i++)
   {  if (OrderSelect(i, SELECT_BY_POS)==true)
      {  
         if (OrderSymbol()!=Symbol() || OrderMagicNumber()!=magic) continue;
         tip=OrderType();
         if (tip==0){
            BUY = true;
         }
         if (tip==1){
            SEL = true;
         }
         if (tip==4) buy++;
         if (tip==5) sel++;
        
         
        
      }   
   }
   
  if ((BUY||SEL)&& !flag){
      DelAllStop();
      flag = true;
      sel = 0;
      buy = 0;
      } 
 //if (OrderClosePrice() <= OrderStopLoss() && BUY) OrderSend(Symbol(),OP_SELL,Lot,OrderStopLoss(),3,StLo,TrPr,"BSELLL",magic,0,Green);
  //if (OrderClosePrice() >= OrderStopLoss() && SEL) OrderSend(Symbol(),OP_SELL,Lot,OrderStopLoss(),3,StLo,TrPr,"BUYYYY",magic,0,Violet);  
   for (i=0; i<OrdersTotal(); i++)
   {  if (OrderSelect(i, SELECT_BY_POS)==true)
      {
   
   if(BUY && sel == 0){
      Greed(OrderStopLoss(),5);
     }
  else if(SEL && buy == 0) Greed(OrderStopLoss(),4);
   }
   }
      // delete stop orders if order opened
   if ( BUY||SEL) 
   {
      if (trailing!=0) TrailingStop(trailing);
      return(0);                                              // opened order exist
   }
   if (TimeStr(CurTime())!=Time_end){
      //TimeSta = TimeCurrent(); 
      flag = false;
      return(0);
      }                // if time isn't equal to order set time
   if(max_spread !=0 && Spread > max_spread) return(0);
   expiration = CurTime()+(23-TimeHour(CurTime()))*3600+(60-TimeMinute(CurTime()))*60;//set order expiration time
   if (risk!=0) Lot = LOT(); 
   if (buy<1)//&&TimeBarbuy!=TimeDay(CurTime()))
   {
      MaxPrice=iHigh(NULL,1440,LastDay)+NormalizeDouble(Delta*Point,Digits);
      if (Ask+STOPLEVEL*Point>MaxPrice) MaxPrice = NormalizeDouble(Ask+STOPLEVEL*Point,Digits);
      if (TP!=0) TrPr = NormalizeDouble(MaxPrice + TP * Point,Digits);                 
      if (SL!=0) StLo = NormalizeDouble(MaxPrice - SL * Point,Digits);                 
      error=OrderSend(Symbol(),OP_BUYSTOP ,Lot,MaxPrice,3,StLo,TrPr,"BUYSTOP BLD",magic,expiration,Blue);
      if (error==-1) Alert("Error BUYSTOP ",GetLastError(),"   ",Symbol(),"   Lot ",Lot,"   Price ",MaxPrice,"   SL ",StLo,"   TP ",TrPr,"   expiration ",expiration);
      else TimeBarbuy=TimeDay(CurTime());
   }
   if (sel<1)//&&TimeBarSell!=TimeDay(CurTime()))
   {
      MinPrice=iLow(NULL,1440,LastDay)-NormalizeDouble(Delta*Point,Digits);
      if (Bid-STOPLEVEL*Point<MinPrice) MinPrice = NormalizeDouble(Bid-STOPLEVEL*Point,Digits);
      if (TP!=0) TrPr = NormalizeDouble(MinPrice - TP * Point,Digits);                 
      if (SL!=0) StLo = NormalizeDouble(MinPrice + SL * Point,Digits);   
      error=OrderSend(Symbol(),OP_SELLSTOP,Lot,MinPrice,3,StLo,TrPr,"SELLSTOP BLD",magic,expiration,Red );
      if (error==-1) Alert("Error SELLSTOP ",GetLastError(),"   ",Symbol(),"   Lot ",Lot,"   Price ",MinPrice,"   SL ",StLo,"   TP ",TrPr,"   expiration ",expiration);
      else TimeBarSell=TimeDay(CurTime());
   }
   
   if (buy<1&&sel<1)
   {
      ObjectDelete("bar0");
      ObjectCreate("bar0", OBJ_RECTANGLE, 0, 0,0, 0,0);
      ObjectSet   ("bar0", OBJPROP_STYLE, STYLE_SOLID);
      ObjectSet   ("bar0", OBJPROP_COLOR, color_BAR);
      ObjectSet   ("bar0", OBJPROP_BACK,  true);
      ObjectSet   ("bar0", OBJPROP_TIME1 ,iTime(NULL,PERIOD_H1,Time_Sta));
      ObjectSet   ("bar0", OBJPROP_PRICE1,MaxPrice);
      ObjectSet   ("bar0", OBJPROP_TIME2 ,CurTime());
      ObjectSet   ("bar0", OBJPROP_PRICE2,MinPrice);
   }
   
   
   return(0);
}
//--------------------------------------------------------------------
void DelAllStop()
{
   int t;
   bool c;
   for (int i=0; i<OrdersTotal(); i++)
   {                                               
      if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
      {
         if (OrderSymbol()!=Symbol()||OrderMagicNumber()!=magic) continue;
         t=OrderType();
         if (t==4||t==5) c = OrderDelete(OrderTicket());
      }   
   }
}
//--------------------------------------------------------------------
void TrailingStop(int trail)
{
   double StLo;
   int t;
   bool err;
   for (int i=0; i<OrdersTotal(); i++) 
   {
      if (OrderSelect(i, SELECT_BY_POS)==true)
      {
         t = OrderType();
         if (t<2 && OrderSymbol()==Symbol())
         {
            if (OrderMagicNumber()!=magic) continue;
            if (t==0) //Buy               
            {  
               StLo = Bid - trail*Point;          
               if (StLo > OrderStopLoss() && StLo > OrderOpenPrice()) 
                  {err=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(StLo,Digits),OrderTakeProfit(),0,White);Comment("Trailing "+OrderTicket());Sleep(500);}
            }                                         
            if (t==1) //Sell               
            {                                         
               StLo = Ask + trail*Point;            
               if (StLo < OrderStopLoss() && StLo < OrderOpenPrice()) 
                  {err=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(StLo,Digits),OrderTakeProfit(),0,White);Comment("Trailing "+OrderTicket());Sleep(500);}
            } 
            if (err==false && SL!=0) Alert("Error SELLSTOP ",GetLastError(),"   ",Symbol(),"   SL ",StLo);
         }//tip<2
      }//OrderSelect
   }//for
}
//------------------------------------------------------------------+
double LOT()
{
   double MINLOT = MarketInfo(Symbol(),MODE_MINLOT);
   double LOT = AccountFreeMargin()*risk/100/MarketInfo(Symbol(),MODE_MARGINREQUIRED)/15;
   if (LOT>MarketInfo(Symbol(),MODE_MAXLOT)) LOT = MarketInfo(Symbol(),MODE_MAXLOT);
   if (LOT<MINLOT) LOT = MINLOT;
   if (MINLOT<0.1) LOT = NormalizeDouble(LOT,2); else LOT = NormalizeDouble(LOT,1);
   return(LOT);
}
//------------------------------------------------------------------+
void Greed(double Price,int t)
{
   int ticket,c;
   double TrPr,StLo;
   expiration = CurTime()+(23-TimeHour(CurTime()))*3600+(60-TimeMinute(CurTime()))*60;
   if (t == 4) {
      if (Ask+STOPLEVEL*Point>Price) Price = NormalizeDouble(Ask+STOPLEVEL*Point,Digits);
      if (TP!=0) TrPr = NormalizeDouble(Price + TP * Point,Digits);                 
      if (SL!=0) StLo = NormalizeDouble(Price - SL * Point,Digits);
      ticket = OrderSend(Symbol(),OP_BUYSTOP,Lot,Price,3,StLo,TrPr,"GREEED BUY_STOP",magic,expiration,Blue);
      if(ticket == -1) Alert("Error BUYSTOP ",GetLastError(),"   ",Symbol(),"   Lot ",Lot,"   Price ",Price,"   SL ",StLo,"   TP ",TrPr,"   expiration ",expiration);
     // if(ticket == -1) c = OrderSend(Symbol(),OP_BUYLIMIT,Lot,Price,3,StLo,TrPr,"BUY_LIMIT",magic,expiration,Blue)
     buy++;
   }
   if (t == 5) {
      if (Bid-STOPLEVEL*Point<Price) Price = NormalizeDouble(Bid-STOPLEVEL*Point,Digits);
      if (TP!=0) TrPr = NormalizeDouble(MaxPrice - TP * Point,Digits);                 
      if (SL!=0) StLo = NormalizeDouble(MaxPrice + SL * Point,Digits);
      ticket = OrderSend(Symbol(),OP_SELLSTOP,Lot,Price,3,StLo,TrPr,"GREEED SELL_STOP",magic,expiration,Red);
      if(ticket == -1) Alert("Error SELLSTOP ",GetLastError(),"   ",Symbol(),"   Lot ",Lot,"   Price ",Price,"   SL ",StLo,"   TP ",TrPr,"   expiration ",expiration);
      sel++;
     // if(ticket == -1) c = OrderSend(Symbol(),OP_SELLLIMIT,Lot,Price,3,StLo,TrPr,"SELL_LIMIT",magic,expiration,Blue);
   }

}
/*void GridPos(double _Dist,double _Take,double _Stop)
  {
   int i,PendingExpiration = 4,GridOrders = 5,GridStep = 0,Ticket;
   datetime _e = 0;
   _e=TimeCurrent()+PendingExpiration*60*60;
//--
   for(i=0; i<GridOrders; i++)
     {
      PriceB = NormalizeDouble(Ask+(_Dist*Point)+(i*GridStep*Point),Digits);
      TakeB  = PriceB + TP * Point;
      StopB  = PriceB - SL * Point;
      Ticket=OrderSend(Symbol(),OP_BUYSTOP,Lot,PriceB,3,StopB,TakeB,"GRID BUY_STOP",magic,_e,Green);
      //--
      PriceS = NormalizeDouble(Bid-(_Dist*Point)-(i*GridStep*Point),Digits);
      TakeS  = PriceS - TP * Point;
      StopS  = PriceS + SL * Point;
      Ticket=OrderSend(Symbol(),OP_SELLSTOP,Lot,PriceS,3,StopS,TakeS,"GRID SELL_STOP",magic,_e,Red);
     }
   if(Ticket<1)
     {
      Print("Order send error - errcode: ",GetLastError());
      return;
     }
   else
     {
      Print("Grid placed successfully!");
     }
   return;
  }*/
//------------------------------------------------------------------+
string TimeStr(int taim)
{
   string sTaim;
   int HH=TimeHour(taim);     // Hour                  
   int MM=TimeMinute(taim);   // Minute   
   if (HH<10) sTaim = StringConcatenate(sTaim,"0",DoubleToStr(HH,0));
   else       sTaim = StringConcatenate(sTaim,DoubleToStr(HH,0));
   if (MM<10) sTaim = StringConcatenate(sTaim,":0",DoubleToStr(MM,0));
   else       sTaim = StringConcatenate(sTaim,":",DoubleToStr(MM,0));
   return(sTaim);
}
//--------------------------------------------------------------------
 
 

Don't double post! You already had another thread open.

          General rules and best pratices of the Forum. - General - MQL5 programming forum 2017.07.19

  1. Use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?

  2.      MaxPrice=iHigh(NULL,1440,LastDay)+NormalizeDouble(Delta*Point,Digits);

    On MT4: Unless the chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
              Download history in MQL4 EA - Forex Calendar - MQL4 programming forum - Page 3 #26.4 2019.05.20
    The function linked to, opens a hidden chart for the symbol/TF in question (if not already open,) thus updating history, and temporarily placing the symbol on Market Watch (if not already there,) so SymbolInfoDouble(symbol, SYMBOL_BID) or MarketInfo(symbol, MODE_BID) don't also return zero on the first call.

General rules and best pratices of the Forum.
General rules and best pratices of the Forum.
  • 2017.03.09
  • www.mql5.com
General rules, enforced by moderators : ‌...
 
Your duplicated topic has been deleted.