LongCut logo

Trading Mean Reversion with Kalman Filters

By Roman Paolucci

Summary

## Key takeaways - **No Precise Mean Needed**: We don't need the precise theoretical mean to be able to generate wealth effectively. We just need appropriate bet sizing and to be correct on average. [03:39], [03:45] - **Reality Breaks Mean Reversion**: Stock price processes do not follow an Ornstein-Uhlenbeck mean reverting stochastic process. Moreover, the parameterization is subject to change over time. [04:31], [04:46] - **Kalman Combines Model and Data**: The Kalman filter is going to combine that ground truth, that model assumption with the data that I'm actually observing. It is effectively combining the knowledge of the model with the experience of the data. [08:15], [08:48] - **Adaptive Models Risk Noise**: Anytime you have a model capable of adapting with the market, it is a double-edged sword. The sensitivity to noise is going to be the primary concern and why it is not a free lunch. [09:00], [09:17] - **No Free Lunch in Adaptation**: You can either trust the model more, hold steadfast in the underlying laws of physics that you assume and trade aggressively back to that level, or you can trust the data more, in which case you are certainly subject to more whipsawing. [11:47], [12:08]

Topics Covered

  • Imperfect Means Generate Wealth
  • Reality Lacks Stationarity
  • Kalman Blends Model and Data
  • Adapting Models Amplifies Noise

Full Transcript

[music] Hey guys, Roman here, quantitative researcher and trader, founder of Quanton Guild. Welcome to Quant Strats.

Quanton Guild. Welcome to Quant Strats.

In this series, we're going to talk about quantitative trading strategies and bridge the gap between theory and practice. I find oftent times we will

practice. I find oftent times we will explore a variety of different strategies and models, but very rarely do we actually discuss the implications

in our decision-making in the live markets. And that's exactly the goal of

markets. And that's exactly the goal of this series and exactly what you see here in front of me. This is a Calman filter trading system that aims to

implement this mean reversion trading strategy in practice. What we're going to do today is talk about theoretically what mean reversion should look like.

We're going to start in the classroom.

We'll talk about the Orinstein olac stochcastic process. this idea of

stochcastic process. this idea of trading mean reversion, what it should look like for your equity curve, and then we will branch out to reality.

We'll talk about non-stationerity, this idea that there is no long-term mean, and we'll talk about the implications of

the calman filter to aid in solving this problem. As we approach the classroom,

problem. As we approach the classroom, this Jupiter notebook to discuss theoretically what mean reversion and trading mean reversion should look like.

and you'd like to access the source code for either this Jupyter notebook or the Calman filter trading system, I'll leave a link in the description below. I'll

also post it to the Quant library on GitHub where you can find all of my Jupyter notebooks and associated YouTube videos along with all of the source code for my Quant builds. But I must

explicitly state because the Calman filter system has execution capacity either in a discretionary or an algorithmic way. You should not trade

algorithmic way. You should not trade with this system live, especially if you have no idea what an Orstein Olympic

process is. Master first your

process is. Master first your quantitative skills, your math, probability, statistics, and finance so that you can make informed decisions in

the face of uncertainty or target trading and marketmaking roles. What

better place to do this than quant.com?

Over 100 hours of lecture content, an adaptive practice engine, 90 plus quant lessons, the list goes on. You can get started for free today. Check out

quant.com.

Let's talk about what mean reversion and trading mean reversion should look like in theory. We don't know what the mean

in theory. We don't know what the mean of a stock price process should be. We

don't even know what the expected return should be. So, what can we do? Well, we

should be. So, what can we do? Well, we

can observe some data and estimate a mean. That's what we're going to do

mean. That's what we're going to do here. I'm going to start by observing

here. I'm going to start by observing the stock price. I'll estimate a mean and then I'm going to use that to inform my trading decisions. What you're seeing here on the right is trading this mean

reversion strategy. Effectively, I'm

reversion strategy. Effectively, I'm going long anywhere we're way below the mean and I'm going short anywhere we're way above the mean. And you can see my equity curve here is just accumulating

P&L. Sure, sometimes I lose a trade here

