#START OF RSI/Stochastic/MACD Confluence Strategy for ThinkOrSwim
#FVO_RSM_MTF_SIGNALS_V2
#
#CHANGELOG
#
# 2020.12.02 V1.0 @cos251 - Added RSM signal calculation for following timeframes:
#                         - 1m, 2m, 5m, 15m, 30m, 1h, 2h, 4h, D
#                         - Label will be added to top of chart for every allowed TF
#                         - Label will read "TF:L(Long):S(Short):I(Idle)"
#                         - Label Color will be green, red or gray accordingly
#
#                  
#REQUIREMENTS - RSI Set to 7, EXPONENTIAL
#               Stoch Slow 5(not14) and 3 WILDERS
#               MACD 12,26,9 WEIGHTED
#
#ORIGINAL REQUEST - @Joseph Patrick 18
#                 - Link: https://usethinkscript.com/threads/mimicking-power-x-strategy-by-markus-heitkoetter.4283/
#
#
AddLabel(yes, "::RSM-Signal::", Color.ORANGE);
# Timeframe boolean variables
def one_min_bool;
def two_min_bool;
def five_min_bool;
def fifteen_min_bool;
def thirty_min_bool;
def one_hour_bool;
def two_hour_bool;
def four_hour_bool;
def one_day_bool;
# Check if current chart is less that Day Aggregation Period
def ltDay = if GetAggregationPeriod() < AggregationPeriod.DAY then 1 else Double.NaN;
# Set Chart for 1m
if GetAggregationPeriod() == AggregationPeriod.MIN {
    one_min_bool = 1;
} else {
    one_min_bool = 0;
}
# Set Chart for 2m
if GetAggregationPeriod() == AggregationPeriod.TWO_MIN {
    two_min_bool = 1;
} else {
    two_min_bool = 0;
}
# Set Chart for 5m
if GetAggregationPeriod() == AggregationPeriod.FIVE_MIN {
    five_min_bool = 1;
} else {
    five_min_bool = 0;
}
# Set Chart for 15mm
if GetAggregationPeriod() == AggregationPeriod.FIFTEEN_MIN {
    fifteen_min_bool = 1;
} else {
    fifteen_min_bool = 0;
}
# Set Chart for 30m
if GetAggregationPeriod() == AggregationPeriod.THIRTY_MIN {
    thirty_min_bool = 1;
} else {
    thirty_min_bool = 0;
}
# Set Chart for 1 HOUR
if GetAggregationPeriod() == AggregationPeriod.HOUR {
    one_hour_bool = 1;
} else {
    one_hour_bool = 0;
}
# Set Chart for 2 HOURS
if GetAggregationPeriod() == AggregationPeriod.TWO_HOURS {
    two_hour_bool = 1;
} else {
    two_hour_bool = 0;
}
# Set Chart for 4 HOUR
if GetAggregationPeriod() == AggregationPeriod.FOUR_HOURS {
    four_hour_bool = 1;
} else {
    four_hour_bool = 0;
}
# Set Chart for 1 DAY
if GetAggregationPeriod() == AggregationPeriod.DAY {
    one_day_bool = 1;
} else {
    one_day_bool = 0;
}
################################################################
##########            Global Variables                 #########
################################################################
# inputs are set per the Power-X Strategy
# Link: https://rockwell-files.s3.amazonaws.com/PXCompanionGuide2ndEd_cover.pdf
# RSI
input lengthRSI = 7;
input averageTypeRSI = AverageType.EXPONENTIAL;
# Stochastic
input over_boughtSt = 80;
input over_soldSt = 20;
input KPeriod = 5;
input DPeriod = 3;
input averageTypeStoch = AverageType.WILDERS;
# MACD
input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageTypeMACD = AverageType.WEIGHTED;
################################################################
##########                 RSI                         #########
################################################################
# 1 Minute
def NetChgAvg1m;
def TotChgAvg1m;
def ChgRatio1m;
def RSI_1m;
if GetAggregationPeriod() <= AggregationPeriod.MIN {
    NetChgAvg1m = MovingAverage(averageTypeRSI, close(period = AggregationPeriod.MIN) - close(period = AggregationPeriod.MIN)[1], lengthRSI);
    TotChgAvg1m = MovingAverage(averageTypeRSI, AbsValue(close(period = AggregationPeriod.MIN) - close(period = AggregationPeriod.MIN)[1]), lengthRSI);
    ChgRatio1m = if TotChgAvg1m != 0 then NetChgAvg1m / TotChgAvg1m else 0;
    RSI_1m = 50 * (ChgRatio1m + 1);
} else {
    NetChgAvg1m = 0;
    TotChgAvg1m = 0;
    ChgRatio1m = 0;
    RSI_1m = 0;
}
################################################################
##########                 Stochastic Slow             #########
################################################################
# Stochastic 1 Minute
def SlowK_1m;
def SlowD_1m;
if GetAggregationPeriod() <= AggregationPeriod.MIN {
    SlowK_1m = reference StochasticFull(over_boughtSt, over_soldSt, KPeriod, DPeriod, high(period = AggregationPeriod.MIN), low(period =
        AggregationPeriod.MIN), close(period = AggregationPeriod.MIN), 3, averageTypeStoch).FullK;
    SlowD_1m = reference StochasticFull(over_boughtSt, over_soldSt, KPeriod, DPeriod, high(period = AggregationPeriod.MIN), low(period =
        AggregationPeriod.MIN), close(period = AggregationPeriod.MIN), 3, averageTypeStoch).FullD;
} else {
    SlowK_1m = 0;
    SlowD_1m = 0;
}
################################################################
##########                 MACD                      ###########
################################################################
#MACD Calculation
# 1 Minute
def Value_1m;
def Avg_1m;
def Diff_1m;
if GetAggregationPeriod() <= AggregationPeriod.MIN {
    Value_1m = MovingAverage(averageTypeMACD, close(period = AggregationPeriod.MIN), fastLength) - MovingAverage(averageTypeMACD, close(period =
        AggregationPeriod.MIN), slowLength);
    Avg_1m = MovingAverage(averageTypeMACD, Value_1m, MACDLength);
    Diff_1m = Value_1m - Avg_1m;
} else {
    Value_1m = 0;
    Avg_1m = 0;
    Diff_1m = 0;
}
#################################################################
##########        1m  Trend  & Labels                   #########
#################################################################
plot UpTrend_1m = if RSI_1m >= 50 and SlowK_1m >= 50 and Value_1m > Avg_1m then 1 else 0;
plot DownTrend_1m = if RSI_1m < 50 and SlowK_1m < 50 and Value_1m < Avg_1m then 1 else 0;
UpTrend_1m.Hide();
DownTrend_1m.Hide();
AddLabel(if (one_min_bool == 1) then yes else no, if UpTrend_1m == 1 then "1m:L" else if DownTrend_1m == 1 then "1m:S" else "1m:I", if UpTrend_1m == 1 then Color.GREEN else if DownTrend_1m == 1 then Color.RED else Color.GRAY);
################################################################
##########                 RSI                         #########
################################################################
# 2 Minute
def NetChgAvg2m;
def TotChgAvg2m;
def ChgRatio2m;
def RSI_2m;
if GetAggregationPeriod() <= AggregationPeriod.TWO_MIN {
    NetChgAvg2m = MovingAverage(averageTypeRSI, close(period = AggregationPeriod.TWO_MIN) - close(period = AggregationPeriod.TWO_MIN)[1], lengthRSI);
    TotChgAvg2m = MovingAverage(averageTypeRSI, AbsValue(close(period = AggregationPeriod.TWO_MIN) - close(period = AggregationPeriod.TWO_MIN)[1]), lengthRSI);
    ChgRatio2m = if TotChgAvg2m != 0 then NetChgAvg2m / TotChgAvg2m else 0;
    RSI_2m = 50 * (ChgRatio2m + 1);
} else {
    NetChgAvg2m = 0;
    TotChgAvg2m = 0;
    ChgRatio2m = 0;
    RSI_2m = 0;
}
################################################################
##########                 Stochastic Slow             #########
################################################################
# Stochastic 2 Minute
def SlowK_2m;
def SlowD_2m;
if GetAggregationPeriod() <= AggregationPeriod.TWO_MIN {
    SlowK_2m = reference StochasticFull(over_boughtSt, over_soldSt, KPeriod, DPeriod, high(period = AggregationPeriod.TWO_MIN), low(period =
        AggregationPeriod.TWO_MIN), close(period = AggregationPeriod.TWO_MIN), 3, averageTypeStoch).FullK;
    SlowD_2m = reference StochasticFull(over_boughtSt, over_soldSt, KPeriod, DPeriod, high(period = AggregationPeriod.TWO_MIN), low(period =
        AggregationPeriod.TWO_MIN), close(period = AggregationPeriod.TWO_MIN), 3, averageTypeStoch).FullD;
} else {
    SlowK_2m = 0;
    SlowD_2m = 0;
}
################################################################
##########                 MACD                      ###########
################################################################
#MACD Calculation
# 2 Minute
def Value_2m;
def Avg_2m;
def Diff_2m;
if GetAggregationPeriod() <= AggregationPeriod.TWO_MIN {
    Value_2m = MovingAverage(averageTypeMACD, close(period = AggregationPeriod.TWO_MIN), fastLength) - MovingAverage(averageTypeMACD, close(period =
        AggregationPeriod.TWO_MIN), slowLength);
    Avg_2m = MovingAverage(averageTypeMACD, Value_2m, MACDLength);
    Diff_2m = Value_2m - Avg_2m;
} else {
    Value_2m = 0;
    Avg_2m = 0;
    Diff_2m = 0;
}
#################################################################
##########        2m  Trend  & Labels                   #########
#################################################################
plot UpTrend_2m = if RSI_2m >= 50 and SlowK_2m >= 50 and Value_2m > Avg_2m then 1 else 0;
plot DownTrend_2m = if RSI_2m < 50 and SlowK_2m < 50 and Value_2m < Avg_2m then 1 else 0;
UpTrend_2m.Hide();
DownTrend_2m.Hide();
AddLabel(if (one_min_bool == 1 or two_min_bool == 1 or ltDay) then yes else no, if UpTrend_2m == 1 then "2m:L" else if DownTrend_2m == 1 then "2m:S" else "2m:I", if UpTrend_2m == 1 then Color.GREEN else if DownTrend_2m == 1 then Color.RED else Color.GRAY);
################################################################
##########                 RSI                         #########
################################################################
# 5 Minute
def NetChgAvg5m;
def TotChgAvg5m;
def ChgRatio5m;
def RSI_5m;
if GetAggregationPeriod() <= AggregationPeriod.FIVE_MIN {
    NetChgAvg5m = MovingAverage(averageTypeRSI, close(period = AggregationPeriod.FIVE_MIN) - close(period = AggregationPeriod.FIVE_MIN)[1], lengthRSI);
    TotChgAvg5m = MovingAverage(averageTypeRSI, AbsValue(close(period = AggregationPeriod.FIVE_MIN) - close(period = AggregationPeriod.FIVE_MIN)[1]), lengthRSI);
    ChgRatio5m = if TotChgAvg5m != 0 then NetChgAvg5m / TotChgAvg5m else 0;
    RSI_5m = 50 * (ChgRatio5m + 1);
} else {
    NetChgAvg5m = 0;
    TotChgAvg5m = 0;
    ChgRatio5m = 0;
    RSI_5m = 0;
}
################################################################
##########                 Stochastic Slow             #########
################################################################
# Stochastic 5 Minute
def SlowK_5m;
def SlowD_5m;
if GetAggregationPeriod() <= AggregationPeriod.FIVE_MIN {
    SlowK_5m = reference StochasticFull(over_boughtSt, over_soldSt, KPeriod, DPeriod, high(period = AggregationPeriod.FIVE_MIN), low(period =
        AggregationPeriod.FIVE_MIN), close(period = AggregationPeriod.FIVE_MIN), 3, averageTypeStoch).FullK;
    SlowD_5m = reference StochasticFull(over_boughtSt, over_soldSt, KPeriod, DPeriod, high(period = AggregationPeriod.FIVE_MIN), low(period =
        AggregationPeriod.FIVE_MIN), close(period = AggregationPeriod.FIVE_MIN), 3, averageTypeStoch).FullD;
} else {
    SlowK_5m = 0;
    SlowD_5m = 0;
}
################################################################
##########                 MACD                      ###########
################################################################
#MACD Calculation
# 5 Minute
def Value_5m;
def Avg_5m;
def Diff_5m;
if GetAggregationPeriod() <= AggregationPeriod.FIVE_MIN {
    Value_5m = MovingAverage(averageTypeMACD, close(period = AggregationPeriod.FIVE_MIN), fastLength) - MovingAverage(averageTypeMACD, close(period =
        AggregationPeriod.FIVE_MIN), slowLength);
    Avg_5m = MovingAverage(averageTypeMACD, Value_5m, MACDLength);
    Diff_5m = Value_5m - Avg_5m;
} else {
    Value_5m = 0;
    Avg_5m = 0;
    Diff_5m = 0;
}
#################################################################
##########        5m  Trend  & Labels                   #########
#################################################################
plot UpTrend_5m = if RSI_5m >= 50 and SlowK_5m >= 50 and Value_5m > Avg_5m then 1 else 0;
plot DownTrend_5m = if RSI_5m < 50 and SlowK_5m < 50 and Value_5m < Avg_5m then 1 else 0;
UpTrend_5m.Hide();
DownTrend_5m.Hide();
AddLabel(if (one_min_bool == 1 or two_min_bool == 1 or five_min_bool == 1 or ltDay) then yes else no, if UpTrend_5m == 1 then "5m:L" else if DownTrend_5m == 1 then "5m:S" else "5m:I", if UpTrend_5m == 1 then Color.GREEN else if DownTrend_5m == 1 then Color.RED else Color.GRAY);
################################################################
##########                 RSI                         #########
################################################################
# 15 Minute
def NetChgAvg15m;
def TotChgAvg15m;
def ChgRatio15m;
def RSI_15m;
if GetAggregationPeriod() <= AggregationPeriod.FIFTEEN_MIN {
    NetChgAvg15m = MovingAverage(averageTypeRSI, close(period = AggregationPeriod.FIFTEEN_MIN) - close(period = AggregationPeriod.FIFTEEN_MIN)[1], lengthRSI);
    TotChgAvg15m = MovingAverage(averageTypeRSI, AbsValue(close(period = AggregationPeriod.FIFTEEN_MIN) - close(period = AggregationPeriod.FIFTEEN_MIN)[1]), lengthRSI);
    ChgRatio15m = if TotChgAvg15m != 0 then NetChgAvg15m / TotChgAvg15m else 0;
    RSI_15m = 50 * (ChgRatio15m + 1);
} else {
    NetChgAvg15m = 0;
    TotChgAvg15m = 0;
    ChgRatio15m = 0;
    RSI_15m = 0;
}
################################################################
##########                 Stochastic Slow             #########
################################################################
# Stochastic 15 Minute
def SlowK_15m;
def SlowD_15m;
if GetAggregationPeriod() <= AggregationPeriod.FIFTEEN_MIN {
    SlowK_15m = reference StochasticFull(over_boughtSt, over_soldSt, KPeriod, DPeriod, high(period = AggregationPeriod.FIFTEEN_MIN), low(period =
        AggregationPeriod.FIFTEEN_MIN), close(period = AggregationPeriod.FIFTEEN_MIN), 3, averageTypeStoch).FullK;
    SlowD_15m = reference StochasticFull(over_boughtSt, over_soldSt, KPeriod, DPeriod, high(period = AggregationPeriod.FIFTEEN_MIN), low(period =
        AggregationPeriod.FIFTEEN_MIN), close(period = AggregationPeriod.FIFTEEN_MIN), 3, averageTypeStoch).FullD;
} else {
    SlowK_15m = 0;
    SlowD_15m = 0;
}
################################################################
##########                 MACD                      ###########
################################################################
#MACD Calculation
# 15 Minute
def Value_15m;
def Avg_15m;
def Diff_15m;
if GetAggregationPeriod() <= AggregationPeriod.FIFTEEN_MIN {
    Value_15m = MovingAverage(averageTypeMACD, close(period = AggregationPeriod.FIFTEEN_MIN), fastLength) - MovingAverage(averageTypeMACD, close(period =
        AggregationPeriod.FIFTEEN_MIN), slowLength);
    Avg_15m = MovingAverage(averageTypeMACD, Value_15m, MACDLength);
    Diff_15m = Value_15m - Avg_15m;
} else {
    Value_15m = 0;
    Avg_15m = 0;
    Diff_15m = 0;
}
#################################################################
##########        15m  Trend  & Labels                   #########
#################################################################
plot UpTrend_15m = if RSI_15m >= 50 and SlowK_15m >= 50 and Value_15m > Avg_15m then 1 else 0;
plot DownTrend_15m = if RSI_15m < 50 and SlowK_15m < 50 and Value_15m < Avg_15m then 1 else 0;
UpTrend_15m.Hide();
DownTrend_15m.Hide();
AddLabel(if (one_min_bool == 1 or two_min_bool == 1 or five_min_bool == 1 or fifteen_min_bool == 1 or ltDay) then yes else no, if UpTrend_15m == 1 then "15m:L" else if DownTrend_15m == 1 then "15m:S" else "15m:I", if UpTrend_15m == 1 then Color.GREEN else if DownTrend_15m == 1 then Color.RED else Color.GRAY);
################################################################
##########                 RSI                         #########
################################################################
# 30 Minute
def NetChgAvg30m;
def TotChgAvg30m;
def ChgRatio30m;
def RSI_30m;
if GetAggregationPeriod() <= AggregationPeriod.THIRTY_MIN {
    NetChgAvg30m = MovingAverage(averageTypeRSI, close(period = AggregationPeriod.THIRTY_MIN) - close(period = AggregationPeriod.THIRTY_MIN)[1], lengthRSI);
    TotChgAvg30m = MovingAverage(averageTypeRSI, AbsValue(close(period = AggregationPeriod.THIRTY_MIN) - close(period = AggregationPeriod.THIRTY_MIN)[1]), lengthRSI);
    ChgRatio30m = if TotChgAvg30m != 0 then NetChgAvg30m / TotChgAvg30m else 0;
    RSI_30m = 50 * (ChgRatio30m + 1);
} else {
    NetChgAvg30m = 0;
    TotChgAvg30m = 0;
    ChgRatio30m = 0;
    RSI_30m = 0;
}
################################################################
##########                 Stochastic Slow             #########
################################################################
# Stochastic 30 Minute
def SlowK_30m;
def SlowD_30m;
if GetAggregationPeriod() <= AggregationPeriod.THIRTY_MIN {
    SlowK_30m = reference StochasticFull(over_boughtSt, over_soldSt, KPeriod, DPeriod, high(period = AggregationPeriod.THIRTY_MIN), low(period =
        AggregationPeriod.THIRTY_MIN), close(period = AggregationPeriod.THIRTY_MIN), 3, averageTypeStoch).FullK;
    SlowD_30m = reference StochasticFull(over_boughtSt, over_soldSt, KPeriod, DPeriod, high(period = AggregationPeriod.THIRTY_MIN), low(period =
        AggregationPeriod.THIRTY_MIN), close(period = AggregationPeriod.THIRTY_MIN), 3, averageTypeStoch).FullD;
} else {
    SlowK_30m = 0;
    SlowD_30m = 0;
}
################################################################
##########                 MACD                      ###########
################################################################
#MACD Calculation
# 30 Minute
def Value_30m;
def Avg_30m;
def Diff_30m;
if GetAggregationPeriod() <= AggregationPeriod.THIRTY_MIN {
    Value_30m = MovingAverage(averageTypeMACD, close(period = AggregationPeriod.THIRTY_MIN), fastLength) - MovingAverage(averageTypeMACD, close(period =
        AggregationPeriod.THIRTY_MIN), slowLength);
    Avg_30m = MovingAverage(averageTypeMACD, Value_30m, MACDLength);
    Diff_30m = Value_30m - Avg_30m;
} else {
    Value_30m = 0;
    Avg_30m = 0;
    Diff_30m = 0;
}
#################################################################
##########        30m  Trend  & Labels                   #########
#################################################################
plot UpTrend_30m = if RSI_30m >= 50 and SlowK_30m >= 50 and Value_30m > Avg_30m then 1 else 0;
plot DownTrend_30m = if RSI_30m < 50 and SlowK_30m < 50 and Value_30m < Avg_30m then 1 else 0;
UpTrend_30m.Hide();
DownTrend_30m.Hide();
AddLabel(if (one_min_bool == 1 or two_min_bool == 1 or five_min_bool == 1 or fifteen_min_bool == 1 or thirty_min_bool == 1 or ltDay) then yes else no, if UpTrend_30m == 1 then "30m:L" else if DownTrend_30m == 1 then "30m:S" else "30m:I", if UpTrend_30m == 1 then Color.GREEN else if DownTrend_30m == 1 then Color.RED else Color.GRAY);
################################################################
##########                 RSI                         #########
################################################################
# 1 HOUR
def NetChgAvg1h;
def TotChgAvg1h;
def ChgRatio1h;
def RSI_1h;
if GetAggregationPeriod() <= AggregationPeriod.HOUR {
    NetChgAvg1h = MovingAverage(averageTypeRSI, close(period = AggregationPeriod.HOUR) - close(period = AggregationPeriod.HOUR)[1], lengthRSI);
    TotChgAvg1h = MovingAverage(averageTypeRSI, AbsValue(close(period = AggregationPeriod.HOUR) - close(period = AggregationPeriod.HOUR)[1]), lengthRSI);
    ChgRatio1h = if TotChgAvg1h != 0 then NetChgAvg1h / TotChgAvg1h else 0;
    RSI_1h = 50 * (ChgRatio1h + 1);
} else {
    NetChgAvg1h = 0;
    TotChgAvg1h = 0;
    ChgRatio1h = 0;
    RSI_1h = 0;
}
################################################################
##########                 Stochastic Slow             #########
################################################################
# Stochastic 1 HOUR
def SlowK_1h;
def SlowD_1h;
if GetAggregationPeriod() <= AggregationPeriod.HOUR {
    SlowK_1h = reference StochasticFull(over_boughtSt, over_soldSt, KPeriod, DPeriod, high(period = AggregationPeriod.HOUR), low(period =
        AggregationPeriod.HOUR), close(period = AggregationPeriod.HOUR), 3, averageTypeStoch).FullK;
    SlowD_1h = reference StochasticFull(over_boughtSt, over_soldSt, KPeriod, DPeriod, high(period = AggregationPeriod.HOUR), low(period =
        AggregationPeriod.HOUR), close(period = AggregationPeriod.HOUR), 3, averageTypeStoch).FullD;
} else {
    SlowK_1h = 0;
    SlowD_1h = 0;
}
################################################################
##########                 MACD                      ###########
################################################################
#MACD Calculation
# 1 HOUR
def Value_1h;
def Avg_1h;
def Diff_1h;
if GetAggregationPeriod() <= AggregationPeriod.HOUR {
    Value_1h = MovingAverage(averageTypeMACD, close(period = AggregationPeriod.HOUR), fastLength) - MovingAverage(averageTypeMACD, close(period =
        AggregationPeriod.HOUR), slowLength);
    Avg_1h = MovingAverage(averageTypeMACD, Value_1h, MACDLength);
    Diff_1h = Value_1h - Avg_1h;
} else {
    Value_1h = 0;
    Avg_1h = 0;
    Diff_1h = 0;
}
#################################################################
##########        1h  Trend  & Labels                   #########
#################################################################
plot UpTrend_1h = if RSI_1h >= 50 and SlowK_1h >= 50 and Value_1h > Avg_1h then 1 else 0;
plot DownTrend_1h = if RSI_1h < 50 and SlowK_1h < 50 and Value_1h < Avg_1h then 1 else 0;
UpTrend_1h.Hide();
DownTrend_1h.Hide();
AddLabel(if (one_min_bool == 1 or two_min_bool == 1 or five_min_bool == 1 or fifteen_min_bool == 1 or thirty_min_bool == 1 or one_hour_bool == 1 or ltDay) then yes else no, if UpTrend_1h == 1 then "1h:L" else if DownTrend_1h == 1 then "1h:S" else "1h:I", if UpTrend_1h == 1 then Color.GREEN else if DownTrend_1h == 1 then Color.RED else Color.GRAY);
################################################################
##########                 RSI                         #########
################################################################
# 2 HOUR
def NetChgAvg2h;
def TotChgAvg2h;
def ChgRatio2h;
def RSI_2h;
if GetAggregationPeriod() <= AggregationPeriod.TWO_HOURS {
    NetChgAvg2h = MovingAverage(averageTypeRSI, close(period = AggregationPeriod.TWO_HOURS) - close(period = AggregationPeriod.TWO_HOURS)[1], lengthRSI);
    TotChgAvg2h = MovingAverage(averageTypeRSI, AbsValue(close(period = AggregationPeriod.TWO_HOURS) - close(period = AggregationPeriod.TWO_HOURS)[1]), lengthRSI);
    ChgRatio2h = if TotChgAvg2h != 0 then NetChgAvg2h / TotChgAvg2h else 0;
    RSI_2h = 50 * (ChgRatio2h + 1);
} else {
    NetChgAvg2h = 0;
    TotChgAvg2h = 0;
    ChgRatio2h = 0;
    RSI_2h = 0;
}
################################################################
##########                 Stochastic Slow             #########
################################################################
# Stochastic 2 HOUR
def SlowK_2h;
def SlowD_2h;
if GetAggregationPeriod() <= AggregationPeriod.TWO_HOURS {
    SlowK_2h = reference StochasticFull(over_boughtSt, over_soldSt, KPeriod, DPeriod, high(period = AggregationPeriod.TWO_HOURS), low(period =
        AggregationPeriod.TWO_HOURS), close(period = AggregationPeriod.TWO_HOURS), 3, averageTypeStoch).FullK;
    SlowD_2h = reference StochasticFull(over_boughtSt, over_soldSt, KPeriod, DPeriod, high(period = AggregationPeriod.TWO_HOURS), low(period =
        AggregationPeriod.TWO_HOURS), close(period = AggregationPeriod.TWO_HOURS), 3, averageTypeStoch).FullD;
} else {
    SlowK_2h = 0;
    SlowD_2h = 0;
}
################################################################
##########                 MACD                      ###########
################################################################
#MACD Calculation
# 2 HOUR
def Value_2h;
def Avg_2h;
def Diff_2h;
if GetAggregationPeriod() <= AggregationPeriod.TWO_HOURS {
    Value_2h = MovingAverage(averageTypeMACD, close(period = AggregationPeriod.TWO_HOURS), fastLength) - MovingAverage(averageTypeMACD, close(period =
        AggregationPeriod.TWO_HOURS), slowLength);
    Avg_2h = MovingAverage(averageTypeMACD, Value_2h, MACDLength);
    Diff_2h = Value_2h - Avg_2h;
} else {
    Value_2h = 0;
    Avg_2h = 0;
    Diff_2h = 0;
}
#################################################################
##########        2h  Trend  & Labels                   #########
#################################################################
plot UpTrend_2h = if RSI_2h >= 50 and SlowK_2h >= 50 and Value_2h > Avg_2h then 1 else 0;
plot DownTrend_2h = if RSI_2h < 50 and SlowK_2h < 50 and Value_2h < Avg_2h then 1 else 0;
UpTrend_2h.Hide();
DownTrend_2h.Hide();
AddLabel(if (one_min_bool == 1 or two_min_bool == 1 or five_min_bool == 1 or fifteen_min_bool == 1 or thirty_min_bool == 1 or one_hour_bool == 1 or two_hour_bool == 1 or ltDay) then yes else no, if UpTrend_2h == 1 then "2h:L" else if DownTrend_2h == 1 then "2h:S" else "2h:I", if UpTrend_2h == 1 then Color.GREEN else if DownTrend_2h == 1 then Color.RED else Color.GRAY);
################################################################
##########                 RSI                         #########
################################################################
# 4 HOUR
def NetChgAvg4h;
def TotChgAvg4h;
def ChgRatio4h;
def RSI_4h;
if GetAggregationPeriod() <= AggregationPeriod.FOUR_HOURS {
    NetChgAvg4h = MovingAverage(averageTypeRSI, close(period = AggregationPeriod.FOUR_HOURS) - close(period = AggregationPeriod.FOUR_HOURS)[1], lengthRSI);
    TotChgAvg4h = MovingAverage(averageTypeRSI, AbsValue(close(period = AggregationPeriod.FOUR_HOURS) - close(period = AggregationPeriod.FOUR_HOURS)[1]), lengthRSI);
    ChgRatio4h = if TotChgAvg4h != 0 then NetChgAvg4h / TotChgAvg4h else 0;
    RSI_4h = 50 * (ChgRatio4h + 1);
} else {
    NetChgAvg4h = 0;
    TotChgAvg4h = 0;
    ChgRatio4h = 0;
    RSI_4h = 0;
}
################################################################
##########                 Stochastic Slow             #########
################################################################
# Stochastic 4 HOUR
def SlowK_4h;
def SlowD_4h;
if GetAggregationPeriod() <= AggregationPeriod.FOUR_HOURS {
    SlowK_4h = reference StochasticFull(over_boughtSt, over_soldSt, KPeriod, DPeriod, high(period = AggregationPeriod.FOUR_HOURS), low(period =
        AggregationPeriod.FOUR_HOURS), close(period = AggregationPeriod.FOUR_HOURS), 3, averageTypeStoch).FullK;
    SlowD_4h = reference StochasticFull(over_boughtSt, over_soldSt, KPeriod, DPeriod, high(period = AggregationPeriod.FOUR_HOURS), low(period =
        AggregationPeriod.FOUR_HOURS), close(period = AggregationPeriod.FOUR_HOURS), 3, averageTypeStoch).FullD;
} else {
    SlowK_4h = 0;
    SlowD_4h = 0;
}
################################################################
##########                 MACD                      ###########
################################################################
#MACD Calculation
# 4 HOUR
def Value_4h;
def Avg_4h;
def Diff_4h;
if GetAggregationPeriod() <= AggregationPeriod.FOUR_HOURS {
    Value_4h = MovingAverage(averageTypeMACD, close(period = AggregationPeriod.FOUR_HOURS), fastLength) - MovingAverage(averageTypeMACD, close(period =
        AggregationPeriod.FOUR_HOURS), slowLength);
    Avg_4h = MovingAverage(averageTypeMACD, Value_4h, MACDLength);
    Diff_4h = Value_4h - Avg_4h;
} else {
    Value_4h = 0;
    Avg_4h = 0;
    Diff_4h = 0;
}
#################################################################
##########        4h  Trend  & Labels                   #########
#################################################################
plot UpTrend_4h = if RSI_4h >= 50 and SlowK_4h >= 50 and Value_4h > Avg_4h then 1 else 0;
plot DownTrend_4h = if RSI_4h < 50 and SlowK_4h < 50 and Value_4h < Avg_4h then 1 else 0;
UpTrend_4h.Hide();
DownTrend_4h.Hide();
AddLabel(if (one_min_bool == 1 or two_min_bool == 1 or five_min_bool == 1 or fifteen_min_bool == 1 or thirty_min_bool == 1 or one_hour_bool == 1 or two_hour_bool == 1 or four_hour_bool == 1 or ltDay) then yes else no, if UpTrend_4h == 1 then "4h:L" else if DownTrend_4h == 1 then "4h:S" else "4h:I", if UpTrend_4h == 1 then Color.GREEN else if DownTrend_4h == 1 then Color.RED else Color.GRAY);
################################################################
##########                 RSI                         #########
################################################################
# 1 DAY
def NetChgAvg1d;
def TotChgAvg1d;
def ChgRatio1d;
def RSI_1d;
if GetAggregationPeriod() <= AggregationPeriod.DAY {
    NetChgAvg1d = MovingAverage(averageTypeRSI, close(period = AggregationPeriod.DAY) - close(period = AggregationPeriod.DAY)[1], lengthRSI);
    TotChgAvg1d = MovingAverage(averageTypeRSI, AbsValue(close(period = AggregationPeriod.DAY) - close(period = AggregationPeriod.DAY)[1]), lengthRSI);
    ChgRatio1d = if TotChgAvg1d != 0 then NetChgAvg1d / TotChgAvg1d else 0;
    RSI_1d = 50 * (ChgRatio1d + 1);
} else {
    NetChgAvg1d = 0;
    TotChgAvg1d = 0;
    ChgRatio1d = 0;
    RSI_1d = 0;
}
################################################################
##########                 Stochastic Slow             #########
################################################################
# Stochastic 1 DAY
def SlowK_1d;
def SlowD_1d;
if GetAggregationPeriod() <= AggregationPeriod.DAY {
    SlowK_1d = reference StochasticFull(over_boughtSt, over_soldSt, KPeriod, DPeriod, high(period = AggregationPeriod.DAY), low(period =
        AggregationPeriod.DAY), close(period = AggregationPeriod.DAY), 3, averageTypeStoch).FullK;
    SlowD_1d = reference StochasticFull(over_boughtSt, over_soldSt, KPeriod, DPeriod, high(period = AggregationPeriod.DAY), low(period =
        AggregationPeriod.DAY), close(period = AggregationPeriod.DAY), 3, averageTypeStoch).FullD;
} else {
    SlowK_1d = 0;
    SlowD_1d = 0;
}
################################################################
##########                 MACD                      ###########
################################################################
#MACD Calculation
# 1 DAY
def Value_1d;
def Avg_1d;
def Diff_1d;
if GetAggregationPeriod() <= AggregationPeriod.DAY {
    Value_1d = MovingAverage(averageTypeMACD, close(period = AggregationPeriod.DAY), fastLength) - MovingAverage(averageTypeMACD, close(period =
        AggregationPeriod.DAY), slowLength);
    Avg_1d = MovingAverage(averageTypeMACD, Value_1d, MACDLength);
    Diff_1d = Value_1d - Avg_1d;
} else {
    Value_1d = 0;
    Avg_1d = 0;
    Diff_1d = 0;
}
#################################################################
##########        1d  Trend  & Labels                   #########
#################################################################
plot UpTrend_1d = if RSI_1d >= 50 and SlowK_1d >= 50 and Value_1d > Avg_1d then 1 else 0;
plot DownTrend_1d = if RSI_1d < 50 and SlowK_1d < 50 and Value_1d < Avg_1d then 1 else 0;
UpTrend_1d.Hide();
DownTrend_1d.Hide();
AddLabel(if (one_min_bool == 1 or two_min_bool == 1 or five_min_bool == 1 or fifteen_min_bool == 1 or thirty_min_bool == 1 or one_hour_bool == 1 or two_hour_bool == 1 or four_hour_bool == 1 or one_day_bool == 1 or ltDay) then yes else no, if UpTrend_1d == 1 then "1d:L" else if DownTrend_1d == 1 then "1d:S" else "1d:I", if UpTrend_1d == 1 then Color.GREEN else if DownTrend_1d == 1 then Color.RED else Color.GRAY);