Author Message:
The "MA Sabres" indicator highlights potential trend reversals based on a moving average direction. Detected reversals are accompanied by an extrapolated "Sabre" looking shape that can be used as support/resistance and as a source of breakouts
More details : https://www.tradingview.com/v/viwa6CR8/
CODE:
CSS:
# //https://www.tradingview.com/v/viwa6CR8/
# // This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International
# // © LuxAlgo
#indicator('MA Sabres [LuxAlgo]', shorttitle='LuxAlgo - MA Sabres', max_polylines_count=100, overlay=true)
# Converted by Sam4Cok@Samer800 - 12/2023 - Not exact Conversion
input maTyp = {"SMA", "EMA", "SMMA (RMA)", "HullMA", "WMA", "VWMA", "DEMA", default "TEMA"}; # 'MA Type'
input source = close;
input lineTypes = {"Curved",Default "Straight"};
input Length = 50; # 'Length'
input count = 20; # 'Previous Trend Duration'
input showMovAvg = yes;
input showBand = no;
input atrLength = 14;
input targetMulti = 2.0;
input stoplossMulti = 1.0;
def na = Double.NaN;
def last = isNaN(close);
def hLen = floor(Length / 2);
def curve = lineTypes==lineTypes."Curved";
DefineGlobalColor("colUp" , CreateColor(41,98,255)); # 'Bullish'
DefineGlobalColor("colDn" , CreateColor(242,54,69)); # 'Bearish'
DefineGlobalColor("dcolUp", CreateColor(0,58,220)); # 'Bullish'
DefineGlobalColor("dcolDn", CreateColor(206,13,29)); # 'Bearish'
DefineGlobalColor("colMa" , Color.GRAY); # 'MA'
script rising {
input src = close;
input len = 20;
def rising = fold i = 0 to len with p=yes while p do
src[i] > GetValue(src, i + 1);
plot out = rising;
}
script falling {
input src = close;
input len = 20;
def falling = fold i = 0 to len with p=1 while p do
src[i] < GetValue(src, i + 1);
plot out = falling;
}
#vwma(source, length)
script VWMA {
input src = close;
input len = 14;
input vol = volume;
def nom = Average(src * vol, len);
def den = Average(vol, len);
def VWMA = nom / den;
Plot result = VWMA;
}
#//Method MA
script ma {
input type = "TEMA";
input source = close;
input length = 50;
def ma =
if type == "SMA" then Average(source, length) else
if type == "EMA" then ExpAverage(source, length) else
if type == "SMMA (RMA)" then WildersAverage(source, length) else
if type == "HullMA" then HullMovingAvg(source, length) else
if type == "WMA" then WMA(source, length) else
if type == "VWMA" then vwma(source, length) else
if type == "DEMA" then DEMA(source, length) else
if type == "TEMA" then TEMA(source, length) else TEMA(source, length);
plot out = ma;
}
#/Calculations
def ma = ma(maTyp, source , Length);
def fl = falling(ma , count);
def rs = rising (ma , count);
def up = fl[1] and ma > ma[1];
def dn = rs[1] and ma < ma[1];
def nATR = ATR(Length = atrLength);
plot maLine = if showMovAvg then ma else na;
maLine.SetDefaultColor(GlobalColor("colMa"));
def atrDn; def dnVal; def dnL; def cntDn; def yDn0;def yDn1; def yDn2; def yDn3;
if dn {
atrDn = nATR;
dnVal = high[1];
dnL = dnVal;
cntDn = 1;
yDn0 = dnVal + atrDn / 15;
yDn1 = dnVal - atrDn / 15;
yDn2 = dnVal - atrDn / 2.5;
yDn3 = dnVal - atrDn * targetMulti;
} else {
atrDn = atrDn[1];
dnVal = dnVal[1];
dnL = na;
cntDn = if cntDn[1] > Length then na else cntDn[1] + 1;
yDn0 = yDn0[1];
yDn1 = yDn1[1];
yDn2 = yDn2[1];
yDn3 = yDn3[1];
}
def atrUp; def upVal; def upL; def cntUp; def yUp0;def yUp1; def yUp2; def yUp3;
if up {
atrUp = nATR;
upVal = low[1];
upL = upVal;
cntUp = 1;
yUp0 = upVal - atrUp / 15;
yUp1 = upVal + atrUp / 15;
yUp2 = upVal + atrUp / 2.5;
yUp3 = upVal + atrUp * targetMulti;
} else {
atrUp = atrUp[1];
upVal = upVal[1];
upL = na;
cntUp = if cntUp[1] > Length then na else cntUp[1] + 1;
yUp0 = yUp0[1];
yUp1 = yUp1[1];
yUp2 = yUp2[1];
yUp3 = yUp3[1];
}
#-- Line Slope
def slpDn1 = (yDn2 - yDn1) / hLen;
def slpDn2 = (yDn2 - yDn0) / hLen;
def slpDn3 = (yDn3 - yDn2) / hLen;
def slpUp1 = (yUp2 - yUp1) / hLen;
def slpUp2 = (yUp2 - yUp0) / hLen;
def slpUp3 = (yUp3 - yUp2) / hLen;
# curve & Line
# Bearish
def slpDn = if !isNaN(cntDn) then atrDn / (length * 17.5 / 50) else slpDn[1];
def slopeDn = if cntDn < Length / 10 then -(Length * Sqr(slpDn)) else (cntDn * Sqr(slpDn));
def factorDn = ma(maTyp, slopeDn, Length / 10);
def legDn1 = yDn0 + cntDn * slpDn2;
def legDn0 = yDn2 + ((cntDn- hLen) * slpDn3);
def legDn2 = yDn1 + cntDn * slpDn1;
def curDn11 = if cntDn then
if cntDn < hLen then
if up then yDn0 else legDn1 else
if cntDn == hLen then yDn2 else legDn0 else na;
def curDn22 = if cntDn < hLen then
if up then yDn1 else legDn2 else na;
def curDn1; def curDn2; def curDn3; def curDn4; def linDn1; def linDn2;
if dn {
curDn1 = dnVal + atrDn / 15;
curDn2 = dnVal - atrDn / 15;
curDn3 = dnVal + atrDn * stoplossMulti;
curDn4 = dnVal - atrDn * targetMulti;
linDn1 = dnVal + atrDn * stoplossMulti;
linDn2 = dnVal - atrDn * targetMulti;
} else {
curDn1 = curDn1[1] - factorDn;
curDn2 = curDn2[1] - factorDn;
curDn3 = curDn3[1] - factorDn;
curDn4 = curDn4[1] - factorDn;
linDn1 = linDn1[1];
linDn2 = linDn2[1];
}
def saberLDn1 = curDn11;
def saberLDn2 = curDn22;
def sabresDn1 = curDn1;
def sabresDn2 = curDn2;
def sabresDn3 = curDn3;
def sabresDn4 = curDn4;
def sabresDn5 = linDn1;
def sabresDn6 = linDn2;
def dnCondCurve = curve and !dn[-1] and cntDn;
def dnCondLine = !curve and !dn[-1] and cntDn;
plot PointDn = dnL[-1];
PointDn.SetLineWeight(4);
PointDn.SetPaintingStrategy(PaintingStrategy.POINTS);
PointDn.SetDefaultColor(GlobalColor("colDn"));
plot CurveDn1 = if dnCondCurve then sabresDn1 else na ;
plot CurveDn2 = if dnCondCurve then sabresDn2 else na;
plot CurveDn3 = if showBand and dnCondCurve then sabresDn3 else na;
plot CurveDn4 = if showBand and dnCondCurve then sabresDn4 else na;
plot LineDn1 = if dnCondLine then saberLDn1 else na;
plot LineDn2 = if dnCondLine then saberLDn2 else na;
plot stDn = if showBand and dnCondLine then sabresDn5 else na;
plot tgDn = if showBand and dnCondLine then sabresDn6 else na;
stDn.SetStyle(Curve.SHORT_DASH);
tgDn.SetStyle(Curve.SHORT_DASH);
LineDn1.SetDefaultColor(GlobalColor("colDn"));
LineDn2.SetDefaultColor(GlobalColor("colDn"));
stDn.SetDefaultColor(GlobalColor("colDn"));
tgDn.SetDefaultColor(GlobalColor("colDn"));
CurveDn3.SetStyle(Curve.SHORT_DASH);
CurveDn4.SetStyle(Curve.SHORT_DASH);
CurveDn1.SetDefaultColor(GlobalColor("colDn"));
CurveDn2.SetDefaultColor(GlobalColor("colDn"));
CurveDn3.SetDefaultColor(GlobalColor("dcolDn"));
CurveDn4.SetDefaultColor(GlobalColor("dcolDn"));
AddCloud(CurveDn1, CurveDn2, GlobalColor("colDn"));
AddCloud(LineDn2, LineDn1, GlobalColor("colDn"));
#-- Bulish
def slpUp = if !isNaN(cntUp) then atrUp / (length * 15 / 50) else slpUp[1];
def slopeUp = if cntUp < Length / 10 then -(Length * Sqr(slpUp)) else (cntUp * Sqr(slpUp));
def factorUp = ma(maTyp, slopeUp, Length / 10);
def legUp1 = yUp0 + cntUp * slpUp2;
def legUp0 = yUp2 + ((cntUp - hLen) * slpUp3);
def legUp2 = yUp1 + cntUp * slpUp1;
def curUp11 = if cntUp then
if cntUp < hLen then
if up then yUp0 else legUp1 else
if cntUp == hLen then yUp2 else legUp0 else na;
def curUp22 = if cntUp < hLen then
if up then yUp1 else legUp2 else na;
def curUp1; def curUp2; def curUp3; def curUp4; def linUp1; def linUp2;
if up {
curUp1 = upVal + atrUp / 15;
curUp2 = upVal - atrUp / 15;
curUp3 = upVal + atrUp * targetMulti;
curUp4 = upVal - atrUp * stoplossMulti;
linUp1 = upVal - atrUp * stoplossMulti;
linUp2 = upVal + atrUp * targetMulti;
} else {
curUp1 = curUp1[1] + factorUp;
curUp2 = curUp2[1] + factorUp;
curUp3 = curUp3[1] + factorUp;
curUp4 = curUp4[1] + factorUp;
linUp1 = linUp1[1];
linUp2 = linUp2[1];
}
def saberLUp1 = curUp11;
def saberLUp2 = curUp22;
def sabresUp1 = curUp1;
def sabresUp2 = curUp2;
def sabresUp3 = curUp3;
def sabresUp4 = curUp4;
def sabresUp5 = linUp1;
def sabresUp6 = linUp2;
def upCondCurve = curve and !up[-1] and cntUp;
def upCondLine = !curve and !up[-1] and cntUp;
plot PointUp = upL[-1];
PointUp.SetLineWeight(4);
PointUp.SetPaintingStrategy(PaintingStrategy.POINTS);
PointUp.SetDefaultColor(GlobalColor("colUp"));
plot CurveUp1 = if upCondCurve then sabresUp1[-1] else na;
plot CurveUp2 = if upCondCurve then sabresUp2[-1] else na;
plot CurveUp3 = if showBand and upCondCurve then sabresUp3 else na;
plot CurveUp4 = if showBand and upCondCurve then sabresUp4 else na;
plot LineUp1 = if upCondLine then saberLUp1 else na;
plot LineUp2 = if upCondLine then saberLUp2 else na;
plot stUp = if showBand and upCondLine then sabresUp5 else na;
plot tgUp = if showBand and upCondLine then sabresUp6 else na;
stUp.SetStyle(Curve.SHORT_DASH);
tgUp.SetStyle(Curve.SHORT_DASH);
LineUp1.SetDefaultColor(GlobalColor("colUp"));
LineUp2.SetDefaultColor(GlobalColor("colUp"));
stUp.SetDefaultColor(GlobalColor("colUp"));
tgUp.SetDefaultColor(GlobalColor("colUp"));
CurveUp3.SetStyle(Curve.SHORT_DASH);
CurveUp4.SetStyle(Curve.SHORT_DASH);
CurveUp1.SetDefaultColor(GlobalColor("colUp"));
CurveUp2.SetDefaultColor(GlobalColor("colUp"));
CurveUp3.SetDefaultColor(GlobalColor("dcolUp"));
CurveUp4.SetDefaultColor(GlobalColor("dcolUp"));
AddCloud(CurveUp1, CurveUp2, GlobalColor("colUp"));
AddCloud(LineUp2, LineUp1, GlobalColor("colUp"));
#-- END of CODE