Multi-Step FlexiSuperTrend For ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
flexiST-TOS_CHARTS.png


Author Message:
is an advanced trading strategy that integrates the well-known SuperTrend indicator with a nuanced and dynamic approach to market trend analysis. Unlike conventional SuperTrend strategies that rely on static thresholds and fixed parameters, this strategy introduces multi-step take profit mechanisms that allow traders to capitalize on varying market conditions in a more controlled and systematic manner.
More details: https://www.tradingview.com/script/FxxdXitn-Multi-Step-FlexiSuperTrend-Strategy-presentTrading/

CODE:


CSS:
#// Indicator for TOS
#// © PresentTrading
#strategy("Multi-Step FlexiSuperTrend - Strategy [presentTrading]", shorttitle = "Multi-Step FlexiST-VT - Strategy
declare lower;

input displayOptios = {Default "Supertrend & Median", "Supertrend Only", "Median Only"};
input tradeDirection = {default "Both", "Long", "Short"}; # "Trading Direction"
input Source = close; #, title="Indicator Source")
input indicatorLength = 10; #, minval = 2, title="Indicator Length")
input startingFactor = 0.618; #, title="Starting Factor", minval = 0)
input incrementFactor = 0.382; #, minval = 0, step = .10, title="Increment Factor")
input NormalizationMethod = {default "None", "Max-Min", "Absolute Sum"}; # "Normalization Method"

def na = Double.NaN;
def last = IsNaN(close);
def both  = displayOptios==displayOptios."Supertrend & Median";
def super = displayOptios==displayOptios."Supertrend Only" or both;
def med = displayOptios==displayOptios."Median Only" or both;
def maxmin = NormalizationMethod == NormalizationMethod."Max-Min";
#-- Color
DefineGlobalColor("bg", CreateColor(10,93,128));
DefineGlobalColor("up", Color.GREEN); #CreateColor(0, 140, 255));
DefineGlobalColor("dn", CreateColor(255, 72, 0));
DefineGlobalColor("up1", CreateColor(49, 244, 212));
DefineGlobalColor("up2", CreateColor(33, 243, 208));
DefineGlobalColor("up3", CreateColor(18, 242, 205));
DefineGlobalColor("up4", CreateColor(12, 232, 196));
DefineGlobalColor("up5", CreateColor(11, 217, 183));
DefineGlobalColor("up6", CreateColor(10, 201, 169));
DefineGlobalColor("up7", CreateColor(9, 186, 156));
DefineGlobalColor("up8", CreateColor(8, 170, 143));
DefineGlobalColor("up9", CreateColor(8, 155, 130));
DefineGlobalColor("up10", CreateColor(7, 139, 117));
DefineGlobalColor("up11", CreateColor(6, 124, 104));
DefineGlobalColor("up12", CreateColor(5, 108, 91));
DefineGlobalColor("up13", CreateColor(4, 93, 78));
DefineGlobalColor("up14", CreateColor(4, 77, 65));
DefineGlobalColor("up15", CreateColor(3, 69, 58));
DefineGlobalColor("up16", CreateColor(3, 62, 52));
DefineGlobalColor("up17", CreateColor(2, 38, 32));
DefineGlobalColor("up18", CreateColor(1, 31, 26));
DefineGlobalColor("up19", CreateColor(1, 23, 19));
DefineGlobalColor("dn1", CreateColor(241, 51, 67));
DefineGlobalColor("dn2", CreateColor(240, 36, 52));
DefineGlobalColor("dn3", CreateColor(239, 21, 38));
DefineGlobalColor("dn4", CreateColor(229, 14, 32));
DefineGlobalColor("dn5", CreateColor(222, 14, 30));
DefineGlobalColor("dn6", CreateColor(206, 13, 28));
DefineGlobalColor("dn7", CreateColor(191, 12, 26));
DefineGlobalColor("dn8", CreateColor(183, 11, 25));
DefineGlobalColor("dn9", CreateColor(168, 10, 23));
DefineGlobalColor("dn10", CreateColor(160, 10, 22));
DefineGlobalColor("dn11", CreateColor(145, 9, 20));
DefineGlobalColor("dn12", CreateColor(130, 8, 18));
DefineGlobalColor("dn13", CreateColor(114, 7, 16));
DefineGlobalColor("dn14", CreateColor(99, 6, 13));
DefineGlobalColor("dn15", CreateColor(84, 5, 11));
DefineGlobalColor("dn16", CreateColor(68, 4, 9));
DefineGlobalColor("dn17", CreateColor(53, 3, 7));
DefineGlobalColor("dn18", CreateColor(45, 2, 6));
DefineGlobalColor("dn19", CreateColor(30, 1, 4));

