My indicator is calculation 4 times every bar

 

Hi,

why is the indicator calculating the value 4 times every bar???

int start()
{
   int    counted_bars=IndicatorCounted();
   if (counted_bars < 0) return(-1);
   if (counted_bars > 0) counted_bars--;
   int    pos=Bars-counted_bars;
 
   if( ThisBarTime == Time[0] ) return(0); 
      else ThisBarTime = Time[0];
             
   while(pos>=0)
   {  
     ...here is my code
     pos--;
 
   }
return(0);
}
 

Is this live or in the Strategy Tester ?

As far as I can see from the code you have posted the Indicator isn't " calculating the value 4 times every bar" what makes you think it is ?

 

For testing I wrote this code:

int start()
  {
   int    counted_bars=IndicatorCounted();

   if (counted_bars < 0) return(-1);
   if (counted_bars > 0) counted_bars--;
   
   int    pos=Bars-counted_bars;

   if( ThisBarTime == Time[0] ) return(0);
   else ThisBarTime = Time[0];
   // it must be a new bar

             
   while(pos>=0)
   {  
   Print(Symbol()+ ": pos=" +pos);   

   pos--;
 
   }
  
//----
   return(0);
  }

I am getting the following print-output:

2011.10.17 10:16:29     test_indi USDJPY,M1: USDJPY: pos=0
2011.10.17 10:16:29     test_indi USDJPY,M1: USDJPY: pos=1
2011.10.17 10:16:29     test_indi USDJPY,M1: USDJPY: pos=2
2011.10.17 10:16:29     test_indi USDJPY,M1: USDJPY: pos=3
2011.10.17 10:16:28     test_indi GBPUSD,M1: GBPUSD: pos=0
2011.10.17 10:16:28     test_indi GBPUSD,M1: GBPUSD: pos=1
2011.10.17 10:16:28     test_indi GBPUSD,M1: GBPUSD: pos=2
2011.10.17 10:16:28     test_indi GBPUSD,M1: GBPUSD: pos=3
2011.10.17 10:15:34     test_indi USDJPY,M1: USDJPY: pos=0
2011.10.17 10:15:34     test_indi USDJPY,M1: USDJPY: pos=1
2011.10.17 10:15:34     test_indi USDJPY,M1: USDJPY: pos=2
2011.10.17 10:15:34     test_indi USDJPY,M1: USDJPY: pos=3

If I understood everything right, that means that every bar the LAST 4 Bars a calculated for my indicator.

When I put the indicator on my chart, it looks fine!

I am saving the High and the Low from the last bar in my local variables and compare the new bar with this values, to check if it between or not. When the value is between, I don't change my local variables and on the next bar I want to compare with these variables and so on.

On the live running the indicator doesn't work very well, I think because it is recalculation the last 4 bars.

How do I have to change the code, so that I am working only with the new bar?

Or do I think anything wrong??

 
sunshineh:

How do I have to change the code, so that I am working only with the new bar?

Or do I think anything wrong??

I think the question you need to answer is why is pos=3 ?
 

Yes correctly.

Why it is always 3 and not 1?? I have tested the code above on a 4 and a 5 digit test account. But everywhere it works the same!!

I am using only the basic functions...

 

Add a print statement after this line: int pos=Bars-counted_bars; and print out the following . . .

counted_bars, Bars, pos

 

Here only a small part of the output

When the pos is changing to 3 the calculation starts again.

2011.10.17 11:03:44     test_indi EURUSD,M1: counted_bars: 1916795 Bars: 1916797 pos: 2
2011.10.17 11:03:35     test_indi EURUSD,M1: counted_bars: 1916795 Bars: 1916797 pos: 2
2011.10.17 11:03:29     test_indi EURUSD,M1: counted_bars: 1916795 Bars: 1916797 pos: 2
2011.10.17 11:03:28     test_indi EURUSD,M1: EURUSD: pos=0
2011.10.17 11:03:28     test_indi EURUSD,M1: EURUSD: pos=1
2011.10.17 11:03:28     test_indi EURUSD,M1: EURUSD: pos=2
2011.10.17 11:03:28     test_indi EURUSD,M1: EURUSD: pos=3
2011.10.17 11:03:28     test_indi EURUSD,M1: counted_bars: 1916794 Bars: 1916797 pos: 3
2011.10.17 11:03:22     test_indi EURUSD,M1: counted_bars: 1916794 Bars: 1916796 pos: 2
2011.10.17 11:03:22     test_indi EURUSD,M1: counted_bars: 1916794 Bars: 1916796 pos: 2
 
So pos is 3 when you actually have a new bar to calculate ? yet then you go and calculate 4 bars ? ?
 

Why don't you just use:

datetime currBar;

int start()
{
if(currBar!=Time[0]){
currBar = Time[0];
//Rest of code
}
}

That should only calculate once per bar, it's what I often use in my indicators.

 
heelflip43:

Why don't you just use:

That should only calculate once per bar, it's what I often use in my indicators.

Didn't you look at the OPs code ?
 

The decrement is unnecessary

The first time, IndicatorCounted is zero so you are referencing outside the arrays.

OP
Corrected
 int    counted_bars=IndicatorCounted();
   if (counted_bars < 0) return(-1);
   if (counted_bars > 0) counted_bars--;
   int    pos=Bars-counted_bars;

   while(pos>=0)
   {  
     ...here is my code
     pos--;
 
   }

#define DRAWBEGIN 0 // How many bars back from pos is accessed
                    // High[pos+2] means DRAWBEGIN == 2
                    // iMA(NULL,0, MAperiod, ..., pos) DRAWBEGIN == MAperiod
int counted_bars=IndicatorCounted();
if (counted_bars < 0) return(-1);
if (counted_bars < DRAWBEGIN)   counted_bars = DRAWBEGIN;
for (int pos = Bars - 1 - counted_bars; pos >= 0; pos--){
    ...here is my code
}

Reason: