Close price below 5sma for 5 consecutive days

MT=C<MA(C,5);
GT=REF(MT,1) AND REF(MT,2) AND REF(MT,3) AND REF(MT,4) AND REF(MT,5) AND C>MA(C,5);
Filter = 1;
AddTextColumn(FullName(), "Name");
AddColumn( C, "Close", 1.2 );
AddColumn( MT, "aa", 1.2 );
AddColumn( GT, "bb", 1.2 );

It seems to get wrong.
Can anyone help me to correct it?

Please use code tags if inserting code. See here.


Your Ref() code looks forward since you have positive numbers as second argument.
Please read docs.
You have to insert negative values if wanting to look backwards.

MT=C<MA(C,5);
GT=REF(MT,-1) AND REF(MT,-2) AND REF(MT,-3) AND REF(MT,-4) AND REF(MT,-5) AND C>MA(C,5);
Filter = 1;
AddTextColumn(FullName(), "Name");
AddColumn( C, "Close", 1.2 );
AddColumn( MT, "aa", 1.2 );
AddColumn( GT, "bb", 1.2 );

But code can be written shorter using Sum() function

MT=C<MA(C,5);
GT=Ref(Sum(MT,5),-1) == 5 AND C>MA(C,5);
Filter = 1;
AddTextColumn(FullName(), "Name");
AddColumn( C, "Close", 1.2 );
AddColumn( MT, "aa", 1.2 );
AddColumn( GT, "bb", 1.2 );

Also you may use different Filter to output results only for cases where condition GT is true.

MT=C<MA(C,5);
GT=Ref(Sum(MT,5),-1) == 5 AND C>MA(C,5);
Filter = GT;
AddTextColumn(FullName(), "Name");
AddColumn( C, "Close", 1.2 );
AddColumn( MT, "aa", 1.2 );
AddColumn( GT, "bb", 1.2 );

BTW please do not write entire essays into thread subject. Make thread subject short and crisp.
You may write essays into post itself.
For further guidance please follow here.

3 Likes