How to code for consecutive signals

Consider a situation where I need to check for 3 consecutive close above RSI(14). Not being from programming background, I attempted to code it this way

Filter = Close> RSI(14) AND Ref(Close,-1)>Ref(RSI(14),-1) AND Ref(Close,-2)>Ref(RSI(14),-2);

Intuitively, this piece of code appears crude and not so elegant. Now, if I have to do a backtest on sample data, say this time with 5 consecutive signals, then adding more instances of ref() seems wrong per se.

a) There should be a simpler and elegant method to filter for consecutive signals wherein one can control the number of repeat from another variable.

b) What would be best method to identify, say for example, repeat in 4 out of last 6 bars.

Thanks much

1 Like

Hi @Andy. The first problem is that you're comparing a price to an RSI value, which is probably not what you want to do. More likely, you wanted to look for RSI(14) > x for N out of the last M periods. To do that, you could use something like the following untested code:

minSignals = 4;
lookback = 6;
rsiThreshold = 70;

Condition = Sum(RSI(14) > rsiThreshold, lookback) >= minSignals;

Notice that I did not use the variable name Filter, as that is a built-in AmiBroker variable used for Explorations.

Breaking down the final line of the example, here is what's happening, working from the inside out:

  1. Find all bars where RSI(14) is greater than 70. The result is an array of Boolean values
  2. Sum up the boolean values over periods of 6 bars
  3. If the sum is >=4, that tells us that the signal (step 1) has occurred four or more times during the current bar plus the previous five bars (i.e. 6 bars including current).
  4. Each element in the Cond array will be true or false depending on whether the "4 signals in 6 bars" condition was met.
9 Likes

@Andy Matt's solution is excellent and you were correct to look for a more elegant solution. I thought this might be a useful opportunity to show that often there are many ways to achieve the same result in AmiBroker. The real programmers out there can discuss the advantages and disadvantages of the solutions as I am not a programmer so am uncertain what's best.

But for an example if you were looking for 3 consecutive "Up" closes or N consecutive "Up" bars some possible solutions, (Here I give a shout out to Cesar Alvarez, Matt's old colleague who taught me variations on the following)

///@link http://forum.amibroker.com/t/how-to-code-for-consecutive-signals/4744
// Different ways of coding Close Up N_Bars days in a row

N_Bars = Param( "N_Bars", 3, 1, 10, 1 );

Method1 = Sum( C > Ref( C, -1 ), N_Bars ) == N_Bars;
Method2 = Sum( ROC( C, 1 ) > 0, N_Bars ) == N_Bars;
Method3 = BarsSince( C <= Ref( C, -1 ) ) >= N_Bars;
Method4 = BarsSince( ROC( C, 1 ) <= 0 ) >= N_Bars;
Method5 = HHV( ROC( C, 1 ) <= 0, N_Bars ) == 0;

Produce the same result which when I use an Explore to debug looks like this,
image

And there are probably more ways to come up with that too. As you coded a simple method if it was just for 3 bars, perhaps not "elegant" but they work.

Up3 = C > Ref( C, -1 ) and Ref( C, -1 ) > Ref( C, -2 ) and Ref( C, -2 ) > Ref( C, -3 );

AnotherUp3 = ROC( C, 1 ) > 0 and Ref( ROC( C, 1 ), -1 ) > 0 and Ref( ROC( C, 1 ), -2 ) > 0;


4 Likes