Ranking based on Moving Averages over 12 months, Formula help needed!

I want to create a 'rank' factor based on the monthly increases in Moving Averages over a year, such that the more recent Moving Average get more weight over the older ones. So points are given for each month the Moving Average was higher than the previous month's Moving Average, with the more recent Moving Averages giving more points.

This is the (incorrect) formula. Please correct it for me!

MA11MonthBack = MA(ref(C, 30),330);
MA10MonthBack = MA(ref(C, 30),300);
MA09MonthBack = MA(ref(C, 30),270);
MA08MonthBack = MA(ref(C, 30),240);
MA07MonthBack = MA(ref(C, 30),210);
MA06MonthBack = MA(ref(C, 30),180);
MA05MonthBack = MA(ref(C, 30),150);
MA04MonthBack = MA(ref(C, 30),120);
MA03MonthBack = MA(ref(C, 30),90);
MA02MonthBack = MA(ref(C, 30),60);
MA01MonthBack = MA(ref(C, 30),30);
MA00MonthBack = MA(C,30);

rank = 0;

if (MA00MonthBack  > MA01MonthBack) rank = rank + 18;
if (MA01MonthBack  > MA02MonthBack) rank = rank + 16;
if (MA02MonthBack  > MA03MonthBack) rank = rank + 14;
if (MA03MonthBack  > MA04MonthBack) rank = rank + 12;
if (MA04MonthBack  > MA05MonthBack) rank = rank + 10;
if (MA05MonthBack  > MA06MonthBack) rank = rank + 08;
if (MA06MonthBack  > MA07MonthBack) rank = rank + 07;
if (MA07MonthBack  > MA08MonthBack) rank = rank + 06;
if (MA08MonthBack  > MA09MonthBack) rank = rank + 05;
if (MA09MonthBack  > MA10MonthBack) rank = rank + 04;
if (MA010MonthBack  > MA11MonthBack) rank = rank + 03;

@Neole the correct way to format some of your lines,

MA11MonthBack = Ref(MA(C, 30),-330);

// and the other portion

rank = iif(MA00MonthBack > MA01MonthBack, rank + 18, rank);
2 Likes

Thanks that works perfectly!

By the way, it should be noted that AmiBroker daily bars correspond to trading days. So if you're considering stocks that only trade on weekdays, then a typical month is 22 bars (trading days), and a year is 252 bars (trading days), at least for US Markets with their holiday schedule.

1 Like

Thanks for the information! I will change the number of days accordingly.