1

I have the following code for function count_event to count the number of times a certain condition related to an event happened in the past.

The problem with this code is it manually gets the information regarding each event one by one. It manually gets 3 events in the past defined by numBars which is the number of bars in the past to count the events. It is entirely possible that far more than 3 events happened in the past. I do not know how to work around this problem due to the way Amibroker handles array.

//get number of bars that have passed since nth occurrence when array was true
function bars_since_n(array, nth_occurence)
{
    bi = BarIndex();
    bars_since_nth = bi - ValueWhen(  array, bi, nth_occurence );

    return bars_since_nth;
}

//Count number of times a certain condition related an event happened in the past defined by numBars
function count_event(numBars)
{
    event_high = get_event();

    bars_since_high_1 = bars_since_n(event_high, 1);
    bars_since_high_2 = bars_since_n(event_high, 2);
    bars_since_high_3 = bars_since_n(event_high, 3);

    event_high_1 = ValueWhen(event_high, High, 1);
    event_high_2 = ValueWhen(event_high, High, 2);
    event_high_3 = ValueWhen(event_high, High, 3);

    count_1 = iif((event_high_1 >= 0.99*Close AND bars_since_high_1 <= numBars), 1, 0 );
    count_2 = iif((event_high_2 >= 0.99*Close AND bars_since_high_2 <= numBars), 1, 0 );
    count_3 = iif((event_high_3 >= 0.99*Close AND bars_since_high_3 <= numBars), 1, 0 );

    count = count_1 + count_2 + count_3;

    return count;
}
user3848207
  • 3,737
  • 17
  • 59
  • 104
  • 2
    This question is already answered in the amibroker community https://forum.amibroker.com/t/how-to-make-this-code-that-counts-the-number-of-times-a-certain-event-happened-in-the-past-more-elegant/14385/5 – Jonas Feb 20 '20 at 19:53

0 Answers0