# © deepphalke
#https://www.tradingview.com/script/lEuCw5DS-JNSAR-DP/
#Peter Luis conversion
# April 24, 2022
#https://usethinkscript.com/threads/blsh-for-thinkorswim.11013/
#https://usethinkscript.com/threads/double-supertrend-atr-for-thinkorswim.10986/
#https://www.tradingview.com/script/OsKg8IXR-Double-SuperTrend-ATR/
#// beardcrack
#study(title="Modified DC for BLSH", shorttitle="BLSH Price Table", overlay=true)
#https://www.tradingview.com/script/MkrCvQ22-BLSH/
#https://usethinkscript.com/threads/jnsar-dp-for-thinkorswim.11058/
def src = hl2;
input displace = 0;
def len = 6;
def w = 4;
def h = high;
def l = low;
def c = close;
def srca = close;
def la = 6;
def std = no;
def bc = no;
def k = 1.0 / la;
def Factor = 3;
def ATR = 12;
def length = 25;
def profit = 5;
def LOW25 = lowest(low, length);
def GTT = LOW25*1.05;
def TARGET = GTT*1.05;
def LinReg = Inertia(src[-displace], len);
def LinReg1 = Inertia(h, 5);
def LinReg2 = Inertia(l, 5);
def LinReg3 = Inertia(c, 5);
def avg = (LinReg1+ LinReg2 + LinReg3 + Inertia(h[1], 5)+ Inertia(l[1], 5)+ Inertia(c[1], 5)+ Inertia(h[2], 5)+ Inertia(l[2], 5)+ Inertia(c[2], 5)+ Inertia(h[3], 5)+ Inertia(l[3], 5)+ Inertia(c[3], 5)+ Inertia(h[4], 5)+ Inertia(l[4], 5)+ Inertia(c[4], 5))/15;
plot Upperband = LinReg1;
Upperband.DefineColor("UpTrend", Color.GREEN);
Upperband.DefineColor("DownTrend", Color.RED);
Upperband.SetLineWeight(1);
Upperband.SetPaintingStrategy(PaintingStrategy.LINE);
Upperband.SetStyle(Curve.FIRM);
Upperband.AssignValueColor(if Upperband > Upperband [1] then Upperband.Color("UpTrend") else Upperband.Color("DownTrend"));
plot lowerband = LinReg2;
lowerband.DefineColor("UpTrend", Color.GREEN);
lowerband.DefineColor("DownTrend", Color.RED);
lowerband.SetLineWeight(1);
lowerband.SetPaintingStrategy(PaintingStrategy.LINE);
lowerband.SetStyle(Curve.FIRM);
lowerband.AssignValueColor(if lowerband > lowerband [1] then lowerband.Color("UpTrend") else lowerband.Color("DownTrend"));
Plot midline = LinReg3;
midline.DefineColor("UpTrend", Color.GREEN);
midline.DefineColor("DownTrend", Color.RED);
midline.SetLineWeight(1);
midline.SetPaintingStrategy(PaintingStrategy.LINE);
midline.SetStyle(Curve.FIRM);
midline.AssignValueColor(if midline >midline [1] then midline.Color("UpTrend") else midline.Color("DownTrend"));
input fill = yes;
def nan = Double.NaN;
plot LineAvg = avg;
LineAvg.DefineColor("UpTrend", Color.GREEN);
LineAvg.DefineColor("DownTrend", Color.RED);
LineAvg.SetLineWeight(3);
LineAvg.SetPaintingStrategy(PaintingStrategy.LINE);
LineAvg.SetStyle(Curve.FIRM);
LineAvg.AssignValueColor(if LineAvg > LineAvg [1] then LineAvg.Color("UpTrend") else LineAvg.Color("DownTrend"));
def a = (UpperBand > UpperBand[1] and Midline > Midline[1]);
def b = (UpperBand < UpperBand[1] and LowerBand < LowerBand[1]);
def c1 = (UpperBand > UpperBand[1] and Midline > Midline[1]);
def d = (UpperBand < UpperBand[1] and midline < midline[1]);
def e = average(close)>midline;
def f = midline<average(close);
def Chg = If(a, 1, If(b, -1, 0));
def Hold = CompoundValue(1, If(Hold[1] == Chg or Chg == 0, Hold[1], If(Chg == 1, 1, -1)), 0);
def Chg_a = If((c1 or e), 1, If((d or f), -1, 0));
def Hold_a = CompoundValue(1, If(Hold[1] == Chg or Chg == 0, Hold[1], If(Chg == 1, 1, -1)), 0);
def LBUp = if fill and Hold[0] == 1 then lowerBand else nan;
def UBUp = if fill and Hold[0] == 1 then upperband else nan;
def LBDn = if fill and Hold[0] == -1 then lowerband else nan;
def UBDn = if fill and Hold[0] == -1 then upperband else nan;
def MBUP = if fill and Hold[0] == 1 then midline else nan;
def MBDn = if fill and Hold[0] == -1 then Midline else nan;
DefineGlobalColor("Cloud Up", Color.red);
DefineGlobalColor("Cloud_Up", Color.downtick);
DefineGlobalColor("Cloud_Dn", Color.light_green);
DefineGlobalColor("Cloud Dn", Color.cyan);
AddCloud(LBUp, UBUp, GlobalColor("Cloud Up"), GlobalColor("Cloud Dn"));
AddCloud(LBDn, UBDn, GlobalColor("Cloud Dn"), GlobalColor("Cloud Up"));
AddCloud(MBUp, UBUp, GlobalColor("Cloud_Up"), GlobalColor("Cloud_Dn"));
AddCloud(MBDn, UBDn, GlobalColor("Cloud_Dn"), GlobalColor("Cloud_Up"));
script nz {
input data = 0;
def ret_val = if IsNaN(data) then 0 else data;
plot return = ret_val;
}
def pdm = Max((srca - srca[1]), 0);
def mdm = Max((srca[1] - srca), 0);
def pdmS = ((1 - k) * nz(pdmS[1]) + k * pdm);
def mdmS = ((1 - k) * nz(mdmS[1]) + k * mdm);
def s = pdmS + mdmS;
def pdi = pdmS / s;
def mdi = mdmS / s;
def pdiS = ((1 - k) * nz(pdiS[1]) + k * pdi);
def mdiS = ((1 - k) * nz(mdiS[1]) + k * mdi);
def da = AbsValue(pdiS - mdiS);
def s1 = pdiS + mdiS;
def iS = ((1 - k) * nz(iS[1]) + k * da / s1);
def hhv = Highest(iS, la);
def llv = Lowest(iS, la);
def d1 = hhv - llv;
def vI = (iS - llv) / d1;
def vma = (1 - k * vI) * nz(vma[1]) + k * vI * src;
plot line = vma;
line.AssignValueColor(if line> line[1] then Color.GREEN else if line <line[1] then Color.MAGENTA else color.gray);
line.SetLineWeight(4);
script nz {
input data = 0;
input data2 = 0;
def ret_val = if IsNaN(data) then data2 else data;
plot return = ret_val;
}
script MTF_ATR {
input Length = 12;
input TF = AggregationPeriod.HOUR;
input averageType = AverageType.WILDERS;
plot ATR =
MovingAverage(
averageType,
TrueRange(
high(period = TF),
close(period = TF),
low(period = TF)
),
Length
)
;
}
def Up = hl2 - (Factor * ATR(ATR));
def Dn = hl2 + (Factor * ATR(ATR));
def TUp = if close[1] > TUp[1] then Max(Up, TUp[1]) else Up;
def TDown = if close[1] < TDown[1] then Min(Dn, TDown[1]) else Dn;
def Trend = if close > TDown[1] then 1 else if close < TUp[1] then -1 else nz(Trend[1],1);
def Tsl1 = if Trend == 1 then TUp else TDown;
def Tsl2 = if Trend == 1 then TDown else TUp;
plot p1 = Tsl1;
plot p2 = Tsl2;
p1.AssignValueColor(if p1 < p2 then Color.green else Color.magenta);
p1.SetLineWeight(1);
p2.AssignValueColor(if p1 < p2 then Color.green else Color.magenta);
p2.SetLineWeight(1);
AddCloud(p1, p2, Color.light_RED, Color.light_GREEN);
plot lineGTT = GTT;
lineGTT.AssignValueColor(if lineGTT> lineGTT[1] then Color.GREEN else if lineGTT <lineGTT[1] then Color.MAGENTA else color.light_gray);
lineGTT.SetLineWeight(3);
plot LineLowe25 = LOW25;
LineLowe25.AssignValueColor(if LineLowe25> LineLowe25[1] then Color.light_GREEN else if LineLowe25 <LineLowe25[1] then Color.light_red else color.yellow);
LineLowe25.SetLineWeight(3);
plot LineTarget = TARGET;
LineTarget.AssignValueColor(if LineTarget> LineTarget[1] then Color.dark_GREEN else if LineTarget<LineTarget[1] then Color.red else color.white);
LineTarget.SetLineWeight(4);