Looping which checking many range for MA

Hi all,

I'd like to ask for some help with a code for some checking:

  • I'd like to do this checking only if there is a first day of a new month
  • I'd like to check range for MA(C,range) . The range should be checking between 10-500 and jump for each range is 10 . so...MA(C,10) -> MA(C,20) -> MA(C,30) -> ... ->MA(C,500)
  • I'd like to find which first range for MA is >L if next MA is <L
  • please see picture - the correct answer is range 100 -> MA(C,100)

I made a lot of atempts with a different loop (do, while, for) but unfortunately without success.

first_day = Month() != Ref(Month(),-1);


for(i=0; i<BarCount; i++)
{
	if(first_day[i])
	{
		.....
	}
}

aaa

Thank You in advance for any help :slight_smile:

You don't need BarCount loop.

mth = Month();
newmth = mth != Ref(mth,-1);

Plot( C, "", colorDefault, styleCandle );

ma_period = sum_cond = 0;
for ( i = 10; i <= 500; i += 10 ) {
	/// @link https://forum.amibroker.com/t/looping-which-checking-many-range-for-ma/9373/1
	/// by fxshrat@gmail.com
	myMA = MA(C, i);
	nextMA = MA(C, i+10);	
	cond = newmth AND myMA > L AND nextMA < L;
	sum_cond += cond;
	ma_period += IIf(cond AND sum_cond == 1, i, 0);
	printf( "\n%g, %g, %g, %1.5f, %g", cond, i, ma_period, myMA, L);	
	Plot( myMA, "MA"+ i, colorRed, styleNoRescale );	
}

SetChartOptions( 0, chartShowDates | chartShowArrows | chartWrapTitle );

MAnew = MA(C, ValueWhen(ma_period>0, ma_period));
Plot( MAnew, "MA"+SelectedValue(ma_period), colorYellow, styleLine, Null, Null, 0, 1 );

_N( Title = StrFormat( "{{NAME}} - {{INTERVAL}} - {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%), Vol %g \n{{VALUES}}",
                           O, H, L, C, SelectedValue( ROC( C, 1 ) ), V ) );

735

5 Likes

This is what I'm looking for.
Thank You so much !
:grinning: