Help needed - Signal appear late after candle opened. How to fix ?

To add comments, please log in or register
Say Tan
145
Say Tan  

Hello MT4 programmers out there!  I needed help on changing some codes in this indicator.

A kind trader provided this indicator in one of the forum for trading Binary Options.  The indicator is able to provide an accurate CALL or PUT signal but the problem is, the signal came later after the candle has opened i.e. delayed.  Therefore, when the candle expired, the order became OTM.  Had the signal appeared at the opening of the current candle (or before the close of previous candle), most of the orders entered will be ITM.

I attached the source code of the indicator.  Need help on how to fix the codes (or where to fix?) so that the appear signal is not delayed.  Thanks!



//+------------------------------------------------------------------+

#property copyright "Mindherostar Inc."

#property link      "www.mindrends.com"

#property indicator_chart_window

#property indicator_buffers 2

#property indicator_color1 Lime

#property indicator_color2 Red

//+------------------------------------------------------------------+

extern int       NumBars=500;

extern bool      PlaySounds=true;

extern bool      Alerts = false;

extern bool      Emails = false;

extern bool      Signals= true;

extern bool      Expiry = true;

extern bool      Date=false;

//+------------------------------------------------------------------+

const int SELL=2,BUY=1,NA=0; int Signal=NA; bool SignalThisBar;

bool bNewCandle()

{ 

  static datetime Bar1Time; 

  if (Bar1Time!=Time[0])

  { 

    Bar1Time=Time[0]; 

    return(true); 

  } 

  return(false); 

}



color cSignal(int iSig)

{ 

  if (iSig==2)

  { 

    return(clrRed); 

  }

  else

  { 

    if (iSig==1)

    { 

      return(clrLimeGreen); 

    } 

  } 

  return(clrWhiteSmoke);

}



string sSignal(int iSig)

{ 

  if (iSig==2)

  { 

    return("SELL"); 

  }

  else

  { 

    if (iSig==1)

    { 

      return("BUY"); 

    } 

  } 

  return("NA");

}



double ExtMapBuffer1[],ExtMapBuffer2[],spread;



string strPeriod(int Per)

{

   if (Per==0)      Per=Period(); 

   if (Per==1)      return("M1"); 

   if (Per==5)      return("M5"); 

   if (Per==15)     return("M15"); 

   if (Per==30)     return("M30"); 

   if (Per==60)     return("H1"); 

   if (Per==240)    return("H4"); 

   if (Per==1440)   return("D1"); 

   if(Per==10080)   return("W1"); 

   if (Per==43200)  return("MN"); 

   return("{ Unknown Timeframe }");

}



string sDay(int iD)

{

   if (iD==0) return("SUN"); 

   if (iD==1) return("MON");  

   if (iD==2) return("TUE");  

   if (iD==3) return("WED"); 

   if (iD==4) return("THU");  

   if (iD==5) return("FRI");  

   if (iD==6) return("SAT");  

   return ("{ Unknown Day }");

}



void hLine(string sName,double dPrice)

{

  if (ObjectFind(sName)== -1)  ObjectCreate(0,sName,OBJ_HLINE,0,Time[0],dPrice);

  ObjectSet(sName,OBJPROP_COLOR,cSignal(Signal));

  ObjectSet(sName,OBJPROP_WIDTH,2);

  ObjectSet(sName,OBJPROP_STYLE,0);

  ObjectSet(sName,OBJPROP_BACK,true);

}



void LABEL(string sName,string sText,string sF,int iFS,color cF,int iX,int iY)

{

  if (ObjectFind(sName)== -1) ObjectCreate(sName,OBJ_LABEL,0,0,0); 

  ObjectSetText(sName,sText,iFS,sF,cF);

  ObjectSetString(0,sName,OBJPROP_TOOLTIP,sText); 

  ObjectSet(sName,OBJPROP_HIDDEN,1); 

  ObjectSet(sName,OBJPROP_SELECTABLE,0);

  ObjectSet(sName,OBJPROP_CORNER,1); 

  ObjectSet(sName,OBJPROP_XDISTANCE,iX); 

  ObjectSet(sName,OBJPROP_YDISTANCE,iY);

} 

//+------------------------------------------------------------------+

int init()

{

   SetIndexStyle(0,DRAW_ARROW); 

   SetIndexBuffer(0,ExtMapBuffer1); 

   SetIndexArrow(0,233); 

   SetIndexLabel(0,"MH-Up");

   SetIndexStyle(1,DRAW_ARROW); 

   SetIndexBuffer(1,ExtMapBuffer2); 

   SetIndexArrow(1,234); 

   SetIndexLabel(1,"MH-Dn");

   spread=MarketInfo(Symbol(),MODE_SPREAD)*Point;

   return(0);

}

//+------------------------------------------------------------------+

int deinit()

{

   ObjectDelete(0,"Signal");

   ObjectDelete(0,"Timer");

   ObjectDelete(0,"Date");

   ObjectDelete(0,"EntryPrice");

   return(0);

}

//+------------------------------------------------------------------+

