# Trouble with for operator

12

Hello coding Guru's.

I am trying to calculate the difference between (0 bar close, and 1 bar close), added to the difference between (1 bar close, and 2 bar close), and so on up to 50 bars. I can't wrap my head around it. Thanks so much in advance.

8801

Diggs:

Hello coding Guru's.

I am trying to calculate the difference between (0 bar close, and 1 bar close), added to the difference between (1 bar close, and 2 bar close), and so on up to 50 bars. I can't wrap my head around it. Thanks so much in advance.

Hi

I'm not at my trading PC so had to do this in notepad. Hopefully there's no compile error! ;) This will give you the raw difference in price. If you want it in pips or points then you'll need to do that calculation as well. I didn't understand if you wanted to add the whole sequence together up to 50 bars, or each pair of bars. If you want the total, just add another variable outside the loop and keep adding to that on each iteration.

double BarZeroToOneDiff;
double BarOneToTwoDiff;

for (int i=0; i<=50;i++)
{
BarZeroToOneDiff=Close[i]-Close[i+1];
BarOneToTwoDiff=Close[i+1]-Close[i+2];

// Now do whatever you want with the result
Print("Bar[",i,"]-Bar[",(i+1),"] = ",BarZeroToOneDiff);
Print("Bar[",(i+1),"]-Bar[",(i+2),"] = ",BarOneToTwoDiff);
}
12

Filter:
Hi

I'm not at my trading PC so had to do this in notepad. Hopefully there's no compile error! ;) This will give you the raw difference in price. If you want it in pips or points then you'll need to do that calculation as well. I didn't understand if you wanted to add the whole sequence together up to 50 bars, or each pair of bars. If you want the total, just add another variable outside the loop and keep adding to that on each iteration.

Thank you very much Filter for the speedy reply! Your answer seems a bit off of my goal, totally my fault for not being perfectly clear.

I would like find the difference of one bar close to the next indexed bar close...(bar 0 close - bar 1 close) + (bar 1 close - bar 2close) + (bar 2 close - bar 3 close) + (bar 3 close - bar 4 close).............+(bar 49 close - bar 50 close). Adding all of the differences together up to and including bar 50.

My goal is to find total price movement from close to close over 50 bars.

I am really not sure about how to nest the loops properly.

Again...thank you very much for your time and patience with me!

12

Thank You so much Filter! I think I have got it. Couldn't have done it without your help!

double BarZeroToOneDiff;
double total;

for (int i=0; i<=50-1;i++)
{
BarZeroToOneDiff=MathAbs(Close[i]-Close[i+1]);
total=total+BarZeroToOneDiff;

// Now do whatever you want with the result
Print("Bar[",i,"]-Bar[",(i+1),"] = ",BarZeroToOneDiff);