script norm {
    input value = close;
    input minn = 0;
    input maxx = 10;
    input den = 20;
    input NormalizationMethod = "None";
    def range = maxx - minn;
    def maxmin = (value - minn) / range;
    def avg = value / den;
    def normalizeMethod = if NormalizationMethod == "Max-Min" then maxmin else
                          if NormalizationMethod == "Absolute Sum" then avg else value;
    plot out = normalizeMethod;
}
#SuperTrend Polyfactor Oscillator
script supertrend {
    input src = hl2;
    input Period = 10;
    input factor = 3;
    def nATR = ATR(Length = Period) * factor;
    def up = hl2 + nATR;
    def dn = hl2 - nATR;
    def lowerBand;
    def upperBand;
    def prevLowerBand = CompoundValue(1, lowerBand[1], 0);
    def prevUpperBand = CompoundValue(1, upperBand[1], 0);
    def trend = if src > prevUpperBand then 1 else
                if src < prevLowerBand then 0 else trend[1];
    lowerBand = CompoundValue(1, if (src[1] > prevLowerBand) then Max(dn, prevLowerBand) else dn, dn);
    upperBand = CompoundValue(1, if (src[1] < prevUpperBand) then Min(up, prevUpperBand) else up, up);
    def superTrend = if trend == 1 then lowerBand else upperBand;
    plot ST  = if IsNaN(superTrend) then 0 else superTrend;
}
script rank {
    input v0 = 0;
    input v01 = 0;
    input v02 = 0;
    input v03 = 0;
    input v04 = 0;
    input v05 = 0;
    input v06 = 0;
    input v07 = 0;
    input v08 = 0;
    input v09 = 0;
    input v10 = 0;
    input v11 = 0;
    input v12 = 0;
    input v13 = 0;
    input v14 = 0;
    input v15 = 0;
    input v16 = 0;
    input v17 = 0;
    input v18 = 0;
    input v19 = 0;
    input v20 = 0;
    def r01 = v0 > v01;
    def r02 = v0 > v02;
    def r03 = v0 > v03;
    def r04 = v0 > v04;
    def r05 = v0 > v05;
    def r06 = v0 > v06;
    def r07 = v0 > v07;
    def r08 = v0 > v08;
    def r09 = v0 > v09;
    def r10 = v0 > v10;
    def r11 = v0 > v11;
    def r12 = v0 > v12;
    def r13 = v0 > v13;
    def r14 = v0 > v14;
    def r15 = v0 > v15;
    def r16 = v0 > v16;
    def r17 = v0 > v17;
    def r18 = v0 > v18;
    def r19 = v0 > v19;
    def r20 = v0 > v20;
    def cnt = r01 + r02 + r03 + r04 + r05 + r06 + r07 + r08 + r09 + r10 +
                  r11 + r12 + r13 + r14 + r15 + r16 + r17 + r18 + r19 + r20;
    plot out = cnt + 1;
}
def factor00 = startingFactor;
def factor01 = factor00 + incrementFactor;
def factor02 = factor01 + incrementFactor;
def factor03 = factor02 + incrementFactor;
def factor04 = factor03 + incrementFactor;
def factor05 = factor04 + incrementFactor;
def factor06 = factor05 + incrementFactor;
def factor07 = factor06 + incrementFactor;
def factor08 = factor07 + incrementFactor;
def factor09 = factor08 + incrementFactor;
def factor10 = factor09 + incrementFactor;
def factor11 = factor10 + incrementFactor;
def factor12 = factor11 + incrementFactor;
def factor13 = factor12 + incrementFactor;
def factor14 = factor13 + incrementFactor;
def factor15 = factor14 + incrementFactor;
def factor16 = factor15 + incrementFactor;
def factor17 = factor16 + incrementFactor;
def factor18 = factor17 + incrementFactor;
def factor19 = factor18 + incrementFactor;

def st00 = supertrend(Source, indicatorLength, factor00);
def st01 = supertrend(Source, indicatorLength, factor01);
def st02 = supertrend(Source, indicatorLength, factor02);
def st03 = supertrend(Source, indicatorLength, factor03);
def st04 = supertrend(Source, indicatorLength, factor04);
def st05 = supertrend(Source, indicatorLength, factor05);
def st06 = supertrend(Source, indicatorLength, factor06);
def st07 = supertrend(Source, indicatorLength, factor07);
def st08 = supertrend(Source, indicatorLength, factor08);
def st09 = supertrend(Source, indicatorLength, factor09);
def st10 = supertrend(Source, indicatorLength, factor10);
def st11 = supertrend(Source, indicatorLength, factor11);
def st12 = supertrend(Source, indicatorLength, factor12);
def st13 = supertrend(Source, indicatorLength, factor13);
def st14 = supertrend(Source, indicatorLength, factor14);
def st15 = supertrend(Source, indicatorLength, factor15);
def st16 = supertrend(Source, indicatorLength, factor16);
def st17 = supertrend(Source, indicatorLength, factor17);
def st18 = supertrend(Source, indicatorLength, factor18);
def st19 = supertrend(Source, indicatorLength, factor19);

