Good morning.
My level of English is very basic, I need to use a translator to follow instructions in the user guide. My level of programming formulas is not very good either and I read the instructions by working through trial, trial and error.. I apologize for that.

I have prepared a formula in which I want the result to be a leading moving average. The formula is very basic if I only want the result obtained to only look back 8 days. However, my interest is to carry this result for several years and therefore it is no longer possible unless I repeat the codes hundreds of times. Amibroker surely has a function that solves that problem and I would be grateful if someone can give me instructions or program the formula for me. I apologize again for my daring, reading in a language that the entire manual is not controlled is very complicated.

I put the formula that I have programmed for your review. Thank you very much.

``````
// First value. Output value: I have put 8 days but I need the value of 20 years ago.
// moving average of 3, S&P500 Future diary
// MA8 value 8 days ago = 3325.56
MA8 = 3325.56;

// MA3 value 7 days ago. The result of MA7 is solved with this formula
MA7 = (((MA8 * 2) + (Ref(Close,-7)))) / 3;  // Result = 3339.62

// MA3 value 6 days ago. The result of MA6 is solved with this formula ....etc.
MA6 = (((MA7 * 2) + (Ref(Close,-6)))) / 3; // Result = 3339.5

// MA3 value 5 days ago
MA5 = (((MA6 * 2) + (Ref(Close,-5)))) / 3; // Result = 3356.25

// MA3 value 4 days ago
MA4 = (((MA5 * 2) + (Ref(Close,-4)))) / 3; // Result = 3355.25

// MA3 value 3 days ago
MA3 = (((MA4 * 2) + (Ref(Close,-3)))) / 3; // Result = 3372.42

// MA3 value 2 days ago
MA2 = (((MA3 * 2) + (Ref(Close,-2)))) / 3; // Result = 3399.4

// MA3 value 1 days ago
MA1 = (((MA2 * 2) + (Ref(Close,-1)))) / 3; // Result = 3424.02

// MA3 value today
MA0 = (((MA1 * 2) + (Close))) / 3; // Result = 3441.64

Plot(MA0,"MA0", colorViolet, styleLine);
``````

Hello @mikelamezaga,

The idea behind your formula is intriguing - you could use a "for()" loop, with a parameter to limit the number of iterations/bars, particularly if using it on intra-day data, otherwise, you might be waiting a long time for your screen to refresh.

``````SetBarsRequired(sbrAll, sbrAll) ;   // Need this, otherwise AB throws an array index error

{
cnsEndBar	= BarCount ;
cnsStartBar	= Max(0, cnsEndBar - inpNumBars + 1) ;

lma	= Null ;
lma[cnsStartBar] = Close[cnsStartBar] ;

for (idx = cnsStartBar + 1; idx < cnsEndBar; idx ++)
{
lma[idx]	= ((lma[idx - 1] * 2) + (Close[idx])) / 3;
}

return lma ;
}

Plot(mlLma, "mlLma", ParamColor("mlLma color", colorRed), ParamStyle("mlLma style", styleLine)) ;
``````
1. The code of previous post is basically just a copy of AMA manual code posted view days ago.
2. You don't even realize while copying code that you re-created just AMA again. Nothing more nothing less.

That happens when you guys just copy and paste without thinking.

Don't you see that this one...

``````lma[idx] = ((lma[idx - 1] * 2) + (Close[idx])) / 3;
``````

...is yelling:

"Hey you, it's me... Ms. AMA. Can't you see me standing here tall and slim and blooming?
We've met already just recently. Remember? Look again:

``````lma[idx] = lma[idx - 1] * 2/3 + Close[idx] * 1/3;
``````

Do you recognize me now?"

So long story short... you do not need to use inferior BarCount loop but you just need inbuilt AMA()/AMA2() functions.

Solution (max. two lines of code):

``````mlLma = AMA(C, 1/3);
Plot(mlLma, "mlLma", ParamColor("mlLma color", colorRed), ParamStyle("mlLma style", styleLine)) ;
``````
2 Likes

Thx @fxshrat

Learning all the time. Thank you very much for your help. I feel happy.

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.