
Author Message:
Introduction
Supertrend is a popular technical indicator used by traders to identify potential trend reversals and determine entry and exit points in financial markets. It is a trend-following indicator that combines price and volatility to generate its signals. Generally supertrend is calculated based on ATR and multiplier value which is used for calculation of stops. In these adaptions, we look to provide few variations to classical methods.
more information may found here:
https://www.tradingview.com/v/vHpsE0Ft/
CODE:
CSS:
#// This work is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License (CC BY-NC-SA 4.0) https://creativecommons.org/licenses/by-nc-sa/4.0/
#// © Trendoscope Pty Ltd
#indicator("Master Supertrend [Trendoscope]", "MST [Trendoscope]", overlay = true)
# converted and mod by Sam4Cok@Samer800 - 05/2023 - Not Typical Conv
input ShowTrendLine = yes;
input ShowSignals = yes;
input supertrendType = {default "Plus/Minus Range", "Ladder TR", "True Range", "Standard Deviation"};
input rangeType = {sma, ema, hma, rma, wma, default "high", median, medianHigh, medianLow}; #'Applied Calculation'
input length = 20; # 'Supertrend range calculation length'
input multiplier = 4; # 'Supertrend range multiplier'
input useClosePrices = yes; # 'Use Close Price'
input waitForClose = yes; # 'Wait for Close'
input useDiminishingRange = yes; # 'Diminishing Stop Distance'
def na = Double.NaN;
def bodyMiddle = OHLC4;
DefineGlobalColor("up", CreateColor(76,175,80));
DefineGlobalColor("dn", CreateColor(255,82,82));
script ma {
input src = close;
input matype = "EMA";
input length = 20;
def ma1;
# if array.size(source) >= length
def sliced = Average(src, length);
def ma = if matype == "sma" then sliced else
if matype == "ema" then ExpAverage(src, length) else
if matype == "rma" then WildersAverage(src, length) else
if matype == "wma" then WMA(src, length) else
if matype == "hma" then HullMovingAvg(src, length) else
if matype == "high" then Highest(src, length) else
if matype == "low" then Lowest(src, length) else
if matype == "median" then Median(src, length) else
if matype == "medianHigh" then Median(src, length) else
if matype == "medianLow" then Median(src, length) else
Double.NaN;
if (matype == "medianHigh") {
ma1 = Highest(ma, length);
} else
if (matype == "medianLow") {
ma1 = Lowest(ma, length);
} else {
ma1 = ma;
}
plot out = ma1;
}
#def bar = AbsValue(CompoundValue(1, BarNumber(), 0));
def tr = TrueRange(high, close, low);
def trUp = fold i = 0 to length * 3 with p do
if (open[i] < close[i]) then Max(trUp[1], tr) else trUp[1];
def trDn = fold j = 0 to length * 3 with q do
if (open[j] >= close[j]) then Max(trDn[1], tr) else trDn[1];
def upTr = fold i1 = 0 to length * 3 with p1 do
if trUp[i1] < length then tr[i1] else upTr[1];
def dnTr = fold j1 = 0 to length * 3 with q1 do
if trDn[j1] < length then tr[j1] else dnTr[1];
def plusLadderAtr = upTr;
def minusLadderAtr = dnTr;
def ladderPositiveAtr = ma(plusLadderAtr, rangeType, length);
def ladderNegativeAtr = ma(minusLadderAtr, rangeType, length);
def range = supertrendType == supertrendType."Plus/Minus Range";
def ladder = supertrendType == supertrendType."Ladder TR";
def True = supertrendType == supertrendType."True Range";
def plusRange = if range then Max(high - open, high - close[1]) else
if ladder then ladderPositiveAtr else
if True then tr else StDev(close, length);
def minusRange = if range then Max(open - low, close[1] - low) else
if ladder then ladderNegativeAtr else
if True then tr else StDev(close, length);
def appliedPlusRange = if ladder then plusRange else ma(plusRange, rangeType, length);
def appliedMinusRange = if ladder then minusRange else ma(minusRange, rangeType, length);
def direction;
def derivedPlusRange;def derivedMinusRange; def buyStop; def sellStop;
def currentDerivedPlusRange = appliedPlusRange * multiplier;
def currentDerivedMinusRange = appliedMinusRange * multiplier;
def Plus = if derivedPlusRange[1]==0 then currentDerivedPlusRange else derivedPlusRange[1];
def Minus = if derivedMinusRange[1]==0 then currentDerivedMinusRange else derivedMinusRange[1];
derivedPlusRange = if direction[1] < 0 and useDiminishingRange then
Min(Plus, currentDerivedPlusRange) else currentDerivedPlusRange;
derivedMinusRange = if direction[1] > 0 and useDiminishingRange then
Min(Minus, currentDerivedMinusRange) else currentDerivedMinusRange;
def buyStopCurrent = (if useClosePrices then close else low) - derivedMinusRange;
def sellStopCurrent = (if useClosePrices then close else high) + derivedPlusRange;
def buy = if buyStop[1]==0 then buyStopCurrent else buyStop[1];
def sell = if sellStop[1]==0 then sellStopCurrent else sellStop[1];
buyStop = if direction[1] > 0 then Max(buy, buyStopCurrent) else buyStopCurrent;
sellStop = if direction[1] < 0 then Min(sell, sellStopCurrent) else sellStopCurrent;
direction = if direction[1] > 0 and (if waitForClose then close else low) < buyStop then -1 else
if direction[1] < 0 and (if waitForClose then close else high) > sellStop then 1 else
if direction[1] == 0 then 1 else direction[1];
def supertrend = if direction[1] > 0 then buyStop else sellStop;
plot upTrend = if !ShowTrendLine then na else if direction[1]>0 then supertrend else na;#, 'Supertrend'
upTrend.SetDefaultColor(GlobalColor("up"));
plot downTrend = if !ShowTrendLine then na else if direction[1]<0 then supertrend else na;#, 'Supertrend'
downTrend.SetDefaultColor(GlobalColor("dn"));
#ST.AssignValueColor(if direction[1]>0 then Color.GREEN else Color.RED);
#-- Singal
def crossUp = ShowSignals and crosses(direction[1], 0, CrossingDirection.ABOVE);
def CrossDn = ShowSignals and crosses(direction[1], 0, CrossingDirection.BELOW);
AddChartBubble(crossUp, if ShowTrendLine then supertrend else low, "Bull", Color.GREEN, no);# 'Supertrend Bullish Breakout')
AddChartBubble(CrossDn, if ShowTrendLine then supertrend else high, "Bear", Color.RED, yes);#'Supertrend Bearish Breakout')
def LongTrigger = direction!=direction[1] and direction[1]<0;
def ShortTrigger = direction!=direction[1] and direction[1]>0;
plot LongDot = if ShowTrendLine and LongTrigger[1] then upTrend else na;
LongDot.SetPaintingStrategy(PaintingStrategy.POINTS);
LongDot.AssignValueColor(GlobalColor("up"));
LongDot.SetLineWeight(3);
plot ShortDot = if ShowTrendLine and ShortTrigger[1] then downTrend else na;
ShortDot.SetPaintingStrategy(PaintingStrategy.POINTS);
ShortDot.AssignValueColor(GlobalColor("dn"));
ShortDot.SetLineWeight(3);
AddCloud(bodyMiddle, upTrend, Color.DARK_GREEN);
AddCloud(downTrend, bodyMiddle, Color.DARK_RED);
#---