def diffs00 = Source - st00;
def diffs01 = Source - st01;
def diffs02 = Source - st02;
def diffs03 = Source - st03;
def diffs04 = Source - st04;
def diffs05 = Source - st05;
def diffs06 = Source - st06;
def diffs07 = Source - st07;
def diffs08 = Source - st08;
def diffs09 = Source - st09;
def diffs10 = Source - st10;
def diffs11 = Source - st11;
def diffs12 = Source - st12;
def diffs13 = Source - st13;
def diffs14 = Source - st14;
def diffs15 = Source - st15;
def diffs16 = Source - st16;
def diffs17 = Source - st17;
def diffs18 = Source - st18;
def diffs19 = Source - st19;
def den00 = AbsValue(diffs00);
def den01 = den00 + AbsValue(diffs01);
def den02 = den01 + AbsValue(diffs02);
def den03 = den02 + AbsValue(diffs03);
def den04 = den03 + AbsValue(diffs04);
def den05 = den04 + AbsValue(diffs05);
def den06 = den05 + AbsValue(diffs06);
def den07 = den06 + AbsValue(diffs07);
def den08 = den07 + AbsValue(diffs08);
def den09 = den08 + AbsValue(diffs09);
def den10 = den09 + AbsValue(diffs10);
def den11 = den10 + AbsValue(diffs11);
def den12 = den11 + AbsValue(diffs12);
def den13 = den12 + AbsValue(diffs13);
def den14 = den13 + AbsValue(diffs14);
def den15 = den14 + AbsValue(diffs15);
def den16 = den15 + AbsValue(diffs16);
def den17 = den16 + AbsValue(diffs17);
def den18 = den17 + AbsValue(diffs18);
def den19 = den18 + AbsValue(diffs19);
def den = den19;
#-- Sorting
def sort01 = rank(diffs00, diffs00, diffs01, diffs02, diffs03, diffs04, diffs05, diffs06, diffs07, diffs08, diffs09,
                           diffs10, diffs11, diffs12, diffs13, diffs14, diffs15, diffs16, diffs17, diffs18, diffs19);
def sort02 = rank(diffs01, diffs00, diffs01, diffs02, diffs03, diffs04, diffs05, diffs06, diffs07, diffs08, diffs09,
                           diffs10, diffs11, diffs12, diffs13, diffs14, diffs15, diffs16, diffs17, diffs18, diffs19);
def sort03 = rank(diffs02, diffs00, diffs01, diffs02, diffs03, diffs04, diffs05, diffs06, diffs07, diffs08, diffs09,
                           diffs10, diffs11, diffs12, diffs13, diffs14, diffs15, diffs16, diffs17, diffs18, diffs19);
def sort04 = rank(diffs03, diffs00, diffs01, diffs02, diffs03, diffs04, diffs05, diffs06, diffs07, diffs08, diffs09,
                           diffs10, diffs11, diffs12, diffs13, diffs14, diffs15, diffs16, diffs17, diffs18, diffs19);
def sort05 = rank(diffs04, diffs00, diffs01, diffs02, diffs03, diffs04, diffs05, diffs06, diffs07, diffs08, diffs09,
                           diffs10, diffs11, diffs12, diffs13, diffs14, diffs15, diffs16, diffs17, diffs18, diffs19);
def sort06 = rank(diffs05, diffs00, diffs01, diffs02, diffs03, diffs04, diffs05, diffs06, diffs07, diffs08, diffs09,
                           diffs10, diffs11, diffs12, diffs13, diffs14, diffs15, diffs16, diffs17, diffs18, diffs19);
def sort07 = rank(diffs06, diffs00, diffs01, diffs02, diffs03, diffs04, diffs05, diffs06, diffs07, diffs08, diffs09,
                           diffs10, diffs11, diffs12, diffs13, diffs14, diffs15, diffs16, diffs17, diffs18, diffs19);
def sort08 = rank(diffs07, diffs00, diffs01, diffs02, diffs03, diffs04, diffs05, diffs06, diffs07, diffs08, diffs09,
                           diffs10, diffs11, diffs12, diffs13, diffs14, diffs15, diffs16, diffs17, diffs18, diffs19);
def sort09 = rank(diffs08, diffs00, diffs01, diffs02, diffs03, diffs04, diffs05, diffs06, diffs07, diffs08, diffs09,
                           diffs10, diffs11, diffs12, diffs13, diffs14, diffs15, diffs16, diffs17, diffs18, diffs19);
