#// Indicator for TOS
#// © QuantEdgeB
#indicator("Hyper MA Loop | QuantEdgeB", overlay = false)
# Converted by Sam4Cok@Samer800 - 02/2025
input colorBars = yes;
input HyperMaSource = close; # "HyperMA Source"
input HyperMaLength = 2; # "Hyper MA Length"
input strat_loop = 1; # 'Start'
input end_loop = 60; # 'End'
input ThresholdForLong = 40; # 'Threshold for long'
input ThresholdForShort = 8; # 'Threshold for short'
def na = Double.NaN;
def last = isNaN(close);
#// Hyper MA Calculation
Script f_HyMA {
input src = close;
input len = 2;
def sum_hyp = fold i = 0 to len with p do
p + src[i] * (1 / (len - i));
def sum_weights = fold j = 0 to len with q do
q + (1 / (len - j));
def f_HyMA = if sum_weights > 0 then sum_hyp / sum_weights else Double.NaN;
plot out = f_HyMA;
}
#// Loop Function
Script loop_f {
input a = 1;
input b = 60;
input ma = close;
def sum2 = fold i = a to b + 1 with p do
p + (if ma > ma[i] then 1 else -1);
plot out = sum2;
}
def HyMa = f_HyMA(HyperMaSource, HyperMaLength);
def hyma_loop = loop_f(strat_loop , end_loop, HyMa);
#// Final Signal
def Long_C = hyma_loop > ThresholdForLong;
def Short_C = hyma_loop < ThresholdForShort;
def QB = if Long_C and !Short_C then 1 else
if Short_C then -1 else QB[1];
#// Color
def col = if QB > 0 then 1 else if QB < 0 then -1 else 0;
#// Extra Plots
def plotline1 = ExpAverage(close,3);
def plotline2 = ExpAverage(plotline1, 16*2);
def plotline3 = ExpAverage(plotline1, 16*3);
def plotline4 = ExpAverage(plotline1, 14);
def band_width = AbsValue(plotline2 - plotline4) / close;
#// Plots
def upCol = (col > 0 or col[-1] > 0);
def dnCol = (col < 0 or col[-1] < 0);
plot Line4 = if !last then plotline4 else na;
Line4.AssignValueColor(if col>0 then Color.CYAN else if col<0 then Color.MAGENTA else Color.GRAY);
AddCloud(if upCol then plotline4 else na, plotline3, Color.CYAN, Color.CYAN);
AddCloud(if upCol then plotline2 else na, plotline3, Color.CYAN, Color.CYAN);
AddCloud(if dnCol then plotline4 else na, plotline3, Color.PLUM, Color.PLUM);
AddCloud(if dnCol then plotline2 else na, plotline3, Color.PLUM, Color.PLUM);
AddCloud(if col > 0 and last[-2] then plotline2 else na, plotline3, Color.CYAN, Color.CYAN);
AddCloud(if col < 0 and last[-2] then plotline4 else na, plotline3, Color.PLUM, Color.PLUM);
AddCloud(if col < 0 and last[-2] then plotline2 else na, plotline3, Color.PLUM, Color.PLUM);
#-- Bar Color
AssignPriceColor(if !colorBars then Color.CURRENT else
if col>0 then Color.CYAN else if col<0 then Color.MAGENTA else Color.GRAY);
#-- END of CODE