P&L. Sure, sometimes I lose a trade here and there, but for the most part, it's up and to the right. And that is exactly what we'd like to see. But what I want

you to notice is in this simulation here, I've estimated the mean level from observed data. It is not precisely the

observed data. It is not precisely the true theoretical mean of the Orinstein back process. What is the implication of

back process. What is the implication of this? Well, the implication is profound.

this? Well, the implication is profound.

We don't need the precise theoretical mean to be able to generate wealth effectively. We just need appropriate

effectively. We just need appropriate bet sizing and to be correct on average to be able to generate wealth with this

strategy. It can't be that easy, can it?

strategy. It can't be that easy, can it?

Well, it's not. Academically, this is justified. In fact, asmmptoically, we

justified. In fact, asmmptoically, we will be trading in a statistically optimal way. By the law of large

optimal way. By the law of large numbers, we will approach the long-term mean of this Orinstein olac process. And

if we trade around that mean we will be generating wealth in an optimal way. And

then it just comes down to optimizing our bet sizing. All right. What's the

problem in reality? The problem in reality is the law of large numbers asmtoically does not converge to the long run mean prices stock price

processes do not follow an Orinstein olac a mean reverting stochastic process. Moreover,

process. Moreover, the parameterization is subject to change over time. So it could revert to a mean for a period, then it can stop,

then it can start reverting again, so on and so forth. That will wipe out this beautiful equity curve that you see

here. So the problem effectively now is

here. So the problem effectively now is we can measure a mean, we can observe data, but how do we observe it? Do we

observe it over five minutes, 30 minutes, 30 seconds, one hour, daily, monthly? What is the correct time frame?

monthly? What is the correct time frame?

More importantly, when should we update this mean? If this mean is subject to

this mean? If this mean is subject to change over time, we don't want to be trading around a stale mean, that's going to destroy our equity curve. This

is exactly the problem that we face in practice. This is where the Calman

practice. This is where the Calman filter can offer a helping hand. We're

now ready to approach the Calman filter trading system. Anytime you follow a

trading system. Anytime you follow a trading strategy, you're going to be making assumptions. Without a crystal

making assumptions. Without a crystal ball, there is nothing else that you can do. You can act statistically optimally,

do. You can act statistically optimally, statistically suboptimally in the classroom, but even still, there's no guarantee how that is going to function in the real world. This is effectively

poker. You don't have an indicator

poker. You don't have an indicator that's going to help you win every single hand of poker. Tell you when to fold, when not to fold. Statistically,

sure, some things are more optimal than others, but sometimes you lose on the turn, and that's the way that it goes.

There is no one single indicator, one single model that's going to make you a profitable trader. That comes from a

profitable trader. That comes from a combination of knowledge and experience.

So what is it that we're looking at here? Well, effectively the Calman

here? Well, effectively the Calman filter is trying to do that. It's trying

to combine knowledge and experience.

What I have here in this live Calman filter trading system is an Orinstein olac process calibrated to data. In this

case, I'm using one minute bars. I have

a calibration window of 60 bars and I find that the mean is this gray dashed line. Well, what's the problem with

line. Well, what's the problem with this? We already talked about this when

this? We already talked about this when we were in the Jupyter notebook in the classroom. That mean is subject to

classroom. That mean is subject to change over time. When do we update it?

How much has it changed by since I calibrated the original process? Those

are all very good and important questions. We can even try to answer

questions. We can even try to answer them with some back tests and walk forward tests. And then we can observe

forward tests. And then we can observe out of sample if we're acting optimally, if there's stability in our decision- making. But at the end of the day, it's

making. But at the end of the day, it's still no guarantee. So what is it right now that we're looking at? What is it that we're trying to do with this Calman filter? So I mentioned that it's trying

filter? So I mentioned that it's trying to combine knowledge and experience.

What is the knowledge? Well, the

knowledge is an assumption component. It

is our laws of physics. I'm assuming

that the stock price follows a mean reversion process. And when I calibrate

reversion process. And when I calibrate that process to this observed data like we saw in the classroom, I get my long-term mean to be this gray dashed

