Indicator won't update on new bar.

To add comments, please log in or register
ldmurra7
24
ldmurra7  

This indicator is working except for that it will not update on each new bar. I have to reload the indicator for it to update. I know the solution is somewhere in the Start function, and because i am not a programmer I have failed to try and fix it.


Could someone tell me what section of the code to change? I would appreciate it very much. Thankyou!

#property indicator_chart_window

#property indicator_buffers 6

#property indicator_width1 4

#property indicator_width2 4

#property indicator_width3 2

#property indicator_width4 2

#property indicator_width5 2

#property indicator_width6 2

#property indicator_color1 Blue

#property indicator_color2 Red

#property indicator_color3 Gold

#property indicator_color4 Gold

#property indicator_color5 White

#property indicator_color6 White

extern int LeftBars  = 2;

extern int RightBars = 2;

double LineUpBuffer1[];

double LineDownBuffer2[];

double ArrowUpBuffer3[];

double ArrowDownBuffer4[];

double ArrowBreakUpBuffer5[];

double ArrowBreakDownBuffer6[];

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

//| Custom indicator initialization function                         |

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

int init()

{

   SetIndexStyle(0, DRAW_LINE);

   SetIndexArrow(0, 158);

   SetIndexBuffer(0, LineUpBuffer1);

   SetIndexEmptyValue(0, 0.0);

   SetIndexLabel(0, " ");

  

   SetIndexStyle(1, DRAW_LINE);

   SetIndexArrow(1, 158);

   SetIndexBuffer(1, LineDownBuffer2);

   SetIndexEmptyValue(1, 0.0);

   SetIndexLabel(1, " ");

   SetIndexStyle(2, DRAW_ARROW);

   SetIndexArrow(2, 119);

   //SetIndexArrow(2, 217);

   SetIndexBuffer(2, ArrowUpBuffer3);

   SetIndexEmptyValue(2, 0.0);

   SetIndexLabel(2, " ");

  

   SetIndexStyle(3, DRAW_ARROW);

   SetIndexArrow(3, 119);

   //SetIndexArrow(3, 218);

   SetIndexBuffer(3, ArrowDownBuffer4);

   SetIndexEmptyValue(3, 0.0);

   SetIndexLabel(3, " ");

   SetIndexStyle(4, DRAW_ARROW);

   SetIndexArrow(4, 119);

   //SetIndexArrow(4, 217);

   SetIndexBuffer(4, ArrowBreakUpBuffer5);

   SetIndexEmptyValue(4, 0.0);

   SetIndexLabel(4, " ");

  

   SetIndexStyle(5, DRAW_ARROW);

   SetIndexArrow(5, 119);

   //SetIndexArrow(5, 218);

   SetIndexBuffer(5, ArrowBreakDownBuffer6);

   SetIndexEmptyValue(5, 0.0);

   SetIndexLabel(5, " ");

   return(0);

}



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

//| Custom indicator deinitialization function                       |

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

int deinit(){return(0);}

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

//| Custom indicator iteration function                              |

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

int start()

{

   int counted_bars = IndicatorCounted();

   if(counted_bars < 0)  return(-1);

   if(counted_bars > 0)   counted_bars--;

   int limit = Bars - counted_bars;

   if(counted_bars==0) limit-=1+MathMax(LeftBars,RightBars);

   for(int i = limit-1; i >= 0; i--)

   {

      LineUpBuffer1[i] = isFractalUp(i, LeftBars, RightBars,limit);

      if(LineUpBuffer1[i] == 0)

         LineUpBuffer1[i] = LineUpBuffer1[i+1];

      else

         ArrowUpBuffer3[i] = LineUpBuffer1[i];

      

      LineDownBuffer2[i] = isFractalDown(i, LeftBars, RightBars,limit);

      if(LineDownBuffer2[i] == 0)

         LineDownBuffer2[i] = LineDownBuffer2[i+1];

      else

         ArrowDownBuffer4[i] = LineDownBuffer2[i];

        

      if(Close[i] < LineDownBuffer2[i] && Close[i+1] >= LineDownBuffer2[i+1])

         ArrowBreakDownBuffer6[i] = Close[i];

   }

   //LineUpBuffer1[-1] = LineUpBuffer1[0];

   //LineDownBuffer2[-1] = LineDownBuffer2[0];

   return(0);

}





double isFractalUp(int index, int lBars, int rBars, int maxind)

{

   int left = lBars, right = rBars;

   double max = High[index]; //

   for(int i = index - right; i <= (index + left); i++)

   {

     if (i<0 || i>maxind) return(0);

      if(!(High[i] > 0.0))return(0);

      if(max < High[i] && i != index)

      {

         if(max < High[i])  return(0);

         if(MathAbs(i - index) > 1) return(0);

      }

   }

   return(max);

}





double isFractalDown(int index, int lBars, int rBars, int maxind)

{

   int left = lBars, right = rBars;

   double min = Low[index], test;

   for(int i = index - right; i <= (index + left); i++)

   {

      if (i<0 || i>maxind) return(0);

      if(!(Low[i] > 0.0))return(0);

      //if(min >= Low[i] && i != index)

      if(min > Low[i] && i != index)

      {

         if(min > Low[i])

            return(0);

         if(MathAbs(i - index) > 1)

            return(0);

      }

   }

   return(min);

}
Mladen Rakic
160867
Mladen Rakic  
ldmurra7:

This indicator is working except for that it will not update on each new bar. I have to reload the indicator for it to update. I know the solution is somewhere in the Start function, and because i am not a programmer I have failed to try and fix it.


Could someone tell me what section of the code to change? I would appreciate it very much. Thankyou!

#property indicator_chart_window

#property indicator_buffers 6

#property indicator_width1 4

#property indicator_width2 4

#property indicator_width3 2

#property indicator_width4 2

#property indicator_width5 2

#property indicator_width6 2

#property indicator_color1 Blue

#property indicator_color2 Red

#property indicator_color3 Gold

#property indicator_color4 Gold

#property indicator_color5 White

#property indicator_color6 White

extern int LeftBars  = 2;

extern int RightBars = 2;

double LineUpBuffer1[];

double LineDownBuffer2[];

double ArrowUpBuffer3[];

double ArrowDownBuffer4[];

double ArrowBreakUpBuffer5[];

double ArrowBreakDownBuffer6[];

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

//| Custom indicator initialization function                         |

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

int init()

{

   SetIndexStyle(0, DRAW_LINE);

   SetIndexArrow(0, 158);

   SetIndexBuffer(0, LineUpBuffer1);

   SetIndexEmptyValue(0, 0.0);

   SetIndexLabel(0, " ");

  

   SetIndexStyle(1, DRAW_LINE);

   SetIndexArrow(1, 158);

   SetIndexBuffer(1, LineDownBuffer2);

   SetIndexEmptyValue(1, 0.0);

   SetIndexLabel(1, " ");

   SetIndexStyle(2, DRAW_ARROW);

   SetIndexArrow(2, 119);

   //SetIndexArrow(2, 217);

   SetIndexBuffer(2, ArrowUpBuffer3);

   SetIndexEmptyValue(2, 0.0);

   SetIndexLabel(2, " ");

  

   SetIndexStyle(3, DRAW_ARROW);

   SetIndexArrow(3, 119);

   //SetIndexArrow(3, 218);

   SetIndexBuffer(3, ArrowDownBuffer4);

   SetIndexEmptyValue(3, 0.0);

   SetIndexLabel(3, " ");

   SetIndexStyle(4, DRAW_ARROW);

   SetIndexArrow(4, 119);

   //SetIndexArrow(4, 217);

   SetIndexBuffer(4, ArrowBreakUpBuffer5);

   SetIndexEmptyValue(4, 0.0);

   SetIndexLabel(4, " ");

  

   SetIndexStyle(5, DRAW_ARROW);

   SetIndexArrow(5, 119);

   //SetIndexArrow(5, 218);

   SetIndexBuffer(5, ArrowBreakDownBuffer6);

   SetIndexEmptyValue(5, 0.0);

   SetIndexLabel(5, " ");

   return(0);

}



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

//| Custom indicator deinitialization function                       |

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

int deinit(){return(0);}

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

//| Custom indicator iteration function                              |

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

int start()

{

   int counted_bars = IndicatorCounted();

   if(counted_bars < 0)  return(-1);

   if(counted_bars > 0)   counted_bars--;

   int limit = Bars - counted_bars;

   if(counted_bars==0) limit-=1+MathMax(LeftBars,RightBars);

   for(int i = limit-1; i >= 0; i--)

   {

      LineUpBuffer1[i] = isFractalUp(i, LeftBars, RightBars,limit);

      if(LineUpBuffer1[i] == 0)

         LineUpBuffer1[i] = LineUpBuffer1[i+1];

      else

         ArrowUpBuffer3[i] = LineUpBuffer1[i];

      

      LineDownBuffer2[i] = isFractalDown(i, LeftBars, RightBars,limit);

      if(LineDownBuffer2[i] == 0)

         LineDownBuffer2[i] = LineDownBuffer2[i+1];

      else

         ArrowDownBuffer4[i] = LineDownBuffer2[i];

        

      if(Close[i] < LineDownBuffer2[i] && Close[i+1] >= LineDownBuffer2[i+1])

         ArrowBreakDownBuffer6[i] = Close[i];

   }

   //LineUpBuffer1[-1] = LineUpBuffer1[0];

   //LineDownBuffer2[-1] = LineDownBuffer2[0];

   return(0);

}





double isFractalUp(int index, int lBars, int rBars, int maxind)

{

   int left = lBars, right = rBars;

   double max = High[index]; //

   for(int i = index - right; i <= (index + left); i++)

   {

     if (i<0 || i>maxind) return(0);

      if(!(High[i] > 0.0))return(0);

      if(max < High[i] && i != index)

      {

         if(max < High[i])  return(0);

         if(MathAbs(i - index) > 1) return(0);

      }

   }

   return(max);

}





double isFractalDown(int index, int lBars, int rBars, int maxind)

{

   int left = lBars, right = rBars;

   double min = Low[index], test;

   for(int i = index - right; i <= (index + left); i++)

   {

      if (i<0 || i>maxind) return(0);

      if(!(Low[i] > 0.0))return(0);

      //if(min >= Low[i] && i != index)

      if(min > Low[i] && i != index)

      {

         if(min > Low[i])

            return(0);

         if(MathAbs(i - index) > 1)

            return(0);

      }

   }

   return(min);

}
Why are you using   "for(int i = limit-1; i >= 0; i--)" in the main loop? Skip the "-1" part
ldmurra7
24
ldmurra7  
Thanks MLaden. I'm testing now to see if it works.
ldmurra7
24
ldmurra7  

Hi Mladen.

 

I changed it to  "for(int i = limit; i >= 0; i--)" and it still doesn't update on new bar. It will eventually update after many bars, it did this before I changed the code also. I should also say that I adjust the fractals from the standard 2 on the left and 2 on the right.

 

What else could it be?  Once again I appreciate your help.

Mladen Rakic
160867
Mladen Rakic  
ldmurra7:

Hi Mladen.

 

I changed it to  "for(int i = limit; i >= 0; i--)" and it still doesn't update on new bar. It will eventually update after many bars, it did this before I changed the code also. I should also say that I adjust the fractals from the standard 2 on the left and 2 on the right.

 

What else could it be?  Once again I appreciate your help.

Change this line :

   if(counted_bars==0) limit-=1+MathMax(LeftBars,RightBars);
to this :

limit =MathMin(Bars-1,MathMax(limit,MathMax(LeftBars,RightBars)));
and try it then
ldmurra7
24
ldmurra7  
Mladen Rakic:
Change this line :

   if(counted_bars==0) limit-=1+MathMax(LeftBars,RightBars);
to this :

limit =MathMin(Bars-1,MathMax(limit,MathMax(LeftBars,RightBars)));
and try it then

Trying it out now. I knew the solution was in these lines. I'm just not good enough to understand. I tried to look for a solution in google, trying many different things but still not working. So I gave up and ask an expert :P

 

I'll get back to you to see how it runs. 

ldmurra7
24
ldmurra7  

Hi Mladen. It is still not updating on each bar. Here is the changes you suggested and the full code:

#property indicator_chart_window

#property indicator_buffers 6


#property indicator_width1 4

#property indicator_width2 4

#property indicator_width3 2

#property indicator_width4 2

#property indicator_width5 2

#property indicator_width6 2


#property indicator_color1 Blue

#property indicator_color2 Red

#property indicator_color3 Gold

#property indicator_color4 Gold

#property indicator_color5 White

#property indicator_color6 White


extern int LeftBars  = 2;

extern int RightBars = 2;


double LineUpBuffer1[];

double LineDownBuffer2[];

double ArrowUpBuffer3[];

double ArrowDownBuffer4[];

double ArrowBreakUpBuffer5[];

double ArrowBreakDownBuffer6[];


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

//| Custom indicator initialization function                         |

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

int init()

{

   SetIndexStyle(0, DRAW_LINE);

   SetIndexArrow(0, 158);

   SetIndexBuffer(0, LineUpBuffer1);

   SetIndexEmptyValue(0, 0.0);

   SetIndexLabel(0, "Ôðàêòàëüíîå ñîïðîòèâëåíèå");

   

   SetIndexStyle(1, DRAW_LINE);

   SetIndexArrow(1, 158);

   SetIndexBuffer(1, LineDownBuffer2);

   SetIndexEmptyValue(1, 0.0);

   SetIndexLabel(1, "Ôðàêòàëüíàÿ ïîääåðæêà");


   SetIndexStyle(2, DRAW_ARROW);

   SetIndexArrow(2, 119);

   //SetIndexArrow(2, 217);

   SetIndexBuffer(2, ArrowUpBuffer3);

   SetIndexEmptyValue(2, 0.0);

   SetIndexLabel(2, "Ôðàêòàë ÂÅÐÕ");

   

   SetIndexStyle(3, DRAW_ARROW);

   SetIndexArrow(3, 119);

   //SetIndexArrow(3, 218);

   SetIndexBuffer(3, ArrowDownBuffer4);

   SetIndexEmptyValue(3, 0.0);

   SetIndexLabel(3, "Ôðàêòàë ÂÍÈÇ");


   SetIndexStyle(4, DRAW_ARROW);

   SetIndexArrow(4, 119);

   //SetIndexArrow(4, 217);

   SetIndexBuffer(4, ArrowBreakUpBuffer5);

   SetIndexEmptyValue(4, 0.0);

   SetIndexLabel(4, "Ïðîáîé ÂÂÅÐÕ");

   

   SetIndexStyle(5, DRAW_ARROW);

   SetIndexArrow(5, 119);

   //SetIndexArrow(5, 218);

   SetIndexBuffer(5, ArrowBreakDownBuffer6);

   SetIndexEmptyValue(5, 0.0);

   SetIndexLabel(5, "Ïðîáîé ÂÍÈÇ");


   return(0);

}



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

//| Custom indicator deinitialization function                       |

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

int deinit(){return(0);}


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

//| Custom indicator iteration function                              |

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

int start()

{

   int counted_bars = IndicatorCounted();

   if(counted_bars < 0)  return(-1);

   if(counted_bars > 0)   counted_bars--;

   int limit = Bars - counted_bars;

   limit =MathMin(Bars-1,MathMax(limit,MathMax(LeftBars,RightBars)));


   for(int i = limit; i >= 0; i--)

   {

      LineUpBuffer1[i] = isFractalUp(i, LeftBars, RightBars,limit);

      if(LineUpBuffer1[i] == 0)

         LineUpBuffer1[i] = LineUpBuffer1[i+1];

      else

         ArrowUpBuffer3[i] = LineUpBuffer1[i];

      

      LineDownBuffer2[i] = isFractalDown(i, LeftBars, RightBars,limit);

      if(LineDownBuffer2[i] == 0)

         LineDownBuffer2[i] = LineDownBuffer2[i+1];

      else

         ArrowDownBuffer4[i] = LineDownBuffer2[i];

         

      if(Close[i] < LineDownBuffer2[i] && Close[i+1] >= LineDownBuffer2[i+1])

         ArrowBreakDownBuffer6[i] = Close[i];

   }

   //LineUpBuffer1[-1] = LineUpBuffer1[0];

   //LineDownBuffer2[-1] = LineDownBuffer2[0];

   return(0);

}






