Need some logic for this idea

exploration
Tags: #<Tag:0x00007f2d79f86850>

#1

i want to compare the number of bars between Moving Average Crossovers. For eg. I have a 9 bar SMA and 50 bar SMA.
how can I check if the prev 9bar crossed below 50 bars had more or less bars compared to the previous 9 bars crossed above 50 bars.

_SECTION_BEGIN( "MA" );
P = ParamField( "Price field", -1 );
Periods = 9;
stopline = MA( P, Periods );
Plot(ma9= MA( P, Periods ), _DEFAULT_NAME(), ParamColor( "Color", colorCycle ), ParamStyle( "Style" ) );
_SECTION_END();

_SECTION_BEGIN( "MA1" );
P = ParamField( "Price field", -1 );
Periods = 50;
Plot(ma50= MA( P, Periods ), _DEFAULT_NAME(), ParamColor( "Color", colorCycle ), ParamStyle( "Style" ) );
_SECTION_END();

cross9Above50=Cross(ma9,ma50);
cross9Below50=Cross(ma50,ma9);

#2

If I understand you correctly, you want to count the number of bars that are spent above and below the crossovers. A possible way is to use the Flip and BarsSince functions.

https://www.amibroker.com/guide/afl/flip.html

https://www.amibroker.com/guide/afl/barssince.html

A possible code would look something like this.

ma9 = MA( C, 9 );
ma50 = MA( C, 50 );

Cross9Above50 = Cross( ma9, ma50 );
Cross9Below50 = Cross( ma50, ma9 );

Condition1 = Flip( Cross9Below50, Cross9Above50 );
Condition2 = Flip( Cross9Above50, Cross9Below50 );

Count1 = BarsSince( Condition1 ); // if you don't want to count the crossing bar, then "-1"
Count2 = BarsSince( Condition2 );

Filter = 1;

// I like to make my Explorations "easy on the eyes"
dynamic_color = IIf(Cross9Above50 OR Cross9Below50, colorYellow, colorDefault );
dynamic_color2 = IIf(Count1 >0, colorlime, colorDefault );
dynamic_color3 = IIf(Count2 >0, colorRose, colorDefault );

Filter = 1;
AddColumn( Close, "Close" );
AddColumn( ma9, "ma9", 1.2, colorDefault, dynamic_color );
AddColumn( ma50, "ma50", 1.2, colorDefault, dynamic_color );
AddColumn( Count1, "Count1", 1.0, colorDefault, dynamic_color2 );
AddColumn( Count2, "Count2", 1.0,colorDefault, dynamic_color3 );


// Charting //
Plot( Count1, "Count1", colorGreen, styleThick );
Plot( Count2, "Count2", colorRed, styleThick );

Producing an Exploration and chart as below example.


#3

thank you for the help.