bruce.lesko

Linear Regression - Reverse Up/Down Strategy

bruce.lesko Updated   
For my first foray into pine script I took the code from the generic "Consecutive up/down" and flipped the logic. I added a linear regression filter to try and stay with the overall trend. ATR added for visual, I eventually want to use it as part of the money management.


Rules to open trade or close the opposite:
IF the linear regression slope is >=0 AND the last candle closes lower, BUY
IF the linear regression slope is <=0 AND the last candle closes higher SELL

Rules to close the opposite:
IF the linear regression slope is >=0 AND the last candle closes higher, close any open SELL
IF the linear regression slope is <=0 AND the last candle closes lower, close any open BUY

Release Notes:
Go with or against the Linear Regression trend using pullbacks or looking for breakouts of consecutive bars.
Release Notes:
Updated smooth moving average for ATR channel
Release Notes:
Added RSI for filtering overbought and oversold conditions
Release Notes:
Code cleanup.
Release Notes:
moved early exit to after entry to reduce redundant position calls
Release Notes:
Set default behavior to go with major trend(200 linear regression slope) and against minor trend pullbacks(4 linear regression slope)
Release Notes:
Added a date range function for choosing which date to test.
Open-source script

In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in a publication is governed by House Rules. You can favorite it to use it on a chart.

Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.

Want to use this script on a chart?