Bars counting gone wrong...

 

Hi,

I have coded an indicator (macount) to count the number consecutive high of another indicator(mafilter). but it behave very strangely. it look into the future bar for counting..

Now I'm confused about

int limit,   counted_bars=IndicatorCounted();
if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;

what is the code actually counting and what does "limit" actually means?

My full code:

//+------------------------------------------------------------------+
//|                                         trendrojak indicator.mq4 |
//|                      Copyright © 2011, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 Red
#property indicator_color2 Blue
#property indicator_color3 Red
//--- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];

double countma=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,ExtMapBuffer2);
   SetIndexStyle(2,DRAW_LINE);
   SetIndexBuffer(2,ExtMapBuffer3);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int limit,   counted_bars=IndicatorCounted();
if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
   Print ("limit is " ,limit, " counted_bars is " ,counted_bars);
for(int i=0; i<limit; i++){
ExtMapBuffer1[i]=mafilter (i);
ExtMapBuffer2[limit]=trendstrength (limit);
ExtMapBuffer3[i]=macount(i);
//Print ("i count is ",i);
}
//----
   
//----
   return(0);
  }

//int mafilter (int shiftma) {
double mafilter(double shiftma) {
double ma5,ma10,ma30;
ma5= iMA (NULL,0,5,0,MODE_EMA,PRICE_MEDIAN,shiftma);
ma10= iMA (NULL,0,10,0,MODE_EMA,PRICE_MEDIAN,shiftma);
ma30= iMA (NULL,0,30,0,MODE_EMA,PRICE_MEDIAN,shiftma);
//Print ("ma5 is " ,ma5); 
   if (ma5 >ma30 && ma10>ma30) {
//    Print ("mafilter return 1");
      return(1); //trend intact
 
      } //end if 
   else { 
// Print ("mafilter return 1");
      return (-1);//trend change

      } //end else
} //end mafilter 

double trendstrength(double shifttrend) {
// iADX( string symbol, int timeframe, int period, int applied_price, int mode, int shift) 

if (iADX(NULL,0,14,PRICE_CLOSE,MODE_MAIN,shifttrend) > 25){
  
   if (iADX(NULL,0,14,PRICE_CLOSE,MODE_MAIN,shifttrend) >iADX(NULL,0,14,PRICE_CLOSE,MODE_MAIN,shifttrend+1)) {
   return (1); //trend picking up strength
   }//end if
   else {
   return (-1);
         }//end else
         
}//endif
else {
return (-1);
}//end else
}//end trendstrength

double macount (double ped) {

if (mafilter(ped)==1) {
countma++;
return (countma);
}//end if
if (mafilter(ped)==-1) {
countma =0; 
return (countma);
Print ("mafilter count reset");
}//end else*/
Print ("mafilter count is now " ,countma, " at bar", ped ); 
}//end macount
//+------------------------------------------------------------------+

will someone please help to enlighten me?

 
praetorian:

Now I'm confused about

what is the code actually counting and what does "limit" actually means?

will someone please help to enlighten me?

int limit,   counted_bars=IndicatorCounted();
if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;

You have
ma30= iMA (NULL,0,30,0,MODE_EMA,PRICE_MEDIAN,shiftma);
that's your largest look back. The Decrement is unneeded.
#define LOOK_BACK 30
int int(){
    :
    SetIndexDrawBegin(..., LOOK_BACK);
}
int start(){
    int counted_bars=IndicatorCounted();
    if (counted_bars < LOOK_BACK) counted_bars = LOOK_BACK;
    for(int i = Bars - 1 - counted_bars; i >= 0; i--){
       :

Count down, you can't make use of future bar values only past ones (i+1) when computing bar i.

If you are using values from oneBuffer[i+1] in computing secondBuffer[i], then you have TWO Look back values, use two loops one for each buffer.

 

Hi,

did the suggested changes to my code and noticed that now i is counting backwards vs i counting forward in the previous code.

comparing results of both code side by side, noticed that besides "i" all else is the same meaning

ExtMapBuffer1[i]=mafilter (i);
ExtMapBuffer2[i]=trendstrength (i);

results are the same. both buffers depended on "i" but when "i" changes their results doesn't change according . I would be expecting them to be opposite at least.

further investigation if i were to plot "i" using forward counting and backward counting, they both yields the result . Why is so?

 
praetorian:
did the suggested changes to my code and noticed that now i is counting backwards vs i counting forward in the previous code.

>comparing results of both code side by side, noticed that besides "i" all else is the same meaning
ExtMapBuffer1[i]=mafilter (i);
ExtMapBuffer2[i]=trendstrength (i);


results are the same. both buffers depended on "i" but when "i" changes their results doesn't change according . I would be expecting them to be opposite at least.
Your original code was
ExtMapBuffer2[limit]=trendstrength (limit);

so unless you changed it that won't work.

You macount will never work, it increments each tick.

 

Yes I've change the code to :

int start()
  {
   int counted_bars=IndicatorCounted();
   if(counted_bars<look_back) counted_bars = look_back;

for(int i=Bars-1-counted_bars; i>=0; i--){
ExtMapBuffer1[i]=mafilter (i);
ExtMapBuffer2[i]=trendstrength (i);
ExtMapBuffer3[i]=macount(i);
}
//----
   
//----
   return(0);
  }

Now All three buffers are showing up as expected. WHen I tried to draw "i" onto the chart it is the same regardless of whether I'm counting backwards or forward .

I can't figure out why.

in this following code,

for(int i=Bars-1-counted_bars; i>=0; i--){

"i" will always be 0 since bars is always '1' ahead of counted_bars.

if the aim is to set i to be "0" why do i need the for lop to do that? I can just set all buffer to be buffer[0] manually

 

The first time or after a chart refresh, counted_bars will be zero and you do all bars. After that it will usually be just zero.

After a disconnect, counted_bars will be one behind the old Bars value. On reconnect Bars will have incremented by the number of new bars. You'll execute on the last bar zero with the final close value, and all new bars.

If you only do zero, then it will not update past bars
 
WHRoeder:
Your original code was

so unless you changed it that won't work.

You macount will never work, it increments each tick.


I've modified the code to add a loop for macount

double macount (double ped) {

int count = ped;
int counted_bars=IndicatorCounted();
if(counted_bars<ped) counted_bars = look_back;

      for(int i=Bars-1-counted_bars; i>=0; i--){
      if (mafilter(count+1)==1) {
      countma++;
      return (countma);
      }//end if
      if (mafilter(count+1)==-1) {
      countma =0; 
      return (countma);

But still this doesn't stop macount from counting every tick. You've mentioned about having a loop for each of the buffers calculation did i place the loop in the wrong place?

 

Hi

I'm back with my newly modified code.

for(int i= Bars-counted_bars-1; i>=0; i--) {
Print ("i in for loop is " , i);
ExtMapBuffer1[i]=mafilter (i);
ExtMapBuffer2[i]=trendstrength (i);
int macount;
if (mafilter(i+1) ==1) {
macount ++;
Print ("macount add 1. macount is now." ,macount);
}
if (mafilter(i+1) ==-1) {
macount=0;
Print ("macount reset to 0. macount is now." ,macount);
}

ExtMapBuffer3[i]=macount; 


} //end for

when i'm counting the number of bars when mafilter is 1, when it comes to the lastest bar, bar(0),

the count automatically reset to 1. and no longer increment. I;m sure that the code went thru the increment part but the result just get reset to 1.

Why the program behaving this way at bar(0)?

in your pervious post you mentioned i need to have 2 loops . can you elaborate further?

Reason: