Australian (ASX) Stock Market Forum

Amibroker Optimisation gave unexpected results - Mean Reversion?

Joined
Jul 14, 2020
Posts
35
Reactions
32
I am doing some Amibroker testing, mostly to learn AFL, and I got some results that have confused me. I copied some code from a website to test the optimisation function and I thought it was testing an MA Crossover strategy, ie. buy when the shorter MA crosses up over a longer MA - indicating an uptrend....but the better results were the opposite of what I expected. Is that Mean Reversion?

I'm a noob, so I'm hoping someone can set me straight.

Here's the code (I've removed my comments to keep it clean). I ran it over the last couple of years on ASX200 equities.

//
fastlength = Optimize( "FMA", 100, 5, 100, 1);
slowlength = Optimize( "SMA", 25, 1, 100, 1);

FastMA = MA(C, fastlength);
SlowMA = MA(C, slowlength);

Buy = Cross(FastMA, SlowMA);
Sell = Cross(SlowMA, FastMA);
//

The best results seem to be where when FastMA = 18 and the SlowMA = 3...or numbers similar to that. At first glance, this wsa the opposite of what I expected.

When I backtested it (as opposed to running an optimisation) I looked at some of the individual trades and found that it was buying when the FastMA (eg 18) crossed above the SlowMA (eg 3). This means you're buying as the price has dropped below the longer MA, is that a Mean Reversion strategy?
 

MovingAverage

Smoke me a kipper
Joined
Jan 23, 2010
Posts
495
Reactions
789
your fastlength and slowlength default parameters should be the other way around. fastlength = 25 and slowlength = 100. In fact just rename "fastlength" to slowlength and "slowlength" to fastlength and you should be right. Try that and re-run your sims it should make sense then. With the parameters you posted your fast MA is actually a slow MA and your slow MA is actually a fast MA. Conventionally a short MA has a shorter period than a slow MA
 
Last edited:
Joined
Jul 14, 2020
Posts
35
Reactions
32
your fastlength and slowlength default parameters should be the other way around. fastlength = 25 and slowlength = 100. In fact just rename "fastlength" to slowlength and "slowlength" to fastlength and you should be right. Try that and re-run your sims it should make sense then. With the parameters you posted your fast MA is actually a slow MA and your slow MA is actually a fast MA. Conventionally a short MA has a shorter period than a slow MA
This was my thought as well when I copied the code in, a fast ma must be shorter...but I thought (being a noob) that I was wrong. The code is still on the site and there's even a YouTube video...but no one seems to have picked up on that glaring mistake. Glad my initial reaction was right.

Thanks for setting me straight.
 

MovingAverage

Smoke me a kipper
Joined
Jan 23, 2010
Posts
495
Reactions
789
This was my thought as well when I copied the code in, a fast ma must be shorter...but I thought (being a noob) that I was wrong. The code is still on the site and there's even a YouTube video...but no one seems to have picked up on that glaring mistake. Glad my initial reaction was right.

Thanks for setting me straight.
Who would have thought you couldn't trust code grabbed off the internet :laugh::laugh: It's a great way to learn...roll up your sleeves an get stuck in to some AFL coding...well done mate you'll learn a lot :xyxthumbs
 
Joined
Nov 27, 2017
Posts
1,156
Reactions
1,741
but just assumed that the site I'd got the coffee from must have been right.
Well I hope the coffee was better than the code ;)

All good mate, and probably a good lesson to learn early on. I like to use the Exploration to help see what is going on and of course charting helps as well.

I have done a coupe of little mod's to your code and in doing so found the error in your ways, as it appears you have swapped the fast and slow period ranges around

fastlength = Optimize( "FMA", 100, 5, 100, 1);
slowlength = Optimize( "SMA", 25, 1, 100, 1);
Fixed code with some additions to help fault find

Code:
slowlength = Optimize( "SMA", 100, 5, 100, 1);
fastlength = Optimize( "FMA", 25, 1, 100, 1);

SlowMA = MA(C, slowlength);
FastMA = MA(C, fastlength);

Buy = Cross(FastMA, SlowMA);
Sell = Cross(SlowMA, FastMA);


Plot( C, "Price", colorDefault, styleBar );
Plot( SlowMA, "SlowMA", colorBlue, styleLine);
Plot( FastMA, "FastMA", colorRed, styleLine);


if( Status( "Action" ) == actionExplore );

Filter = Buy OR Sell;

AddColumn(Close, "Close", 1.2, colorDefault, colorDefault, 50);
AddColumn(ROC( C, 1 ), "%", 1.2, colorDefault, colorDefault, 50);
AddColumn(SlowMA, "SlowMA", 1.2, colorDefault, colorDefault, 50);
AddColumn(FastMA, "FastMA", 1.2, colorDefault, colorDefault, 50);

PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);
PlotShapes(IIf(Buy, shapeupArrow, shapeNone),colorWhite, 0,L, Offset=-45);

PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed, 0, L, Offset=-60);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange, 0,L, Offset=-70);
PlotShapes(IIf(Sell, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-65);


upload_2020-8-14_18-25-53.png
 
Joined
Jul 14, 2020
Posts
35
Reactions
32
Thanks Trav, yeah if you look up the thread, this was the discovery...and was my suspicion early on. It just seemed backward [emoji15].

It was definitely a good learning, to go through the trades and verify what I suspected.

Thanks for refining the code. I've re-run mine just to see the proper output, which worked well.

I'll try yours over the weekend.

I have to start with simple code before I start getting into loops and more complex entry and exit strategies.
 

Similar threads

Top