Using the BARSTRUE function

The BARSTRUE function calculates how many times a condition is true over a given lookback period. In this example, we want to know when the low price has crossed the 13period EMA for the first time in 10 days. To do this we can create 2 variables, one for the crossover and one for the barstrue lookback, and then return the result when both are true:

c1 = LOW() CrossesBelow MA(BARS=13, STYLE=Exponential);
c2 = BARSTRUE(LOW()[1] > MA(BARS=13, STYLE=Exponential)[1], LOOKBACK=10) == 10;
c1 and c2

In the c2 condition we’re looking at 10 bars prior to the crossover (c1) where the low is above the 13EMA every day, so == 10 (remember to use the ‘==’ for equality).

Capture

If you wanted to look back 20 days and know when any 15 days out of the 20 are above the 13EMA you would use the following for c2:

c2 = BARSTRUE(LOW()[1] > MA(BARS=13, STYLE=Exponential)[1], LOOKBACK=20) == 15;

Tom McClellan’s 9 Day Up-Down Oscillator (as seen here) can be calculated with the following BARSTRUE() function:

V1 = BARSTRUE(CLOSE() IsUp, LOOKBACK=9)/9 * 100;
MA(V1, BARS=3, CALC=Close)

Turn it in to a boolean (ie MA(V1, BARS=3, CALC=Close) > 70) to colour the bars. Workbook attached.

BarsTrue.owb (14.4 KB)