Back to Community
Finding the best moving averages (now with 2012 testing period)

Market Tech kindly reminded me that blindly trying to over-fit an algorithm for one testing period, will not result in values that are optimized for another time period. This notebook is a good example of that principle.

My previous notebook tested the year 2014 with AAPL stock and found that moving averages of 25 and 70 days provided the best results (even though these results didn't beat the actual performance of Apple stock). However, this new notebook calculates that the best moving averages for 2012 were 15 and 25 days.

It might be beneficial to test for optimal values once a week, and "walk-forward" by updating your algorithm with the most recent optimal values. However, this simple moving average example is not likely to be one that can predict future optimal values.

Loading notebook preview...
Notebook previews are currently unavailable.
17 responses

Indeed, parameter optimization (heck, having parameters at all) opens the door to over-fitting. Remember, if you run a moving average parameter optimization on entirely random data, some of them will still look excellent, but it's all meaningless. This is probably the most depressing part of building strategies in my opinion. The temptation to keep trying random nonsense until something backtests well is overwhelming, but dangerous and futile.

Tristan R., Your efforts are not in vain. I think your research projects are going to incentivize many to try it out. So thanks up front for that.

You're lucky the optimization is so difficult. Early on when I first started build strategies I had access to a superior strategy construction tool and optimizer. Exhaustive and stochastic parameter sets could be built and run through any strategy. I built some doozies and optimized the hell out of them. Millions of tests. Incredible "ramp-to-the-right" P&L lines. "Boy," I thought, "I'm gonna be rich!" Well, the guys I was working for would smilingly accept my fantastical charts and caution me with links to PDFs and various books I should read. "Paper trade your best candidates," they would advise, "if they're good we'll find you some investors." And so I paper traded. And I retested after months of new out of sample data. After a year of flattening results I was not quite cured. But I was suspicious. After five years of building strategies and testing millions more I was generally skeptical. After 10 years now I am down right cynical.

But this stuff is like the fountain of youth. Ponce de Leon would have made an exceedingly poor trader. Over and over I blunder back to refill my cup with buoyant optimism. And over and over I'm reminded of the futility of it all. But by all means, try, try again. Who knows, maybe its just the luck of the draw and you'll strike a spring of gushing financial glory.

With our eyes we instantly identify graph curve peaks and valleys. I thought it might be interesting if an average periodicity of those local highs/lows could be determined programmatically per symbol and whether they might correlate to one's empirically determined optimal lookback windows, then maybe they could be adjusted dynamically. I haven't been able to think of a way to determine highs/lows looking back.

Some people have attempted to use fourier transforms to identify dominant frequencies. I don't think it works very well though.

As I said that, I wondered if screening stocks for dominant tradable frequencies would have any value in selecting a portfolio to trade thusly...

@Tristan. I might have missed your explanation somewhere. Is there a reason you use 282 days to calculate your Sharpe ratio?

sharpe = (perf_manual.returns.mean()*282)/(perf_manual.returns.std() * np.sqrt(282))

@Simon, years ago I thought the same thing. I figured every instrument must have some recurring behavior driven by internals particular to only it. Whether weather, seasonality, or business cycles, or earnings, or FOMC, or CEO inspired holidays or market maker foibles I proposed that something probably happened in cycles. And the way I think I solved it (in my own primitive fashion) was similar to Tristan's approach. I searched for the frequency of moving average crosses with small STD around the mean. I figured if some instrument crossed, on average, every 37 days with STD of oh, say 3, that I could identify those and trade them waiting until they became overdue to cross one way or another. Didn't work. Or maybe it did and I was so disheartened by that time I merely thought my results were anomalous, spurious, and therefore worthless.

Yeah I would be surprised if it did...

Oh, there are definitely cyclical patterns that exist -- maybe not in equities, but in futures : Capturing those however was problematic as they are more subtle than one would hope. Often overlaid with big decadal weather patterns or disruptive governmental injections. Commodities are so much more entertaining that equities due to the global nature of the agent players.

I figured most of those seasonal patterns were just artifacts of the generation of continuous contracts, stitching together non-fungible contracts. Are there actually any reliable predictive patterns in futures, beyond the usual momentum and spreads?

Hard to say. As a statistician you'd have to argue no. But all the farmers out there who do spend time trying to hedge to all the supply chain agents to the grain and consumable metals industries -- I'd bet they take seasonality pretty seriously. I know from my watching the COT for years (a favorite pastime of mine, (gawd I must be strange)) I've seen the impacts of weather and blights and political turmoil. Cyclical? Some of it.

@tristan is this available nowin quantopian or zipline only for now??

The notebook I shared above was created in Quantopian's Research tool, powered by Zipline (which uses a slightly different syntax).

You should be able to get access to the Research tab of Q's website (this is located between Community and Algorithms) by submitting an algorithm to the monthly contest. Create an algo in minute mode, submit a full-back test, and the select "Enter Contest".

Let me know if you have more questions!

Hi Tristan... can you make this note clonable... I'm having some issues just by copy paste it didnt work....

This is good example of notebook (and what you can do) for us newbies.

Same. I would like to learn how to do this.

The best way is to just do it...(although I'm not going to go down the route of fitting the parameters in the back test).
Here's my first notebook.

Loading notebook preview...
Notebook previews are currently unavailable.