int start()

  {

   int       counted_bars=IndicatorCounted();

   int       artp=7;

   double    dartp=7.0;

   double    cecf=0.7;

   int       satb=0;

   int       Shift=0;

   bool      river=True;

   double    Emaxtra=0;

   double    widcha=0;

   double    TR=0;

   double    Values[100];

   int       glava=0;

   double    ATR=0;

   int       J=0;

   double    Weight=0;

   double    r=0;

   double    r1=0;

   int       p=0;

   int       Curr=0;

   double    Range1=0;

   double    s=2;

   double    f=10;

   double    val1=0;

   double    val2=0;

   double    h11=0;

   double    h12=0;

   double    h13=0;

   double    orig=0;

   double    st=0;

   double    h2=0;

   double    h1=0;

   double    h10=0;

   double    sxs=0;

   double    sms=0;

   double    temp=0;

   double    h5=0;

   double    r1s=0;

   double    r2s=0;

   double    r3s=0;

   double    r4s=0;

   double    pt=0;

   double    pts=0;

   double    r2=0;

   double    r3=0;

   double    r4=0;

   double    tt=0;

   

   if (Bars<NumBars) 

     satb=Bars; 

   else 

     satb=NumBars;

   if (Close[satb-2] > Close[satb-1]) 

     river=True; 

   else 

     river=False;

     

   Emaxtra = Close[satb-2];

   Shift = satb-3;

   while (Shift >= 0)

   {

     TR=spread+High[Shift]-Low[Shift];

     if (MathAbs(spread+High[Shift]-Close[Shift+1])>TR)TR=MathAbs(spread+High[Shift]-Close[Shift+1]);

     if (MathAbs(Low[Shift]-Close[Shift+1])>TR) TR=MathAbs(Low[Shift]-Close[Shift+1]);

     if (Shift==satb-3)

     {

       for (J=0;Shift<=artp-1;J++)

       {

         Values[J]=TR;

       }

     }

     Values[glava]=TR;

     ATR=0;

     Weight=artp;

     Curr=glava;

     for (J=0;J<=artp-1;J++)

     {

       ATR+=Values[Curr]*Weight;

       Weight-=1.0;

       Curr--;

       if (Curr==-1) Curr=artp-1;

     }

     ATR=2.0*ATR/(dartp *(dartp+1.0));

     glava++;

     if (glava==artp) glava=0;

     widcha=cecf*ATR;

     if (river && Low[Shift]<Emaxtra-widcha)

     {

       river=False;

       Emaxtra=spread+High[Shift];

     }

     if (!river && spread+High[Shift]>Emaxtra+widcha)

     {

       river=True;

       Emaxtra=Low[Shift];

     }

     if (river && Low[Shift]>Emaxtra)

     {

       Emaxtra=Low[Shift];

     }

     if (!river && spread+High[Shift]<Emaxtra)

     {

       Emaxtra=spread+High[Shift];

     }

     Range1=iATR(NULL,0,10,Shift);

     val1=0;

     val2=0;



     if (river)

     {

       if (p!=1) r1=Low[Shift]-Range1*s/3.0;

       if (p==1) r1=-1.0;

       if (r1>0)

       {

         val1=r1;

         val2=0;

       }

       else

       {

         val1=0;

         val2=0;

       }

       ExtMapBuffer1[Shift]=val1;

       Signal=BUY;

       p=1;

     }

     else

     {

       if (p!=2) r1=spread+High[Shift]+Range1*s/3.0;

       if (p==2) r1=-1.0;

       if (r1>0)

       {

         val1=0;

         val2=r1;

       }

       else

       {

         val1=0;

         val2=0;

       }

       ExtMapBuffer2[Shift]=val2;

       Signal=SELL;

       p=2;

     }

     Shift--;

   }   // while



   if (bNewCandle())

   {

     SignalThisBar=FALSE; 

     ObjectDelete(0,"EntryPrice"); 

     if (Signals)

     {

       ObjectDelete(0,"Signal");

     } 

     if (Expiry)

     { 

       ObjectDelete(0,"Timer");

     }

   }

   if (!SignalThisBar && ((ExtMapBuffer1[0]!=EMPTY_VALUE && ExtMapBuffer1[0]>0) || (ExtMapBuffer2[0]!=EMPTY_VALUE && ExtMapBuffer2[0]>0)))

   {

     if(Alerts)

     {

       Alert("Signal of "+sSignal(Signal)+" on the "+Symbol()+" "+strPeriod(Period())+" chart.");

     }

     if (Emails)

     { 

       SendMail("MIND HERO: "+sSignal(Signal)+" on "+Symbol(),sSignal(Signal)+" signal generated on the "+strPeriod(Period())+" "+Symbol()+" chart @ "+DoubleToStr(Bid,Digits)+" | "+TimeToStr(CurTime(),TIME_DATE)+" | "+IntegerToString(TimeHour(CurTime()))+":"+IntegerToString(TimeMinute(CurTime()))); 

     }

     if (PlaySounds)

     {

       PlaySound("alert2.wav");

     }

     hLine("EntryPrice",Close[0]);

     SignalThisBar=TRUE;

   }

   if ((ExtMapBuffer1[0]!=EMPTY_VALUE && ExtMapBuffer1[0]>0) || (ExtMapBuffer2[0]!=EMPTY_VALUE && ExtMapBuffer2[0]>0))

   {

     if (Expiry)

     {

       int m=Time[0]+Period()*60-CurTime(),sec=m%60; m=(m-m%60)/60;

       LABEL("Timer",IntegerToString(m)+"m,"+IntegerToString(sec)+"s","Verdana",14,cSignal(Signal),9,28);

     }

     if (Signals){LABEL("Signal",sSignal(Signal),"Verdana",28,cSignal(Signal),9,53);}

   }

   if (Date)

   {

     LABEL("Date","["+sDay(DayOfWeek())+"] "+IntegerToString(TimeMonth(TimeCurrent()))+"/"+IntegerToString(TimeDay(TimeCurrent()))+"/"+IntegerToString(TimeYear(TimeCurrent())),"Arial",10,clrWhiteSmoke,9,10);

   }



   return(0);

}

//+------------------------------------------------------------------+
William Roeder
22458
William Roeder  
  1. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
              Messages Editor

  2. 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?

Keith Watford
Moderator
24458
Keith Watford  
In future please post in the correct section
I will move this topic to the MQL4 and Metatrader 4 section.
To add comments, please log in or register