Supertrend + Details:
If the confirmation closing bars value is greater than zero (default of two bars), the trend will only reverse if the price closes outside the boundary for that number of bars (does not need to be consecutive) or if the opposite side of the bar exceeds the boundary.
The confirmation bar count will reset if the trend has resumed (opposite boundary is broken).
Confirmation examples:
- In a down-trend, but the previous two bars close above the boundary.
- In a down-trend, but the low of the previous bar exceeds the boundary.
- In a down-trend, but the high of the previous bar exceeds the boundary when confirmation bars are set to zero
CODE:
CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © Electrified (electrifiedtrading)
#indicator("SuperTrend+", by Electrified
##study("Pivot Point SuperTrend", by LonesomeTheBlue)
# Converted and mod by Sam4Cok@Samer800
input maType = { "VAWMA", "eVWMA",default "SMMA", "EMA", "TEMA", "WMA", "VWMA", "SMA", "HMA", "McGinley"};
input atrPeriod = 21; # Period use in calculating the ATR value.
input atrFactor = 2.8; # The multiplier used when defining the super trend limits
input CalcType = {Default PivotPoint, HighLow}; # "Pivot Point Period"
input PivotPeriod = 2; # "Pivot Point Period"
input CenterLine = no; # "Show PP Center Line"
input maxDeviation = 0; # The max deviation of true range before being considered and outlier
input closeBars = 2; # Closed bars that have to exceed the ST value before the trend reversal is confirmed
input Wicks = yes;
input showsignals = no; # Show Buy/Sell Bubble
input ShowCloud = yes; # Cloud On/Off
def na = Double.NaN;
def bar = BarNumber();
def h = if Wicks then high else hlc3;
def l = if Wicks then low else hlc3;
def c = close;
DefineGlobalColor("green", CreateColor(76, 175, 80));
DefineGlobalColor("red", CreateColor(255, 82, 82));
#----- Scripts----
script nz {
input data = close;
input repl = 0;
def ret_val = if IsNaN(data) then repl else data;
plot return = ret_val;
}
#naOutliers( src, len, maxDeviation = 4)=>
script naOutliers {
input src = close;
input len = 120;
input maxDeviation = 0;
def prev = src[1];# // ignore current in measurment as it could throw off result.
def avg = SimpleMovingAvg(prev, len);
def dev = StDev(prev, len) * maxDeviation;
def upper = avg + dev;
def lower = avg - dev;
def newUpper;
def newLower;
newUpper = if IsNaN(newUpper[1]) then upper else
if prev >= newUpper[1]
then upper else if(isNaN(newUpper[1]), upper,newUpper[1]);
newLower = if isNaN(newLower[1]) then lower else
if prev <= newLower[1]
then lower else if(isNaN(newLower[1]),lower,newLower[1]);
def newSrcUp = newUpper;
def newStcDn = newLower;
def naOutliers = if src > upper then newSrcUp else
if src < lower then newStcDn else src;
plot return = naOutliers;
}
#------------scripts-----------
# vawma(src, len) =>
script vawma {
input src = hlc3;
input len = 50;
def sum; def vol; def v;
def s = fold i = 0 to len with p do
src[i];
def volm = if IsNaN(volm[1]) then 0 else
fold j = 0 to len with u do
volume[j];
def ma = if !IsNaN(s) and !IsNaN(volm) then
fold k = 0 to len with w do
len - k else 0;
v = fold l=1 to len with q do
if IsNaN(v[1]) then volm else
volume * ma;
vol = fold m = 0 to len with r do
r + v[m];
sum = fold n = 0 to len with t do
t + src[n] * v[n];
plot vawma = sum/vol;
}
#vwma(source, length)
script VWMA {
input x = close;
input y = 15;
def VWMA = SimpleMovingAvg(x * volume, y) / SimpleMovingAvg(volume, y);
plot result = VWMA;
}
#==========================
# PIVOT SCRIPT
#==========================
script FindPivots {
input dat = high; # default data or study being evaluated
input HL = 0; # default high or low pivot designation, -1 low, +1 high
input PF = 14; # default pivot forward period
input PB = 14; # default pivot backward period
##############
def _nan; # used for non-number returns
def _BN; # the current barnumber
def _VStop; # confirms that the lookforward period continues the pivot trend
def _V; # the Value at the actual pivot point
def _VBar; # the bar number at the pivot point
def _PV; # the previous pivot Value
def _PVBar; # the previous pivot bar number
def _VDiff; # the difference in values between last two pivot points
def _VDist; # the diffence in barnumbers between last two pivot points
def _VSlope; # the Slope calculated using value and distance changes
def _VPivot; # used for the pivot point connector line only
##############
_BN = BarNumber();
_nan = Double.NaN;
_VStop =
fold a = 1 to PF + 1
with b = 1 while b
do if HL > 0 then
dat > GetValue(dat, -a) else
dat < GetValue(dat, -a) ;
if (HL > 0) {
_V = if _BN > PB and dat == Highest(dat, PB) and _VStop
then dat else _nan;
} else {
_V = if _BN > PB and dat == Lowest(dat, PB) and _VStop
then dat else _nan;
}
_VBar = if !IsNaN(_V) then _BN else _VBar[1];
_PV = if !IsNaN(_V) then GetValue(dat, _BN - _VBar[1]) else _PV[1];
_PVBar = if _VBar != _VBar[1]
then _PVBar[1] else _VBar;
_VDiff = AbsValue(_V) - AbsValue(_PV);
_VDist = _BN - _PVBar;
_VSlope = if _V > _PV then 1 else
if _V < _PV then -1 else 0;
if (HL > 0) {
_VPivot = _BN >= HighestAll(_PVBar);
} else {
_VPivot = _BN >= LowestAll(_PVBar);
}
plot result = if !IsNaN(_V) and _VStop then _V else _nan; #return the final _dat value at the most
}
#ma(mode, len, src) =>
script ma {
input type = "EMA";
input src = close;
input len = 100;
def volumeSum = Sum(volume, len);
def evwma = ((volumeSum - volume) * nz(evwma[1]) + volume * src) / volumeSum;
def e = ExpAverage(src, len);
def Mcg = if IsNaN(Mcg[1]) then Average(src, len) else
CompoundValue(1, Mcg[1] + ((src - Mcg[1]) / (0.6 * len * Power(src / Mcg[1], 4))), src);
def ma;
ma = if type == "SMA" then SimpleMovingAvg(src, len) else
if type == "VAWMA" then vawma(src, len) else
if type == "EMA" then ExpAverage(src, len) else
if type == "TEMA" then 3 * (e - ExpAverage(e, len)) + ExpAverage(ExpAverage(e, len), len) else
if type == "WMA" then WMA(src, len) else
if type == "VWMA" then vwma(src, len) else
if type == "SMMA" then WildersSmoothing(src, len) else
if type == "HMA" then WMA(2 * WMA(src, len / 2) - WMA(src, len), Round(Sqrt(len), 0)) else
if type == "eVWMA" then evwma else
if type == "McGinley" then Mcg else Double.NaN;
plot result = ma;
}
def ph = if bar > PivotPeriod then findpivots(h, 1, PivotPeriod, PivotPeriod) else highest(h,PivotPeriod);
def pl = if bar > PivotPeriod then findpivots(l, -1, PivotPeriod, PivotPeriod) else lowest(l,PivotPeriod);
def lastpp = if !isNaN(ph) then ph else if !isNaN(pl) then pl else na;
#// calculate the Center line using pivot points
def center;
if (lastpp) {
if isNaN(center[1]) {
center = lastpp;
} else {
center = (center[1]*2 + lastpp) / 3;}
} else {
center = center[1];
}
plot MidLine = if isNaN(close) then na else center[PivotPeriod];
midLine.SetHiding(!CenterLine);
midLine.AssignValueColor(if center < hlc3 then CreateColor(33,150,243) else Color.RED);
midLine.SetPaintingStrategy(PaintingStrategy.LINE);
#centerLine.EnableApproximation();
def vLow = if CalcType == CalcType.PivotPoint then center else l;
def vHigh = if CalcType == CalcType.PivotPoint then center else h;
#------------MA Line-----------
def tr = if IsNaN(c[1]) then h - l else TrueRange(h, c, l);
def trCleaned = if maxDeviation == 0 then tr else naOutliers(tr, atrPeriod, maxDeviation);
def atATR = ma(maType , trCleaned, atrPeriod);
def getATR = atATR;
def up = vLow - (atrFactor * getATR);
def dn = vHigh + (atrFactor * getATR);
def up1 = if (up > up1[1]) or (h[1] < up1[1]) then if l[1] > up1[1] then Max(up1[1], up) else up else up1[1];
def dn1 = if (dn < dn1[1]) or (l[1] > dn1[1]) then if h[1] < dn1[1] then Min(dn1[1], dn) else dn else dn1[1];
#def up1 = if (up > up1[1]) or (C[1] < up1[1]) then if C[1] > up1[1] then Max(up1[1], up) else up else up1[1];
#def dn1 = if (dn < dn1[1]) or (C[1] > dn1[1]) then if C[1] < dn1[1] then Min(dn1[1], dn) else dn else dn1[1];
#def up1 = if low[1] >up1[1] then max(up1[1], up) else up ;
#def dn1 = if high[1]<dn1[1] then min(dn1[1], dn) else dn;
def lastU = up1[1];
def lastD = dn1[1];
def lastUp;
def lastDn;
def state = {default init, long, short};
def trend;
def confirm;
def unconfirm;
switch (state[1]) {
case init:
state = state.short;
trend = 1;
confirm = confirm[1];
unconfirm = unconfirm[1];
case long:
if (trend[1] != 1 and c > lastDn[1])
then {
state = state.short;
trend = 1;
confirm = confirm[1];
unconfirm = unconfirm[1];
} else {
state = state.long;
trend = trend[1];
confirm = If((trend != +1 and h > lastDn) and (confirm[1] < closeBars and c[1] > lastDn)
or (trend != -1 and l < lastUp) and (confirm[1] < closeBars and c[1] < lastUp), confirm[1] + 1,
If(trend[1] != trend or lastUp[1] != lastUp or lastDn[1] != lastDn, 0, confirm[1]));
unconfirm = If((trend != +1 and h > lastD) or (trend != -1 and l < lastU), unconfirm[1] + 1,
If(trend[1] != trend or lastUp[1] != lastUp or lastDn[1] != lastDn, 0, unconfirm[1]));
}
case short:
if (trend[1] != -1 and c < lastUp[1])
then {
state = state.long;
trend = -1;
confirm = confirm[1];
unconfirm = unconfirm[1];
} else {
state = state.short;
if (trend[1] != +1 and c > lastDn[1])
then {
unconfirm = If((trend[1] != trend), 0, unconfirm[1] + 1);
confirm = If(confirm[1] < closeBars and c[1] > lastUp[1], confirm[1] + 1, confirm[1]);
trend = If(confirm[1] >= closeBars, +1, trend[1]);
} else {
if (trend[1] != -1 and l < lastUp[1])
then {
unconfirm = If(trend[1] != trend , 0, unconfirm[1] + 1);
confirm = If(confirm[1] < closeBars and c[1] < lastUp[1], confirm[1] + 1, confirm[1]);
trend = If(confirm[1] >= closeBars, -1, trend[1]);
} else {
trend = trend[1];
confirm = If(trend[1] != trend or lastUp[1] != lastUp or lastDn[1] != lastDn, 0, confirm[1]);
unconfirm = If(trend[1] != trend or lastUp[1] != lastUp or lastDn[1] != lastDn, 0, unconfirm[1]);
}
}
}
}
if (trend[1] != trend)
then {
lastUp = lastU;
lastDn = lastD;
} else {
lastUp = if trend == +1 then Max(lastUp[1], up1) else up1;
lastDn = if trend == -1 then Min(lastDn[1], dn1) else dn1;
}
#----- Calculations -------
def upTrend = if trend == 1 then lastUp else na;
def buySignal = trend == 1 and trend[1] == -1;
def upPoint = if buySignal then lastUp else na;
def dnTrend = if trend == 1 then na else lastDn;
def sellSignal = trend == -1 and trend[1] == 1;
def dnPoint = if sellSignal then lastDn else na;
#------ plot ----------------
plot upPlot = upTrend; # "Up Trend"
upPlot.AssignValueColor(if unconfirm == 0 then GlobalColor("green") else Color.YELLOW);
plot pointUp = upPoint; # "UpTrend Begins"
pointUp.SetDefaultColor(GlobalColor("green"));
pointUp.SetStyle(Curve.POINTS);
pointUp.SetLineWeight(3);
plot dnPlot = dnTrend; # "Down Trend"
dnPlot.AssignValueColor(if unconfirm == 0 then GlobalColor("red") else Color.YELLOW);
plot pointDn = dnPoint; # "DownTrend Begins"
pointDn.SetDefaultColor(GlobalColor("red"));
pointDn.SetStyle(Curve.POINTS);
pointDn.SetLineWeight(3);
#------- Cloud -------
AddCloud(ohlc4, if ShowCloud then upPlot else na, Color.DARK_GREEN, Color.WHITE);
AddCloud(if ShowCloud then dnPlot else na, ohlc4, Color.DARK_RED, Color.WHITE);
#------- Bubble ------
AddChartBubble(buySignal and showsignals, lastUp, "Buy", Color.GREEN, no);
AddChartBubble(sellSignal and showsignals, lastDn, "Sell", Color.RED, yes);
### END