🐍 Why your moving average window is probably wrong


EXCLUSIVE PARTNER DEAL FOR PYQUANT NEWS READERS:

15% off virtual private trading servers

Host your trading algorithms on secure, low-latency environments. QuantVPS’s robust infrastructure lets you focus on perfecting your strategy, not babysitting your computer.

Most beginners pick a moving average window that looks good on old data, then watch it fail on new data (here's how to fix that)

You've probably spent time tweaking a moving average, trying 20 days, then 30, then 50, looking for the one that makes your strategy look profitable.

Each time you change the number, the results shift.

You're not learning anything about the market.

You're just fitting your analysis to the past.

Studies consistently show that most retail traders lose money, and a big reason is building strategies around parameters that only worked on historical data.

When you pick the moving average window that looks best in a backtest (a simulation of how a strategy would have performed on past prices), you're almost guaranteed to be disappointed when you trade it live.

The strategy didn't find a real pattern.

It found a coincidence.

The good news?

There's a way to get past this.

Instead of guessing at parameters, you can use methods that adapt automatically to new data as it arrives.

That removes one of the biggest sources of false confidence in early-stage trading research, and it's a skill that translates directly into building strategies you can actually trust with real money.

Here's a Python example you can use now.

Assumes `prices` is a pandas Series of daily closing prices with a datetime index, and that `KalmanFilter` has been imported from `pykalman`.

This code produces a smoothed price series that updates itself with every new data point.

Unlike a traditional moving average where you have to pick a fixed number of days, the Kalman filter decides at each step how much to trust the new price versus its running estimate.

The ratio between the two covariance parameters (observation_covariance and transition_covariance) is the only thing you set, and it controls how smooth the output is.

A higher ratio means the filter treats more of the daily price movement as noise and smooths more aggressively.

You can use this smoothed series anywhere you’d normally use a moving average, as a trend indicator or as a baseline to measure how far the current price has moved from its recent average.

Getting Started With Python for Quant Finance walks through this exact kind of workflow, from pulling market data to testing whether a trading idea actually makes money on data it hasn’t seen before.

The course covers how to avoid the common traps that make strategies look good in testing but fall apart in live trading.

Inside Getting Started With Python for Quant Finance

  • 40 code templates. Instead of writing a Kalman filter or a backtest (a historical simulation of a strategy) from scratch, you start with working code and modify it for your own ideas.
  • Step-by-step strategy workflow. The course follows a structured process for designing tests and running simulations, so you don’t waste weeks tweaking parameters that don’t matter.
  • Private community of 1,700+ Python traders. When you get stuck on something like choosing the right covariance ratio, you can ask people who’ve already solved the same problem.

These resources are designed to take you from “I have a trading idea” to “I’ve tested it properly and know whether it works.”

What students are saying

⭐️ ⭐️ ⭐️ ⭐️ ⭐️
" A top-tier introduction to Python and quant finance. "
– Zac T

Your next steps

Start by replacing one moving average in your current analysis with the Kalman filter code above and compare the two side by side.

Once you see how adaptive smoothing works on real prices, the natural next step is learning how to test whether that smoother actually improves a trading strategy on data it hasn't seen.

A structured curriculum gets you there faster than piecing together random tutorials, and you'll finish with a working strategy tested on years of historical data.

Build trading strategies you can actually trust 👉

Learn a complete workflow for going from a raw trading idea to a tested, automated strategy, using the same tools and methods that professional trading teams rely on.


Jason Strimpel, founder of PyQuant News

Say 👋 on X/Twitter, LinkedIn, or book a call with me. You can always reply to these emails. I check them all

You got this email because you joined the PyQuant Newsletter or bought a product of mine.

You told me your email address is Reader.

Not right? Update your profile.

Want to stop getting these emails? No problem! Unsubscribe from all my emails.

Never investment advice. Use at your own risk. For educational purposes.

113 Cherry St #92768, Seattle, WA 98104

© 2026 PyQuant News, LLC

PyQuant Tips

For finance professionals, coders, and complete beginners. Automate your trading, investing, and data analysis with Python. No jargon, no fancy math, code you can use.

Read more from PyQuant Tips
Decompose time series into trend, seasonality, and noise with Python to build better forecasting models for quant finance.

Most beginners model a whole time series at once (here's why that fails) You sit down with a price chart or an economic indicator, build a model, and the predictions look solid on historical data. Then you feed in new data and the results fall apart. If that sounds familiar, you're not alone. The problem usually isn't your model. It's that you skipped a step professionals never skip. Most financial and economic data contains several patterns layered on top of each other. There's a long-term...

Calculate the Hurst exponent in Python to detect trending or mean-reverting markets. Pick the right trading strategy.

EXCLUSIVE PARTNER DEAL FOR PYQUANT NEWS READERS: 15% off virtual private trading servers Host your trading algorithms on secure, low-latency environments. QuantVPS’s robust infrastructure lets you focus on perfecting your strategy, not babysitting your computer. Grab your 15% discount with this link 👉 Most traders use the same strategy no matter what the market is doing (here's how to know when to switch) You've probably found a strategy that looked great on a chart, then watched it bleed...

Decompose portfolio risk with Python using the Fama-French factor model. Measure size and value exposure with OLS regression.

EXCLUSIVE PARTNER DEAL FOR PYQUANT NEWS READERS: 15% off virtual private trading servers Host your trading algorithms on secure, low-latency environments. QuantVPS’s robust infrastructure lets you focus on perfecting your strategy, not babysitting your computer. Grab your 15% discount with this link 👉 Most portfolios move with the market whether you want them to or not (here's how to measure it) You've picked a handful of stocks, maybe built a small portfolio, and you're watching it go up or...