def sort10 = rank(diffs09, diffs00, diffs01, diffs02, diffs03, diffs04, diffs05, diffs06, diffs07, diffs08, diffs09,
                           diffs10, diffs11, diffs12, diffs13, diffs14, diffs15, diffs16, diffs17, diffs18, diffs19);
def sort11 = rank(diffs10, diffs00, diffs01, diffs02, diffs03, diffs04, diffs05, diffs06, diffs07, diffs08, diffs09,
                           diffs10, diffs11, diffs12, diffs13, diffs14, diffs15, diffs16, diffs17, diffs18, diffs19);
def sort12 = rank(diffs11, diffs00, diffs01, diffs02, diffs03, diffs04, diffs05, diffs06, diffs07, diffs08, diffs09,
                           diffs10, diffs11, diffs12, diffs13, diffs14, diffs15, diffs16, diffs17, diffs18, diffs19);
def sort13 = rank(diffs12, diffs00, diffs01, diffs02, diffs03, diffs04, diffs05, diffs06, diffs07, diffs08, diffs09,
                           diffs10, diffs11, diffs12, diffs13, diffs14, diffs15, diffs16, diffs17, diffs18, diffs19);
def sort14 = rank(diffs13, diffs00, diffs01, diffs02, diffs03, diffs04, diffs05, diffs06, diffs07, diffs08, diffs09,
                           diffs10, diffs11, diffs12, diffs13, diffs14, diffs15, diffs16, diffs17, diffs18, diffs19);
def sort15 = rank(diffs14, diffs00, diffs01, diffs02, diffs03, diffs04, diffs05, diffs06, diffs07, diffs08, diffs09,
                           diffs10, diffs11, diffs12, diffs13, diffs14, diffs15, diffs16, diffs17, diffs18, diffs19);
def sort16 = rank(diffs15, diffs00, diffs01, diffs02, diffs03, diffs04, diffs05, diffs06, diffs07, diffs08, diffs09,
                           diffs10, diffs11, diffs12, diffs13, diffs14, diffs15, diffs16, diffs17, diffs18, diffs19);
def sort17 = rank(diffs16, diffs00, diffs01, diffs02, diffs03, diffs04, diffs05, diffs06, diffs07, diffs08, diffs09,
                           diffs10, diffs11, diffs12, diffs13, diffs14, diffs15, diffs16, diffs17, diffs18, diffs19);
def sort18 = rank(diffs17, diffs00, diffs01, diffs02, diffs03, diffs04, diffs05, diffs06, diffs07, diffs08, diffs09,
                           diffs10, diffs11, diffs12, diffs13, diffs14, diffs15, diffs16, diffs17, diffs18, diffs19);
def sort19 = rank(diffs18, diffs00, diffs01, diffs02, diffs03, diffs04, diffs05, diffs06, diffs07, diffs08, diffs09,
                           diffs10, diffs11, diffs12, diffs13, diffs14, diffs15, diffs16, diffs17, diffs18, diffs19);
def sort20 = rank(diffs19, diffs00, diffs01, diffs02, diffs03, diffs04, diffs05, diffs06, diffs07, diffs08, diffs09,
                           diffs10, diffs11, diffs12, diffs13, diffs14, diffs15, diffs16, diffs17, diffs18, diffs19);

def minDiff = if sort01 == 1 then diffs00 else if sort02 == 1 then diffs01 else if sort03 == 1 then diffs02 else
            if sort04 == 1 then diffs03 else if sort05 == 1 then diffs04 else if sort06 == 1 then diffs05 else
            if sort07 == 1 then diffs06 else if sort08 == 1 then diffs07 else if sort09 == 1 then diffs08 else
            if sort10 == 1 then diffs09 else if sort11 == 1 then diffs10 else if sort12 == 1 then diffs11 else
            if sort13 == 1 then diffs12 else if sort14 == 1 then diffs13 else if sort15 == 1 then diffs14 else
            if sort16 == 1 then diffs15 else if sort17 == 1 then diffs16 else if sort18 == 1 then diffs17 else
            if sort19 == 1 then diffs18 else diffs19;
def maxDiff = if sort01 == 20 then diffs00 else if sort02 == 20 then diffs01 else if sort03 == 20 then diffs02 else
            if sort04 == 20 then diffs03 else if sort05 == 20 then diffs04 else if sort06 == 20 then diffs05 else
            if sort07 == 20 then diffs06 else if sort08 == 20 then diffs07 else if sort09 == 20 then diffs08 else
            if sort10 == 20 then diffs09 else if sort11 == 20 then diffs10 else if sort12 == 20 then diffs11 else
            if sort13 == 20 then diffs12 else if sort14 == 20 then diffs13 else if sort15 == 20 then diffs14 else
            if sort16 == 20 then diffs15 else if sort17 == 20 then diffs16 else if sort18 == 20 then diffs17 else
            if sort19 == 20 then diffs18 else diffs19;
