comparing all bars in a period to one another

Back to topics list To post a new topic, please log in or register
avatar
56
Dannoo007 2016.03.03 19:42
 

Hello forum.

I am looking to set up this indicator where I want to add ALL differences among closing price

 - for ALL bars,

 - compared to one another,

 - within specified Period.


How do you go about it?? I was thinking of a "Nested FOR" logic, but it wouldn't compile (see below).

before I double check, will this ever work, at a conceptual level, or should this be done differently? 

simplified excerpt from OnCalculate() :


etc etc
//main cycle
int i,j,pos;
pos=Period-1;   
for(i=pos; i<rates_total && !IsStopped(); i++)
     {
     for (j=pos; j<rates_total && !(j==i); j++)
      {    
      double sum=0.0;
       sum+=(close[i]-close[j]);
      
etc etc
avatar
12985
WHRoeder 2016.03.03 19:50 #
 
  1. You can't sum when you clear it each time in the inner loop.
  2. No need for the j==i, start the loop at j=pos+1
avatar
66
jamescater 2016.03.03 20:23 #
 
Dannoo007:

Hello forum.

How do you go about it??

before I double check, will this ever work, at a conceptual level, or should this be done differently? 

double sum = 0.0;
int    Period = 10;
int    pos = Period - 1;
    
for(int i = pos + 1; i < rates_total; i++)
{
   sum += close[i] - close[i-1];
}


Although the loop is redundent because you are just summing the differences between the close prices. The problem can be reduced to the following

int    Period = 10;
int    pos = Period - 1;
double sum = close[pos] - close[rates_total-1];
Back to topics list  

To add comments, please log in or register