#//@Bjorgum on Stocktwits
#//@version=5
#indicator ( "Bjorgum TSI", "BJ TSI" )
#
https://www.tradingview.com/script/VX852ekB-Bjorgum-TSI/
# Converted by SAM4COk - Not Exact Convert
# ================================== //
# ---------> User Input <----------- //
# ================================== //
declare lower;
def Na = Double.NaN;
def RTH = SecondsFromTime(0930) >= 0 and SecondsTillTime(1600) >= 0;
input strat = {default Fast, Slow};
#// TSI Colors //
DefineGlobalColor("tsiBull", CreateColor( 23, 255, 0 ));
DefineGlobalColor("tsiMid", CreateColor(149, 152, 161));
DefineGlobalColor("tsiBear", CreateColor(243, 255, 0 ));
DefineGlobalColor("tslBull", CreateColor(100, 181, 246));
DefineGlobalColor("tslBear", CreateColor(211, 47 , 47 ));
DefineGlobalColor("fillBull", CreateColor(100, 181, 246));
DefineGlobalColor("fillBear", CreateColor(211, 47 , 47 ));
DefineGlobalColor("oBot", CreateColor(255, 0 , 0 ));
DefineGlobalColor("oSold", CreateColor(100, 181, 246));
DefineGlobalColor("oZero", CreateColor(178, 181, 190));
DefineGlobalColor("obBar", CreateColor(255, 0 , 0 ));
DefineGlobalColor("neBar", CreateColor(255, 255, 255));
DefineGlobalColor("osBar", CreateColor( 23, 255, 0 ));
input FastLong = 25; # "Long Length"
input FastShort = 5; # "Short Length"
input FastSignal = 14; # "Signal Length"
input SlowLong = 25; # "Long Length"
input Slowshort = 13; # "Short Length"
input Slowsignal = 13; # "Signal Length"
input OBValue = 30; # "Ob Value"
input OSValue = -30; # "Os Value"
input OBOSLines = yes; # "Show Ob/Os Lines"
input ZeroLine = yes; # "Show Zero Line"
input BarColor = no; # "Ob/Os Curl Color"
# ================================== //,
# ---> Functional Declarations <---- //
# ================================== //
#// TSI Scripts //
script nz
{
input data1 = 0;
input data2 = close;
def ret_val = if IsNaN(data1) then data1 else data2;
plot return = ret_val;
}
script _doubleSmooth {
input src = 0;
input longvar = 0;
input shortvar = 0;
def fist_smooth = ExpAverage(src, longvar);
def smooth = ExpAverage(fist_smooth, shortvar);
plot return = smooth;
}
# ================================== //
# ----> Variable Calculations <----- //
# ================================== //
def tsifast = strat == strat.Fast;
def tsislow = strat == strat.Slow;
def shortvar = if tsifast then FastShort else if tsislow then Slowshort else Na;
def longvar = if tsifast then FastLong else if tsislow then SlowLong else Na;
def signalvar = if tsifast then FastSignal else if tsislow then Slowsignal else Na;
def pc = close - close[1];
def dSmoothPc = _doubleSmooth(pc, longvar, shortvar);
def dSmoothAbs = _doubleSmooth(AbsValue(pc), longvar, shortvar);
def tsi = 100 * (dSmoothPc / dSmoothAbs);
def tsl = ExpAverage(tsi, signalvar);
# ================================== //
# ----> Conditional Parameters <---- //
# ================================== //
def data = tsi > tsi[1] and tsi < tsl;
def dtat = tsi < tsi[1] and tsi > tsl;
def lot1 = tsl >= tsl[1];
def lot2 = tsl < tsl[1];
def data1 = tsi >= tsl;
def data2 = tsi < tsl;
def obcol = tsi > OBValue and tsi < tsi[1] and tsi[1] > tsl[1];
def oscol = tsi < OSValue and tsi > tsi[1] and tsi[1] < tsl[1];
Plot obcol1 = if RTH and obcol then tsl else na;
obcol1.SetPaintingStrategy(PaintingStrategy.POINTS);
obcol1.AssignValueColor(GlobalColor("tslBear"));
obcol1.SetLineWeight(3);
Plot oscol1 = if RTH and oscol then tsl else na;
oscol1.SetPaintingStrategy(PaintingStrategy.POINTS);
oscol1.AssignValueColor(GlobalColor("tsiBull"));
oscol1.SetLineWeight(3);
def buy = Crosses (tsi, tsl , CrossingDirection.ABOVE);
def sell = Crosses (tsi, tsl , CrossingDirection.BELOW);
def cross = Crosses (tsi, tsl , CrossingDirection.ANY);
def tsixup = Crosses (tsi, OBValue, CrossingDirection.ABOVE);
def tsixdwn = Crosses (tsi, OSValue, CrossingDirection.BELOW);
# ================================== //
# ------> Graphical Display <------- //
# ================================== //
plot p1 = tsi; # "TSI Value Line"
p1.AssignValueColor(if data then GlobalColor("tsiBull") else
if dtat then GlobalColor("tsiBear") else GlobalColor("tsiMid"));
plot p2 = tsl; # "TSI Signal Line"
p2.AssignValueColor(if lot1 then GlobalColor("tslBull") else
if lot2 then GlobalColor("tslBear") else color.current);
plot p3 = if OBOSLines then OBValue else Na; # "Overbought"
P3.SetPaintingStrategy(PaintingStrategy.DASHES);
p3.AssignValueColor(GlobalColor("oBot"));
plot p4 = if OBOSLines then OSValue else Na; # "Oversold"
P4.SetPaintingStrategy(PaintingStrategy.DASHES);
p4.AssignValueColor(GlobalColor("oSold"));
plot p5 = if ZeroLine then 0 else Na; # "Zero"
P5.SetPaintingStrategy(PaintingStrategy.DASHES);
p5.AssignValueColor(GlobalColor("oZero"));
Def jercol = if obcol then -1 else if oscol then 1 else 0;
AssignPriceColor ( if BarColor then if jercol > 0 then GlobalColor("osBar") else
if jercol < 0 then GlobalColor("obBar") else
GlobalColor("neBar")else Color.CURRENT);
AddCloud(p1, p2, GlobalColor("fillBull") , GlobalColor("fillBear"));