def med10 = if sort01 == 10 then diffs00 else if sort02 == 10 then diffs01 else if sort03 == 10 then diffs02 else
            if sort04 == 10 then diffs03 else if sort05 == 10 then diffs04 else if sort06 == 10 then diffs05 else
            if sort07 == 10 then diffs06 else if sort08 == 10 then diffs07 else if sort09 == 10 then diffs08 else
            if sort10 == 10 then diffs09 else if sort11 == 10 then diffs10 else if sort12 == 10 then diffs11 else
            if sort13 == 10 then diffs12 else if sort14 == 10 then diffs13 else if sort15 == 10 then diffs14 else
            if sort16 == 10 then diffs15 else if sort17 == 10 then diffs16 else if sort18 == 10 then diffs17 else
            if sort19 == 10 then diffs18 else diffs19;
def med11 = if sort01 == 11 then diffs00 else if sort02 == 11 then diffs01 else if sort03 == 11 then diffs02 else
            if sort04 == 11 then diffs03 else if sort05 == 11 then diffs04 else if sort06 == 11 then diffs05 else
            if sort07 == 11 then diffs06 else if sort08 == 11 then diffs07 else if sort09 == 11 then diffs08 else
            if sort10 == 11 then diffs09 else if sort11 == 11 then diffs10 else if sort12 == 11 then diffs11 else
            if sort13 == 11 then diffs12 else if sort14 == 11 then diffs13 else if sort15 == 11 then diffs14 else
            if sort16 == 11 then diffs15 else if sort17 == 11 then diffs16 else if sort18 == 11 then diffs17 else
            if sort19 == 11 then diffs18 else diffs19;
def median = (med10 + med11) / 2;

#-- median
def medianValue = norm(median, minDiff, maxDiff, den, NormalizationMethod);

# -- Stdv
def sumDiff = diffs00 + diffs01 + diffs02 + diffs03 + diffs04 + diffs05 + diffs06 + diffs07 + diffs08 + diffs09
            + diffs10 + diffs11 + diffs12 + diffs13 + diffs14 + diffs15 + diffs16 + diffs17 + diffs18 + diffs19;
def avgDiff = sumDiff / 20;
def sqrDiff = Sqr(AbsValue(avgDiff - diffs00)) + Sqr(AbsValue(avgDiff - diffs01)) + Sqr(AbsValue(avgDiff - diffs02)) +
              Sqr(AbsValue(avgDiff - diffs03)) + Sqr(AbsValue(avgDiff - diffs04)) + Sqr(AbsValue(avgDiff - diffs05)) +   
              Sqr(AbsValue(avgDiff - diffs06)) + Sqr(AbsValue(avgDiff - diffs07)) + Sqr(AbsValue(avgDiff - diffs08)) +
              Sqr(AbsValue(avgDiff - diffs09)) + Sqr(AbsValue(avgDiff - diffs10)) + Sqr(AbsValue(avgDiff - diffs11)) +
              Sqr(AbsValue(avgDiff - diffs12)) + Sqr(AbsValue(avgDiff - diffs13)) + Sqr(AbsValue(avgDiff - diffs14)) +
              Sqr(AbsValue(avgDiff - diffs15)) + Sqr(AbsValue(avgDiff - diffs16)) + Sqr(AbsValue(avgDiff - diffs17)) +
              Sqr(AbsValue(avgDiff - diffs18)) + Sqr(AbsValue(avgDiff - diffs19));
def Variance = sqrDiff / 20;
def stdev = Sqrt(Variance);
def normStdv = norm(stdev,  minDiff, maxDiff, den, NormalizationMethod);
def stdevValue = if !maxmin then normStdv else na;

