amibroker

For loop doesn’t accept array. How to work around for this Amibroker code?


The following code will calculate the high-low range in percentage of certain bars when an event occurred and calculate the average of the high-low range.

event_up = get_event_up();
event_down = get_event_low();   
num_events_in_range = get_num_events_in_range();

sum_pct_updown_dist = 0;
for (i=1;i<num_events_in_range; i++) //for cannot accept array type 
{
    event_up_high_i = ValueWhen(event_up, High, i);
    event_down_low_i = ValueWhen(event_down, Low, i);
    pct_updown_dist_i = (event_up_high_i-event_down_low_i)/event_up_high_i*100;
    sum_pct_updown_dist = sum_pct_updown_dist + pct_updown_dist_i;
}
avg_pct_updown_dist = sum_pct_updown_dist/num_events_in_range;

The code doesn't work in Amibroker because of this line for (i=1;i<num_events_in_range; i++) which violates Amibroker syntax. For loop does not accept array type. num_events_in_range is an array.

How can this code be modified to work around this problem?


Solution

  • The syntax of for loop is

    for ( init-expression ; cond-expression ; loop-expression )
    

    In your code i < num_events_in_range is an invalid condition, since i is an integer and num_events_in_range is an array.

    In place of num_events_in_range, use the length of num_events_in_range