#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © ramki_simple
#// Thanks to LonesomeTheBlue for the original code
#strategy("Multi Supertrend with no-repaint HTF option strategy", overlay = true, shorttitle='Multi Supertrend')
# Converted by Sam4Cok@Samer800 - 02/2023 - request from UseThinkScript.com memeber
#//auto higher time frame
input ShowSuperTrend1 = yes;
input ShowSuperTrend2 = yes;
input ShowSignals = yes;
input AutoTimeFrame = no; # 'Select HTF Automatically for Additional Supertrend'
input ManualHigherTimeFrame = no; # 'Select Higher Timeframe for Additional Supertrend'
input ManualTimeFrame = AggregationPeriod.FIFTEEN_MIN;
input Mult1 = 3.0; # 'Multiplier for Default Supertrend'
input Period1 = 10; # 'Period for Default Supertrend'
input Mult2 = 2.0; # 'Multiplier for Additional Supertrend'
input Period2 = 14; # 'Period for Additional Supertrend'
input chbarcol = yes; # "Change Bar Color"
input UseVwapFilter = no;
input ShowVwap = no;
def na = Double.NaN;
def last = isNaN(close);
#---Colors
DefineGlobalColor("teal" , CreateColor(0, 137, 123));
DefineGlobalColor("Red" , CreateColor(255, 82, 82));
DefineGlobalColor("maroon" , CreateColor(136, 14, 79));
DefineGlobalColor("blue" , CreateColor(33, 150, 243));
DefineGlobalColor("lime" , CreateColor(0,230,118));
def CurrentAgg = GetAggregationPeriod();
def HTFAuto = if CurrentAgg < AggregationPeriod.FIVE_MIN then AggregationPeriod.FIVE_MIN else
if CurrentAgg < AggregationPeriod.FIFTEEN_MIN then AggregationPeriod.FIFTEEN_MIN else
if CurrentAgg < AggregationPeriod.HOUR then AggregationPeriod.HOUR else
if CurrentAgg < AggregationPeriod.FOUR_HOURS then AggregationPeriod.FOUR_HOURS else
if CurrentAgg < AggregationPeriod.DAY then AggregationPeriod.DAY else
if CurrentAgg < AggregationPeriod.WEEK then AggregationPeriod.WEEK else
if CurrentAgg < AggregationPeriod.MONTH then AggregationPeriod.MONTH else AggregationPeriod.YEAR;
def HTF = if AutoTimeFrame then HTFAuto else if ManualHigherTimeFrame then ManualTimeFrame else CurrentAgg;
script supertrend {
input src = hl2;
input factor = 3;
input atrPeriod = 10;
input htf = 0;
def hl2HTF = hl2(Period = htf);
def highHTF = high(Period = htf);
def lowHTF = low(Period = htf);
def closeHTF = close(Period = htf);
def tr = TrueRange(highHTF, closeHTF, lowHTF);
def atr = WildersAverage(tr, atrPeriod);
def lowerBand;
def upperBand;
def upperBand1 = src + factor * atr;
def lowerBand1 = src - factor * atr;
def prevLowerBand = (lowerBand[1]);
def prevUpperBand = (upperBand[1]);
lowerBand = if lowerBand1 > prevLowerBand or closeHTF[1] < prevLowerBand then lowerBand1 else prevLowerBand;
upperBand = if upperBand1 < prevUpperBand or closeHTF[1] > prevUpperBand then upperBand1 else prevUpperBand;
def direction;# = na
def superTrend;# = na
def prevSuperTrend = superTrend[1];
if IsNaN(atr[1]) {
direction = 1;
} else if prevSuperTrend == prevUpperBand {
direction = if closeHTF > upperBand then -1 else 1;
} else {
direction = if closeHTF < lowerBand then 1 else -1;
}
superTrend = if direction == -1 then lowerBand else upperBand;
plot ST = superTrend;
plot dir = direction;
}
def hlSrc = hl2(Period = htf);
def Trailings = supertrend(hl2, Mult1, Period1, CurrentAgg).ST;
def Trend = supertrend(hl2, Mult1, Period1, CurrentAgg).DIR;
def TrailingHtf = supertrend(hl2, Mult2, Period2, CurrentAgg).ST;
def TrendHt = supertrend(hl2, Mult2, Period2, CurrentAgg).DIR;
def Trail_ = supertrend(hlSrc, Mult2, Period2, HTF).ST;
def Trend_ = supertrend(hlSrc, Mult2, Period2, HTF).DIR;
def CompositeTrailHtf = Trail_ * Trend_;
plot TrailingsUp = if !last and ShowSuperTrend1 and Trend > 0 then Trailings else na;
TrailingsUp.SetDefaultColor(GlobalColor("Red"));
TrailingsUp.SetLineWeight(2);
plot TrailingsDn = if last or !ShowSuperTrend1 or Trend > 0 then na else Trailings;
TrailingsDn.SetDefaultColor(GlobalColor("teal"));
TrailingsDn.SetLineWeight(2);
def TrailingslHtf;def TrendHtf;
if HTF != CurrentAgg {
TrailingslHtf = AbsValue(CompositeTrailHtf[1]);
TrendHtf = if CompositeTrailHtf[1] > 0 then 1 else -1;
} else {
TrailingslHtf = TrailingHtf;
TrendHtf = TrendHt;
}
plot TrailingshtfUp = if !last and ShowSuperTrend2 and TrendHtf > 0 then TrailingslHtf else na;
TrailingshtfUp.SetDefaultColor(GlobalColor("maroon"));
TrailingshtfUp.SetLineWeight(2);
plot TrailingshtfDn = if last or !ShowSuperTrend1 or TrendHtf > 0 then na else TrailingslHtf;
TrailingshtfDn.SetDefaultColor(GlobalColor("blue"));
TrailingshtfDn.SetLineWeight(2);
#----- BAR Color
def periodIndx = getYyyyMmDd();
def isPeriodRolled = compoundValue(1, periodIndx != periodIndx[1], yes);
def volumeSum;
def volumeVwapSum;
if (isPeriodRolled) {
volumeSum = volume;
volumeVwapSum = volume * hlc3;
} else {
volumeSum = compoundValue(1, volumeSum[1] + volume, volume);
volumeVwapSum = compoundValue(1, volumeVwapSum[1] + volume * hlc3, volume * hlc3);
}
plot vwap = volumeVwapSum / volumeSum;
VWAP.setDefaultColor(getColor(0));
vwap.SetHiding(!ShowVwap);
def barcolor_ = if Trend == -1 and TrendHtf == -1 then 1 else
if Trend == 1 and TrendHtf == 1 then -1 else 0;
AssignPriceColor(if !chbarcol then Color.CURRENT else
if barcolor_>0 then GlobalColor("lime") else
if barcolor_<0 then GlobalColor("red") else Color.GRAY);
#--- Signals
AddChartBubble(ShowSignals and barcolor_>0 and barcolor_[1]!=1 and (UseVwapFilter and close>vwap or !UseVwapFilter), low, "BUY", Color.GREEN, no);
AddChartBubble(ShowSignals and barcolor_<0 and barcolor_[1]!=-1 and (UseVwapFilter and close<vwap or !UseVwapFilter), high, "SELL", Color.RED, yes);
# --- END CODE