# rsi_avg_mtf_line_color_00b
#https://usethinkscript.com/threads/moving-average-crossover-rsi-mtf.14631/
#Questions UnAnswered Graveyard
#Unanswered Moving Average Crossover RSI MTF?
#rwfarrell Feb 25, 2023
#Based on the moving-average-crossover-rsi from BenTen
#https://usethinkscript.com/threads/...-rsi-indicator-for-thinkorswim.185/#post-1070
# Is there a way to
# make this a MTF indicator
# where the prsiMA has the option to change the color,
# example. Once the prsiMA crosses the RSI downtrending the prsiMA turns red
# and vice versa uptrending turns green,
# but the entire MA changes color?
#I have already added the aggregation period to make this a MTF but the "prsiMA.DefineColor( "def", Color.current ); " skews the full movement.
#---------------------------
# Moving Average Crossover RSI Indicator for ThinkorSwim
#https://usethinkscript.com/threads/moving-average-crossover-rsi-indicator-for-thinkorswim.185/
# TD Ameritrade IP Company, Inc. (c) 2007-2019
# Tweaked by @korygill
# https://usethinkscript.com/d/185-moving-average-crossover-rsi-indicator-for-thinkorswim
declare lower;
def na = double.nan;
def bn = barnumber();
def lastbn = HighestAll(If(IsNaN(close), 0, bn));
def lastbar = if (bn == lastbn) then 1 else 0;
#def lastbar = !isnan(close[0]) and isnan(close[-1]);
input agg = AggregationPeriod.HOUR;
def aggmin = agg/60000;
input length = 14;
input over_Bought = 70;
input over_Sold = 30;
#input price = close;
def price = close(period = agg);
input averageType = AverageType.WILDERS;
input showBreakoutSignals = no;
input rsiMALength = 5; #hint rsiMALength: RSI Moving Average Length
input rsiAverageType = AverageType.SIMPLE;
def NetChgAvg = MovingAverage(averageType, price - price[1], length);
def TotChgAvg = MovingAverage(averageType, AbsValue(price - price[1]), length);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;
plot RSI = 50 * (ChgRatio + 1);
plot OverSold = over_Sold;
plot OverBought = over_Bought;
plot UpSignal = if RSI crosses above OverSold then OverSold else Double.NaN;
plot DownSignal = if RSI crosses below OverBought then OverBought else Double.NaN;
# plot the RSI Moving Average
def rsiMA = MovingAverage(rsiAverageType, RSI, rsiMALength);
UpSignal.SetHiding(!showBreakoutSignals);
DownSignal.SetHiding(!showBreakoutSignals);
RSI.DefineColor("OverBought", GetColor(5));
RSI.DefineColor("Normal", GetColor(7));
RSI.DefineColor("OverSold", GetColor(1));
RSI.AssignValueColor(if RSI > over_Bought then RSI.color("OverBought") else if RSI < over_Sold then RSI.color("OverSold") else RSI.color("Normal"));
OverSold.SetDefaultColor(GetColor(8));
OverBought.SetDefaultColor(GetColor(8));
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
#-------------------------------
input one_color_last_bar = no;
plot prsiMA1 = if one_color_last_bar then na else rsiMA;
prsiMA1.AssignValueColor(if rsiMA < rsi then color.red else color.green);
prsima1.setlineweight(2);
prsima1.hidebubble();
def dir;
def off;
if bn == 1 then {
off = lastbn - 1;
dir = if getvalue(rsiMA, -off) > getvalue(rsi, -off) then 1 else 0;
} else {
off = 0;
dir = dir[1];
}
plot prsiMA2 = if one_color_last_bar then rsiMA else na;
prsiMA2.AssignValueColor(if dir == 1 then color.green else color.red);
prsima2.setlineweight(2);
prsima2.hidebubble();
#