Introduction
In this tutorial, we will create a simple SPY Meltdown backtester to analyze the behavior of the S&P 500 following significant sell-off days. Specifically, we aim to determine whether the S&P tends to continue its downward momentum or if it experiences a rebound the next day.
You can use the backtester that we will build to test sell-offs in the S&P 500 as they happen, and gauge the recent term performance. SPY meltdowns are inevitable, and the patterns are proof that history continues to repeat itself.
Understanding the Bias and Context
Our analysis begins with a bearish bias, based on a recent downtrending channel and overbought signals. After a significant sell-off, our goal is to see if the S&P continues to decline or if buyers step in, viewing the move as exhaustive and pushing prices higher.
Defining the thinkScript Code
We will write a few lines of thinkScript code to create our backtester. Follow these steps to get started:
- Click the studies icon, switch to the strategies tab, and click create.
- Name the script (e.g., “TI SPY Research”) and define the threshold for our analysis.
The threshold represents the percentage move that defines a significant sell-off. For this example, we’ll use a 3% threshold.
input threshold = 3;
Next, we need to calculate the percentage move from the open to the closing price:
def move = (close - open) / open;
Now, we can define the conditions for our backtester:
AddOrder(OrderType.BUY_TO_OPEN, move < threshold, open, 1);
AddOrder(OrderType.SELL_TO_CLOSE, close, close, 1);
This code sets up our buy and sell orders based on the defined threshold.
Testing and Adjusting the Code
To test the code, click OK, apply the strategy, and check if the floating P/L appears on your chart. Ensure “Display Floating P/L Study with Strategies” is turned on in the global strategy settings.
Review the initial trades and validate the calculations. If needed, adjust the code to correct any discrepancies.
For example, if the opening price for a day does not match the expected value, ensure the move calculation considers the previous bar’s data.
After making necessary adjustments, apply the code and validate the updated results.
Analyzing the Results
Zoom out to review the P/L graph. In our case, we observed that buying the next day after a significant sell-off often resulted in a positive P/L. This counter-intuitive result suggests that buyers tend to step in after a major sell-off, pushing prices higher the following day.
Expand the analysis to the maximum available data to understand the long-term trends. Review the P/L report to identify patterns and commonalities in the data.
Conclusion
Through this tutorial, we demonstrated how to write simple thinkScript code to create a backtester for analyzing SPY meltdown days. Our findings indicate that buying the next day after a significant sell-off can often be profitable.
We hope you found this tutorial helpful. Good luck with your trading, and stay tuned for our next update!