Bars SInce recent cross above/below MACD zero line

Dear Experts,

Please help me in getting the number of bars since the recent cross above/below the MACD zero line. Tried various options but none of them seam to work properly.

image

Any help will be highly appreciated.

Thanks.

_SECTION_BEGIN("MACD");
r1 = Param( "Fast avg", 12, 2, 200, 1 );
r2 = Param( "Slow avg", 26, 2, 200, 1 );
r3 = Param( "Signal avg", 9, 2, 200, 1 );
Plot( ml = MACD(r1, r2), StrFormat(_SECTION_NAME()+"(%g,%g)", r1, r2), ParamColor("MACD color", colorRed ), ParamStyle("MACD style") );
Plot( sl = Signal(r1,r2,r3), "Signal" + _PARAM_VALUES(), ParamColor("Signal color", colorBlue ), ParamStyle("Signal style") );
Plot( ml-sl, "MACD Histogram", ParamColor("Histogram color", colorDefault ), styleNoTitle | ParamStyle("Histogram style", styleHistogram | styleNoLabel, maskHistogram ) );
_SECTION_END();

Title="Above ="+WriteVal(BarsSince(ml < sl),1)+ "\nBelow ="  +WriteVal(BarsSince(ml > sl),1) ;

Hope this helps.

4 Likes

@Anthony,
Exactly this is what I was looking for.
Thank you very much,

Regards,
Venkat

Your welcome, could you mark the solved icon and possibly like icon. Thanks

1 Like

This is great I think. However, you could easily refine this algorithm using the MACD strategies as written in this blog.
Hope this helps!