From 203a8937eb5a665dd82a0334e3dc9088c133c70f Mon Sep 17 00:00:00 2001 From: Oswald Reyes Date: Thu, 14 Oct 2021 11:54:49 -0400 Subject: [PATCH] Added RSI a a check --- ...> strategy-rsi-divergences-fixed-stop.pine | 2 +- ...trategy-rsi-divergences-trailing-stop.pine | 35 +++++++++++++++---- ...rgences.pine => study-rsi-divergences.pine | 8 ++--- 3 files changed, 33 insertions(+), 12 deletions(-) rename strategy-rsi-stoch-divergences-fixed-stop.pine => strategy-rsi-divergences-fixed-stop.pine (99%) rename strategy-rsi-stoch-divergences-trailing-stop.pine => strategy-rsi-divergences-trailing-stop.pine (91%) rename study-rsi-stoch-divergences.pine => study-rsi-divergences.pine (97%) diff --git a/strategy-rsi-stoch-divergences-fixed-stop.pine b/strategy-rsi-divergences-fixed-stop.pine similarity index 99% rename from strategy-rsi-stoch-divergences-fixed-stop.pine rename to strategy-rsi-divergences-fixed-stop.pine index 73a2153..094667d 100644 --- a/strategy-rsi-stoch-divergences-fixed-stop.pine +++ b/strategy-rsi-divergences-fixed-stop.pine @@ -2,7 +2,7 @@ // © Ossy1717 //@version=4 -strategy("(Strategy) RSI + Stoch Divergences %SL/TP", overlay = true, max_bars_back = 1000, max_lines_count = 400, max_labels_count = 400, initial_capital = 1000, default_qty_value = 1000, default_qty_type = strategy.cash) +strategy("(Strategy) RSI Divergences %SL/TP", overlay = true, max_bars_back = 1000, max_lines_count = 400, max_labels_count = 400, initial_capital = 1000, default_qty_value = 1000, default_qty_type = strategy.cash) // Inputs onlyAPlus = input(defval = false, title = "Backtest Only A+ Setups?", tooltip="A+ is when all the divergerce occurs in the oversold/overbought area") diff --git a/strategy-rsi-stoch-divergences-trailing-stop.pine b/strategy-rsi-divergences-trailing-stop.pine similarity index 91% rename from strategy-rsi-stoch-divergences-trailing-stop.pine rename to strategy-rsi-divergences-trailing-stop.pine index 54ff377..6850101 100644 --- a/strategy-rsi-stoch-divergences-trailing-stop.pine +++ b/strategy-rsi-divergences-trailing-stop.pine @@ -2,12 +2,16 @@ // © Ossy1717 //@version=4 -strategy("(Strategy) RSI + Stoch Divergences Trailing Stop", overlay = true, max_bars_back = 1000, max_lines_count = 400, max_labels_count = 400, initial_capital = 1000, default_qty_value = 1000, default_qty_type = strategy.cash) +strategy("(Strategy) RSI Divergences Trailing Stop", overlay = true, max_bars_back = 1000, max_lines_count = 400, max_labels_count = 400, initial_capital = 1000, default_qty_value = 1000, default_qty_type = strategy.cash) // Inputs -onlyAPlus = input(defval = "All Setups", title = "Show Only A+ Setups", options = ["All Setups", "Only A+"], tooltip="Show only setups where RSI & Stoch are out of bands") +onlyAPlus = input(defval = false, title = "Backtest Only A+ Setups?", tooltip="A+ is when all the divergerce occurs in the oversold/overbought area") +tradeDirection = input(defval = "All", title = "Backtest Only Long, Only Shorts or All Setups?", options = ["All", "Longs", "Shorts"]) showTrailingStopLine = input(defval = "Yes", title = "Show Trailing Stop Line", options = ["Yes", "No"]) -trailingStopPerc = input(defval = 2, title = "Stop Loss %", minval = 1, maxval = 100) / 100 +trailingStopPerc = input(defval = 2, title = "Stop Loss %", minval = 0.5, step = 0.5, maxval = 100) / 100 +checkRsiIndicator = input(defval = "Yes", title = "Check RSI Div Indicator", options = ["Yes", "No"]) +lenFast = input(5, minval=1, title="Length Fast RSI") +lenSlow = input(14, minval=1, title="Length Slow RSI") prd = input(defval = 5, title = "Pivot Period", minval = 1, maxval = 50) source = input(defval = "Close", title = "Source for Pivot Points", options = ["Close", "High/Low"]) searchdiv = input(defval = "Regular", title = "Divergence Type", options = ["Regular", "Hidden", "Regular/Hidden"]) @@ -76,6 +80,23 @@ maFast = vwma(close, 12), maSlow = vwma(close, 26), vwmacd = maFast - maSlow // Cmfm = ((close-low) - (high-close)) / (high - low), Cmfv = Cmfm * volume, cmf = sma(Cmfv, 21) / sma(volume,21) // Chaikin money flow Mfi = mfi(close, 14) // Moneyt Flow Index +// RSI DIV Indicator +upFast = rma(max(change(close), 0), lenFast) +downFast = rma(-min(change(close), 0), lenFast) +rsiFast = downFast == 0 ? 100 : upFast == 0 ? 0 : 100 - (100 / (1 + upFast / downFast)) +upSlow = rma(max(change(close), 0), lenSlow) +downSlow = rma(-min(change(close), 0), lenSlow) +rsiSlow = downSlow == 0 ? 100 : upSlow == 0 ? 0 : 100 - (100 / (1 + upSlow / downSlow)) +//plotfast = plot(rsiFast, color=blue) +//plotslow = plot(rsiSlow, color=orange) +divergence = rsiFast - rsiSlow +isDivBullish = divergence > 0 +isDivBearish = divergence < 0 +// plotdiv = plot(divergence, color = divergence > 0 ? color.lime: color.red, linewidth = 2) +//band1 = hline(70,color=green) +//band0 = hline(30,color=red) +// band = hline(0) + // Calcs co = crossover(k, d) cu = crossunder(k, d) @@ -388,7 +409,7 @@ for x = 0 to 10 if y == 0 isRsiOversold = rsi[startpoint] <= rsiOversold isRsiComingFromOversold = rsi[barAmount] <= rsiOversold - if isRsiComingFromOversold + if ((onlyAPlus and isRsiComingFromOversold and isRsiOversold) or (not onlyAPlus and isRsiComingFromOversold) and (checkRsiIndicator == 'No' or (checkRsiIndicator == 'Yes' and isDivBullish))) pos_reg_div_detected := true signalTooltip = isRsiOversold ? "A+: Div In Oversold Zone" : "A: RSI NOT Oversold" signalColor = isRsiOversold ? color.green : color.blue @@ -396,7 +417,7 @@ for x = 0 to 10 if y == 1 isRsiOverbought = rsi[startpoint] >= rsiOverbought isRsiComingFromOverbought = rsi[barAmount] >= rsiOverbought - if isRsiComingFromOverbought + if (onlyAPlus and isRsiComingFromOverbought and isRsiOverbought) or (not onlyAPlus and isRsiComingFromOverbought) neg_reg_div_detected := true signalTooltip = isRsiOverbought ? "A+: Div In Overbought Zone" : "A: RSI NOT In Overbought Zone" signalColor = isRsiOverbought ? color.red : color.orange @@ -445,8 +466,8 @@ if showindis != "Don't Show" or shownum // Conditions timePeriod = time >= fromDate notInTrade = strategy.position_size == 0 -goLong = pos_reg_div_detected and notInTrade and timePeriod -goShort = neg_reg_div_detected and notInTrade and timePeriod +goLong = pos_reg_div_detected and notInTrade and timePeriod and (tradeDirection == "All" or tradeDirection == "Longs") +goShort = neg_reg_div_detected and notInTrade and timePeriod and (tradeDirection == "All" or tradeDirection == "Shorts") // Determine trail stop loss prices longStopPrice = 0.0, shortStopPrice = 0.0 diff --git a/study-rsi-stoch-divergences.pine b/study-rsi-divergences.pine similarity index 97% rename from study-rsi-stoch-divergences.pine rename to study-rsi-divergences.pine index 299c1be..364fd50 100644 --- a/study-rsi-stoch-divergences.pine +++ b/study-rsi-divergences.pine @@ -2,10 +2,10 @@ // © Ossy1717 //@version=4 -study("(Study) RSI + Stoch Divergences", overlay = true, max_bars_back = 1000, max_lines_count = 400, max_labels_count = 400) +study("(Study) RSI Divergences", overlay = true, max_bars_back = 1000, max_lines_count = 400, max_labels_count = 400) // Inputs -onlyAPlus = input(defval = "All Setups", title = "Show Only A+ Setups", options = ["All Setups", "Only A+"], tooltip="Show only setups where RSI & Stoch are out of bands") +onlyAPlus = input(defval = false, title = "Show Only A+ Setups?", tooltip="A+ is when all the divergerce occurs in the oversold/overbought area") showTrailingStopLine = input(defval = "Yes", title = "Show Trailing Stop Line", options = ["Yes", "No"]) trailingStopPerc = input(defval = 2, title = "Stop Loss %", minval = 1, maxval = 100) / 100 prd = input(defval = 5, title = "Pivot Period", minval = 1, maxval = 50) @@ -388,7 +388,7 @@ for x = 0 to 10 if y == 0 isRsiOversold = rsi[startpoint] <= rsiOversold isRsiComingFromOversold = rsi[barAmount] <= rsiOversold - if isRsiComingFromOversold + if (onlyAPlus and isRsiComingFromOversold and isRsiOversold) or (not onlyAPlus and isRsiComingFromOversold) pos_reg_div_detected := true signalTooltip = isRsiOversold ? "A+: Div In Oversold Zone" : "A: RSI NOT Oversold" signalColor = isRsiOversold ? color.green : color.blue @@ -396,7 +396,7 @@ for x = 0 to 10 if y == 1 isRsiOverbought = rsi[startpoint] >= rsiOverbought isRsiComingFromOverbought = rsi[barAmount] >= rsiOverbought - if isRsiComingFromOverbought + if (onlyAPlus and isRsiComingFromOverbought and isRsiOverbought) or (not onlyAPlus and isRsiComingFromOverbought) neg_reg_div_detected := true signalTooltip = isRsiOverbought ? "A+: Div In Overbought Zone" : "A: RSI NOT In Overbought Zone" signalColor = isRsiOverbought ? color.red : color.orange