#-- norm
def Dev01 = norm(diffs00, minDiff, maxDiff, den, NormalizationMethod);
def Dev02 = norm(diffs01, minDiff, maxDiff, den, NormalizationMethod);
def Dev03 = norm(diffs02, minDiff, maxDiff, den, NormalizationMethod);
def Dev04 = norm(diffs03, minDiff, maxDiff, den, NormalizationMethod);
def Dev05 = norm(diffs04, minDiff, maxDiff, den, NormalizationMethod);
def Dev06 = norm(diffs05, minDiff, maxDiff, den, NormalizationMethod);
def Dev07 = norm(diffs06, minDiff, maxDiff, den, NormalizationMethod);
def Dev08 = norm(diffs07, minDiff, maxDiff, den, NormalizationMethod);
def Dev09 = norm(diffs08, minDiff, maxDiff, den, NormalizationMethod);
def Dev10 = norm(diffs09, minDiff, maxDiff, den, NormalizationMethod);
def Dev11 = norm(diffs10, minDiff, maxDiff, den, NormalizationMethod);
def Dev12 = norm(diffs11, minDiff, maxDiff, den, NormalizationMethod);
def Dev13 = norm(diffs12, minDiff, maxDiff, den, NormalizationMethod);
def Dev14 = norm(diffs13, minDiff, maxDiff, den, NormalizationMethod);
def Dev15 = norm(diffs14, minDiff, maxDiff, den, NormalizationMethod);
def Dev16 = norm(diffs15, minDiff, maxDiff, den, NormalizationMethod);
def Dev17 = norm(diffs16, minDiff, maxDiff, den, NormalizationMethod);
def Dev18 = norm(diffs17, minDiff, maxDiff, den, NormalizationMethod);
def Dev19 = norm(diffs18, minDiff, maxDiff, den, NormalizationMethod);


#-- Supertrend Cals

input supertrendLength  = 10; #, "Length", inline = '1', group = g1)
input supertrendFactor  = 15.0; #, "Factor", 1, 20, 0.5, inline = '1', group = g1)
input showBubbles = yes; #(true, "Contrarian Signals", group = gu)

script st {
    input hl = hl2;
    input bc = close;
    input nATR = close;
    def up = hl + nATR;
    def dn = hl - nATR;
    def loBand;
    def upBand;
    def dn1 = CompoundValue(1, loBand[1], 0);
    def up1 = CompoundValue(1, upBand[1], 0);
    loBand = CompoundValue(1, if dn > dn1 or bc[1] < dn1 then dn else dn1, dn);
    upBand = CompoundValue(1, if up < up1 or bc[1] > up1 then up else up1, up);
    def superTrend;
    def superTrend1 = CompoundValue(1, superTrend[1], 0);
    def direction = if IsNaN(nATR[1]) then 1 else
                    if superTrend1 == up1 then if bc > upBand then -1 else 1 else
                    if bc < loBand then 1 else -1;
    superTrend = if direction == -1 then loBand else upBand;
    plot ST  = if IsNaN(superTrend) then 0 else superTrend;
    plot dir = if IsNaN(direction) then 1 else direction;
}
def src  = medianValue;
def bo  = if IsNaN(src[1]) then 0 else src[1];
def bh  = Max(bo, src);
def bl  = Min(bo, src);
def bc  = src;
def hl = (bh + bl) / 2;
def tr1 = TrueRange(bh, bc, bl);
def tr = WildersAverage(tr1, supertrendLength);
def bATR = tr * supertrendFactor;
def sts  = st(hl, bc, bATR).st;
def std  = st(hl, bc, bATR).dir;
def scon = !last and Sign(std - std[1]) ==  1;
def bcon = !last and Sign(std - std[1]) == -1;

#-- PLot ST
plot ptDn = if !maxmin and scon then sts else na;
plot ptUp = if !maxmin and bcon then sts else na;
plot trendDn = if !last and !maxmin and std > 0 then sts else na; #, 'Bear ST'
plot trendUp = if !last and !maxmin and std < 0 then sts else na; #, 'Bull ST'
ptDn.SetLineWeight(2);
ptUp.SetLineWeight(2);
trendDn.SetLineWeight(2);
trendUp.SetLineWeight(2);
ptDn.SetStyle(Curve.POINTS);
ptUp.SetStyle(Curve.POINTS);
trendDn.SetDefaultColor(GlobalColor("dn"));
trendUp.SetDefaultColor(GlobalColor("up"));
ptDn.SetDefaultColor(GlobalColor("dn"));
ptUp.SetDefaultColor(GlobalColor("up"));

AddChartBubble(showBubbles and scon, sts + tr / 3, "S", GlobalColor("dn"));
AddChartBubble(showBubbles and bcon, sts - tr / 3, "B", GlobalColor("up"), no);

#// Median
def th = if maxmin then 0.5 else 0;
def colM = if medianValue > th then 1 else 0;

plot meanLine = if med then medianValue else na; #, 'Median'
plot midLine = if last or super then na else if maxmin then 0.5 else 0;
meanLine.SetLineWeight(2);
meanLine.AssignValueColor(if colM then CreateColor(0, 140, 255) else GetColor(4));
midLine.SetDefaultColor(Color.DARK_GRAY);

