Building A Mean Reversion Trading System With Python
Building A Mean Reversion Trading System With Python
Exit Conditions:
1. Imports
import numpy as np
import pandas as pd
import yfinance as yf
import vectorbt as vbt
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
3. Data Download
[%] column.
import numpy as np
import pandas as pd
import yfinance as yf
import vectorbt as vbt
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
Best Parameters:
Window: 41.0
Period: 21.0
import numpy as np
import pandas as pd
import yfinance as yf
import vectorbt as vbt
import matplotlib.pyplot as plt
Performance Metrics
Performance Visualization
Window: 48.0
Period: 29.0
Performance Metrics
Performance Visualization
Window: 37.0
Period: 15.0
Performance Metrics
Performance Visualization
From the backtest with gold, it can be seen that the system
generates more consistent profits compared to buy and
hold but buy and hold yields a higher profit over the 11
years.
The trading system and buy-and-hold alternate in
profitability, with the system performing better during
periods when gold enters consolidation or retracement
phases, as observed from 2014 to 2019.
However, when gold entered a bullish phase from 2020 to
2021, the system could not generate profits comparable to
the market’s uptrend. But once it entered a consolidation
phase again, the system outperformed buy and hold.
From the experiment with the mean reversion system
across all 3 markets, the conclusion is that this
system performs well in sideways and bearish
markets, as it can filter out losses.
Therefore, if we want to apply this system, it is essential to
understand the overall market fundamentals and predict
when the market is in an uptrend to switch to a buy-and-
hold strategy.
Conversely, when the market enters a consolidation phase,
we should switch back to using this trading system. This
approach will make trading more effective. Understanding
the overall market fundamentals is not an impossible task,
and you can further study it through the article below:
M2 as a Predictor for Market Trends
Building an Investment Strategy Around Money Supply (M2)
wire.insiderfinance.io
If you read this article to the end, you will notice that
almost every image I included has a watermark with
my own logo. I want to explain that this is necessary
because someone copied my article and posted it on
their own website under their name, without giving
credit. To prevent anyone from copying my work, I
had to add these watermarks. I truly apologize for
this, as I originally wrote the entire article as a
personal study record. However, I felt the need to
add these watermarks in order to maintain my
dignity. I apologize again and thank you for your
understanding.
The purpose of this article is solely educational. It is not
intended as investment advice or recommendations.
Readers are encouraged to use their own judgment and
test the system independently before making any
decisions.