line. Whether it is or is not, we will

line. Whether it is or is not, we will never know. But we do know that it is

never know. But we do know that it is subject to change over time. But how

much has it changed since I originally calibrated that process? Well, the

Calman filter is going to combine that ground truth, that model assumption with the data that I'm actually observing.

That is this green line here. The green

line is the Calman filter mean. You can

see that it has moved the Orinstein Olen's long-term mean down. Why is that the case? Well, look at the trend. the

the case? Well, look at the trend. the

trend is dragging that mean down and the Calman filter is being informed by the data that it's experienced the long-term mean level. It is effectively combining

mean level. It is effectively combining the knowledge of the model with the experience of the data that it is seeing in the live market. Anytime you have a

model capable of adapting with the market, it is a double-edged sword.

Sure, it's going to be able to adapt in real time. However, the sensitivity to

real time. However, the sensitivity to noise is going to be the primary concern and why it is not a free lunch.

Effectively, you as the quant, the trader, the researcher, you are going to have to set how noisy you anticipate the data that you observe to be. This is

going to dictate how much the mean level is going to change over time as you observe new data relative to the fixed

parameterization of the laws of physics, your original mean reversion model. And

what I have here is effectively a lever that you can flip to see what it looks like to trust the model more, the original Orinstein ol which you

calibrate to the data originally or trust the market data more in which case it's going to adapt much more aggressively. That is the tradeoff when

aggressively. That is the tradeoff when you implement a model capable of adapting to market prices in real time.

You never get something for nothing.

There is no free lunch. That is your trade-off. Now that we have a reasonable

trade-off. Now that we have a reasonable idea of what's going on in this trading system, how do we actually generate trades and make money with it? Well, I

can't tell you how to play your hand of poker. You can't tell me how to play

poker. You can't tell me how to play mine. There are a lot of different ways

mine. There are a lot of different ways you could go about doing this. Like I

said earlier, you can go about conducting back tests, walk forward tests to try to find an optimal window when you should reparameterize your Orinstein Olen back. What kind of noise

you should anticipate in observed market data. There is a quantitative approach

data. There is a quantitative approach to it. But again, there's no asmmptotic

to it. But again, there's no asmmptotic guarantee like we have in the classroom.

You could go through and jump through all of those hoops and at the end of the day when you deploy it live, everything falls apart. Moreover, you could be

falls apart. Moreover, you could be sitting here trading Apple for the last 10 years and understand qualitatively the dynamics based on macro factors,

political factors, sector factors, the list goes on. And you could use this as a model to inform your decision making.

And you could outperform systematic strategies just because you have a better grasp on the overall climate of the market. Some key takeaways today for

the market. Some key takeaways today for you. The mean level that you calibrate

you. The mean level that you calibrate for reversion is certainly going to change over time. You just need to be reasonably correct on average to make money. Just because you calibrate it in

money. Just because you calibrate it in one window, it doesn't mean that it is going to be correct. It is subject to change over time. Who knows what the true mean actually is. Moreover, when

you deploy models to aid in trying to circumvent this issue of mean changing over time like the Calman filter, there is no free lunch. You can either trust

the model more, hold steadfast in the underlying laws of physics that you assume and trade aggressively back to that level, or you can trust the data

more, in which case you are certainly subject to more whipsawing. You are

subject to your assumptions violently changing over time, in which case you are not going to be capable of watching those thesis play out or generating any

wealth. That's going to do it for this

wealth. That's going to do it for this video on trading mean reversion strategies. This quant strats video. I

strategies. This quant strats video. I

hope you enjoyed. I hope you learned something. If you like this video and

something. If you like this video and you want to see more like it in the future, please like, comment, subscribe, share. It helps me out tremendously. It

share. It helps me out tremendously. It

is always greatly appreciated. Check out

quankill.com to master your quantitative skills before approaching a project like this before approaching the markets or for targeting trading and market making roles. Other than that, I want to thank

roles. Other than that, I want to thank you so much for watching and I will see you in the next video.

Loading...

Loading video analysis...