[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Loop Statements



PureBytes Links

Trading Reference Links

You do not have a variable to use in the loop count. You also need to
use that variable to reference the past values. You also probably
need to initialize the counter to zero each bar since you appear to
be trying to look back ten bars with a Count of zero before the test:

    Vars: Count(0), j(0);
    Count = 0;
    for j = 1 to 10 begin
       if Close[j-1] > Close[j] then Count = Count + 1;
       if Close[j-1] < Close[j] then Count = Count - 1;
    end;

A somewhat more elegant solution, rather than looking back at each
bar, is to keep a running count, "j", as you move forward. Then the
Count you are looking for is simply the value of "j" today less the
value 10 days ago. This code uses a built-in function "Sign" that
returns 1, 0, -1 so it makes the code very simple:

    Vars: Count(0), j(0);
    j = j + Sign(Close - Close[1]);
    Count = j - j[10];

The two solutions will give the same result after the start-up bars.
The latter code is much much faster since it doesn't have to count
back.

Bob Fulks

>Hello,
>
>I am trying to create a loop statement to count how many up days and how many down days for a specified look back period. For example, I wish to create a counter called "count" that will add and/or subtract 1 each time the market closes above/below previous close. I have tried the following but need some help;
>
>Variables: count(0);
>
>For 1 = 10 Begin
>If C > C[1] Then Count = Count + 1;
>If C < C[1] Then Count = Count – 1 ;
>End;
>
>This doesn’t work, as I don’t have much of an idea how these statements work. Can somebody please help me understand the fundamentals of these statements? Thanks.
>
>Jodie.