#START OF RSI/Stochastic/MACD Confluence Strategy for ThinkOrSwim
#
#CHANGELOG
# 2020.10.27 V1.0 @cos251 - Added RSI, StochasticSlow and MACD to same indicator
# - also calculates MACD;
# Will shade the lower plot area if 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;
################################################################
########## RSI #########
################################################################
input paintBars = yes;
input showShade = no;
input tradetype = { default "long", "short", "both" };
input lengthRSI = 7;
input price = close;
input averageTypeRSI = AverageType.EXPONENTIAL;
def NetChgAvg = MovingAverage(averageTypeRSI, price - price[1], lengthRSI);
def TotChgAvg = MovingAverage(averageTypeRSI, AbsValue(price - price[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 = 14; #Originally 14 but changed to 5 by coder
input DPeriod = 3;
input priceH = high;
input priceL = low;
input priceC = close;
input averageTypeStoch = AverageType.WILDERS;
def SlowK = reference StochasticFull(over_boughtSt, over_soldSt, KPeriod, DPeriod, priceH, priceL, priceC, 3, averageTypeStoch).FullK;
def SlowD = reference StochasticFull(over_boughtSt, over_soldSt, KPeriod, DPeriod, priceH, priceL, priceC, 3, averageTypeStoch).FullD;
#################################################################
#MACD Calculation
input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageTypeMACD = AverageType.WEIGHTED;
def Value = MovingAverage(averageTypeMACD, close, fastLength) - MovingAverage(averageTypeMACD, close, slowLength);
def Avg = MovingAverage(averageTypeMACD, Value, MACDLength);
def Diff = Value - Avg;
####### AssignPriceColor - paint candles / bars #######
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.pink else Color.CURRENT);
#################################################################
############ Shade areas based on criteria; adjust as needed ##
#################################################################
AddCloud(if showShade 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 showShade 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;
def LongBuy = if UpTrend == 1 and UpTrend[1] == 0 then 1 else 0;
def LongExit = if UpTrend[1] == 1 and UpTrend == 0 then 1 else 0;
def ShortSell = if DownTrend == 1 and DownTrend[1] == 0 then 1 else 0;
def ShortExit = if DownTrend == 0 and DownTrend[1] == 1 then 1 else 0;
UpTrend.Hide();
DownTrend.Hide();
AddLabel(yes, 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.yellow);
########### my additions to PowerX #########
# Big Kudos to Cos251 for the main code without which this would not be possible. To Welkin for the Horizontal Line and Cloud code. To Dublin_Capital for the number counting.
#I guess this could be considered ver 2 ?
AddLabel(yes, "Daily ver. " , color.cyan);
def ADR = Average(High(period = AggregationPeriod.DAY) - Low(period = AggregationPeriod.DAY), 7);
def EntryPoint = close(period = AggregationPeriod.DAY);
def TargetUp = close(period = AggregationPeriod.DAY) + (ADR * 3);
def TargetDn = close(period = AggregationPeriod.DAY) - (ADR * 3);
def StopLossUp = close(period = AggregationPeriod.DAY) - (ADR * 1.5);
def StopLossDn = close(period = AggregationPeriod.DAY) + (ADR * 1.5);
#number counting during Trend
# Up
def longSignal = UpTrend == 1;
def barNumberLong = CompoundValue(1, if LongSignal == 1 then barNumberLong[1] + 1 else 0, 0);
plot barsUp = if barNumberLong > 0 then barNumberLong else Double.NaN;
barsUp.SetPaintingStrategy(PaintingStrategy.VALUES_ABOVE);
barsUp.SetDefaultColor(Color.GREEN);
# Dn
def shortSignal = DownTrend == 1;
def barNumberShort = CompoundValue(1, if shortSignal == 1 then barNumberShort[1] + 1 else 0, 0);
plot barsDn = if barNumberShort > 0 then barNumberShort else Double.NaN;
barsDn.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);
barsDn.SetDefaultColor(Color.RED);
#- Use instead of crossingType / switch ?
def TrendUp = UpTrend crosses above DownTrend;
def TrendDn = DownTrend crosses above UpTrend;
#--Entry, actually Daily Close
#up
def EntrylineUp = if TrendUp then EntryPoint else if TrendUp[1] and !TrendUp then EntryPoint[1] else EntrylineUp[1];
plot EntrySigUp = if UpTrend == 1 then EntrylineUp else Double.NaN;
EntrySigUp.SetDefaultColor(Color.GREEN);
EntrySigUp.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
EntrySigUp.Hide(); # Add a # in front if you want a solid line displayed
#dn
def EntrylineDn = if TrendDn then EntryPoint else if TrendDn[1] and !TrendDn then EntryPoint[1] else EntrylineDn[1];
plot EntrySigDn = if DownTrend == 1 then EntrylineDn else Double.NaN;
EntrySigDn.SetDefaultColor(Color.GREEN);
EntrySigDn.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
EntrySigDn.Hide(); # Add a # in front if you want a solid line displayed
#--Profit Target points
#up
def TargetLineUp = if TrendUp then TargetUp else if TrendUp[1] and !TrendUp then TargetUp[1] else TargetLineUp[1];
plot TargetSigUp = if UpTrend == 1 then TargetLineUp else Double.NaN;
TargetSigUp.SetDefaultColor(Color.GREEN);
TargetSigUp.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
TargetSigUp.Hide(); # Add a # in front if you want a solid line displayed
#dn
def TargetLineDn = if TrendDn then TargetDn else if TrendDn[1] and !TrendDn then TargetDn[1] else TargetLineDn[1];
plot TargetSigDn = if DownTrend == 1 then TargetLineDn else Double.NaN;
TargetSigDn.SetDefaultColor(Color.GREEN);
TargetSigDn.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
TargetSigDn.Hide(); # Add a # in front if you want a solid line displayed
#--Stop Loss points
#up
def StopLineUp = if TrendUp then StopLossUp else if TrendUp[1] and !TrendUp then StopLossUp[1] else StopLineUp[1];
plot StopSigUp = if UpTrend == 1 then StopLineUp else Double.NaN;
StopSigUp.SetDefaultColor(Color.PINK);
StopSigUp.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
StopSigUp.Hide(); # Add a # in front if you want a solid line displayed
#dn
def StopLineDn = if TrendDn then StopLossDn else if TrendDn[1] and !TrendDn then StopLossDn[1] else StopLineDn[1];
plot StopSigDn = if DownTrend == 1 then StopLineDn else Double.NaN;
StopSigDn.SetDefaultColor(Color.PINK);
StopSigDn.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
StopSigDn.Hide(); # Add a # in front if you want a solid line displayed
#--Clouds
#up
AddCloud( if UpTrend == 1 then TargetSigUp else Double.NaN, EntrySigUp, Color.Light_Green, Color.Light_Green);
AddCloud(if UpTrend == 1 then EntrySigUp else Double.NaN, StopSigUp, Color.Pink, Color.Pink);
#dn
AddCloud( if DownTrend == 1 then TargetSigDn else Double.NaN, EntrySigDn, Color.Light_Green, Color.Light_Green);
AddCloud(if DownTrend == 1 then EntrySigDn else Double.NaN, StopSigDn, Color.Pink, Color.Pink);
#---Labels
#up
AddLabel(yes and UpTrend, "TargetUp = " +TargetSigUp+ "", color.cyan);
AddLabel(yes and UpTrend, " : StopUp = " +StopSigUp+ "", color.Pink);
#dn
AddLabel(yes and DownTrend, "TargetDn = " +TargetSigDn+ "", color.cyan);
AddLabel(yes and DownTrend, " : StopDn = " +StopSigDn+ "", color.Pink);
#-- extra ADR test - (the .1 is a made up test value)
plot Targ2Up = if UpTrend == 1 then TargetLineUp + (TargetLineUp * .1) else Double.NAN;
plot Targ2Dn = if DownTrend == 1 then TargetLineDn - (TargetLineDn * .1) else Double.NAN;
# end code