A Rare Buy Signal Just Flashed For US Stocks
With so much economic uncertainty afoot, you might be wondering whether stocks can still go the distance this year. So I used ChatGPT-4 to help me program and backtest a long-term investment strategy in TradingView. And it’s just flashed a rare buy signal for US stocks.
What’s this buy signal then?
The strategy tells you to buy the S&P 500 index when its 21-week exponential moving average (EMA, yellow line) crosses above the 50-week simple moving average (SMA, blue line). And then sell it (into the safety US dollars) when its 21-week EMA crosses back below the 50-week SMA.
A fresh buy signal (blue arrow) officially fired last Friday when the US stock market closed for the week. At that point, the yellow line was above the blue one for the first time since March last year…
It’s a moving average cross strategy. A “faster” moving average (the 21-week-EMA) crosses a “slower” moving average (the 50-week SMA) to show changes in buyer and seller momentum. So here, when the faster moving average crosses above the slower one, it tends to suggest a strong uptrend ahead for the S&P 500. I’ve explained more about moving averages here if you’re interested.
So how have these signals played out in the past?
The strategy isn’t perfect, but the results are good enough to take it seriously. Interestingly, it would’ve gotten you out of the market before the 2008 collapse, and back in once the dust settled in mid-2009. It’s the same thing with the dotcom crash in 2000-01. You’d have been sitting pretty in cash for over two years and skipped out most of the carnage. And it’d be a similar story if you’d used the signal for the tech-heavy Nasdaq too.
I used the strategy tester function in TradingView to backtest the strategy on the S&P 500 from the year 1880 (yes, you read that right). I also added in a trading fee of 0.1% of the position size per trade to make it seem more like real-world trading.
Here’s the highlights reel for how the strategy would’ve performed over the past 143 years. Of course, these results don’t guarantee it’ll do the same thing in the future…
If you fancy playing those odds, you can get exposure to the S&P 500 index through the SPDR S&P 500 ETF Trust (ticker: SPY, expense ratio: 0.095%) or the iShares Core S&P 500 UCITS ETF (CSPX, 0.07%). The signal also just fired for bitcoin too, but keep in mind there’s a lot less history to go by on that.
How do you get the signal on your own TradingView chart?
If you want to play around with the strategy, just open up a new chart in TradingView and set it to the weekly time frame. You can try it with the Nasdaq, bitcoin, gold, or any other investment of your choosing. You can then copy this code below and paste it into the “Pine Editor” section at the bottom left of your TradingView chart. I’ve explained how you can do that in this quick video.
//@version=4 strategy("EMA21 Cross SMA50 Strategy", shorttitle="EMA21xSMA50 Strategy", overlay=true) // Input for moving averages emaLength = input(21, title="EMA Length", minval=1) smaLength = input(50, title="SMA Length", minval=1) // Calculate moving averages emaValue = ema(close, emaLength) smaValue = sma(close, smaLength) // Check for crossover (buy) and crossunder (sell) conditions crossCondition = crossover(emaValue, smaValue) crossUnderCondition = crossunder(emaValue, smaValue) // Plot moving averages plot(emaValue, title="21-EMA", color=color.yellow, linewidth=2) plot(smaValue, title="50-SMA", color=color.blue, linewidth=2) // Execute buy and sell orders strategy.entry("Buy", strategy.long, when=crossCondition) strategy.close("Buy", when=crossUnderCondition)