How to count the fulfilment of a specific condition for the last N bars

Hello,

I would like to test a system which is supposed to count how many times the following condition is true for the last N bars:

Close > MA(Close, 12)

I should appreciate it if someone would help me to find the easiest way to programme it properly. Probably it is not so difficult, but I can not think of it.

Thank you in advance.

1 Like

@jptrader maybe something like this,

NumBars = Param( "NumBars", 10, 2, 50, 1 );

condition = Close > MA( Close, 12 );

BarsConditionTrue = Sum( condition, NumBars );

// the rest is for Exploration and debugging //
ConditionColour = IIf( condition, colorGreen, colorDefault );
Filter = 1;
AddColumn( C, "Close" );
AddColumn( MA( C, 12 ), "MA(12)", 1.2, colorDefault, ConditionColour );
AddColumn( BarsConditionTrue, "True in past " + NumBars + " Bars", 1.0 );

image

4 Likes

portfoliobuilder,

Thank you for your answer, so quick and illustrative.

Regards.