Linear Regression Hook on SPX
- Algomatic Trading
- Jun 1, 2024
- 2 min read
Updated: Aug 10, 2024
A Mean-Reversion Strategy for S&P500
The Idea
The Linear Regression Slope is often used for trend detection, but can also work well to identify reversal points. This strategy uses a "hook" in the short-term Linear Regression Slope for entries, while a custom-made volatility band is made for taking profit or stopping loss. It's optimized for SPX until 2021, but can also be used for other indices.

Setup for Backtest
Market: USA500 (SPX)
Contract: 1 € per point
Broker: IG
Testing environment: ProRealtime 12
Timeframe: Daily
Time zone: CET
No fees and commissions are included.
Result
Total gain: 3 527.2 €
Average gain: 11.8 €
Total trades: 298
Winners: 221
Losers: 76
Breakeven: 1
Max drawdown: –487.9 €
Risk/reward ratio: 0.7
Total time in the market: 24.7 %
Average time in the market: 7 days, 9 hours
CAGR (10 000 € in starting capital): 0.87 %

Entry Conditions
Close is less than open.
The 3-period Linear Regression Slope is higher than yesterday.
Yesterday's 3-period Linear Regression Slope is lower than the day before.
The day of the week is not a Friday.
Exit Conditions
The trade is exited when:
Close is above the upper custom-made ATR band (take profit).
Close is below the lower custom-made ATR band (stop loss).
Code
ProRealTime
//-------------------------------------------------------------------------
// Concept: Mean-reversion with a linear regression slope hook
// Market: SPX
// Direction: Long only
// Timeframe: Daily
// Timezone: CET
// Versions: v.1 | By: Algomatictrading.com | 2024-02-01 | OOS since: 2021-01-01
//-------------------------------------------------------------------------
defparam preloadbars = 500
defparam cumulateorders = false
// POSITION MANAGEMENT
//-------------------------------------------------------------------------
once positionsize = 1
// INDICATORS
//-------------------------------------------------------------------------
lrs = linearregressionslope[3](typicalprice)
atr = averagetruerange[10]
ma = average[5]
bandup = ma+(atr*0.75)
banddown = ma-(atr*2)
// TRADING CONDITIONS
//-------------------------------------------------------------------------
// Long - Enter
// ------------------
cl = close < open
cl = cl and lrs > lrs[1]
cl = cl and lrs[1] < lrs[2]
cl = cl and dayofweek <> 5
// Long - Exit
// ------------------
clx = close > bandup or close < banddown
// TRADING ACTION
//-------------------------------------------------------------------------
// Enter
// ------------------
if cl then
buy positionsize contract at market
endif
// Exit
// ------------------
if longonmarket and clx then
sell at market
endif
FAQ
Q: Am I free to use this strategy however I want?
A: Sure! But we're happy if you refer to our website and keep the creator's name in the code.
Q: I'm using another platform, can you write the code in another language?
A: No, sorry. We might add code in more languages later to the website, but until then, we recommend you try ChatGPT or something similar to help you rewrite it.
Q: Why does my backtest look different when I run the code?
A: The strategy might give different backtest results for several reasons, like added spread or fees, the wrong time-zone settings, or that you're using another type of contract.
Q: Will I make money if I run this strategy live?
A: We don't know. We're not financial advisors, we're just traders sharing our ideas.