Linear-Weighted Moving Average Trading Strategy: Backtest
Linear-weighted moving average strategy backtest
Moving averages are a technical analysis tool that smooths price data over a specific period. They come in different types, and each type has its advantages and limitations. The linear-weighted moving average is one of the common ones. But do you know what it is? And do you know if a linear-weighted moving average works?
Yes, linear-weighted moving average strategies do work. Our backtests show that a linear-weighted moving average can be used profitably for both mean-reversion and trend-following strategies on stocks.
The linearly weighted moving average (LWMA) is a moving average that puts more weight on recent price data in a linear fashion: the most recent price has the highest weighting, with each prior price getting progressively less weight. As a result, for any given period, the LWMA reacts more quickly to price changes than a simple moving average (SMA) and an exponential moving average (EMA).
Linear-weighted moving average strategy backtest and best settings
Before we go on to explain what a linear-weighted moving average is and how you can calculate it, we go straight to the essence of what this website is all about: quantified backtests.
Our hypothesis is simple:
Does a linear-weighted moving average strategy work? Can you make money by using linear-weighted moving average strategies?
We look at the most traded instrument in the world: the S&P 500. We test on SPDR S&P 500 Trust ETF which has the ticker code SPY.
All in all, we do four different backtests:
- Strategy 1: When the close of SPY crosses BELOW the N-day moving average, we buy SPY at the close. We sell when SPY’s closes ABOVE the same average. We use CAGR as the performance metric.
- Strategy 2: Opposite, when the close of SPY crosses ABOVE the N-day moving average, we buy SPY at the close. We sell when SPY’s closes BELOW the same average. We use CAGR as the performance metric.
- Strategy 3: When the close of SPY crosses BELOW the N-day moving average, we sell after N-days. We use average gain per trade in percent to evaluate performance, not CAGR.
- Strategy 4: When the close of SPY crosses ABOVE the N-day moving average, we sell after N-days. We use average gain per trade in percent to evaluate performance, not CAGR.
The results of the first two backtests look like this:
Strategy 1
Period |
5 |
10 |
25 |
50 |
100 |
200 |
CAR |
8.53 |
8.05 |
5.99 |
5.17 |
5 |
3.28 |
MDD |
-23.82 |
-33.9 |
-42.66 |
-40.24 |
-49.38 |
-49.2 |
Strategy 2
Period |
5 |
10 |
25 |
50 |
100 |
200 |
CAR |
1.09 |
1.54 |
3.5 |
4.31 |
4.48 |
6.21 |
MDD |
-75.45 |
-65.93 |
-39.08 |
-41.21 |
-44.53 |
-42.69 |
The results from the backtests are pretty revealing: in the short run, the stock market shows tendencies to mean-reversion. In the long run, it is better to use trend-following strategies.
Why do we reach that conclusion?
Because if we use a short moving average, the best strategy is to buy when stocks drop below the average and sell when it turns around and closes above the moving average (buy on weakness and sell on strength). This can clearly be seen in the first test above for the 5-day moving average. The 5-day moving average returns a CAGR of 8.53%, which is almost as good as buy and hold even though the time spent in the market is substantially lower.
When we buy on strength and sell on weakness, in the second test in the table above, the best strategy is to use many days in the average. The longer the average is, the better. The 200-day moving average returns 6.21%, which is pretty decent.
The results from backtests 3 and 4 looks like this (the results are not CAGR, but average gains per trade):
Strategy 3
Period |
5 |
10 |
25 |
50 |
100 |
200 |
5 |
0.22 |
0.33 |
1.2 |
1.85 |
4.07 |
9.2 |
10 |
0.22 |
0.51 |
1.15 |
2.41 |
3.85 |
9.85 |
25 |
0.16 |
0.52 |
1.14 |
2.09 |
4.54 |
8.78 |
50 |
0.22 |
0.61 |
1.24 |
2.47 |
4.34 |
8.94 |
100 |
0.67 |
0.76 |
1.32 |
1.96 |
4.31 |
8.79 |
200 |
0.53 |
0.66 |
1.87 |
3.19 |
4.71 |
7.72 |
Strategy 4
Period |
5 |
10 |
25 |
50 |
100 |
200 |
5 |
0.22 |
0.26 |
0.93 |
2.16 |
4.3 |
8.58 |
10 |
0.2 |
0.27 |
0.93 |
2.52 |
4.24 |
8.82 |
25 |
0.23 |
0.31 |
0.78 |
2.16 |
3.63 |
7.47 |
50 |
0.17 |
0.19 |
0.76 |
1.2 |
3.78 |
8.63 |
100 |
0.1 |
0.26 |
1 |
1.25 |
4.1 |
8.11 |
200 |
0.27 |
0.01 |
0.78 |
2.42 |
3.87 |
6.75 |
As expected, the longer you are in the stock market, the better returns you get. This is because of the tailwind in the form of inflation and productivity gains.
However, be aware that this is just one method of testing a moving average. There are basically unlimited ways you can use a moving average and your imagination is probably the most restricting factor!
What is a linear-weighted moving average (LWMA)?
A linearly weighted moving average (LWMA) is a type of moving average that assigns more weight to recent price data and less weight to old data. Just like in the EMA, the most recent price has the highest weighting, and each prior price has progressively less weight. But in this case, the weights drop in a linear fashion.
This average takes each of the closing prices over a given period and multiplies them by a predetermined “weight” coefficient. So, once the price data of the various periods have been taken into account, they are summed up and divided by the sum of the number of periods. As a result, LWMAs are quicker to react to price changes than simple moving averages (SMA) and exponential moving averages (EMA).
That is, when compared to the SMA, the linear moving average is less inert. Also, compared to the EMA, it considers fewer bars and does not depend on the previous value, so when applied to the same prices, it will always show the same value, regardless of how much data is preloaded.
Since the linear weight reacts to price changes more quickly than other moving averages, it produces more “noise” in a flat market. This is why many traders who use LWMA, use it in conjunction with a simple moving average. In that case, buy and sell signals can be generated during breakouts and crossovers of moving averages, while trends are confirmed when SMA and LWMA move in the same direction.
How to calculate LWMA
The formula for the linearly weighted moving average (LWMA) is quite straightforward. It is given as follows:
LWMA = [(P_{n }x W_{1}) + (P_{n-1} x W_{2}) + (P_{n-2} x W_{3}) …]/∑W
where:
P = the price for the period concerned
n = the most recent period
n-1 = the prior period
n-2 is two periods prior
W = the assigned weight to each period: the highest weight goes to the most recent, and the weight decreases linearly based on the number of periods being used.
While you can do the calculations manually, the trading platform does it for you and plots the indicator on the chart when attached.
Why use an LWMA?
The linearly weighted moving average is a continuously calculated average price of an asset over a given period. It helps you to reduce noise so that you can clearly see what the price is doing. You can use it to see the trend and the individual price swings. Generally, when the price is above the LWMA, and the LWMA is rising, there is likely an uptrend, but if the price is below the LWMA and the LWMA is pointed down, there is likely a downtrend.
The LWMA can also help you to spot a change in trend: when the price crosses the LWMA that could signal a trend change. That is, if the price is above the LWMA and then drops below it, there may be a shift from an uptrend to a downtrend, while the opposite is true for a shift to an uptrend.
How to use an LWMA
Most trading platform out there has a built-in LWMA indicator, but some may simply name it weighted moving average (MWA). To use the indicator, search for it on your trading platform and attach it to the chart. Then, adjust the settings to what they want.
To use the LWMA to show a trend, set the averaging period to a big number, say 100 or 200. Lower values would have the indicator follow the price more closely and, instead of showing the trend, it would show the individual price swings.
We recommend backtesting whatever you are doing. If you want to use moving averages to define the trend, then you backtest your ideas. Never use untested ideas!
How can you use an LWMA?
You can the LWMA to do a number of things when trading. One of them is to see the direction of the trend. When you are using LWMA to identify a trend, it can also serve as a support level during an uptrend or a resistance level during a downtrend. To get the best of the indicator, you can combine it with other indicators, such as oscillators, or price action analysis, to spot the right moment to enter a trade.
For example, if a bullish candlestick pattern, such as the hammer, occurs around a rising long-period LWMA after a pullback, it could signal the end of the pullback and a continuation of the uptrend.
Alternatively, you can use two LWMA indicators: one with a long-period setting (200) and the other with a short period (say 30 or 50). When the short period LWMA crosses above the 200-period LWMA, you have a buy signal, and when the short period LWMA crosses below the 200-period LWMA, you have a sell signal.
Drawbacks with an LWMA
As with any technical indicator, the linearly weighted moving average indicator has its limitations. You need to understand these limitations to be able to use the indicator more effectively.
Here are the three most common limitations of LWMA:
- The lag factor: As with other indicators, the LWMA also lags.
- Ranging markets: It provides little information when the price action is choppy or moving predominantly sideways. During such periods, the price will oscillate around the LWMA, so the LWMA will not provide good crossover or support/resistance signals.
- False signals: It can give multiple false signals. A false signal is when the price crosses the LWMA but then fails to move in the direction expected, which can result in bad trades. It is not uncommon to have multiple false signals before a significant trend develops.
Relevant articles about moving averages strategies and backtests
Moving averages have been around in the trading markets for a long time. Most likely, moving average strategies were the start of the systematic and automated trading strategies developed in the 1970s, for example by Ed Seykota. We believe it’s safe to assume moving averages were a much better trading indicator before the 1990s due to the rise of the personal computer. The most low-hanging fruit has been “arbed away”.
That said, our backtests clearly show that you can develop profitable trading strategies based on moving averages but mainly based on short-term mean-reversion and longer trend-following. Furthermore, there exist many different moving averages and you can use a moving average differently/creatively, or you can combine moving averages with other parameters.
What’s the difference between exponential and linear weighted moving averages?
The main difference is the sensitivity to the data used. LWMA has more emphasis on the most recent data than EMA.
Is linear weighted moving average same as weighted moving average?
Not exactly – the LWMA puts more emphasis on the most recent data. The prior data drops the weightings on older data linearly – hence the name.
Which is better moving average – exponential moving average or linear weighted moving average?
That is hard to tell. If you backtest, you’ll find that some assets work better by using EMA while others work best with LWMA. That’s just how markets are.
What are the best settings for linear weighted moving average?
Again, only specific backtests can give you the right answer. There are no “best” or “worst” settings. It can vary from asset to asset.
Linear-weighted moving average – takeaways
Our takeaway from the backtests is that linear-weighted moving average strategies work well if you buy on weakness (a close below the moving average) when you use a short number of days. Opposite, it’s best to buy on strength (a close above the moving average) when you use a longer moving average.
What is a Linear-Weighted Moving Average (LWMA)?
A Linear-Weighted Moving Average (LWMA) is a dynamic technical indicator utilized in financial markets to analyze price trends and patterns. Unlike traditional moving averages, the LWMA places a higher emphasis on recent price data, making it particularly responsive to short-term market changes.
Why does the LWMA react quickly to price changes?
The rapid reaction of the LWMA to price changes is a result of its emphasis on recent data. By assigning higher weights to the most recent prices, the LWMA captures the latest market sentiment and adjusts quickly to shifts in the price trajectory. This characteristic makes it a valuable tool for traders looking to identify short-term trends and make timely decisions.
How can traders use the LWMA in their strategies?
Traders can leverage the LWMA in various ways, such as trend identification and confirmation. When the price is above a rising LWMA, it suggests an uptrend, while a falling LWMA indicates a potential downtrend. Additionally, combining the LWMA with other indicators or using different time periods can enhance its effectiveness in generating buy and sell signals.