moving sum of the signs of the bars

 

I want to create a moving sum of the sign of the bars: an up bar is +1, a down bar is -1, on a window of adjustable size: how do I work with a window? should be extern int window=10; and then??

   for(int i=0; i<limit; i++)
   {  
   if (iClose(0,0,i)>iClose(0,0,i+1)) sign[i]=1;
   else sign[i]=-1;
   sum[i]+=sum[i];
   }
 
int extern int Window.Length = 10;
:
SetIndexDrawBegin(0, Window.Length+1);
:
int counted = IndicatorCounted();
if (counted <= Window.Length) counted = Window.Length+1;  // Look back
for(int iBar = Bars - 1 - counted; iBar >= 0; iBar--){
   sum[iBar]=0;
   for (int iWin = iBar + Window.Length - 1; iWin >= iBar; iWin--)
      if (Close[iWin] > Close[iWin+1]) sum[iBar] += 1;
      else                             sum[iBar] -= 1;
}
 
WHRoeder:

Yes, I thought about doing it that way, then I realised how horribly inefficient it would be, especially if the Window is quite long. It seems that you could probably use the built-in moving average on array function of the plus or minus values.
 
Make it 100. How inefficient is it to test 100 values and increment/decrement 100 times per tick? Unnoticeable on a MHz machine and I'll bet yours is a GHz. Don't optimize until you know you have a problem. Then don't update bar zero (ibar >= 0 becomes iBar > 0). Now your testing/incrmenting once per new bar.
 
WHRoeder:
Make it 100. How inefficient is it to test 100 values and increment/decrement 100 times per tick? Unnoticeable on a MHz machine and I'll bet yours is a GHz. Don't optimize until you know you have a problem. Then don't update bar zero (ibar >= 0 becomes iBar > 0). Now your testing/incrmenting once per new bar.

That's true. I just hate the idea of inefficient code when all you need to do is add the new one and subtract the old one. I was brought up with punched cards, operating systems on mylar tape, and trying to fit code into the 1K memory of a ZX81! It's hard for me to code things non-optimally :-)

And even then on my dual core GHz machine my indicators run too slowly, lagging behind the market noticeably, and I have been thinking about upgrading to a ram-disk hard drive to speed things up.

EDIT: and of course when you change profiles, or change accounts, or change timeframe, and the indicator recalculates from scratch, this part of the calculation would then be 50x longer with the simple "unoptimized" method.

 
Thank you both.
Reason: