Hi Zeek --
The for loop indexed by i steps through each bar, starting at the oldest and
going through the newest or most recent.
Within that loop, write whatever code you need to compute whatever you want
to know about each bar.
The example you have written uses a for loop indexed by j to reference each
bar from the first bar newer than the ith bar to the most recent data.
Without knowing what it is you want to accomplish, I cannot tell you whether
that loop construction is correct or not.
But, it is more common for the bars at and older than the ith bar to be the
ones referenced. That is, the j variable will computed so that every bar
referenced is older than i. Something like this:
for (j=0;j<5;j++)
{
k = i-j;
something = c[k];
}
When you do this, start the i loop far enough into the data so that k is
never less than 0.
Something like this:
for (i=5; i<BarCount; i++)
{
for (j=0;j<5;j++)
{
k = i-j;
something = c[k];
}
}
--------
Finally -- use a variable rather than the number 5. Something like this:
lookback = 5;
for (i=lookback; i<BarCount; i++)
{
for (j=0;j<lookback;j++)
{
k = i-j;
something = c[k];
}
}
The way this example is written, it looks into the future. That might be
valuable if you are investigating something, but it cannot be done in real
time because we cannot know tomorrow's data.
----------
The statement i = j; is an assignment statement -- not a test of equality.
It assigns to the variable i the value currently stored in the variable j.
In this case, it is poor programming practice, since i is the index variable
of a for loop. Better practice is to allow the for loop to control the i
variable itself, and not change it within the loop. Depending on the
compiler, the results of changing the value of the index variable within the
loop may give you exactly what you want (whatever that is), or it may cause
abnormal results.
The short advice is -- don't do that.
-------------
Thanks,
Howard
On Mon, May 11, 2009 at 5:58 AM, zeek ing <[email protected]> wrote:
>
>
> Hello all
> I am trying to understand loops more clearly. I have a question on
> some code that i have been studying maybe someone can help:
> If I have a loop
>
>
> for( i = 0; i < BarCount; i++ )
> {
> condition .....
>
> {
>
>
> for (j = i + 1; j < BarCount; j++)
> { ........ .....
>
>
> i = j;
> break;
> }
> }
>
>
> I am trying to understand this loop construct.
> a) why would i set J to i+1? why not have j=i?
> b) what does i=j accomplish??
>
>
> if anyone can clarify that would be great, I have seen this loop construct
> a few times b4.
>
>
> thanks
> zeek
>
>