#// Inspired by nextSignals Two MA strategy (@nextSignals).
#// Original code was in ThinkScript, so I spent some time converting this to TV
#indicator("Two MAs", shorttitle="DocMAs", overlay=true)
# Converted and modifiedTrueRange by Sam4Cok@Samer800 - 11/2023 - Request from UseThinkScript.com memeber
input timeframe = {Default "Chart", "Custom"};
input cusotmTimeframe = AggregationPeriod.FIFTEEN_MIN;
input movAvgType = AverageType.EXPONENTIAL;
input movAvgSrc = FundamentalType.CLOSE;
input movAvgLength = 20;#, "Slow EMA Length")
input lookbackPeriod = 5;
#// Slow Reference
def custom = timeframe==timeframe."Custom";
def tfS = Fundamental(FundamentalType = movAvgSrc);
def tfol = ohlc4;
def mtfS = Fundamental(FundamentalType = movAvgSrc, Period = cusotmTimeframe);
def mtfol = Fundamental(FundamentalType = FundamentalType.OHLC4, Period = cusotmTimeframe);;
def src = if custom then mtfS else tfS;
def ohlc = if custom then mtfol else tfol;
def EMA = MovingAverage(movAvgType, src, movAvgLength);
plot emaLine = EMA;#, color=slow_ma_color, linewidth = 2)
emaLine.SetDefaultColor(Color.ORANGE);
#// Instantaneous MA (exhibits permanence)
def P = ohlc;
def ON = highest(P, lookbackPeriod);
def O1 = lowest(ON, lookbackPeriod);
def O1Loc = fold j1 = 0 to lookbackPeriod with s do
if P[j1] == O1 and s == 0 then j1 else s;
def O2Loc = fold j2 = 0 to lookbackPeriod with q do
if P[j2] == ON and q == 0 and j2 != O1Loc then j2 else q;
def O3Loc = fold i3 = 0 to lookbackPeriod with r = ON do
if i3 == O1Loc and i3 == O2Loc then min(r, P[i3]) else r;
def iMA = O3Loc;
def iMA_up = iMA > EMA;
plot iMaLine = iMA;#, color=colorcode, linewidth = 2)
iMaLine.AssignValueColor(if iMA_up then Color.CYAN else Color.MAGENTA);
iMaLine.SetLineWeight(2);
#-- END of CODE