#// Indicator for TOS
#// © AlgoAlpha
#indicator("Zero Lag Trend Signals (MTF) [AlgoAlpha]", shorttitle="AlgoAlpha - 0️⃣Zero Lag Signals", overlay=true)
#Hint length: "The Look-Back window for the Zero-Lag EMA calculations"
#Hint BandMultiplier: "This value controls the thickness of the bands, a larger value makes the indicato less noisy"
# Converted by Sam4Cok@Samer800 - 11/2024
input showDashboard = no;
input bands = yes;
input movAvgType = AverageType.EXPONENTIAL;
input length = 70; # "Length",
input BandMultiplier = 1.0; # 1.2, "Band Multiplier",
input BandMultiplier2 = 2.0;
input BandMultiplier3 = 3.0;
input timeframe1 = AggregationPeriod.FIVE_MIN; # "Time frame 1"
input timeframe2 = AggregationPeriod.FIFTEEN_MIN; # "Time frame 2"
input timeframe3 = AggregationPeriod.HOUR; # "Time frame 3"
input timeframe4 = AggregationPeriod.FOUR_HOURS; # "Time frame 4"
input timeframe5 = AggregationPeriod.DAY; # "Time frame 5"
def na = Double.NaN;
def last = IsNaN(close);
def oc2 = (open + close) / 2;
def current = GetAggregationPeriod();
def t1 = Max(current, timeframe1);
def t2 = Max(current, timeframe2);
def t3 = Max(current, timeframe3);
def t4 = Max(current, timeframe4);
def t5 = Max(current, timeframe5);
def atr = ATR(Length = length);
def lag = Floor((length - 1) / 2);
def diff = (2 * close) - close[lag];
def zlema = MovingAverage(movAvgType, diff, length);
def volatility = Highest(atr, length * 3) * BandMultiplier;
def bandUp = zlema + volatility;
def bandDn = zlema - volatility;
def crossover = (close > bandUp) and (close[1] <= bandUp[1]);
def crossunder = (close < bandDn) and (close[1] >= bandDn[1]);
def trend = if crossover then 1 else
if crossunder then -1 else trend[1];
def trendCrossUp = (close > zlema) and (close[1] <= zlema) and trend == 1 and trend[1] == 1 ;
def trendCrossDn = (close < zlema) and (close[1] >= zlema) and trend == -1 and trend[1] == -1;
def crossUp = (trend > 0) and (trend[1] <= 0);
def crossDn = (trend < 0) and (trend[1] >= 0);
def volatility2 = Highest(atr, length * 3) * BandMultiplier2;
def volatility3 = Highest(atr, length * 3) * BandMultiplier3;
#-- PLots
plot ptUp = if crossUp then bandDn else na;
plot ptDn = if crossDn then bandUp else na;
plot BearishEntry = if trendCrossDn then high else na;
plot BullishEntry = if trendCrossUp then low else na;
plot basis = if zlema then zlema else na; #, title="Zero Lag Basis"
plot upper = if (trend < 0) then bandUp else na; # "Upper Deviation Band"
plot lower = if (trend > 0) then bandDn else na; # "Lower Deviation Band"
#Band1
plot u = if bands then bandUp else double.nan;
plot d = if bands then bandDn else double.nan;
#Band2
plot bandUp2 = if bands then zlema + volatility2 else double.nan;
plot bandDn2 = if bands then zlema - volatility2 else double.nan;
#Band3
plot bandUp3 = if bands then zlema + volatility3 else double.nan;
plot bandDn3 = if bands then zlema - volatility3 else double.nan;
ptUp.SetDefaultColor(Color.CYAN);
ptDn.SetDefaultColor(Color.MAGENTA);
ptUp.SetStyle(Curve.POINTS);
ptDn.SetStyle(Curve.POINTS);
BearishEntry.SetDefaultColor(Color.MAGENTA);
BullishEntry.SetDefaultColor(Color.CYAN);
BearishEntry.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
BullishEntry.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
basis.SetLineWeight(3);
basis.AssignValueColor(if trend > 0 then Color.GREEN else Color.RED);
upper.SetDefaultColor(Color.MAGENTA);
lower.SetDefaultColor(Color.CYAN);
upper.SetLineWeight(2);
lower.SetLineWeight(2);
u.setDefaultColor(color.light_gray);
d.setdefaultColor(color.light_gray);
bandUp2.setDefaultColor(color.dark_orange);
banddn2.setdefaultColor(color.light_green);
bandup3.SetDefaultColor(Color.red);
banddn3.SetDefaultColor(Color.green);
#-- Label
#AddChartBubble(crossUp, bandDn, "UP", Color.GREEN, no);
#AddChartBubble(crossDn, bandUp, "DN", Color.RED);
#-- Cloud
AddCloud(Min(basis, oc2), lower, Color.DARK_GREEN, Color.DARK_GREEN);
AddCloud(upper, Max(basis, oc2), Color.DARK_RED, Color.DARK_RED);
#--
script trend {
input movAvgType = AverageType.EXPONENTIAL;
input tf = 300000;
input length = 70;
input mult = 1.2;
def closeTf = close(Period = tf);
def tr = TrueRange(high(Period = tf), closeTf, low(Period = tf));
def atr = WildersAverage(tr, length);
def lag = Floor((length - 1) / 2);
def diff = 2 * closeTf - close(Period = tf)[lag];
def zlema = MovingAverage(movAvgType, diff, length);
def volatility = Highest(atr, length * 3) * mult;
def bandUp = zlema + volatility;
def bandDn = zlema - volatility;
def crossover = (closeTf > bandUp) and (close(Period = tf)[1] <= bandUp[1]);
def crossunder = (closeTf < bandDn) and (close(Period = tf)[1] >= bandDn[1]);
def trend = if crossover then 1 else
if crossunder then -1 else trend[1];
plot out = trend;
}
def s1 = trend(movAvgType, t1, length, BandMultiplier);
def s2 = trend(movAvgType, t2, length, BandMultiplier);
def s3 = trend(movAvgType, t3, length, BandMultiplier);
def s4 = trend(movAvgType, t4, length, BandMultiplier);
def s5 = trend(movAvgType, t5, length, BandMultiplier);
def s1a = if last[-1] then s1 else s1a[1];
def s2a = if last[-1] then s2 else s2a[1];
def s3a = if last[-1] then s3 else s3a[1];
def s4a = if last[-1] then s4 else s4a[1];
def s5a = if last[-1] then s5 else s5a[1];
AddLabel(showDashboard, "(" + t1 / 60000 + " Min, " + (if s1a > 0 then "Bullish" else "Bearish") + ")",
if s1a > 0 then Color.GREEN else Color.RED);
AddLabel(showDashboard, "(" + t2 / 60000 + " Min, " + (if s2a > 0 then "Bullish" else "Bearish") + ")",
if s2a > 0 then Color.GREEN else Color.RED);
AddLabel(showDashboard, "(" + t3 / 60000 + " Min, " + (if s3a > 0 then "Bullish" else "Bearish") + ")",
if s3a > 0 then Color.GREEN else Color.RED);
AddLabel(showDashboard, "(" + t4 / 60000 + " Min, " + (if s4a > 0 then "Bullish" else "Bearish") + ")",
if s4a > 0 then Color.GREEN else Color.RED);
AddLabel(showDashboard, "(" + t5 / 60000 + " Min, " + (if s5a > 0 then "Bullish" else "Bearish") + ")",
if s5a > 0 then Color.GREEN else Color.RED);
#-- END of CODE