#// Stdev Area
plot up = if stdevValue then stdevValue else na;
plot dn = if stdevValue then -stdevValue else na;
up.SetDefaultColor(GlobalColor("bg"));
dn.SetDefaultColor(GlobalColor("bg"));
up.SetStyle(Curve.MEDIUM_DASH);
dn.SetStyle(Curve.MEDIUM_DASH);

# plot

plot Deviation01 = if super then Dev01 else na;
plot Deviation02 = if super then Dev02 else na;
plot Deviation03 = if super then Dev03 else na;
plot Deviation04 = if super then Dev04 else na;
plot Deviation05 = if super then Dev05 else na;
plot Deviation06 = if super then Dev06 else na;
plot Deviation07 = if super then Dev07 else na;
plot Deviation08 = if super then Dev08 else na;
plot Deviation09 = if super then Dev09 else na;
plot Deviation10 = if super then Dev10 else na;
plot Deviation11 = if super then Dev11 else na;
plot Deviation12 = if super then Dev12 else na;
plot Deviation13 = if super then Dev13 else na;
plot Deviation14 = if super then Dev14 else na;
plot Deviation15 = if super then Dev15 else na;
plot Deviation16 = if super then Dev16 else na;
plot Deviation17 = if super then Dev17 else na;
plot Deviation18 = if super then Dev18 else na;
plot Deviation19 = if super then Dev19 else na;

Deviation01.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
Deviation02.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
Deviation03.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
Deviation04.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
Deviation05.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
Deviation06.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
Deviation07.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
Deviation08.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
Deviation09.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
Deviation10.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
Deviation11.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
Deviation12.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
Deviation13.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
Deviation14.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
Deviation15.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
Deviation16.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
Deviation17.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
Deviation18.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
Deviation19.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);

Deviation01.AssignValueColor(if diffs00 > 0 then GlobalColor("up1") else GlobalColor("dn1"));
Deviation02.AssignValueColor(if diffs01 > 0 then GlobalColor("up2") else GlobalColor("dn2"));
Deviation03.AssignValueColor(if diffs02 > 0 then GlobalColor("up3") else GlobalColor("dn3"));
Deviation04.AssignValueColor(if diffs03 > 0 then GlobalColor("up4") else GlobalColor("dn4"));
Deviation05.AssignValueColor(if diffs04 > 0 then GlobalColor("up5") else GlobalColor("dn5"));
Deviation06.AssignValueColor(if diffs05 > 0 then GlobalColor("up6") else GlobalColor("dn6"));
Deviation07.AssignValueColor(if diffs06 > 0 then GlobalColor("up7") else GlobalColor("dn7"));
Deviation08.AssignValueColor(if diffs07 > 0 then GlobalColor("up8") else GlobalColor("dn8"));
Deviation09.AssignValueColor(if diffs08 > 0 then GlobalColor("up9") else GlobalColor("dn9"));
Deviation10.AssignValueColor(if diffs09 > 0 then GlobalColor("up10") else GlobalColor("dn10"));
Deviation11.AssignValueColor(if diffs10 > 0 then GlobalColor("up11") else GlobalColor("dn11"));
Deviation12.AssignValueColor(if diffs11 > 0 then GlobalColor("up12") else GlobalColor("dn12"));
Deviation13.AssignValueColor(if diffs12 > 0 then GlobalColor("up13") else GlobalColor("dn13"));
Deviation14.AssignValueColor(if diffs13 > 0 then GlobalColor("up14") else GlobalColor("dn14"));
Deviation15.AssignValueColor(if diffs14 > 0 then GlobalColor("up15") else GlobalColor("dn15"));
Deviation16.AssignValueColor(if diffs15 > 0 then GlobalColor("up16") else GlobalColor("dn16"));
Deviation17.AssignValueColor(if diffs16 > 0 then GlobalColor("up17") else GlobalColor("dn17"));
Deviation18.AssignValueColor(if diffs17 > 0 then GlobalColor("up18") else GlobalColor("dn18"));
Deviation19.AssignValueColor(if diffs18 > 0 then GlobalColor("up19") else GlobalColor("dn19"));

AddCloud(stdevValue, -stdevValue, GlobalColor("bg"));

#-- BackTest
input labelOptions = {Default "Summary", "Detailed", "Don't Show"};
input entryExitOptions = {"Show Entry", "Show Exit",default "Show Entry/Exit", "Don't Show Signals"};
input TakeProfiPercent = 2.25; # "Take Profit %"
input StopLossPercent = 1.75;  # "Stop Loss %"
input lotSize = 1;

def full = labelOptions == labelOptions."Detailed";
def summ = labelOptions == labelOptions."Summary" or full;