double isFractalUp(int index, int lBars, int rBars, int maxind)

{

   int left = lBars, right = rBars;

   double max = High[index]; //Ïðèíèìàåì çà ìàêñèìóì çíà÷åíèå Õàÿ èññëåäóåìîãî áàðà

   for(int i = index - right; i <= (index + left); i++)

   {

     if (i<0 || i>maxind) return(0);

      if(!(High[i] > 0.0))return(0);

      if(max < High[i] && i != index)

      {

         if(max < High[i])  return(0);

         if(MathAbs(i - index) > 1) return(0);

      }

   }

   return(max);

}





double isFractalDown(int index, int lBars, int rBars, int maxind)

{

   int left = lBars, right = rBars;

   double min = Low[index], test;

   for(int i = index - right; i <= (index + left); i++)

   {

      if (i<0 || i>maxind) return(0);

      if(!(Low[i] > 0.0))return(0);

      //if(min >= Low[i] && i != index)

      if(min > Low[i] && i != index)

      {

         if(min > Low[i])

            return(0);


         if(MathAbs(i - index) > 1)

            return(0);

      }


   }

   return(min);

}

William Roeder
20249
William Roeder  
  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it

  2.    if(counted_bars > 0)   counted_bars--;

       int limit = Bars - counted_bars;

       limit =MathMin(Bars-1,MathMax(limit,MathMax(LeftBars,RightBars)));

       for(int i = limit; i >= 0; i--)
    No need for the decrement. Contradictory information on IndicatorCounted() - MQL4 forum
  3. Your look back is LeftBars. Do your lookbacks correctly.
  4. Your look forward is RightBars. The last bar you can process is RightBars. See How to do your lookbacks correctly.
  5. Start using the new Event Handling Functions - Functions - Language Basics - MQL4 Reference
  6. Start using strict and you would have learned why.



ldmurra7
24
ldmurra7  

Ok thanks for suggestion whoroeder1.

 

Your 3rd and 4th point seems to be the one that is causing the problem? Is that correct? 

 

limit =MathMin(Bars-1,MathMax(limit,MathMax(LeftBars,RightBars)));


This line needs changing? This is really hard for someone that doesn't code haha.

For you it's like counting to 10 :P

I'm trying my best 

ldmurra7
24
ldmurra7  
whroeder1:
  1. Please edit your post.
    For large amounts of code, attach it

  2.    if(counted_bars > 0)   counted_bars--;

       int limit = Bars - counted_bars;

       limit =MathMin(Bars-1,MathMax(limit,MathMax(LeftBars,RightBars)));

       for(int i = limit; i >= 0; i--)
    No need for the decrement. Contradictory information on IndicatorCounted() - MQL4 forum
  3. Your look back is LeftBars. Do your lookbacks correctly.
  4. Your look forward is RightBars. The last bar you can process is RightBars. See How to do your lookbacks correctly.
  5. Start using the new Event Handling Functions - Functions - Language Basics - MQL4 Reference
  6. Start using strict and you would have learned why.



I worked it out :D I'm so dam happy. Your the best whroeder1.

I wish you the best in all your endeavors :D
Duc Anh Le
157
Duc Anh Le  
William Roeder:
  1. Please edit your post.
    For large amounts of code, attach it

  2.    if(counted_bars > 0)   counted_bars--;

       int limit = Bars - counted_bars;

       limit =MathMin(Bars-1,MathMax(limit,MathMax(LeftBars,RightBars)));

       for(int i = limit; i >= 0; i--)
    No need for the decrement. Contradictory information on IndicatorCounted() - MQL4 forum
  3. Your look back is LeftBars. Do your lookbacks correctly.
  4. Your look forward is RightBars. The last bar you can process is RightBars. See How to do your lookbacks correctly.
  5. Start using the new Event Handling Functions - Functions - Language Basics - MQL4 Reference
  6. Start using strict and you would have learned why.



Hi @William Roeder

I bump into this issue where my custom Indicator work fine when attached on the chart but not in strategy tester (i.e. the indicator just stop at the prev_calculated bar which is the starting time of the testing period). I follow your guide above to computing the 'limit' of the loop but seems not avail. Can you please have a look, thank

Files:
12
To add comments, please log in or register