Backtesting an Automated Buy-Sell Model Based on Smoothing Factors for SPY and SPXL
Buy-Sell models are among my favorite analytical techniques because they simulate what self-directed and institutional traders do on a regular basis – namely, buy and sell financial securities. A buy-sell model can evaluate different strategies for profitably buying and selling securities.
Over the past several months, this blog introduced and examined variations of a model for buying and selling financial securities based on exponentially smoothed security close prices. Simple exponential smoothing and exponential moving averages are two different approaches available for computing exponentially weighted historical values of an underlying set of time series values – like ticker prices. This post relies on securities prices smoothed by a combination of simple exponential smoothing and exponential moving averages. The model evaluated in the post compares unsmoothed historical close prices to the exponentially smoothed close prices to assess if it is likely to be a good time to buy, hold, or sell a security on each trading day.
The “Building and Backtesting a Buy-Sell Model with Close Prices and EMAs” post introduced the model. Data were downloaded from the Google Finance site and processed based on the model’s rules implemented in a Google Sheets worksheet. This post was intended to be a proof of concept for the model’s logic and a demonstration of the model with two tickers (SPY and SPXL).
The “Programming and Backtesting Demonstrations for a Buy-Sell Model for Eight Tickers” post focused on how to automate the model with T-SQL in SQL Server for each of eight tickers (SPY, SPXL, QQQ, TQQQ, GOOGL, MSFT, NVDA, PLTR). This post also demonstrated automated steps for backtesting the model with extensive historical data.
The “Comparative Market Performance for a Buy-Sell Model Versus a Buy-and-Hold Strategy for Eight Tickers” post highlighted an approach for comparing the buy-sell model performance based on exponentially smoothed close prices with a buy-and-hold strategy. The buy-and-sell model outperformed the buy-and-hold strategy.
All three prior posts on the model used smoothing factors based on ten and two-hundred lookback windows. This post focuses on implementing the model with three different pairs of smoothing factors. The current post uses five-period, ten-period, and thirty-period smoothing factors along with a two-hundred-period smoothing factor. The main objective of this post is to compare model performance for each pair of smoothing averages to find out which pair yields the best performance outcomes. Model performance in this post is evaluated for the SPY and SPXL tickers.
A Quick Introduction to Computing Exponentially Weighted Values
A common way to implement exponential smoothing is with the following equations and process.
s1 = x0,
st = αxt + (1 – α)st-1,
where α is between 0 and 1, and the sum of α plus (1 – α) equals one.
The set of x values are the underlying time series values, such as the successive Close prices for a ticker symbol.
The set of s values are the exponentially smoothed values.
The t values begin at time zero for the initial underlying value. Because the second equation requires a prior exponentially smoothed value, the exponentially smoothed value for time zero is undefined. Therefore, the computed exponentially smoothed values commence at time one.
The s value for time one is set to the underlying time series value for time zero.
Then, the exponentially smoothed value for time one is computed using the initial value underlying value (x0).
The value of α is the weight for the current period’s underlying value. The value of (1 – α) is the weight for the prior period’s exponentially smoothed value.
Wikipedia asserts there is no universally accepted way for assigning a value to α, but least squares estimation techniques are one common approach. This post employs four different criteria, depending on the context, to assign values to α based on the length of a lookback window. Additionally, there is no universally accepted way of assigning a value to s0. Consequently, this post leaves s0 null and assigns x0 to s1. I recommend that you examine adaptations of simplified exponential smoothing and pick the one that is best for your application. If you are trying to predict when to buy and sell securities, this post may be a great place to start.
Here are some expressions for the value of α based on the period length for exponential smoothing lookback window.
For a five-period window, the value of α is 2/(5+1).
For a ten-period window, the value of α is 2/(10+1).
For a thirty-period window, the value of α is 2/(30+1).
For a two-hundred period window, the value of α is 2/( 200+1).
The following pair of screenshots show the first and last ten computed smoothed close values for the SPY ticker. Columns D, E, F, and G are, respectively, for five-period, ten-period, thirty-period, and two-hundred-period smoothed values. Rows 2 and 3 display α and (1-α) values for computing smoothed values. Columns A and B show the original Close dates and values returned from the Google Finance site with the GOOGLEFINANCE function into a Google Sheets worksheet from 1/29/1993 through 6/30/2025. The first computed smoothed values appear in row 8. The last computed smoothed values appear in row 8166.
Model Review and Analysis Plan
The basic model structure for this post was described and initially programmed in a T-SQL script within the “Programming and Backtesting Demonstrations for a Buy-Sell Model for Eight Tickers” post. The post included a T-SQL script that indicated when to buy and sell securities based on the relationship between a security’s closing price and its smoothed closing values. The post relied on exponentially weighted closing prices derived from ten-period and two-hundred-period lookback windows.
The current post runs the model with three sets of lookback windows (five-period and two-hundred period, ten-period and two-hundred period, and thirty-period and two-hundred period lookback windows). Notice that each of the three sets is comprised of a shorter lookback window and a longer lookback window. The longer lookback window is always the same (a two-hundred period lookback window). The shorter lookback window has one of three period length parameters (five, ten, or thirty). The shorter lookback window reflects short-run trends, and the longer lookback window reflects long-run trends.
When the close price for the current period is greater than the exponentially smoothed close prices for both the shorter and longer lookback windows, then the model assumes it is a profitable time to own the security. In contrast, the model assumes it is not a profitable time to own a security if the close price for the current period is not greater than the smoothed close prices for the shorter and longer lookback windows.
The T-SQL Script for Implementing the Model
The code in the T-SQL script assigns a value of TRUE to the CloseAboveSMOOTH column value in the FinancialData table if the current period’s close price is greater than both smoothed close prices for the current row. Otherwise, the CloseAboveSMOOTH column value in the FinancialData table is set equal to FALSE. The FinancialData table has ticker and date column values that uniquely identify each row in the table.
After assigning each CloseAboveSMOOTH column value in the FinancialData table, the script computes unique RunGroup identifiers for sets of consecutive rows with the same CloseAboveSMOOTH column value. Each ticker has its own unique set of RunGroup numerical identifier values. A temporary table (#temp_summary) logs RunGroups and their attributes. There is a separate set of RunGroups for each ticker tracked by the analysis in this post. The script for the model, the #temp_summary table, and related data based on the original close values can enable a comparison of market performance from a buy-and-hold strategy versus a buy-and-sell strategy.
Excerpts from the Log Table for Each Ticker
The model’s core innovation is its ability to algorithmically detect sequences of trading days where the closing price consistently exceeds a smoothed benchmark—typically derived via exponential smoothing or EMA logic. When CloseAboveSMOOTH = TRUE persists across multiple rows, it signals a statistically supported uptrend. Initiating a position at the start of such a sequence and holding through its duration can materially improve portfolio performance. Conversely, rows flagged with CloseAboveSMOOTH = FALSE indicate insufficient trend strength, and the model suppresses exposure to those securities to avoid drawdown risk.
You should keep in mind that within the context of an uptrend, it is possible to have counter-trend movements where asset prices decline until they end the uptrend. After the initiation of an uptrend, it is also possible for some trading days with counter-trend price actions to substantially diminish previously accumulated gains before resuming a previously initiated uptrend.
The current model design does not require two consecutive uptrend trading days before issuing a buy recommendation. This gets you into a trade sooner rather than later to capture more of the uptrend before the uptrend inevitably ends.
The following pair of result sets shows outcomes for the first eight consecutive RunGroups for the SPY and SPXL tickers with ten day and two-hundred day lookback windows.
The RunGroup column values in each result set for a ticker is an identifier assigned by the T-SQL script for implementing the model.
The FirstDate and LastDate column values indicate the beginning and ending dates for each RunGroup.
When a CloseAboveSMOOTH column value of TRUE in the FinancialData table is followed on the next trading day by a CloseAboveSMOOTH column value of FALSE, then a RunGroup lasts just one trading day. For the tickers and dates examined in this post, this outcome is substantially less frequent than RunGroups that last more than a single day.
The FirstClose and LastClose column values indicate the closing values for the beginning and ending dates of each RunGroup.
The Close Change and LastClose/FirstClose column values are computed for each RunGroup to facilitate a summary of the trading outcomes for each RunGroup.
o The Close Change column values are computed by subtracting the FirstClose column value from the LastClose column value for each row. This column value indicates the amount of gain or loss associated with each RunGroup.
o LastClose/FirstClose column value is merely the ratio of the LastClose and FirstClose column values for each RunGroup. This is the primary metric used in the post to assess performance across RunGroups.
The reason the LastClose/FirstClose column values are critical to evaluating market performance from the model buy and sell recommendations is because the cumulative product across multiple RunGroups less one is the cumulative compound percentage change across the RunGroups for a ticker. If you think of an individual RunGroup as a trade, the cumulative compound percentage change is appropriate for indicating growth across multiple trades when the balance from the prior RunGroup is applied to the amount traded in the current RunGroup.
Summary Performance Metrics for Three Smoothing Factors
The following table presents summary performance metrics for three lookback windows with each of two tickers (SPY and SPXL) .
The SPY ticker is for an ETF based on the S & P 500 index. SPY ticker price changes reflect S & P 500 index percentage changes.
The SPXL ticker is for a leveraged ETF based on the S & P 500 index. The leveraged factor is three to one. SPXL ticker price changes are based on a three-to-one ratio for the S & P 500 index daily percentage changes.
The model for buy and sell recommendations relies on a pair of short-run and long-run lookback windows for computing exponentially smoothed closed prices.
o The three short-run lookback windows have lengths of five, ten, and thirty days, respectively.
o The long-run lookback window with a period length of 200 days is the same for each of the three pairs of lookback windows.
The column headers in the body of the following table denote the short-run lookback window for each ticker. For example, the column header SPY_05 represents the SPY ticker with a short-run lookback window of five periods, while the SPXL_05 header denotes the SPXL ticker using the same five-period lookback window.
The fourth row in the following table (Percent Winning Trades) indicates the percent of trades that are winning ones for each ticker-lookback window length combination.
o The lookback windows, which are for five and ten periods, have percent winning trade values of around sixty percent.
o The lookback windows which are based on a thirty-day lookback window have a percent of winning trades between fifty-three and fifty-five percent.
The bottom three rows in the following table display percentage change metrics.
o The Compounded Percent Change with Buy-and-Sell Strategy has the largest values for short-run lookback window of ten periods. The pair of columns with the second largest values for the percentage change has a short-run lookback window of five periods. The compounded percent change values derived from a thirty-period short-run lookback window has the smallest performance values.
o The second row in the bottom three rows reports percentage change values for the buy-and-hold strategy. Two trends are evident from this row.
The buy-and-sell strategy row performance values in the first of the bottom three rows are always substantially larger than the performance values in the second of the bottom three rows.
Also, the columns in the second of the bottom three rows are about an order of magnitude larger for the leveraged ticker (SPXL) than the unleveraged ticker (SPY). In other words, the buy-and-sell strategy for the leveraged ticker (SPXL) returns especially large gains relative to the unleveraged ticker (SPY).
The third row in the bottom three rows also confirms that the buy-and-sell model especially outperforms the buy-and-hold strategy for the leveraged ticker as opposed to the unleveraged ticker.
Concluding Comments
This post presents selected results from backtests for a buy-sell model with a pair of securities (SPY and SPXL). Each ticker is analyzed with three different degrees of exponential smoothing to find the degree of smoothing that returns the most profitable results.
The buy-sell model designates when to buy and sell a security’s ticker based on the relationship between the security’s close prices and its exponentially smoothed values. The model assumes it is profitable to hold a financial security whenever its close price is greater than two exponentially smoothed values based on a short lookback window and a longer lookback window. When the close price is not greater than both exponentially smoothed values, then the model assumes it is not likely to be a profitable time to own the security.
The profitability of the trades recommended by the model is evaluated for all three degrees of exponential smoothing. Returns are statistically more profitable with a ten-period short-run lookback window versus either a five-period or a thirty-period short-run lookback window. The use of the long-run lookback window (200 trading days) ensures the model only recommends buys for securities that are profitable in the long run. Also, it is known from a prior post (“Can Leveraged ETFs Safely Grow Long-Term Investments?”) that the leveraged SPXL ticker returns more profitable results than the SPY ticker. This outcome is confirmed in this post, and this post additionally indicates that the model amplifies the gains for the leveraged SPXL ticker over those from the unleveraged SPY ticker.
I close by inviting you to leave a comment with any questions that you have about this post or suggestions for future posts on the model. If you would like the Google Sheets worksheet and T-SQL scripts that I used to generate analytical results shown in this post, please leave your email address in a comment so that I can share them with you.
Keywords: # Buy-SellModel, # Buy-and-holdStrategy, # ExponentialSmoothing, # Backtesting, # CumulativeCompoundPercentageChange, # GoogleSheets, # SPY, # SPXL
Comments
Post a Comment