TradingView
joseph_lemery
18 apr 2024 18:53

RSI and ATR Trend Reversal SL/TP 

Tesla, Inc.NASDAQ

Beskrivning

Quick History:
I was frustrated with a standard fixed percent TP/SL as they often were not receptive to quick market rallies/reversals. I developed this TP/SL and eventually made it into a full fledge strategy and found it did well enough to publish. This strategy can be used as a standalone or tacked onto another strategy as a TP/SL. It does function as both with a single line. This strategy has been tested with TSLA , AAPL, NVDA, on the 15 minutes timeframe.

HOW IT WORKS:
Inputs:
Length: Simple enough, it determines the length of the RSI and ATR used.

Multiplier: This multiplies the RSI and ATR calculation, more on this later.

Delay to prevent Idealization: TradingView will use the open of the bar the strategy triggers on when calculating the backtest. This can produce unrealistic results depending on the source. If your source is open, set to 0, if anything else, set to 1.

Minimum Difference: This is essentially a traditional SL/TP, it is borderline unnecessary, but if the other parameters are wacky this can be used to ensure the SL/TP. It multiplies the source by the percent, so if it is set to 10, the SL/TP is initialized at src +- 10%.

Source input: Self Explanatory, be sure to update the Delay if you use open.

CALCULATION:
Parameters Initialization:
The strategy uses Heikinashi values for calculations, this is not toggleable in parameters, but can be easily changed by changing hclose to equal src.

FUNCTION INITIALIZATION:
highest_custom and lowest_custom do the same thing as ta.highest and ta.lowest, however the built in ta library does not allow for var int input, so I had to create my own functions to be used here. I actually developed these years ago and have used them in almost every strategy since. Feel especially free to use these in your own scripts.

The rsilev is where the magic happens.

SL/TP min/max are initially calculated to be used later.
Then we begin by establishing variables.

BullGuy is used to determine the length since the last crossup or crossdown, until one happens, it returns na, breaking the function. BearGuy is used in all the calculations, and is the same as BullGuy, unless BullGuy is na, where BearGuy counts up from 1 on each bar from 0.

We create our rsi and have to modify the second one to suit the function. In the case of the upper band, we mirror the lower one. So if the RSI is 80, we want it to be 20 on the upper band.

the upper band and lower band are calculated the exact same way, but mirrored. For the purpose of writing, I'm going to talk about the lower band. Assume everything is mirrored for the upper one. It finds the highest source since the last crossup or crossdown. It then multiplies from 1 / the RSI, this means that a rapid RSI increase will increase the band dramatically, so it is able to capture quick rally/reversals. We add this to the atr to source ratio, as the general volatility is a massive factor to be included. We then multiply this number by our chosen amount, and subtract it from the highest source, creating the band.

We do this same process but mirrored with both bands and compared it to the source. If the source is above the lower band, it suggests an uptrend, so the lower band is outputted, and vice versa for the upper one.
PLOTTING:
We also determine the line color in the same manner as we do the trend direction.

STRATEGY:
We then use the source again, and if it crosses up or down relative to the selected band, we enter a long or short respectively.

This may not be the most superb independent strategy, but it can be very useful as a TP/SL for your chosen entry conditions, especially in volatile markets or tickers.

Thank you for taking the time to read, and please enjoy.

Versionsinformation

Updated Chart

Versionsinformation

Updated Slippage.
Kommentarer
Dr_St
Thank you for the indicator! Can't wait to test it.
Pardon3000
Nice, but can u fix the repainting? Signals seems to disappear.
joseph_lemery
@Pardon3000, If the delay is set to 0 it will produce false signals, make sure it's set to 1 unless you use open as the source.
Pardon3000
@joseph_lemery The delay was set to 1. I will gather some examples if it happens again.
joseph_lemery
I made some slight modifications to these lines here that seems to help performance.
Lines 42-45
rsi = math.abs(ta.rsi(src, length) - 50) + 50 atr = (ta.atr(length) / src) * .5 lower := highest_custom(math.max((highest_custom(src, BearGuy) * (1 - (((1 / (rsi) * mult)) - atr))), src * sl), BearGuy) upper := lowest_custom(math.min((lowest_custom(src, BearGuy) * (1 + (((1 / (rsi) * mult)) - atr))), src * tp), BearGuy)

Both the upper band and lower band will use the same RSI, except it had been partially flipped. So an RSI of 30 == 70, and 70 == 70. Just for consistency in spikes for either direction. This change is completely optional and more of a matter of preference, but it can sometimes perform better.
The next change comes from how the ATR is Implemented in the band.
Rather than the ATR working with the RSI to increase or decrease the band size, it acts as a barrier for the RSI to overcome. This makes a lot more sense to me as a choppy market is less likely to produce false signals.
The ATR is also multiplied by .5, this can be modified and/or added as a parameter.
I wasn't sure if I should update the script, as it will replace the original version which does work fine and may be the preference for some people, also the changes weren't significant enough to warrant an entirely separate upload. So here it is for those who want it, and apologies for the excessive parentheses.
apdusp
@joseph_lemery, I tried to implement the change but got a weird result, so could you please publish the full alternative? I was interested in the original version and concept, but has many whipsaws, which you try to mitigate in the alternative version. Thanks you work your work and help

ps: though different, it reminds me of the Follow Line Indicator by @Dreadblitz, which also tries to avoid too many whipsaws ( I like the 6,1,6 setup instead of the original 21,1,5 one)
joseph_lemery
@apdusp, I'll have a V2 uploaded later today, expect an update via reply when I do.
apdusp
@joseph_lemery, great news. take your time
joseph_lemery
@apdusp, Apologies for the delay, I decided to listen and take my time, if you're wanting I can just DM you the updated version. V2 is going to be a complete revamp, and testing is taking longer than expected. I want to produce something truly exceptional. Thank you for your patience.
apdusp
@joseph_lemery, thanks for your work and reply. I prefer to wait till you feel you are satisfied. No rush here.
Mer