def ent = entryExitOptions==entryExitOptions."Show Entry" or entryExitOptions==entryExitOptions."Show Entry/Exit";
def ext = entryExitOptions==entryExitOptions."Show Exit" or entryExitOptions==entryExitOptions."Show Entry/Exit";
def long  = tradeDirection == tradeDirection."Long" or tradeDirection == tradeDirection."Both";
def short = tradeDirection == tradeDirection."Short" or tradeDirection == tradeDirection."Both";

#/ Apply Take Profit and Stop Loss conditions
#/ Entry/Exit conditions
def LongEntry  = std < 0 and medianValue > th;
def ShortEntry = std > 0 and medianValue < th;
def exitLong   = std > 0;
def exitShort  = std < 0;
def state = {Default "ini", "long", "short"};
def takeProfit;
def stoploss;
def entryPrice;
def closePrice;
def win;
def tradingHours = atr(1) < atr(100);
def longCond  = barNumber()> 0 and long and LongEntry and tradingHours;
def shortCond = barNumber()> 0 and short and ShortEntry and tradingHours;

Switch (state[1]) {
Case "long" :
if (exitLong or (high >= takeProfit[1]) or (close < stoploss[1])) {
    state = state."ini";
    closePrice = close - entryPrice[1];
    entryPrice = na;
    takeProfit = na;
    stoploss = na;
    win = if closePrice > 0 then 1 else 0;
    } else {
    state = state[1];
    closePrice = 0;
    entryPrice = entryPrice[1];
    takeProfit = takeProfit[1];
    stoploss = stoploss[1];
    win = 0;
}
Case "short" :
if (exitShort or (low <= takeProfit[1]) or (close > stoploss[1])) {
    state = state."ini";
    closePrice = entryPrice[1] - close;
    entryPrice = na;
    takeProfit = na;
    stoploss = na;
    win = if closePrice > 0 then -1 else 0;
    } else {
    state = state[1];
    closePrice = 0;
    entryPrice = entryPrice[1];
    takeProfit = takeProfit[1];
    stoploss = stoploss[1];
    win = 0;
}
Default :
    state     = if longCond then state."long" else if shortCond then state."short" else state[1];
    entryPrice = open[-1];
    takeProfit = entryPrice * (1 + (if longCond then 1 else -1) * TakeProfiPercent / 100);
    stoploss   = entryPrice * (1 + (if longCond then -1 else 1) * StopLossPercent / 100);
    closePrice = 0;
    win = 0;
}
def longSig  = state==state."long"  and state!=state[1];
def shortSig = state==state."short" and state!=state[1];
def exitL = state[1]==state."long"  and state!=state."long";
def exitS = state[1]==state."short" and state!=state."short";

AddVerticalLine(ent and longSig, "B", Color.CYAN, Curve.FIRM);
AddVerticalLine(ent and shortSig,"S", Color.MAGENTA, Curve.FIRM);
AddVerticalLine(ext and exitL, "E", Color.DARK_GREEN);
AddVerticalLine(ext and exitS, "E", Color.DARK_RED);

def totLong  = TotalSum(if exitL then 1 else 0);
def totShort = TotalSum(if exitS then 1 else 0);
def tottrd =  totLong + totShort;
def WinL = TotalSum(if win>0 then 1 else 0);
def WinS = TotalSum(if win<0 then 1 else 0);
def totWin = (WinL + WinS);
def winRt = if !tottrd then 0 else Round(totWin / tottrd * 100, 1);
def winRtL = if !totLong then 0 else Round(WinL / totLong * 100, 1);
def winRtS = if !totShort then 0 else Round(WinS / totShort * 100, 1);
def amtL = TotalSum(if exitL then closePrice else 0) * lotSize;
def amtS = TotalSum(if exitS then closePrice else 0) * lotSize;
def pl = RoundDown(amtL + amtS, 2);
def plL = if isNaN(amtL) then 0 else RoundDown(amtL, 2);
def plS = if isNaN(amtS) then 0 else RoundDown(amtS, 2);


AddLabel(summ, "WinRate(" + winRt + "%, " + tottrd + ")", if winRt >= 50 then Color.GREEN else Color.RED);

AddLabel(full, "Long(" + winRtL + "%, " + totLong + ")", if winRtL >= 50 then Color.GREEN else Color.RED);
AddLabel(full, "Short(" + winRtS + "%, " + totShort + ")", if winRtS >= 50 then Color.GREEN else Color.RED);

AddLabel(full, "AmtLong($" + plL + ")", if amtL > 0 then Color.GREEN else Color.RED);
AddLabel(full, "AmtShort($" + plS + ")", if amtS > 0 then Color.GREEN else Color.RED);

AddLabel(summ, "P/L($" + pl + ")", if pl > 0 then Color.GREEN else if pl < 0 then Color.RED else Color.GRAY);


#-- END of CODE
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
435 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top