# I want to check which moving is crossed recently from 40 different ma

Out of this All 40 Mov
i want to chk recently Price Crossed Which Mov
cross(c,MA(c,X));

Why do you create new thread on same topic? (Besides better create short topic names naming core issue.)

You could create multi-dim array since multiple periods can cause cross at same bar.
So you have an array of [n-cross occurrences] x [m-periods]. But multi-dim array is not mandatory...

1 Like

@rajan a possible solution (needs to be double checked for accuracy)

``````// using some of fxshrat's code from previous post
AboveTotal = 0;
BelowTotal = 0;

for( i = 10; i <=400 && i < BarCount; i+= 10 )
{
TheMA =  MA( C, i );

Above = C > TheMA;
Below = C < TheMA;

AboveTotal += Above;
BelowTotal += Below;

}

/////////////////////
///  Exploration  ///
/////////////////////
Filter = 1;
AddColumn( AboveTotal, "AboveTotal", 1.0, colorBlack, colorLime );
AddColumn( BelowTotal, "BelowTotal", 1.0 , colorBlack, colorRose );

// a large number of columns
for( i = 10; i <=400 && i < BarCount; i+= 10 )
{
AddColumn( MA( C, i ), "MA(" + i + ")", 1.2, colorBlack, IIf( C > MA( C, i ), colorLime, colorRose ) );
}

``````

Debug using an Exploration, 2 Likes

sry

Thanks to AmiBroker's biggest fan who has also answered on this thread , it was pointed out to me I could tighten up my code a bit,

``````AboveTotal = 0;
BelowTotal = 0;

for( i = 10; i <=400; i+= 10 )
{
TheMA =  MA( C, i );

Above = C > TheMA;
Below = C < TheMA;

AboveTotal += Above;
BelowTotal += Below;

}

/////////////////////
///  Exploration  ///
/////////////////////
Filter = 1;
AddColumn( AboveTotal, "AboveTotal", 1.0, colorBlack, colorLime );
AddColumn( BelowTotal, "BelowTotal", 1.0 , colorBlack, colorRose );

// a large number of columns
for( i = 10; i <=400; i+= 10 )
{
AddColumn( MA( C, i ), "MA(" + i + ")", 1.2, colorBlack, IIf( C > MA( C, i ), colorLime, colorRose ) );
}

``````
3 Likes

@portfoliobuilder,
You misunderstood a little bit...
I did not say "it could be tightened". I was only giving info that "&& i < Barcount" is not (really) required in this case (additional info now... because AB will not throw error message if "i" would be larger than Barcount-1) as there is not used subscripting ([...]) within that loop -> there is not any element wise operation. But both versions are OK here. So including "&& i < Barcount" (as in your other post) is not wrong.

2 Likes

I have similar Question about it . I How can I get Recently which moving average is crossed by Price

"AboveTotal " in your code will give me price is above how many of moving average . but how can we get which moving average is crossed recently

@Rakesh_Lala,

In case you want to store to array which periods were causing cross of price over MA plus MA value then you can do that via matrix operations.

No one knows what you mean by "recently" but if recently means "at most recent bar" then you may use LastValue() or LastVisibleValue or occurrence at Status( "LastBarInRange" ).

Either you keep column number static (column number is equal to number of different periods defined e.g from 10 to 400 and increment of 10). If being at last bar and there is a cross returning true then column(s) would show period in first row and MA value in 2nd row. If there is not a cross occurrence for some or all periods then column(s) would return initial matrix value (e.g. NULL).

``````startperiod = 10;
endperiod = 400;
incr = 10;

colnum = (endperiod-startperiod)/incr + 1;
period_mat = Matrix( 2, colnum, null );

n = 0;
for( j = startperiod; j <= endperiod; j += incr ) {
myma = MA(C, j );
cr = Cross( C, myma );

//if( LastValue( cr ) ) { // if cross at last bar
if( LastVisibleValue( cr ) ) { // if cross at last visible bar
period_mat[n] = j;// store period in 1st row
period_mat[n] = LastValue(myma);// store MA value in 2nd row
}
n++;
}

printf( MxToString( period_mat ) );
``````

So in below example output period 30,40,50 had cross at last visible bar Of course you could make matrix being dynamically sized (dynamic number of columns depending on cross returning true).

``````n = 0;
for( j = startperiod; j <= endperiod; j += incr ) {
myma = MA(C, j );
cr = Cross( C, myma );

//if( LastValue( cr ) ) { // if cross at last bar
if( LastVisibleValue( cr ) ) { // if cross at last visible bar
n++;
}

period_mat = Matrix( 2, Max(1, n), null );

n = 0;
for( j = startperiod; j <= endperiod; j += incr ) {
myma = MA(C, j );
cr = Cross( C, myma );

//if( LastValue( cr ) ) { // if cross at last bar
if( LastVisibleValue( cr ) ) { // if cross at last visible bar
period_mat[n] = j;// store period in 1st row
period_mat[n] = LastValue(myma);// store MA value in 2nd row
n++;
}
}

printf( MxToString( period_mat ) );
``````

So then in same example case number of columns is three now.
(Ma periods 30, 40, 50 in first row
MA values of each period in 2nd row) If there wouldn't be any cross occurence then Matrix output would be

``````{
{ NULL },
{ NULL }
}
``````
2 Likes

Thanks for such a detailed response.

Ihave just learnt Matrix function and nothing could be a better exercise than this.