RSI Divergence
RSI Divergence
RSI Divergence
indName = "RSI"; // Specify the name of the indicator. This is cosmetic only.
length = Param(indName + " Length", 14, 8, 100, 1); // Indicator length
threshold = Param("Zig/Zag Threshold %", 10, 1, 50, 1); // Minimum % change in
indicator value to be considered a zig/zag
indVal = RSI(length); // Load the indVal array with the values from the indicator
of your choice
indZig = Zig(indVal, threshold); // Set the zig/zag values for the indicator
indPeakVal1 = Peak(indVal, threshold, 1); // Most recent peak value
indPeakBars1 = PeakBars(indVal, threshold, 1); // Bars since most recent peak
indPeakVal2 = Peak(indVal, threshold, 2); // Second most recent peak value
indPeakBars2 = PeakBars(indVal, threshold, 2); // Bars since second most recent
peak
indTroughVal1 = Trough(indVal, threshold, 1); // Most recent trough value
indTroughBars1 = TroughBars(indVal, threshold, 1); // Bars since most recent trough
indTroughVal2 = Trough(indVal, threshold, 2); // Second most recent trough value
indTroughBars2 = TroughBars(indVal, threshold, 2); // Bars since second most recent
trough
// Bearish divergence
divergeBear = IIf(peakBar AND (indPeakVal1 < indPeakVal2) AND High > Ref(High,
-indPeakBars2), True, False);
Short = Ref(divergeBear, -1); // Enter short if the previous bar was a bearish
divergence
Cover = Ref(troughBar, -1); // Cover short if the previous bar was a trough
// Bullish divergence
divergeBull = IIf((indTroughBars1 == 0) AND (indTroughVal1 > indTroughVal2) AND Low
< Ref(Low, -indTroughBars2), True, False);
Buy = Ref(divergeBull, -1); // Enter long if the previous bar was a bullish
divergence
Sell = Ref(peakBar, -1); // Close long if the previous bar was a peak