# comparing all bars in a period to one another

195

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`
15086

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
4304

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];```