#START OF RSI/Stochastic/MACD Confluence Strategy for ThinkOrSwim
#FVO_RSM_TREND_ADR_UPPER_MTF
#
#CHANGELOG
#
# 2020.11.30 V1.2 @cos251 - Add ADR plots and shading per request; will tweak these settings to improve overall
# options
# 2020.11.12 V1.1 @cos251 - Changed from strategy to standard study. Added Green Arrow UP
# and Red Arrow down when trend starts. Added option to change to high
# timeframe but this WILL repaint; if used, should be used with other
# indicators to confirm an entry/exit.
#
# 2020.10.27 V1.0 @cos251 - Added RSI, StochasticSlow and MACD to same indicator
# - also calculates MACD;
# - Will shade the lower plot area of the following conditions are met
# Shade GREEN = RSI > 50 and SlowK > 50 and (macd)Value > (macd)Avg
# Shade RED = RSI < 50 and SlowK < 50 and (macd)Value < (macd)Avg
#
#
#REQUIREMENTS - RSI Set to 7, EXPONENTIAL
# Stoch Slow 5(not14) and 3 WILDERS
# MACD 12,26,9 WEIGHTED
declare upper;
################################################################
########## Variables #########
################################################################
input aggPeriod = AggregationPeriod.MIN;
input paintBars = yes;
input showTrendShade = no;
input tradetype = { "long", "short", default "both" };
input showTrendLabels = no;
input showIndLabels = no;
input plotADR = yes;
input showADRZones = no;
input showADRLabels = no;
################################################################
########## RSI #########
################################################################
input lengthRSI = 7;
input price = close;
input averageTypeRSI = AverageType.EXPONENTIAL;
def NetChgAvg = MovingAverage(averageTypeRSI, close(period = aggPeriod) - close(period = aggPeriod)[1], lengthRSI);
def TotChgAvg = MovingAverage(averageTypeRSI, AbsValue(close(period = aggPeriod) - close(period = aggPeriod)[1]), lengthRSI);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;
def RSI = 50 * (ChgRatio + 1);
################################################################
########## Stochastic Slow #########
################################################################
input over_boughtSt = 80;
input over_soldSt = 20;
input KPeriod = 5;
input DPeriod = 3;
input averageTypeStoch = AverageType.WILDERS;
def SlowK = reference StochasticFull(over_boughtSt, over_soldSt, KPeriod, DPeriod, high(period = aggPeriod), low(period = aggPeriod), close(period = aggPeriod), 3, averageTypeStoch).FullK;
def SlowD = reference StochasticFull(over_boughtSt, over_soldSt, KPeriod, DPeriod, high(period = aggPeriod), low(period = aggPeriod), close(period = aggPeriod), 3, averageTypeStoch).FullD;
#################################################################
#MACD Calculation
input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageTypeMACD = AverageType.WEIGHTED;
def Value = MovingAverage(averageTypeMACD, close(period = aggPeriod), fastLength) - MovingAverage(averageTypeMACD, close(period = aggPeriod), slowLength);
def Avg = MovingAverage(averageTypeMACD, Value, MACDLength);
def Diff = Value - Avg;
#AssignPriceColor
AssignPriceColor(if paintBars and RSI >= 50 and SlowK >= 50 and Value > Avg then Color.GREEN else if paintBars and RSI < 50 and SlowK < 50 and Value < Avg then Color.RED else if paintBars then Color.DARK_GRAY else Color.CURRENT);
#################################################################
############ Shade areas based on criteria; adjust as needed ##
#################################################################
AddCloud(if showTrendShade and RSI >= 50 and SlowK >= 50 and Value > Avg then Double.POSITIVE_INFINITY else Double.NaN, if RSI >= 50 and SlowK >= 50 and Value > Avg then Double.NEGATIVE_INFINITY else Double.NaN, Color.LIGHT_GREEN);
AddCloud(if showTrendShade and RSI < 50 and SlowK < 50 and Value < Avg then Double.POSITIVE_INFINITY else Double.NaN, if RSI < 50 and SlowK < 50 and Value < Avg then Double.NEGATIVE_INFINITY else Double.NaN, Color.LIGHT_RED);
#################################################################
############ SCAN Variables #########
#################################################################
plot UpTrend = if RSI >= 50 and SlowK >= 50 and Value > Avg then 1 else 0;
plot DownTrend = if RSI < 50 and SlowK < 50 and Value < Avg then 1 else 0;
UpTrend.Hide();
DownTrend.Hide();
AddLabel(if showTrendLabels then yes else no, if UpTrend == 1 then "::RSM-Signal:LONG" else if DownTrend == 1 then "::RSM-Signal:SHORT" else "::RSM-Signal:IDLE", if UpTrend == 1 then Color.GREEN else if DownTrend == 1 then Color.RED else Color.GRAY);
plot upArrow = if UpTrend == 1 and UpTrend[1] == 0 and (tradetype == tradetype.long or tradetype == tradetype.both) then low else Double.NaN;
upArrow.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
upArrow.SetDefaultColor(Color.GREEN);
upArrow.SetLineWeight(4);
plot downArrow = if DownTrend == 1 and DownTrend[1] == 0 and (tradetype == tradetype.short or tradetype == tradetype.both) then high else Double.NaN;
downArrow.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
downArrow.SetDefaultColor(Color.RED);
downArrow.SetLineWeight(4);
# Average Daily Range
def isLastBar = !IsNaN(close) and IsNaN(close[-1]);
def beforeStart = GetTime() > RegularTradingStart(GetYYYYMMDD());
def afterEnd = GetTime() < RegularTradingEnd(GetYYYYMMDD());
def ADR = Average(high(period = AggregationPeriod.DAY) - low(period = AggregationPeriod.DAY), 7);
def dailyO = DailyOpen();
def profitADR = (ADR * 3) + DailyOpen();
def stopADR = dailyO - (ADR * 1.5);
plot profitZone = if plotADR and (beforeStart and afterEnd) then profitADR else Double.NaN; # uncheckmark the box in settings to hide this.
plot stopZone = if plotADR and (beforeStart and afterEnd) then stopADR else Double.NaN;
plot o = if plotADR and (beforeStart and afterEnd) then DailyOpen() else Double.NaN;
plot oneADRPlus = if plotADR and (beforeStart and afterEnd) then (dailyO + ADR) else Double.NaN;
plot twoADRPlus = if plotADR and (beforeStart and afterEnd) then dailyO + (ADR * 2) else Double.NaN;
plot oneADRMinus = if plotADR and (beforeStart and afterEnd) then dailyO - ADR else Double.NaN;
profitZone.SetDefaultColor(Color.GREEN);
stopZone.SetDefaultColor(Color.RED);
o.SetDefaultColor(Color.CYAN);
oneADRPlus.SetDefaultColor(Color.LIGHT_GREEN);
twoADRPlus.SetDefaultColor(Color.LIGHT_GREEN);
oneADRMinus.SetDefaultColor(Color.LIGHT_RED);
AddLabel(if showADRLabels then yes else no, "Open=" + dailyO);
AddLabel(if showADRLabels then yes else no, "Profit Target 1 = " + oneADRPlus, Color.GREEN);
AddLabel(if showADRLabels then yes else no, " : Stop Loss = -" + ADR * 1.5 + "", Color.PINK);
AddLabel(if showADRLabels then yes else no, " : ADR daily = " + ADR + "", Color.GRAY);
AddLabel(if showADRLabels then yes else no, "RSI:" + RSI + " sK:" + SlowK + " Macd:" + Value, Color.ORANGE);
# ADR Zones
AddCloud(if showADRZones and (beforeStart and afterEnd) then profitADR else Double.NaN, if (beforeStart and afterEnd) then dailyO else Double.NaN, CreateColor(69, 224, 255));
AddCloud(if showADRZones and (beforeStart and afterEnd) then dailyO else Double.NaN, if (beforeStart and afterEnd) then stopADR else Double.NaN, CreateColor(255, 12, 151));