Key Levels For ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
hi @samer800 - think this is possible to convert? arrays in the script.
rPzDdHM.png


I converted partially. pls check.

CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © Bjorgum
#indicator ('Bjorgum Key Levels', 'Bj Key Levels', ount=500)
# partial conversion by Sam4Cok@Samer800 - 01 /2023
input lookLeft    = 20;            #  "Look Left"   
input lookRight   = 15;            #  "Look Right"   
input NumberOfPivots = 3;          #  "Number of Pivots"
input atrLength      = 30;         # "ATR Length"
input ZoneWidth      = 0.5;        # "Zone Width (ATR)"
input MaxZonePercent = 5;          #  "Max Zone Percent"
input SourceForPivots = {default "HeikinAshi", "High/Low Body", "High/Low"}; # "Source For Pivots"
input ExtendRight     = no;        #   "Extend Right"
input PivotHighs      = yes;       #  "Detect Pivot Highs"
input PivotLows       = yes;       #  "Detect Pivot Lows"
input ShowBreakUp     = yes;
input ShowBreakDn     = yes;

def na = Double.NaN;
def extend = if ExtendRight then isNaN(close[lookRight]) else isNaN(close);
#--- Color
DefineGlobalColor("bullBorder", CreateColor(7,71,123));
DefineGlobalColor("bearBorder", CreateColor(118,106,0));
DefineGlobalColor("bearCloud", CreateColor(3,29,51));
DefineGlobalColor("bullCloud", CreateColor(42,37,0));

script nz {
    input data  = close;
    input repl  = 0;
    def ret_val = if !isNaN(data) then data else repl;
    plot return = ret_val;
}
script fixnan {
    input source = close;
    def fix = if !IsNaN(source) then source else fix[1];
    plot result = fix;
}
#valuewhen (cond, source, occurrence) =>
script valuewhen {
    input cond = 0;
    input source = 0;
    input occurrence = 0;
    def n = occurrence + 1;
# start at 0 so it looks at current bar
    def offset2 = fold j = 0 to 400
    with p
    while p < n + 1
    do p + ( if p == n then j - n else if GetValue(cond, j) then 1 else 0 );
# def bnz = bn - offset2 + 1;
    plot price = GetValue(source, offset2 - 1);
}

script FindPivots {
    input dat = close; # default data or study being evaluated
    input HL  = 0;    # default high or low pivot designation, -1 low, +1 high
    input lbL  = 5;    # default Pivot Lookback Left
    input lbR  = 1;    # default Pivot Lookback Right
    ##############
    def _nan;    # used for non-number returns
    def _BN;     # the current barnumber
    def _VStop;  # confirms that the lookforward period continues the pivot trend
    def _V;      # the Value at the actual pivot point
    ##############
    _BN  = BarNumber();
    _nan = Double.NaN;
    _VStop = if !IsNaN(dat) and lbR > 0 and lbL > 0 then
                fold a = 1 to lbR + 1 with b=1 while b do
                    if HL > 0 then dat > GetValue(dat, -a) else dat < GetValue(dat, -a) else _nan;
    if (HL > 0) {
        _V = if _BN > lbL and dat == Highest(dat, lbL + 1) and _VStop
            then dat else _nan;
    } else {
        _V = if _BN > lbL and dat == Lowest(dat, lbL + 1) and _VStop
            then dat else _nan;
    }
    plot result = if !IsNaN(_V) and _VStop then _V else _nan;
}
def haSrc   = SourceForPivots == SourceForPivots."HeikinAshi";
def hiLoSrc = SourceForPivots == SourceForPivots."High/Low";
#// ---> Functional Declarations <---- //

def nATR    = ATR(LENGTH = atrLength);
#def perMax = close * 0.02;
#def min    = Min(perMax, atr * 0.3);

#// ----> Variable Calculations <----- //
def close_ = (open + high  +  low  + close) / 4;
def open_;
open_  = if IsNaN(open_[1]) then (open + close) / 2 else
                 (nz(open_[1],open[1]) + nz(close_[1],close[1]))   / 2;
def hiHaBod = Max(close_, open_);
def loHaBod = Min(close_, open_);
def hiBod   = Max(close, open);
def loBod   = Min(close, open);
def srcHigh = if haSrc then hiHaBod else if hiLoSrc then high else hiBod;
def srcLow  = if haSrc then loHaBod else if hiLoSrc then low  else loBod;
def highest = close == Highest(close, lookRight);
def lowest  = close == Lowest (close, lookRight);
def pivot_high = FindPivots(srcHigh, 1, lookLeft, lookRight);
def pivot_low  = FindPivots(srcLow, -1, lookLeft, lookRight);
def ph = if !isNaN(pivot_high) then pivot_high else ph[1];
def pl = if !isNaN(pivot_low) then pivot_low else pl[1];
def perc = close * (MaxZonePercent / 100);
def minATR = Min(nATR * ZoneWidth, perc);
def band = minATR / 2;
def HH   = ph + band;
def HL   = ph - band;
def LH   = pl + band;
def LL   = pl - band;

def coDiff = close - open;
def barRange = AbsValue(high - low);
def phHH;
def phHL;
def plLH;
def plLL;
def phHH1;
def phHL1;
def plLH1;
def plLL1;
def phHH2;
def phHL2;
def plLH2;
def plLL2;
def phHH3;
def phHL3;
def plLH3;
def plLL3;
def phHH4;
def phHL4;
def plLH4;
def plLL4;
def cond1 = !isNaN(pivot_high) and pivot_high!=0 and !IsNaN(close[1]);
def cond2 = !isNaN(pivot_low)  and pivot_low !=0 and !IsNaN(close[1]);
if cond1 {
    phHH = HH;
    phHL = HL;
    phHH1 = phHH[1];
    phHL1 = phHL[1];
    phHH2 = phHH1[1];
    phHL2 = phHL1[1];
    phHH3 = phHH2[1];
    phHL3 = phHL2[1];
    phHH4 = phHH3[1];
    phHL4 = phHL3[1];
    plLH = plLH[1];
    plLL = plLL[1];
    plLH1 = plLH1[1];
    plLL1 = plLL1[1];
    plLH2 = plLH2[1];
    plLL2 = plLL2[1];
    plLH3 = plLH3[1];
    plLL3 = plLL3[1];
    plLH4 = plLH4[1];
    plLL4 = plLL4[1];
} else
if cond2 {
    plLH = LH;
    plLL = LL;
    plLH1 = plLH[1];
    plLL1 = plLL[1];
    plLH2 = plLH1[1];
    plLL2 = plLL1[1];
    plLH3 = plLH2[1];
    plLL3 = plLL2[1];
    plLH4 = plLH3[1];
    plLL4 = plLL3[1];
    phHH =  phHH[1];
    phHL =  phHL[1];
    phHH1 = phHH1[1];
    phHL1 = phHL1[1];
    phHH2 = phHH2[1];
    phHL2 = phHL2[1];
    phHH3 = phHH3[1];
    phHL3 = phHL3[1];
    phHH4 = phHH4[1];
    phHL4 = phHL4[1];
} else {
    phHH = if phHH[1] == 0 then hiHaBod else phHH[1];
    phHL = if phHL[1] == 0 then hiHaBod else phHL[1];
    phHH1 = if phHH1[1] == 0 then hiHaBod else phHH1[1];
    phHL1 = if phHL1[1] == 0 then hiHaBod else phHL1[1];
    phHH2 = if phHH2[1] == 0 then hiHaBod else phHH2[1];
    phHL2 = if phHL2[1] == 0 then hiHaBod else phHL2[1];
    phHH3 = if phHH3[1] == 0 then hiHaBod else phHH3[1];
    phHL3 = if phHL3[1] == 0 then hiHaBod else phHL3[1];
    phHH4 = if phHH4[1] == 0 then hiHaBod else phHH4[1];
    phHL4 = if phHL4[1] == 0 then hiHaBod else phHL4[1];
    plLH = if plLH[1] == 0 then loHaBod else plLH[1];
    plLL = if plLL[1] == 0 then loHaBod else plLL[1];
    plLH1 = if plLH1[1] == 0 then loHaBod else plLH1[1];
    plLL1 = if plLL1[1] == 0 then loHaBod else plLL1[1];
    plLH2 = if plLH2[1] == 0 then loHaBod else plLH2[1];
    plLL2 = if plLL2[1] == 0 then loHaBod else plLL2[1];
    plLH3 = if plLH3[1] == 0 then loHaBod else plLH3[1];
    plLL3 = if plLL3[1] == 0 then loHaBod else plLL3[1];
    plLH4 = if plLH4[1] == 0 then loHaBod else plLH4[1];
    plLL4 = if plLL4[1] == 0 then loHaBod else plLL4[1];
}
def BullColor  = nz(close,close[lookRight])>phHH;
def BearColor  = nz(close,close[lookRight])<plLL;
def BullColor1 = nz(close,close[lookRight])>phHH1;
def BearColor1 = nz(close,close[lookRight])<plLL1;
def BullColor2 = nz(close,close[lookRight])>phHH2;
def BearColor2 = nz(close,close[lookRight])<plLL2;
def BullColor3 = nz(close,close[lookRight])>phHH3;
def BearColor3 = nz(close,close[lookRight])<plLL3;
def BullColor4 = nz(close,close[lookRight])>phHH4;
def BearColor4 = nz(close,close[lookRight])<plLL4;

plot hhLine =  if extend then na else phHH;
plot hlLine =  if extend then na else phHL;
def  counthhLine = if hhLine==hhLine[1] then counthhLine[1] + 1 else 0;
def  counthlLine = if hlLine==hlLine[1] then counthlLine[1] + 1 else 0;
plot exthhLine = if counthhLine<=lookRight then phHH[lookRight] else na;
plot exthlLine = if counthlLine<=lookRight then phHL[lookRight] else na;
exthhLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
exthhLine.AssignValueColor(if close>exthhLine then GlobalColor("bullBorder") else GlobalColor("bearBorder"));
exthlLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
exthlLine.AssignValueColor(if close>exthhLine then GlobalColor("bullBorder") else GlobalColor("bearBorder"));
hhLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
hhLine.AssignValueColor(if BullColor then GlobalColor("bullBorder") else GlobalColor("bearBorder"));
hlLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
hlLine.AssignValueColor(if BullColor then GlobalColor("bullBorder") else GlobalColor("bearBorder"));


plot llLine =  if extend then na else plLL ;
plot lhLine =  if extend then na else plLH;
def  countllLine = if llLine==llLine[1] then countllLine[1] + 1 else 0;
def  countlhLine = if lhLine==lhLine[1] then countlhLine[1] + 1 else 0;
plot extllLine = if countllLine<=lookRight then plLL[lookRight] else na;
plot extlhLine = if countlhLine<=lookRight then plLH[lookRight] else na;
extllLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
extllLine.AssignValueColor(if close<extllLine then GlobalColor("bearBorder") else GlobalColor("bullBorder"));
extlhLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
extlhLine.AssignValueColor(if close<extllLine then GlobalColor("bearBorder") else GlobalColor("bullBorder"));
llLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
llLine.AssignValueColor(if BearColor then GlobalColor("bearBorder") else GlobalColor("bullBorder"));
lhLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
lhLine.AssignValueColor(if BearColor then GlobalColor("bearBorder") else GlobalColor("bullBorder"));

plot hhLine1 =  if extend or NumberOfPivots<2 then na else phHH1;
plot hlLine1 =  if extend or NumberOfPivots<2 then na else phHL1 ;
def counthhLine1 = if hhLine1==hhLine1[1] then counthhLine1[1] + 1 else 0;
def counthlLine1 = if hlLine1==hlLine1[1] then counthlLine1[1] + 1 else 0;
plot exthhLine1  = if counthhLine1<=lookRight then phHH1[lookRight] else na;
plot exthlLine1  = if counthlLine1<=lookRight then phHL1[lookRight] else na;
exthhLine1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
exthhLine1.AssignValueColor(if close>exthhLine1 then GlobalColor("bullBorder") else GlobalColor("bearBorder"));
exthlLine1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
exthlLine1.AssignValueColor(if close>exthhLine1 then GlobalColor("bullBorder") else GlobalColor("bearBorder"));
hhLine1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
hhLine1.AssignValueColor(if BullColor1 then GlobalColor("bullBorder") else GlobalColor("bearBorder"));
hlLine1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
hlLine1.AssignValueColor(if BullColor1 then GlobalColor("bullBorder") else GlobalColor("bearBorder"));

plot llLine1 =  if extend or NumberOfPivots<2 then na else plLL1;
plot lhLine1 =  if extend or NumberOfPivots<2 then na else plLH1;
def  countllLine1 = if llLine1==llLine1[1] then countllLine1[1] + 1 else 0;
def  countlhLine1 = if lhLine1==lhLine1[1] then countlhLine1[1] + 1 else 0;
plot extllLine1 = if countllLine1<=lookRight then plLL1[lookRight] else na;
plot extlhLine1 = if countlhLine1<=lookRight then plLH1[lookRight] else na;
extllLine1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
extllLine1.AssignValueColor(if close<extllLine1 then GlobalColor("bearBorder") else GlobalColor("bullBorder"));
extlhLine1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
extlhLine1.AssignValueColor(if close<extllLine1 then GlobalColor("bearBorder") else GlobalColor("bullBorder"));
llLine1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
llLine1.AssignValueColor(if BearColor1 then GlobalColor("bearBorder") else GlobalColor("bullBorder"));
lhLine1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
lhLine1.AssignValueColor(if BearColor1 then GlobalColor("bearBorder") else GlobalColor("bullBorder"));

plot hhLine2 =  if extend or NumberOfPivots<3 then na else phHH2;
plot hlLine2 =  if extend or NumberOfPivots<3 then na else phHL2;
def counthhLine2 = if hhLine2==hhLine2[1] then counthhLine2[1] + 1 else 0;
def counthlLine2 = if hlLine2==hlLine2[1] then counthlLine2[1] + 1 else 0;
plot exthhLine2  = if counthhLine2<=lookRight then phHH2[lookRight] else na;
plot exthlLine2  = if counthlLine2<=lookRight then phHL2[lookRight] else na;
exthhLine2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
exthhLine2.AssignValueColor(if close>exthhLine2 then GlobalColor("bullBorder") else GlobalColor("bearBorder"));
exthlLine2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
exthlLine2.AssignValueColor(if close>exthhLine2 then GlobalColor("bullBorder") else GlobalColor("bearBorder"));
hhLine2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
hhLine2.AssignValueColor(if BullColor2 then GlobalColor("bullBorder") else GlobalColor("bearBorder"));
hlLine2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
hlLine2.AssignValueColor(if BullColor2 then GlobalColor("bullBorder") else GlobalColor("bearBorder"));

plot llLine2 =  if extend or NumberOfPivots<3 then na else plLL2;
plot lhLine2 =  if extend or NumberOfPivots<3 then na else plLH2;
def  countllLine2 = if llLine2==llLine2[1] then countllLine2[1] + 1 else 0;
def  countlhLine2 = if lhLine2==lhLine2[1] then countlhLine2[1] + 1 else 0;
plot extllLine2 = if countllLine2<=lookRight then plLL2[lookRight] else na;
plot extlhLine2 = if countlhLine2<=lookRight then plLH2[lookRight] else na;
extllLine2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
extllLine2.AssignValueColor(if close<extllLine2 then GlobalColor("bearBorder") else GlobalColor("bullBorder"));
extlhLine2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
extlhLine2.AssignValueColor(if close<extllLine2 then GlobalColor("bearBorder") else GlobalColor("bullBorder"));
llLine2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
llLine2.AssignValueColor(if BearColor2 then GlobalColor("bearBorder") else GlobalColor("bullBorder"));
lhLine2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
lhLine2.AssignValueColor(if BearColor2 then GlobalColor("bearBorder") else GlobalColor("bullBorder"));

plot hhLine3 =  if extend or NumberOfPivots<4 then na else phHH3;
plot hlLine3 =  if extend or NumberOfPivots<4 then na else phHL3;
def counthhLine3 = if hhLine3==hhLine3[1] then counthhLine3[1] + 1 else 0;
def counthlLine3 = if hlLine3==hlLine3[1] then counthlLine3[1] + 1 else 0;
plot exthhLine3  = if counthhLine3<=lookRight then phHH3[lookRight] else na;
plot exthlLine3  = if counthlLine3<=lookRight then phHL3[lookRight] else na;
exthhLine3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
exthhLine3.AssignValueColor(if close>exthhLine3 then GlobalColor("bullBorder") else GlobalColor("bearBorder"));
exthlLine3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
exthlLine3.AssignValueColor(if close>exthhLine3 then GlobalColor("bullBorder") else GlobalColor("bearBorder"));
hhLine3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
hhLine3.AssignValueColor(if BullColor3 then GlobalColor("bullBorder") else GlobalColor("bearBorder"));
hlLine3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
hlLine3.AssignValueColor(if BullColor3 then GlobalColor("bullBorder") else GlobalColor("bearBorder"));

plot llLine3 =  if extend or NumberOfPivots<4 then na else plLL3;
plot lhLine3 =  if extend or NumberOfPivots<4 then na else plLH3;
def  countllLine3 = if llLine3==llLine3[1] then countllLine3[1] + 1 else 0;
def  countlhLine3 = if lhLine3==lhLine3[1] then countlhLine3[1] + 1 else 0;
plot extllLine3 = if countllLine3<=lookRight then plLL3[lookRight] else na;
plot extlhLine3 = if countlhLine3<=lookRight then plLH3[lookRight] else na;
extllLine3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
extllLine3.AssignValueColor(if close<extllLine3 then GlobalColor("bearBorder") else GlobalColor("bullBorder"));
extlhLine3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
extlhLine3.AssignValueColor(if close<extllLine3 then GlobalColor("bearBorder") else GlobalColor("bullBorder"));
llLine3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
llLine3.AssignValueColor(if BearColor3 then GlobalColor("bearBorder") else GlobalColor("bullBorder"));
lhLine3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
lhLine3.AssignValueColor(if BearColor3 then GlobalColor("bearBorder") else GlobalColor("bullBorder"));

plot hhLine4 =  if extend or NumberOfPivots<5 then na else phHH4;
plot hlLine4 =  if extend or NumberOfPivots<5 then na else phHL4;
def counthhLine4 = if hhLine4==hhLine4[1] then counthhLine4[1] + 1 else 0;
def counthlLine4 = if hlLine4==hlLine4[1] then counthlLine4[1] + 1 else 0;
plot exthhLine4  = if counthhLine4<=lookRight then phHH4[lookRight] else na;
plot exthlLine4  = if counthlLine4<=lookRight then phHL4[lookRight] else na;
exthhLine4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
exthhLine4.AssignValueColor(if close>exthhLine4 then GlobalColor("bullBorder") else GlobalColor("bearBorder"));
exthlLine4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
exthlLine4.AssignValueColor(if close>exthhLine4 then GlobalColor("bullBorder") else GlobalColor("bearBorder"));
hhLine4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
hhLine4.AssignValueColor(if BullColor4 then GlobalColor("bullBorder") else GlobalColor("bearBorder"));
hlLine4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
hlLine4.AssignValueColor(if BullColor4 then GlobalColor("bullBorder") else GlobalColor("bearBorder"));

plot llLine4 =  if extend or NumberOfPivots<5 then na else plLL4;
plot lhLine4 =  if extend or NumberOfPivots<5 then na else plLH4;
def  countllLine4 = if llLine4==llLine4[1] then countllLine4[1] + 1 else 0;
def  countlhLine4 = if lhLine4==lhLine4[1] then countlhLine4[1] + 1 else 0;
plot extllLine4 = if countllLine4<=lookRight then plLL4[lookRight] else na;
plot extlhLine4 = if countlhLine4<=lookRight then plLH4[lookRight] else na;
extllLine4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
extllLine4.AssignValueColor(if close<extllLine4 then GlobalColor("bearBorder") else GlobalColor("bullBorder"));
extlhLine4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
extlhLine4.AssignValueColor(if close<extllLine4 then GlobalColor("bearBorder") else GlobalColor("bullBorder"));
llLine4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
llLine4.AssignValueColor(if BearColor4 then GlobalColor("bearBorder") else GlobalColor("bullBorder"));
lhLine4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
lhLine4.AssignValueColor(if BearColor4 then GlobalColor("bearBorder") else GlobalColor("bullBorder"));

#--
AddCloud(if hhLine==hhLine[1] and BullColor then hhLine else na, hlLine, GlobalColor("bearCloud"));
AddCloud(if hhLine==hhLine[1] and !BullColor then hhLine else na, hlLine, GlobalColor("bullCloud"));
AddCloud(if hhLine1==hhLine1[1] and BullColor1 then hhLine1 else na, hlLine1, GlobalColor("bearCloud"));
AddCloud(if hhLine1==hhLine1[1] and !BullColor1 then hhLine1 else na, hlLine1, GlobalColor("bullCloud"));
AddCloud(if hhLine2==hhLine2[1] and BullColor2 then hhLine2 else na, hlLine2, GlobalColor("bearCloud"));
AddCloud(if hhLine2==hhLine2[1] and !BullColor2 then hhLine2 else na, hlLine2, GlobalColor("bullCloud"));
AddCloud(if hhLine3==hhLine3[1] and BullColor3 then hhLine3 else na, hlLine3, GlobalColor("bearCloud"));
AddCloud(if hhLine3==hhLine3[1] and !BullColor3 then hhLine3 else na, hlLine3, GlobalColor("bullCloud"));
AddCloud(if hhLine4==hhLine4[1] and BullColor4 then hhLine4 else na, hlLine4, GlobalColor("bearCloud"));
AddCloud(if hhLine4==hhLine4[1] and !BullColor4 then hhLine4 else na, hlLine4, GlobalColor("bullCloud"));

AddCloud(if llLine==llLine[1] and BearColor then lhLine else na, llLine, GlobalColor("bullCloud"));
AddCloud(if llLine==llLine[1] and !BearColor then lhLine else na, llLine, GlobalColor("bearCloud"));
AddCloud(if llLine1==llLine1[1] and BearColor1 then lhLine1 else na, llLine1, GlobalColor("bullCloud"));
AddCloud(if llLine1==llLine1[1] and !BearColor1 then lhLine1 else na, llLine1, GlobalColor("bearCloud"));
AddCloud(if llLine2==llLine2[1] and BearColor2 then lhLine2 else na, llLine2, GlobalColor("bullCloud"));
AddCloud(if llLine2==llLine2[1] and !BearColor2 then lhLine2 else na, llLine2, GlobalColor("bearCloud"));
AddCloud(if llLine3==llLine3[1] and BearColor3 then lhLine3 else na, llLine3, GlobalColor("bullCloud"));
AddCloud(if llLine3==llLine3[1] and !BearColor3 then lhLine3 else na, llLine3, GlobalColor("bearCloud"));
AddCloud(if llLine4==llLine4[1] and BearColor3 then lhLine3 else na, llLine3, GlobalColor("bullCloud"));
AddCloud(if llLine4==llLine4[1] and !BearColor3 then lhLine3 else na, llLine3, GlobalColor("bearCloud"));
#--- Ext
AddCloud(if exthhLine==exthhLine[1] and close>exthhLine then exthhLine else na, exthlLine, GlobalColor("bearCloud"));
AddCloud(if exthhLine==exthhLine[1] and close<=exthhLine then exthhLine else na, exthlLine, GlobalColor("bullCloud"));
AddCloud(if exthhLine1==exthhLine1[1] and close>exthhLine1 then exthhLine1 else na, exthlLine1, GlobalColor("bearCloud"));
AddCloud(if exthhLine1==exthhLine1[1] and close<=exthhLine1 then exthhLine1 else na, exthlLine1, GlobalColor("bullCloud"));
AddCloud(if exthhLine2==exthhLine2[1] and close>exthhLine2 then exthhLine2 else na, exthlLine2, GlobalColor("bearCloud"));
AddCloud(if exthhLine2==exthhLine2[1] and close<=exthhLine2 then exthhLine2 else na, exthlLine2, GlobalColor("bullCloud"));
AddCloud(if exthhLine3==exthhLine3[1] and close>exthhLine3 then exthhLine3 else na, exthlLine3, GlobalColor("bearCloud"));
AddCloud(if exthhLine3==exthhLine3[1] and close<=exthhLine3 then exthhLine3 else na, exthlLine3, GlobalColor("bullCloud"));
AddCloud(if exthhLine4==exthhLine4[1] and close>exthhLine4 then exthhLine4 else na, exthlLine4, GlobalColor("bearCloud"));
AddCloud(if exthhLine4==exthhLine4[1] and close<=exthhLine4 then exthhLine4 else na, exthlLine4, GlobalColor("bullCloud"));

AddCloud(if extllLine==extllLine[1] and close<extllLine then extlhLine else na, extllLine, GlobalColor("bullCloud"));
AddCloud(if extllLine==extllLine[1] and close>=extllLine then extlhLine else na, extllLine, GlobalColor("bearCloud"));
AddCloud(if extllLine1==extllLine1[1] and close<extllLine1 then extlhLine1 else na, extllLine1, GlobalColor("bullCloud"));
AddCloud(if extllLine1==extllLine1[1] and close>=extllLine1 then extlhLine1 else na, extllLine1, GlobalColor("bearCloud"));
AddCloud(if extllLine2==extllLine2[1] and close<extllLine2 then extlhLine2 else na, extllLine2, GlobalColor("bullCloud"));
AddCloud(if extllLine2==extllLine2[1] and close>=extllLine2 then extlhLine2 else na, extllLine2, GlobalColor("bearCloud"));
AddCloud(if extllLine3==extllLine3[1] and close<extllLine3 then extlhLine3 else na, extllLine3, GlobalColor("bullCloud"));
AddCloud(if extllLine3==extllLine3[1] and close>=extllLine3 then extlhLine3 else na, extllLine3, GlobalColor("bearCloud"));
AddCloud(if extllLine4==extllLine4[1] and close<extllLine4 then extlhLine4 else na, extllLine4, GlobalColor("bullCloud"));
AddCloud(if extllLine4==extllLine4[1] and close>=extllLine4 then extlhLine4 else na, extllLine4, GlobalColor("bearCloud"));

#--- Break
AddChartBubble(ShowBreakUp and srcHigh crosses above hhLine and !BullColor[lookRight], low, "Break", Color.GREEN, no);
AddChartBubble(ShowBreakDn and srcLow crosses below llLine and !BearColor[lookRight], high, "Break", Color.RED, yes);

#--- END CODE
 

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

rPzDdHM.png


I converted partially. pls check.

CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © Bjorgum
#indicator ('Bjorgum Key Levels', 'Bj Key Levels', ount=500)
# partial conversion by Sam4Cok@Samer800 - 01 /2023
input lookLeft    = 20;            #  "Look Left"  
input lookRight   = 15;            #  "Look Right"  
input NumberOfPivots = 3;          #  "Number of Pivots"
input atrLength      = 30;         # "ATR Length"
input ZoneWidth      = 0.5;        # "Zone Width (ATR)"
input MaxZonePercent = 5;          #  "Max Zone Percent"
input SourceForPivots = {default "HeikinAshi", "High/Low Body", "High/Low"}; # "Source For Pivots"
input ExtendRight     = no;        #   "Extend Right"
input PivotHighs      = yes;       #  "Detect Pivot Highs"
input PivotLows       = yes;       #  "Detect Pivot Lows"
input ShowBreakUp     = yes;
input ShowBreakDn     = yes;

def na = Double.NaN;
def extend = if ExtendRight then isNaN(close[lookRight]) else isNaN(close);
#--- Color
DefineGlobalColor("bullBorder", CreateColor(7,71,123));
DefineGlobalColor("bearBorder", CreateColor(118,106,0));
DefineGlobalColor("bearCloud", CreateColor(3,29,51));
DefineGlobalColor("bullCloud", CreateColor(42,37,0));

script nz {
    input data  = close;
    input repl  = 0;
    def ret_val = if !isNaN(data) then data else repl;
    plot return = ret_val;
}
script fixnan {
    input source = close;
    def fix = if !IsNaN(source) then source else fix[1];
    plot result = fix;
}
#valuewhen (cond, source, occurrence) =>
script valuewhen {
    input cond = 0;
    input source = 0;
    input occurrence = 0;
    def n = occurrence + 1;
# start at 0 so it looks at current bar
    def offset2 = fold j = 0 to 400
    with p
    while p < n + 1
    do p + ( if p == n then j - n else if GetValue(cond, j) then 1 else 0 );
# def bnz = bn - offset2 + 1;
    plot price = GetValue(source, offset2 - 1);
}

script FindPivots {
    input dat = close; # default data or study being evaluated
    input HL  = 0;    # default high or low pivot designation, -1 low, +1 high
    input lbL  = 5;    # default Pivot Lookback Left
    input lbR  = 1;    # default Pivot Lookback Right
    ##############
    def _nan;    # used for non-number returns
    def _BN;     # the current barnumber
    def _VStop;  # confirms that the lookforward period continues the pivot trend
    def _V;      # the Value at the actual pivot point
    ##############
    _BN  = BarNumber();
    _nan = Double.NaN;
    _VStop = if !IsNaN(dat) and lbR > 0 and lbL > 0 then
                fold a = 1 to lbR + 1 with b=1 while b do
                    if HL > 0 then dat > GetValue(dat, -a) else dat < GetValue(dat, -a) else _nan;
    if (HL > 0) {
        _V = if _BN > lbL and dat == Highest(dat, lbL + 1) and _VStop
            then dat else _nan;
    } else {
        _V = if _BN > lbL and dat == Lowest(dat, lbL + 1) and _VStop
            then dat else _nan;
    }
    plot result = if !IsNaN(_V) and _VStop then _V else _nan;
}
def haSrc   = SourceForPivots == SourceForPivots."HeikinAshi";
def hiLoSrc = SourceForPivots == SourceForPivots."High/Low";
#// ---> Functional Declarations <---- //

def nATR    = ATR(LENGTH = atrLength);
#def perMax = close * 0.02;
#def min    = Min(perMax, atr * 0.3);

#// ----> Variable Calculations <----- //
def close_ = (open + high  +  low  + close) / 4;
def open_;
open_  = if IsNaN(open_[1]) then (open + close) / 2 else
                 (nz(open_[1],open[1]) + nz(close_[1],close[1]))   / 2;
def hiHaBod = Max(close_, open_);
def loHaBod = Min(close_, open_);
def hiBod   = Max(close, open);
def loBod   = Min(close, open);
def srcHigh = if haSrc then hiHaBod else if hiLoSrc then high else hiBod;
def srcLow  = if haSrc then loHaBod else if hiLoSrc then low  else loBod;
def highest = close == Highest(close, lookRight);
def lowest  = close == Lowest (close, lookRight);
def pivot_high = FindPivots(srcHigh, 1, lookLeft, lookRight);
def pivot_low  = FindPivots(srcLow, -1, lookLeft, lookRight);
def ph = if !isNaN(pivot_high) then pivot_high else ph[1];
def pl = if !isNaN(pivot_low) then pivot_low else pl[1];
def perc = close * (MaxZonePercent / 100);
def minATR = Min(nATR * ZoneWidth, perc);
def band = minATR / 2;
def HH   = ph + band;
def HL   = ph - band;
def LH   = pl + band;
def LL   = pl - band;

def coDiff = close - open;
def barRange = AbsValue(high - low);
def phHH;
def phHL;
def plLH;
def plLL;
def phHH1;
def phHL1;
def plLH1;
def plLL1;
def phHH2;
def phHL2;
def plLH2;
def plLL2;
def phHH3;
def phHL3;
def plLH3;
def plLL3;
def phHH4;
def phHL4;
def plLH4;
def plLL4;
def cond1 = !isNaN(pivot_high) and pivot_high!=0 and !IsNaN(close[1]);
def cond2 = !isNaN(pivot_low)  and pivot_low !=0 and !IsNaN(close[1]);
if cond1 {
    phHH = HH;
    phHL = HL;
    phHH1 = phHH[1];
    phHL1 = phHL[1];
    phHH2 = phHH1[1];
    phHL2 = phHL1[1];
    phHH3 = phHH2[1];
    phHL3 = phHL2[1];
    phHH4 = phHH3[1];
    phHL4 = phHL3[1];
    plLH = plLH[1];
    plLL = plLL[1];
    plLH1 = plLH1[1];
    plLL1 = plLL1[1];
    plLH2 = plLH2[1];
    plLL2 = plLL2[1];
    plLH3 = plLH3[1];
    plLL3 = plLL3[1];
    plLH4 = plLH4[1];
    plLL4 = plLL4[1];
} else
if cond2 {
    plLH = LH;
    plLL = LL;
    plLH1 = plLH[1];
    plLL1 = plLL[1];
    plLH2 = plLH1[1];
    plLL2 = plLL1[1];
    plLH3 = plLH2[1];
    plLL3 = plLL2[1];
    plLH4 = plLH3[1];
    plLL4 = plLL3[1];
    phHH =  phHH[1];
    phHL =  phHL[1];
    phHH1 = phHH1[1];
    phHL1 = phHL1[1];
    phHH2 = phHH2[1];
    phHL2 = phHL2[1];
    phHH3 = phHH3[1];
    phHL3 = phHL3[1];
    phHH4 = phHH4[1];
    phHL4 = phHL4[1];
} else {
    phHH = if phHH[1] == 0 then hiHaBod else phHH[1];
    phHL = if phHL[1] == 0 then hiHaBod else phHL[1];
    phHH1 = if phHH1[1] == 0 then hiHaBod else phHH1[1];
    phHL1 = if phHL1[1] == 0 then hiHaBod else phHL1[1];
    phHH2 = if phHH2[1] == 0 then hiHaBod else phHH2[1];
    phHL2 = if phHL2[1] == 0 then hiHaBod else phHL2[1];
    phHH3 = if phHH3[1] == 0 then hiHaBod else phHH3[1];
    phHL3 = if phHL3[1] == 0 then hiHaBod else phHL3[1];
    phHH4 = if phHH4[1] == 0 then hiHaBod else phHH4[1];
    phHL4 = if phHL4[1] == 0 then hiHaBod else phHL4[1];
    plLH = if plLH[1] == 0 then loHaBod else plLH[1];
    plLL = if plLL[1] == 0 then loHaBod else plLL[1];
    plLH1 = if plLH1[1] == 0 then loHaBod else plLH1[1];
    plLL1 = if plLL1[1] == 0 then loHaBod else plLL1[1];
    plLH2 = if plLH2[1] == 0 then loHaBod else plLH2[1];
    plLL2 = if plLL2[1] == 0 then loHaBod else plLL2[1];
    plLH3 = if plLH3[1] == 0 then loHaBod else plLH3[1];
    plLL3 = if plLL3[1] == 0 then loHaBod else plLL3[1];
    plLH4 = if plLH4[1] == 0 then loHaBod else plLH4[1];
    plLL4 = if plLL4[1] == 0 then loHaBod else plLL4[1];
}
def BullColor  = nz(close,close[lookRight])>phHH;
def BearColor  = nz(close,close[lookRight])<plLL;
def BullColor1 = nz(close,close[lookRight])>phHH1;
def BearColor1 = nz(close,close[lookRight])<plLL1;
def BullColor2 = nz(close,close[lookRight])>phHH2;
def BearColor2 = nz(close,close[lookRight])<plLL2;
def BullColor3 = nz(close,close[lookRight])>phHH3;
def BearColor3 = nz(close,close[lookRight])<plLL3;
def BullColor4 = nz(close,close[lookRight])>phHH4;
def BearColor4 = nz(close,close[lookRight])<plLL4;

plot hhLine =  if extend then na else phHH;
plot hlLine =  if extend then na else phHL;
def  counthhLine = if hhLine==hhLine[1] then counthhLine[1] + 1 else 0;
def  counthlLine = if hlLine==hlLine[1] then counthlLine[1] + 1 else 0;
plot exthhLine = if counthhLine<=lookRight then phHH[lookRight] else na;
plot exthlLine = if counthlLine<=lookRight then phHL[lookRight] else na;
exthhLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
exthhLine.AssignValueColor(if close>exthhLine then GlobalColor("bullBorder") else GlobalColor("bearBorder"));
exthlLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
exthlLine.AssignValueColor(if close>exthhLine then GlobalColor("bullBorder") else GlobalColor("bearBorder"));
hhLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
hhLine.AssignValueColor(if BullColor then GlobalColor("bullBorder") else GlobalColor("bearBorder"));
hlLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
hlLine.AssignValueColor(if BullColor then GlobalColor("bullBorder") else GlobalColor("bearBorder"));


plot llLine =  if extend then na else plLL ;
plot lhLine =  if extend then na else plLH;
def  countllLine = if llLine==llLine[1] then countllLine[1] + 1 else 0;
def  countlhLine = if lhLine==lhLine[1] then countlhLine[1] + 1 else 0;
plot extllLine = if countllLine<=lookRight then plLL[lookRight] else na;
plot extlhLine = if countlhLine<=lookRight then plLH[lookRight] else na;
extllLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
extllLine.AssignValueColor(if close<extllLine then GlobalColor("bearBorder") else GlobalColor("bullBorder"));
extlhLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
extlhLine.AssignValueColor(if close<extllLine then GlobalColor("bearBorder") else GlobalColor("bullBorder"));
llLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
llLine.AssignValueColor(if BearColor then GlobalColor("bearBorder") else GlobalColor("bullBorder"));
lhLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
lhLine.AssignValueColor(if BearColor then GlobalColor("bearBorder") else GlobalColor("bullBorder"));

plot hhLine1 =  if extend or NumberOfPivots<2 then na else phHH1;
plot hlLine1 =  if extend or NumberOfPivots<2 then na else phHL1 ;
def counthhLine1 = if hhLine1==hhLine1[1] then counthhLine1[1] + 1 else 0;
def counthlLine1 = if hlLine1==hlLine1[1] then counthlLine1[1] + 1 else 0;
plot exthhLine1  = if counthhLine1<=lookRight then phHH1[lookRight] else na;
plot exthlLine1  = if counthlLine1<=lookRight then phHL1[lookRight] else na;
exthhLine1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
exthhLine1.AssignValueColor(if close>exthhLine1 then GlobalColor("bullBorder") else GlobalColor("bearBorder"));
exthlLine1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
exthlLine1.AssignValueColor(if close>exthhLine1 then GlobalColor("bullBorder") else GlobalColor("bearBorder"));
hhLine1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
hhLine1.AssignValueColor(if BullColor1 then GlobalColor("bullBorder") else GlobalColor("bearBorder"));
hlLine1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
hlLine1.AssignValueColor(if BullColor1 then GlobalColor("bullBorder") else GlobalColor("bearBorder"));

plot llLine1 =  if extend or NumberOfPivots<2 then na else plLL1;
plot lhLine1 =  if extend or NumberOfPivots<2 then na else plLH1;
def  countllLine1 = if llLine1==llLine1[1] then countllLine1[1] + 1 else 0;
def  countlhLine1 = if lhLine1==lhLine1[1] then countlhLine1[1] + 1 else 0;
plot extllLine1 = if countllLine1<=lookRight then plLL1[lookRight] else na;
plot extlhLine1 = if countlhLine1<=lookRight then plLH1[lookRight] else na;
extllLine1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
extllLine1.AssignValueColor(if close<extllLine1 then GlobalColor("bearBorder") else GlobalColor("bullBorder"));
extlhLine1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
extlhLine1.AssignValueColor(if close<extllLine1 then GlobalColor("bearBorder") else GlobalColor("bullBorder"));
llLine1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
llLine1.AssignValueColor(if BearColor1 then GlobalColor("bearBorder") else GlobalColor("bullBorder"));
lhLine1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
lhLine1.AssignValueColor(if BearColor1 then GlobalColor("bearBorder") else GlobalColor("bullBorder"));

plot hhLine2 =  if extend or NumberOfPivots<3 then na else phHH2;
plot hlLine2 =  if extend or NumberOfPivots<3 then na else phHL2;
def counthhLine2 = if hhLine2==hhLine2[1] then counthhLine2[1] + 1 else 0;
def counthlLine2 = if hlLine2==hlLine2[1] then counthlLine2[1] + 1 else 0;
plot exthhLine2  = if counthhLine2<=lookRight then phHH2[lookRight] else na;
plot exthlLine2  = if counthlLine2<=lookRight then phHL2[lookRight] else na;
exthhLine2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
exthhLine2.AssignValueColor(if close>exthhLine2 then GlobalColor("bullBorder") else GlobalColor("bearBorder"));
exthlLine2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
exthlLine2.AssignValueColor(if close>exthhLine2 then GlobalColor("bullBorder") else GlobalColor("bearBorder"));
hhLine2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
hhLine2.AssignValueColor(if BullColor2 then GlobalColor("bullBorder") else GlobalColor("bearBorder"));
hlLine2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
hlLine2.AssignValueColor(if BullColor2 then GlobalColor("bullBorder") else GlobalColor("bearBorder"));

plot llLine2 =  if extend or NumberOfPivots<3 then na else plLL2;
plot lhLine2 =  if extend or NumberOfPivots<3 then na else plLH2;
def  countllLine2 = if llLine2==llLine2[1] then countllLine2[1] + 1 else 0;
def  countlhLine2 = if lhLine2==lhLine2[1] then countlhLine2[1] + 1 else 0;
plot extllLine2 = if countllLine2<=lookRight then plLL2[lookRight] else na;
plot extlhLine2 = if countlhLine2<=lookRight then plLH2[lookRight] else na;
extllLine2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
extllLine2.AssignValueColor(if close<extllLine2 then GlobalColor("bearBorder") else GlobalColor("bullBorder"));
extlhLine2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
extlhLine2.AssignValueColor(if close<extllLine2 then GlobalColor("bearBorder") else GlobalColor("bullBorder"));
llLine2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
llLine2.AssignValueColor(if BearColor2 then GlobalColor("bearBorder") else GlobalColor("bullBorder"));
lhLine2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
lhLine2.AssignValueColor(if BearColor2 then GlobalColor("bearBorder") else GlobalColor("bullBorder"));

plot hhLine3 =  if extend or NumberOfPivots<4 then na else phHH3;
plot hlLine3 =  if extend or NumberOfPivots<4 then na else phHL3;
def counthhLine3 = if hhLine3==hhLine3[1] then counthhLine3[1] + 1 else 0;
def counthlLine3 = if hlLine3==hlLine3[1] then counthlLine3[1] + 1 else 0;
plot exthhLine3  = if counthhLine3<=lookRight then phHH3[lookRight] else na;
plot exthlLine3  = if counthlLine3<=lookRight then phHL3[lookRight] else na;
exthhLine3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
exthhLine3.AssignValueColor(if close>exthhLine3 then GlobalColor("bullBorder") else GlobalColor("bearBorder"));
exthlLine3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
exthlLine3.AssignValueColor(if close>exthhLine3 then GlobalColor("bullBorder") else GlobalColor("bearBorder"));
hhLine3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
hhLine3.AssignValueColor(if BullColor3 then GlobalColor("bullBorder") else GlobalColor("bearBorder"));
hlLine3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
hlLine3.AssignValueColor(if BullColor3 then GlobalColor("bullBorder") else GlobalColor("bearBorder"));

plot llLine3 =  if extend or NumberOfPivots<4 then na else plLL3;
plot lhLine3 =  if extend or NumberOfPivots<4 then na else plLH3;
def  countllLine3 = if llLine3==llLine3[1] then countllLine3[1] + 1 else 0;
def  countlhLine3 = if lhLine3==lhLine3[1] then countlhLine3[1] + 1 else 0;
plot extllLine3 = if countllLine3<=lookRight then plLL3[lookRight] else na;
plot extlhLine3 = if countlhLine3<=lookRight then plLH3[lookRight] else na;
extllLine3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
extllLine3.AssignValueColor(if close<extllLine3 then GlobalColor("bearBorder") else GlobalColor("bullBorder"));
extlhLine3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
extlhLine3.AssignValueColor(if close<extllLine3 then GlobalColor("bearBorder") else GlobalColor("bullBorder"));
llLine3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
llLine3.AssignValueColor(if BearColor3 then GlobalColor("bearBorder") else GlobalColor("bullBorder"));
lhLine3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
lhLine3.AssignValueColor(if BearColor3 then GlobalColor("bearBorder") else GlobalColor("bullBorder"));

plot hhLine4 =  if extend or NumberOfPivots<5 then na else phHH4;
plot hlLine4 =  if extend or NumberOfPivots<5 then na else phHL4;
def counthhLine4 = if hhLine4==hhLine4[1] then counthhLine4[1] + 1 else 0;
def counthlLine4 = if hlLine4==hlLine4[1] then counthlLine4[1] + 1 else 0;
plot exthhLine4  = if counthhLine4<=lookRight then phHH4[lookRight] else na;
plot exthlLine4  = if counthlLine4<=lookRight then phHL4[lookRight] else na;
exthhLine4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
exthhLine4.AssignValueColor(if close>exthhLine4 then GlobalColor("bullBorder") else GlobalColor("bearBorder"));
exthlLine4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
exthlLine4.AssignValueColor(if close>exthhLine4 then GlobalColor("bullBorder") else GlobalColor("bearBorder"));
hhLine4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
hhLine4.AssignValueColor(if BullColor4 then GlobalColor("bullBorder") else GlobalColor("bearBorder"));
hlLine4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
hlLine4.AssignValueColor(if BullColor4 then GlobalColor("bullBorder") else GlobalColor("bearBorder"));

plot llLine4 =  if extend or NumberOfPivots<5 then na else plLL4;
plot lhLine4 =  if extend or NumberOfPivots<5 then na else plLH4;
def  countllLine4 = if llLine4==llLine4[1] then countllLine4[1] + 1 else 0;
def  countlhLine4 = if lhLine4==lhLine4[1] then countlhLine4[1] + 1 else 0;
plot extllLine4 = if countllLine4<=lookRight then plLL4[lookRight] else na;
plot extlhLine4 = if countlhLine4<=lookRight then plLH4[lookRight] else na;
extllLine4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
extllLine4.AssignValueColor(if close<extllLine4 then GlobalColor("bearBorder") else GlobalColor("bullBorder"));
extlhLine4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
extlhLine4.AssignValueColor(if close<extllLine4 then GlobalColor("bearBorder") else GlobalColor("bullBorder"));
llLine4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
llLine4.AssignValueColor(if BearColor4 then GlobalColor("bearBorder") else GlobalColor("bullBorder"));
lhLine4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
lhLine4.AssignValueColor(if BearColor4 then GlobalColor("bearBorder") else GlobalColor("bullBorder"));

#--
AddCloud(if hhLine==hhLine[1] and BullColor then hhLine else na, hlLine, GlobalColor("bearCloud"));
AddCloud(if hhLine==hhLine[1] and !BullColor then hhLine else na, hlLine, GlobalColor("bullCloud"));
AddCloud(if hhLine1==hhLine1[1] and BullColor1 then hhLine1 else na, hlLine1, GlobalColor("bearCloud"));
AddCloud(if hhLine1==hhLine1[1] and !BullColor1 then hhLine1 else na, hlLine1, GlobalColor("bullCloud"));
AddCloud(if hhLine2==hhLine2[1] and BullColor2 then hhLine2 else na, hlLine2, GlobalColor("bearCloud"));
AddCloud(if hhLine2==hhLine2[1] and !BullColor2 then hhLine2 else na, hlLine2, GlobalColor("bullCloud"));
AddCloud(if hhLine3==hhLine3[1] and BullColor3 then hhLine3 else na, hlLine3, GlobalColor("bearCloud"));
AddCloud(if hhLine3==hhLine3[1] and !BullColor3 then hhLine3 else na, hlLine3, GlobalColor("bullCloud"));
AddCloud(if hhLine4==hhLine4[1] and BullColor4 then hhLine4 else na, hlLine4, GlobalColor("bearCloud"));
AddCloud(if hhLine4==hhLine4[1] and !BullColor4 then hhLine4 else na, hlLine4, GlobalColor("bullCloud"));

AddCloud(if llLine==llLine[1] and BearColor then lhLine else na, llLine, GlobalColor("bullCloud"));
AddCloud(if llLine==llLine[1] and !BearColor then lhLine else na, llLine, GlobalColor("bearCloud"));
AddCloud(if llLine1==llLine1[1] and BearColor1 then lhLine1 else na, llLine1, GlobalColor("bullCloud"));
AddCloud(if llLine1==llLine1[1] and !BearColor1 then lhLine1 else na, llLine1, GlobalColor("bearCloud"));
AddCloud(if llLine2==llLine2[1] and BearColor2 then lhLine2 else na, llLine2, GlobalColor("bullCloud"));
AddCloud(if llLine2==llLine2[1] and !BearColor2 then lhLine2 else na, llLine2, GlobalColor("bearCloud"));
AddCloud(if llLine3==llLine3[1] and BearColor3 then lhLine3 else na, llLine3, GlobalColor("bullCloud"));
AddCloud(if llLine3==llLine3[1] and !BearColor3 then lhLine3 else na, llLine3, GlobalColor("bearCloud"));
AddCloud(if llLine4==llLine4[1] and BearColor3 then lhLine3 else na, llLine3, GlobalColor("bullCloud"));
AddCloud(if llLine4==llLine4[1] and !BearColor3 then lhLine3 else na, llLine3, GlobalColor("bearCloud"));
#--- Ext
AddCloud(if exthhLine==exthhLine[1] and close>exthhLine then exthhLine else na, exthlLine, GlobalColor("bearCloud"));
AddCloud(if exthhLine==exthhLine[1] and close<=exthhLine then exthhLine else na, exthlLine, GlobalColor("bullCloud"));
AddCloud(if exthhLine1==exthhLine1[1] and close>exthhLine1 then exthhLine1 else na, exthlLine1, GlobalColor("bearCloud"));
AddCloud(if exthhLine1==exthhLine1[1] and close<=exthhLine1 then exthhLine1 else na, exthlLine1, GlobalColor("bullCloud"));
AddCloud(if exthhLine2==exthhLine2[1] and close>exthhLine2 then exthhLine2 else na, exthlLine2, GlobalColor("bearCloud"));
AddCloud(if exthhLine2==exthhLine2[1] and close<=exthhLine2 then exthhLine2 else na, exthlLine2, GlobalColor("bullCloud"));
AddCloud(if exthhLine3==exthhLine3[1] and close>exthhLine3 then exthhLine3 else na, exthlLine3, GlobalColor("bearCloud"));
AddCloud(if exthhLine3==exthhLine3[1] and close<=exthhLine3 then exthhLine3 else na, exthlLine3, GlobalColor("bullCloud"));
AddCloud(if exthhLine4==exthhLine4[1] and close>exthhLine4 then exthhLine4 else na, exthlLine4, GlobalColor("bearCloud"));
AddCloud(if exthhLine4==exthhLine4[1] and close<=exthhLine4 then exthhLine4 else na, exthlLine4, GlobalColor("bullCloud"));

AddCloud(if extllLine==extllLine[1] and close<extllLine then extlhLine else na, extllLine, GlobalColor("bullCloud"));
AddCloud(if extllLine==extllLine[1] and close>=extllLine then extlhLine else na, extllLine, GlobalColor("bearCloud"));
AddCloud(if extllLine1==extllLine1[1] and close<extllLine1 then extlhLine1 else na, extllLine1, GlobalColor("bullCloud"));
AddCloud(if extllLine1==extllLine1[1] and close>=extllLine1 then extlhLine1 else na, extllLine1, GlobalColor("bearCloud"));
AddCloud(if extllLine2==extllLine2[1] and close<extllLine2 then extlhLine2 else na, extllLine2, GlobalColor("bullCloud"));
AddCloud(if extllLine2==extllLine2[1] and close>=extllLine2 then extlhLine2 else na, extllLine2, GlobalColor("bearCloud"));
AddCloud(if extllLine3==extllLine3[1] and close<extllLine3 then extlhLine3 else na, extllLine3, GlobalColor("bullCloud"));
AddCloud(if extllLine3==extllLine3[1] and close>=extllLine3 then extlhLine3 else na, extllLine3, GlobalColor("bearCloud"));
AddCloud(if extllLine4==extllLine4[1] and close<extllLine4 then extlhLine4 else na, extllLine4, GlobalColor("bullCloud"));
AddCloud(if extllLine4==extllLine4[1] and close>=extllLine4 then extlhLine4 else na, extllLine4, GlobalColor("bearCloud"));

#--- Break
AddChartBubble(ShowBreakUp and srcHigh crosses above hhLine and !BullColor[lookRight], low, "Break", Color.GREEN, no);
AddChartBubble(ShowBreakDn and srcLow crosses below llLine and !BearColor[lookRight], high, "Break", Color.RED, yes);

#--- END CODE
@samer800 Looks good any chance to fully complete this? Thanks a bunch.

rPzDdHM.png


I converted partially. pls check.

CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © Bjorgum
#indicator ('Bjorgum Key Levels', 'Bj Key Levels', ount=500)
# partial conversion by Sam4Cok@Samer800 - 01 /2023
input lookLeft    = 20;            #  "Look Left"  
input lookRight   = 15;            #  "Look Right"  
input NumberOfPivots = 3;          #  "Number of Pivots"
input atrLength      = 30;         # "ATR Length"
input ZoneWidth      = 0.5;        # "Zone Width (ATR)"
input MaxZonePercent = 5;          #  "Max Zone Percent"
input SourceForPivots = {default "HeikinAshi", "High/Low Body", "High/Low"}; # "Source For Pivots"
input ExtendRight     = no;        #   "Extend Right"
input PivotHighs      = yes;       #  "Detect Pivot Highs"
input PivotLows       = yes;       #  "Detect Pivot Lows"
input ShowBreakUp     = yes;
input ShowBreakDn     = yes;

def na = Double.NaN;
def extend = if ExtendRight then isNaN(close[lookRight]) else isNaN(close);
#--- Color
DefineGlobalColor("bullBorder", CreateColor(7,71,123));
DefineGlobalColor("bearBorder", CreateColor(118,106,0));
DefineGlobalColor("bearCloud", CreateColor(3,29,51));
DefineGlobalColor("bullCloud", CreateColor(42,37,0));

script nz {
    input data  = close;
    input repl  = 0;
    def ret_val = if !isNaN(data) then data else repl;
    plot return = ret_val;
}
script fixnan {
    input source = close;
    def fix = if !IsNaN(source) then source else fix[1];
    plot result = fix;
}
#valuewhen (cond, source, occurrence) =>
script valuewhen {
    input cond = 0;
    input source = 0;
    input occurrence = 0;
    def n = occurrence + 1;
# start at 0 so it looks at current bar
    def offset2 = fold j = 0 to 400
    with p
    while p < n + 1
    do p + ( if p == n then j - n else if GetValue(cond, j) then 1 else 0 );
# def bnz = bn - offset2 + 1;
    plot price = GetValue(source, offset2 - 1);
}

script FindPivots {
    input dat = close; # default data or study being evaluated
    input HL  = 0;    # default high or low pivot designation, -1 low, +1 high
    input lbL  = 5;    # default Pivot Lookback Left
    input lbR  = 1;    # default Pivot Lookback Right
    ##############
    def _nan;    # used for non-number returns
    def _BN;     # the current barnumber
    def _VStop;  # confirms that the lookforward period continues the pivot trend
    def _V;      # the Value at the actual pivot point
    ##############
    _BN  = BarNumber();
    _nan = Double.NaN;
    _VStop = if !IsNaN(dat) and lbR > 0 and lbL > 0 then
                fold a = 1 to lbR + 1 with b=1 while b do
                    if HL > 0 then dat > GetValue(dat, -a) else dat < GetValue(dat, -a) else _nan;
    if (HL > 0) {
        _V = if _BN > lbL and dat == Highest(dat, lbL + 1) and _VStop
            then dat else _nan;
    } else {
        _V = if _BN > lbL and dat == Lowest(dat, lbL + 1) and _VStop
            then dat else _nan;
    }
    plot result = if !IsNaN(_V) and _VStop then _V else _nan;
}
def haSrc   = SourceForPivots == SourceForPivots."HeikinAshi";
def hiLoSrc = SourceForPivots == SourceForPivots."High/Low";
#// ---> Functional Declarations <---- //

def nATR    = ATR(LENGTH = atrLength);
#def perMax = close * 0.02;
#def min    = Min(perMax, atr * 0.3);

#// ----> Variable Calculations <----- //
def close_ = (open + high  +  low  + close) / 4;
def open_;
open_  = if IsNaN(open_[1]) then (open + close) / 2 else
                 (nz(open_[1],open[1]) + nz(close_[1],close[1]))   / 2;
def hiHaBod = Max(close_, open_);
def loHaBod = Min(close_, open_);
def hiBod   = Max(close, open);
def loBod   = Min(close, open);
def srcHigh = if haSrc then hiHaBod else if hiLoSrc then high else hiBod;
def srcLow  = if haSrc then loHaBod else if hiLoSrc then low  else loBod;
def highest = close == Highest(close, lookRight);
def lowest  = close == Lowest (close, lookRight);
def pivot_high = FindPivots(srcHigh, 1, lookLeft, lookRight);
def pivot_low  = FindPivots(srcLow, -1, lookLeft, lookRight);
def ph = if !isNaN(pivot_high) then pivot_high else ph[1];
def pl = if !isNaN(pivot_low) then pivot_low else pl[1];
def perc = close * (MaxZonePercent / 100);
def minATR = Min(nATR * ZoneWidth, perc);
def band = minATR / 2;
def HH   = ph + band;
def HL   = ph - band;
def LH   = pl + band;
def LL   = pl - band;

def coDiff = close - open;
def barRange = AbsValue(high - low);
def phHH;
def phHL;
def plLH;
def plLL;
def phHH1;
def phHL1;
def plLH1;
def plLL1;
def phHH2;
def phHL2;
def plLH2;
def plLL2;
def phHH3;
def phHL3;
def plLH3;
def plLL3;
def phHH4;
def phHL4;
def plLH4;
def plLL4;
def cond1 = !isNaN(pivot_high) and pivot_high!=0 and !IsNaN(close[1]);
def cond2 = !isNaN(pivot_low)  and pivot_low !=0 and !IsNaN(close[1]);
if cond1 {
    phHH = HH;
    phHL = HL;
    phHH1 = phHH[1];
    phHL1 = phHL[1];
    phHH2 = phHH1[1];
    phHL2 = phHL1[1];
    phHH3 = phHH2[1];
    phHL3 = phHL2[1];
    phHH4 = phHH3[1];
    phHL4 = phHL3[1];
    plLH = plLH[1];
    plLL = plLL[1];
    plLH1 = plLH1[1];
    plLL1 = plLL1[1];
    plLH2 = plLH2[1];
    plLL2 = plLL2[1];
    plLH3 = plLH3[1];
    plLL3 = plLL3[1];
    plLH4 = plLH4[1];
    plLL4 = plLL4[1];
} else
if cond2 {
    plLH = LH;
    plLL = LL;
    plLH1 = plLH[1];
    plLL1 = plLL[1];
    plLH2 = plLH1[1];
    plLL2 = plLL1[1];
    plLH3 = plLH2[1];
    plLL3 = plLL2[1];
    plLH4 = plLH3[1];
    plLL4 = plLL3[1];
    phHH =  phHH[1];
    phHL =  phHL[1];
    phHH1 = phHH1[1];
    phHL1 = phHL1[1];
    phHH2 = phHH2[1];
    phHL2 = phHL2[1];
    phHH3 = phHH3[1];
    phHL3 = phHL3[1];
    phHH4 = phHH4[1];
    phHL4 = phHL4[1];
} else {
    phHH = if phHH[1] == 0 then hiHaBod else phHH[1];
    phHL = if phHL[1] == 0 then hiHaBod else phHL[1];
    phHH1 = if phHH1[1] == 0 then hiHaBod else phHH1[1];
    phHL1 = if phHL1[1] == 0 then hiHaBod else phHL1[1];
    phHH2 = if phHH2[1] == 0 then hiHaBod else phHH2[1];
    phHL2 = if phHL2[1] == 0 then hiHaBod else phHL2[1];
    phHH3 = if phHH3[1] == 0 then hiHaBod else phHH3[1];
    phHL3 = if phHL3[1] == 0 then hiHaBod else phHL3[1];
    phHH4 = if phHH4[1] == 0 then hiHaBod else phHH4[1];
    phHL4 = if phHL4[1] == 0 then hiHaBod else phHL4[1];
    plLH = if plLH[1] == 0 then loHaBod else plLH[1];
    plLL = if plLL[1] == 0 then loHaBod else plLL[1];
    plLH1 = if plLH1[1] == 0 then loHaBod else plLH1[1];
    plLL1 = if plLL1[1] == 0 then loHaBod else plLL1[1];
    plLH2 = if plLH2[1] == 0 then loHaBod else plLH2[1];
    plLL2 = if plLL2[1] == 0 then loHaBod else plLL2[1];
    plLH3 = if plLH3[1] == 0 then loHaBod else plLH3[1];
    plLL3 = if plLL3[1] == 0 then loHaBod else plLL3[1];
    plLH4 = if plLH4[1] == 0 then loHaBod else plLH4[1];
    plLL4 = if plLL4[1] == 0 then loHaBod else plLL4[1];
}
def BullColor  = nz(close,close[lookRight])>phHH;
def BearColor  = nz(close,close[lookRight])<plLL;
def BullColor1 = nz(close,close[lookRight])>phHH1;
def BearColor1 = nz(close,close[lookRight])<plLL1;
def BullColor2 = nz(close,close[lookRight])>phHH2;
def BearColor2 = nz(close,close[lookRight])<plLL2;
def BullColor3 = nz(close,close[lookRight])>phHH3;
def BearColor3 = nz(close,close[lookRight])<plLL3;
def BullColor4 = nz(close,close[lookRight])>phHH4;
def BearColor4 = nz(close,close[lookRight])<plLL4;

plot hhLine =  if extend then na else phHH;
plot hlLine =  if extend then na else phHL;
def  counthhLine = if hhLine==hhLine[1] then counthhLine[1] + 1 else 0;
def  counthlLine = if hlLine==hlLine[1] then counthlLine[1] + 1 else 0;
plot exthhLine = if counthhLine<=lookRight then phHH[lookRight] else na;
plot exthlLine = if counthlLine<=lookRight then phHL[lookRight] else na;
exthhLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
exthhLine.AssignValueColor(if close>exthhLine then GlobalColor("bullBorder") else GlobalColor("bearBorder"));
exthlLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
exthlLine.AssignValueColor(if close>exthhLine then GlobalColor("bullBorder") else GlobalColor("bearBorder"));
hhLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
hhLine.AssignValueColor(if BullColor then GlobalColor("bullBorder") else GlobalColor("bearBorder"));
hlLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
hlLine.AssignValueColor(if BullColor then GlobalColor("bullBorder") else GlobalColor("bearBorder"));


plot llLine =  if extend then na else plLL ;
plot lhLine =  if extend then na else plLH;
def  countllLine = if llLine==llLine[1] then countllLine[1] + 1 else 0;
def  countlhLine = if lhLine==lhLine[1] then countlhLine[1] + 1 else 0;
plot extllLine = if countllLine<=lookRight then plLL[lookRight] else na;
plot extlhLine = if countlhLine<=lookRight then plLH[lookRight] else na;
extllLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
extllLine.AssignValueColor(if close<extllLine then GlobalColor("bearBorder") else GlobalColor("bullBorder"));
extlhLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
extlhLine.AssignValueColor(if close<extllLine then GlobalColor("bearBorder") else GlobalColor("bullBorder"));
llLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
llLine.AssignValueColor(if BearColor then GlobalColor("bearBorder") else GlobalColor("bullBorder"));
lhLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
lhLine.AssignValueColor(if BearColor then GlobalColor("bearBorder") else GlobalColor("bullBorder"));

plot hhLine1 =  if extend or NumberOfPivots<2 then na else phHH1;
plot hlLine1 =  if extend or NumberOfPivots<2 then na else phHL1 ;
def counthhLine1 = if hhLine1==hhLine1[1] then counthhLine1[1] + 1 else 0;
def counthlLine1 = if hlLine1==hlLine1[1] then counthlLine1[1] + 1 else 0;
plot exthhLine1  = if counthhLine1<=lookRight then phHH1[lookRight] else na;
plot exthlLine1  = if counthlLine1<=lookRight then phHL1[lookRight] else na;
exthhLine1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
exthhLine1.AssignValueColor(if close>exthhLine1 then GlobalColor("bullBorder") else GlobalColor("bearBorder"));
exthlLine1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
exthlLine1.AssignValueColor(if close>exthhLine1 then GlobalColor("bullBorder") else GlobalColor("bearBorder"));
hhLine1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
hhLine1.AssignValueColor(if BullColor1 then GlobalColor("bullBorder") else GlobalColor("bearBorder"));
hlLine1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
hlLine1.AssignValueColor(if BullColor1 then GlobalColor("bullBorder") else GlobalColor("bearBorder"));

plot llLine1 =  if extend or NumberOfPivots<2 then na else plLL1;
plot lhLine1 =  if extend or NumberOfPivots<2 then na else plLH1;
def  countllLine1 = if llLine1==llLine1[1] then countllLine1[1] + 1 else 0;
def  countlhLine1 = if lhLine1==lhLine1[1] then countlhLine1[1] + 1 else 0;
plot extllLine1 = if countllLine1<=lookRight then plLL1[lookRight] else na;
plot extlhLine1 = if countlhLine1<=lookRight then plLH1[lookRight] else na;
extllLine1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
extllLine1.AssignValueColor(if close<extllLine1 then GlobalColor("bearBorder") else GlobalColor("bullBorder"));
extlhLine1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
extlhLine1.AssignValueColor(if close<extllLine1 then GlobalColor("bearBorder") else GlobalColor("bullBorder"));
llLine1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
llLine1.AssignValueColor(if BearColor1 then GlobalColor("bearBorder") else GlobalColor("bullBorder"));
lhLine1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
lhLine1.AssignValueColor(if BearColor1 then GlobalColor("bearBorder") else GlobalColor("bullBorder"));

plot hhLine2 =  if extend or NumberOfPivots<3 then na else phHH2;
plot hlLine2 =  if extend or NumberOfPivots<3 then na else phHL2;
def counthhLine2 = if hhLine2==hhLine2[1] then counthhLine2[1] + 1 else 0;
def counthlLine2 = if hlLine2==hlLine2[1] then counthlLine2[1] + 1 else 0;
plot exthhLine2  = if counthhLine2<=lookRight then phHH2[lookRight] else na;
plot exthlLine2  = if counthlLine2<=lookRight then phHL2[lookRight] else na;
exthhLine2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
exthhLine2.AssignValueColor(if close>exthhLine2 then GlobalColor("bullBorder") else GlobalColor("bearBorder"));
exthlLine2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
exthlLine2.AssignValueColor(if close>exthhLine2 then GlobalColor("bullBorder") else GlobalColor("bearBorder"));
hhLine2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
hhLine2.AssignValueColor(if BullColor2 then GlobalColor("bullBorder") else GlobalColor("bearBorder"));
hlLine2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
hlLine2.AssignValueColor(if BullColor2 then GlobalColor("bullBorder") else GlobalColor("bearBorder"));

plot llLine2 =  if extend or NumberOfPivots<3 then na else plLL2;
plot lhLine2 =  if extend or NumberOfPivots<3 then na else plLH2;
def  countllLine2 = if llLine2==llLine2[1] then countllLine2[1] + 1 else 0;
def  countlhLine2 = if lhLine2==lhLine2[1] then countlhLine2[1] + 1 else 0;
plot extllLine2 = if countllLine2<=lookRight then plLL2[lookRight] else na;
plot extlhLine2 = if countlhLine2<=lookRight then plLH2[lookRight] else na;
extllLine2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
extllLine2.AssignValueColor(if close<extllLine2 then GlobalColor("bearBorder") else GlobalColor("bullBorder"));
extlhLine2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
extlhLine2.AssignValueColor(if close<extllLine2 then GlobalColor("bearBorder") else GlobalColor("bullBorder"));
llLine2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
llLine2.AssignValueColor(if BearColor2 then GlobalColor("bearBorder") else GlobalColor("bullBorder"));
lhLine2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
lhLine2.AssignValueColor(if BearColor2 then GlobalColor("bearBorder") else GlobalColor("bullBorder"));

plot hhLine3 =  if extend or NumberOfPivots<4 then na else phHH3;
plot hlLine3 =  if extend or NumberOfPivots<4 then na else phHL3;
def counthhLine3 = if hhLine3==hhLine3[1] then counthhLine3[1] + 1 else 0;
def counthlLine3 = if hlLine3==hlLine3[1] then counthlLine3[1] + 1 else 0;
plot exthhLine3  = if counthhLine3<=lookRight then phHH3[lookRight] else na;
plot exthlLine3  = if counthlLine3<=lookRight then phHL3[lookRight] else na;
exthhLine3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
exthhLine3.AssignValueColor(if close>exthhLine3 then GlobalColor("bullBorder") else GlobalColor("bearBorder"));
exthlLine3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
exthlLine3.AssignValueColor(if close>exthhLine3 then GlobalColor("bullBorder") else GlobalColor("bearBorder"));
hhLine3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
hhLine3.AssignValueColor(if BullColor3 then GlobalColor("bullBorder") else GlobalColor("bearBorder"));
hlLine3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
hlLine3.AssignValueColor(if BullColor3 then GlobalColor("bullBorder") else GlobalColor("bearBorder"));

plot llLine3 =  if extend or NumberOfPivots<4 then na else plLL3;
plot lhLine3 =  if extend or NumberOfPivots<4 then na else plLH3;
def  countllLine3 = if llLine3==llLine3[1] then countllLine3[1] + 1 else 0;
def  countlhLine3 = if lhLine3==lhLine3[1] then countlhLine3[1] + 1 else 0;
plot extllLine3 = if countllLine3<=lookRight then plLL3[lookRight] else na;
plot extlhLine3 = if countlhLine3<=lookRight then plLH3[lookRight] else na;
extllLine3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
extllLine3.AssignValueColor(if close<extllLine3 then GlobalColor("bearBorder") else GlobalColor("bullBorder"));
extlhLine3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
extlhLine3.AssignValueColor(if close<extllLine3 then GlobalColor("bearBorder") else GlobalColor("bullBorder"));
llLine3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
llLine3.AssignValueColor(if BearColor3 then GlobalColor("bearBorder") else GlobalColor("bullBorder"));
lhLine3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
lhLine3.AssignValueColor(if BearColor3 then GlobalColor("bearBorder") else GlobalColor("bullBorder"));

plot hhLine4 =  if extend or NumberOfPivots<5 then na else phHH4;
plot hlLine4 =  if extend or NumberOfPivots<5 then na else phHL4;
def counthhLine4 = if hhLine4==hhLine4[1] then counthhLine4[1] + 1 else 0;
def counthlLine4 = if hlLine4==hlLine4[1] then counthlLine4[1] + 1 else 0;
plot exthhLine4  = if counthhLine4<=lookRight then phHH4[lookRight] else na;
plot exthlLine4  = if counthlLine4<=lookRight then phHL4[lookRight] else na;
exthhLine4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
exthhLine4.AssignValueColor(if close>exthhLine4 then GlobalColor("bullBorder") else GlobalColor("bearBorder"));
exthlLine4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
exthlLine4.AssignValueColor(if close>exthhLine4 then GlobalColor("bullBorder") else GlobalColor("bearBorder"));
hhLine4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
hhLine4.AssignValueColor(if BullColor4 then GlobalColor("bullBorder") else GlobalColor("bearBorder"));
hlLine4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
hlLine4.AssignValueColor(if BullColor4 then GlobalColor("bullBorder") else GlobalColor("bearBorder"));

plot llLine4 =  if extend or NumberOfPivots<5 then na else plLL4;
plot lhLine4 =  if extend or NumberOfPivots<5 then na else plLH4;
def  countllLine4 = if llLine4==llLine4[1] then countllLine4[1] + 1 else 0;
def  countlhLine4 = if lhLine4==lhLine4[1] then countlhLine4[1] + 1 else 0;
plot extllLine4 = if countllLine4<=lookRight then plLL4[lookRight] else na;
plot extlhLine4 = if countlhLine4<=lookRight then plLH4[lookRight] else na;
extllLine4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
extllLine4.AssignValueColor(if close<extllLine4 then GlobalColor("bearBorder") else GlobalColor("bullBorder"));
extlhLine4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
extlhLine4.AssignValueColor(if close<extllLine4 then GlobalColor("bearBorder") else GlobalColor("bullBorder"));
llLine4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
llLine4.AssignValueColor(if BearColor4 then GlobalColor("bearBorder") else GlobalColor("bullBorder"));
lhLine4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
lhLine4.AssignValueColor(if BearColor4 then GlobalColor("bearBorder") else GlobalColor("bullBorder"));

#--
AddCloud(if hhLine==hhLine[1] and BullColor then hhLine else na, hlLine, GlobalColor("bearCloud"));
AddCloud(if hhLine==hhLine[1] and !BullColor then hhLine else na, hlLine, GlobalColor("bullCloud"));
AddCloud(if hhLine1==hhLine1[1] and BullColor1 then hhLine1 else na, hlLine1, GlobalColor("bearCloud"));
AddCloud(if hhLine1==hhLine1[1] and !BullColor1 then hhLine1 else na, hlLine1, GlobalColor("bullCloud"));
AddCloud(if hhLine2==hhLine2[1] and BullColor2 then hhLine2 else na, hlLine2, GlobalColor("bearCloud"));
AddCloud(if hhLine2==hhLine2[1] and !BullColor2 then hhLine2 else na, hlLine2, GlobalColor("bullCloud"));
AddCloud(if hhLine3==hhLine3[1] and BullColor3 then hhLine3 else na, hlLine3, GlobalColor("bearCloud"));
AddCloud(if hhLine3==hhLine3[1] and !BullColor3 then hhLine3 else na, hlLine3, GlobalColor("bullCloud"));
AddCloud(if hhLine4==hhLine4[1] and BullColor4 then hhLine4 else na, hlLine4, GlobalColor("bearCloud"));
AddCloud(if hhLine4==hhLine4[1] and !BullColor4 then hhLine4 else na, hlLine4, GlobalColor("bullCloud"));

AddCloud(if llLine==llLine[1] and BearColor then lhLine else na, llLine, GlobalColor("bullCloud"));
AddCloud(if llLine==llLine[1] and !BearColor then lhLine else na, llLine, GlobalColor("bearCloud"));
AddCloud(if llLine1==llLine1[1] and BearColor1 then lhLine1 else na, llLine1, GlobalColor("bullCloud"));
AddCloud(if llLine1==llLine1[1] and !BearColor1 then lhLine1 else na, llLine1, GlobalColor("bearCloud"));
AddCloud(if llLine2==llLine2[1] and BearColor2 then lhLine2 else na, llLine2, GlobalColor("bullCloud"));
AddCloud(if llLine2==llLine2[1] and !BearColor2 then lhLine2 else na, llLine2, GlobalColor("bearCloud"));
AddCloud(if llLine3==llLine3[1] and BearColor3 then lhLine3 else na, llLine3, GlobalColor("bullCloud"));
AddCloud(if llLine3==llLine3[1] and !BearColor3 then lhLine3 else na, llLine3, GlobalColor("bearCloud"));
AddCloud(if llLine4==llLine4[1] and BearColor3 then lhLine3 else na, llLine3, GlobalColor("bullCloud"));
AddCloud(if llLine4==llLine4[1] and !BearColor3 then lhLine3 else na, llLine3, GlobalColor("bearCloud"));
#--- Ext
AddCloud(if exthhLine==exthhLine[1] and close>exthhLine then exthhLine else na, exthlLine, GlobalColor("bearCloud"));
AddCloud(if exthhLine==exthhLine[1] and close<=exthhLine then exthhLine else na, exthlLine, GlobalColor("bullCloud"));
AddCloud(if exthhLine1==exthhLine1[1] and close>exthhLine1 then exthhLine1 else na, exthlLine1, GlobalColor("bearCloud"));
AddCloud(if exthhLine1==exthhLine1[1] and close<=exthhLine1 then exthhLine1 else na, exthlLine1, GlobalColor("bullCloud"));
AddCloud(if exthhLine2==exthhLine2[1] and close>exthhLine2 then exthhLine2 else na, exthlLine2, GlobalColor("bearCloud"));
AddCloud(if exthhLine2==exthhLine2[1] and close<=exthhLine2 then exthhLine2 else na, exthlLine2, GlobalColor("bullCloud"));
AddCloud(if exthhLine3==exthhLine3[1] and close>exthhLine3 then exthhLine3 else na, exthlLine3, GlobalColor("bearCloud"));
AddCloud(if exthhLine3==exthhLine3[1] and close<=exthhLine3 then exthhLine3 else na, exthlLine3, GlobalColor("bullCloud"));
AddCloud(if exthhLine4==exthhLine4[1] and close>exthhLine4 then exthhLine4 else na, exthlLine4, GlobalColor("bearCloud"));
AddCloud(if exthhLine4==exthhLine4[1] and close<=exthhLine4 then exthhLine4 else na, exthlLine4, GlobalColor("bullCloud"));

AddCloud(if extllLine==extllLine[1] and close<extllLine then extlhLine else na, extllLine, GlobalColor("bullCloud"));
AddCloud(if extllLine==extllLine[1] and close>=extllLine then extlhLine else na, extllLine, GlobalColor("bearCloud"));
AddCloud(if extllLine1==extllLine1[1] and close<extllLine1 then extlhLine1 else na, extllLine1, GlobalColor("bullCloud"));
AddCloud(if extllLine1==extllLine1[1] and close>=extllLine1 then extlhLine1 else na, extllLine1, GlobalColor("bearCloud"));
AddCloud(if extllLine2==extllLine2[1] and close<extllLine2 then extlhLine2 else na, extllLine2, GlobalColor("bullCloud"));
AddCloud(if extllLine2==extllLine2[1] and close>=extllLine2 then extlhLine2 else na, extllLine2, GlobalColor("bearCloud"));
AddCloud(if extllLine3==extllLine3[1] and close<extllLine3 then extlhLine3 else na, extllLine3, GlobalColor("bullCloud"));
AddCloud(if extllLine3==extllLine3[1] and close>=extllLine3 then extlhLine3 else na, extllLine3, GlobalColor("bearCloud"));
AddCloud(if extllLine4==extllLine4[1] and close<extllLine4 then extlhLine4 else na, extllLine4, GlobalColor("bullCloud"));
AddCloud(if extllLine4==extllLine4[1] and close>=extllLine4 then extlhLine4 else na, extllLine4, GlobalColor("bearCloud"));

#--- Break
AddChartBubble(ShowBreakUp and srcHigh crosses above hhLine and !BullColor[lookRight], low, "Break", Color.GREEN, no);
AddChartBubble(ShowBreakDn and srcLow crosses below llLine and !BearColor[lookRight], high, "Break", Color.RED, yes);

#--- END CODE
@samer800 Could you finish this code? I think it could work well with this code.

STD-Filtered Jurik Volty Adaptive TEMA [Loxx] for ThinkOrScript
https://usethinkscript.com/threads/...ema-loxx-for-thinkorscript.15190/#post-124089
 
Last edited by a moderator:
@halcyonguy Thank you for your time and efforts with this.
https://usethinkscript.com/threads/finding-major-pivot-points.15354/

I found a code recently called "Bjorgum Key Levels" that is probably the best so far.
Is there a way to modify this code so that it will search for the plots on say an 1Hr TF while using a 1m TF? What I am trying to accomplish, is to look for the pivot points on higher time frame without needing to change the look left and look right variables every time. what are your thought on this? Does this seem necessary to you? This one plots clouds across the chart as needed and also allows for size of cloud. @a1cturner (Let me know what you think of this one)
Code:
#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © Bjorgum
#indicator ('Bjorgum Key Levels', 'Bj Key Levels', ount=500)
# partial conversion by Sam4Cok@Samer800 - 01 /2023
## alerts added by ZupDog


input alert = yes;
input lookLeft    = 20;            #  "Look Left"   
input lookRight   = 15;            #  "Look Right"   
input NumberOfPivots = 3;          #  "Number of Pivots"
input atrLength      = 30;         # "ATR Length"
input ZoneWidth      = 0.5;        # "Zone Width (ATR)"
input MaxZonePercent = 5;          #  "Max Zone Percent"
input SourceForPivots = {default "HeikinAshi", "High/Low Body", "High/Low"}; # "Source For Pivots"
input ExtendRight     = no;        #   "Extend Right"
input PivotHighs      = yes;       #  "Detect Pivot Highs"
input PivotLows       = yes;       #  "Detect Pivot Lows"
input ShowBreakUp     = yes;
input ShowBreakDn     = yes;

def na = Double.NaN;
def extend = if ExtendRight then isNaN(close[lookRight]) else isNaN(close);
#--- Color
DefineGlobalColor("bullBorder", CreateColor(255,255,255));
#DefineGlobalColor("bearBorder", CreateColor(0,0,0));
#DefineGlobalColor("bullBorder", CreateColor(7,71,123));
DefineGlobalColor("bearBorder", CreateColor(255,153,255));
#DefineGlobalColor("bearCloud", CreateColor(3,29,51));
#DefineGlobalColor("bullCloud", CreateColor(42,37,0));
DefineGlobalColor("bearCloud", CreateColor(0,0,255));
DefineGlobalColor("bullCloud", CreateColor(255,0,255));

script nz {
    input data  = close;
    input repl  = 0;
    def ret_val = if !isNaN(data) then data else repl;
    plot return = ret_val;
}
script fixnan {
    input source = close;
    def fix = if !IsNaN(source) then source else fix[1];
    plot result = fix;
}
#valuewhen (cond, source, occurrence) =>
script valuewhen {
    input cond = 0;
    input source = 0;
    input occurrence = 0;
    def n = occurrence + 1;
# start at 0 so it looks at current bar
    def offset2 = fold j = 0 to 400
    with p
    while p < n + 1
    do p + ( if p == n then j - n else if GetValue(cond, j) then 1 else 0 );
# def bnz = bn - offset2 + 1;
    plot price = GetValue(source, offset2 - 1);
}

script FindPivots {
    input dat = close; # default data or study being evaluated
    input HL  = 0;    # default high or low pivot designation, -1 low, +1 high
    input lbL  = 5;    # default Pivot Lookback Left
    input lbR  = 1;    # default Pivot Lookback Right
    ##############
    def _nan;    # used for non-number returns
    def _BN;     # the current barnumber
    def _VStop;  # confirms that the lookforward period continues the pivot trend
    def _V;      # the Value at the actual pivot point
    ##############
    _BN  = BarNumber();
    _nan = Double.NaN;
    _VStop = if !IsNaN(dat) and lbR > 0 and lbL > 0 then
                fold a = 1 to lbR + 1 with b=1 while b do
                    if HL > 0 then dat > GetValue(dat, -a) else dat < GetValue(dat, -a) else _nan;
    if (HL > 0) {
        _V = if _BN > lbL and dat == Highest(dat, lbL + 1) and _VStop
            then dat else _nan;
    } else {
        _V = if _BN > lbL and dat == Lowest(dat, lbL + 1) and _VStop
            then dat else _nan;
    }
    plot result = if !IsNaN(_V) and _VStop then _V else _nan;
}
def haSrc   = SourceForPivots == SourceForPivots."HeikinAshi";
def hiLoSrc = SourceForPivots == SourceForPivots."High/Low";
#// ---> Functional Declarations <---- //

def nATR    = ATR(LENGTH = atrLength);
#def perMax = close * 0.02;
#def min    = Min(perMax, atr * 0.3);

#// ----> Variable Calculations <----- //
def close_ = (open + high  +  low  + close) / 4;
def open_;
open_  = if IsNaN(open_[1]) then (open + close) / 2 else
                 (nz(open_[1],open[1]) + nz(close_[1],close[1]))   / 2;
def hiHaBod = Max(close_, open_);
def loHaBod = Min(close_, open_);
def hiBod   = Max(close, open);
def loBod   = Min(close, open);
def srcHigh = if haSrc then hiHaBod else if hiLoSrc then high else hiBod;
def srcLow  = if haSrc then loHaBod else if hiLoSrc then low  else loBod;
def highest = close == Highest(close, lookRight);
def lowest  = close == Lowest (close, lookRight);
def pivot_high = FindPivots(srcHigh, 1, lookLeft, lookRight);
def pivot_low  = FindPivots(srcLow, -1, lookLeft, lookRight);
def ph = if !isNaN(pivot_high) then pivot_high else ph[1];
def pl = if !isNaN(pivot_low) then pivot_low else pl[1];
def perc = close * (MaxZonePercent / 100);
def minATR = Min(nATR * ZoneWidth, perc);
def band = minATR / 2;
def HH   = ph + band;
def HL   = ph - band;
def LH   = pl + band;
def LL   = pl - band;

def coDiff = close - open;
def barRange = AbsValue(high - low);
def phHH;
def phHL;
def plLH;
def plLL;
def phHH1;
def phHL1;
def plLH1;
def plLL1;
def phHH2;
def phHL2;
def plLH2;
def plLL2;
def phHH3;
def phHL3;
def plLH3;
def plLL3;
def phHH4;
def phHL4;
def plLH4;
def plLL4;
def cond1 = !isNaN(pivot_high) and pivot_high!=0 and !IsNaN(close[1]);
def cond2 = !isNaN(pivot_low)  and pivot_low !=0 and !IsNaN(close[1]);
if cond1 {
    phHH = HH;
    phHL = HL;
    phHH1 = phHH[1];
    phHL1 = phHL[1];
    phHH2 = phHH1[1];
    phHL2 = phHL1[1];
    phHH3 = phHH2[1];
    phHL3 = phHL2[1];
    phHH4 = phHH3[1];
    phHL4 = phHL3[1];
    plLH = plLH[1];
    plLL = plLL[1];
    plLH1 = plLH1[1];
    plLL1 = plLL1[1];
    plLH2 = plLH2[1];
    plLL2 = plLL2[1];
    plLH3 = plLH3[1];
    plLL3 = plLL3[1];
    plLH4 = plLH4[1];
    plLL4 = plLL4[1];
} else
if cond2 {
    plLH = LH;
    plLL = LL;
    plLH1 = plLH[1];
    plLL1 = plLL[1];
    plLH2 = plLH1[1];
    plLL2 = plLL1[1];
    plLH3 = plLH2[1];
    plLL3 = plLL2[1];
    plLH4 = plLH3[1];
    plLL4 = plLL3[1];
    phHH =  phHH[1];
    phHL =  phHL[1];
    phHH1 = phHH1[1];
    phHL1 = phHL1[1];
    phHH2 = phHH2[1];
    phHL2 = phHL2[1];
    phHH3 = phHH3[1];
    phHL3 = phHL3[1];
    phHH4 = phHH4[1];
    phHL4 = phHL4[1];
} else {
    phHH = if phHH[1] == 0 then hiHaBod else phHH[1];
    phHL = if phHL[1] == 0 then hiHaBod else phHL[1];
    phHH1 = if phHH1[1] == 0 then hiHaBod else phHH1[1];
    phHL1 = if phHL1[1] == 0 then hiHaBod else phHL1[1];
    phHH2 = if phHH2[1] == 0 then hiHaBod else phHH2[1];
    phHL2 = if phHL2[1] == 0 then hiHaBod else phHL2[1];
    phHH3 = if phHH3[1] == 0 then hiHaBod else phHH3[1];
    phHL3 = if phHL3[1] == 0 then hiHaBod else phHL3[1];
    phHH4 = if phHH4[1] == 0 then hiHaBod else phHH4[1];
    phHL4 = if phHL4[1] == 0 then hiHaBod else phHL4[1];
    plLH = if plLH[1] == 0 then loHaBod else plLH[1];
    plLL = if plLL[1] == 0 then loHaBod else plLL[1];
    plLH1 = if plLH1[1] == 0 then loHaBod else plLH1[1];
    plLL1 = if plLL1[1] == 0 then loHaBod else plLL1[1];
    plLH2 = if plLH2[1] == 0 then loHaBod else plLH2[1];
    plLL2 = if plLL2[1] == 0 then loHaBod else plLL2[1];
    plLH3 = if plLH3[1] == 0 then loHaBod else plLH3[1];
    plLL3 = if plLL3[1] == 0 then loHaBod else plLL3[1];
    plLH4 = if plLH4[1] == 0 then loHaBod else plLH4[1];
    plLL4 = if plLL4[1] == 0 then loHaBod else plLL4[1];
}
def BullColor  = nz(close,close[lookRight])>phHH;
def BearColor  = nz(close,close[lookRight])<plLL;
def BullColor1 = nz(close,close[lookRight])>phHH1;
def BearColor1 = nz(close,close[lookRight])<plLL1;
def BullColor2 = nz(close,close[lookRight])>phHH2;
def BearColor2 = nz(close,close[lookRight])<plLL2;
def BullColor3 = nz(close,close[lookRight])>phHH3;
def BearColor3 = nz(close,close[lookRight])<plLL3;
def BullColor4 = nz(close,close[lookRight])>phHH4;
def BearColor4 = nz(close,close[lookRight])<plLL4;

plot hhLine =  if extend then na else phHH;
plot hlLine =  if extend then na else phHL;
def  counthhLine = if hhLine==hhLine[1] then counthhLine[1] + 1 else 0;
def  counthlLine = if hlLine==hlLine[1] then counthlLine[1] + 1 else 0;
plot exthhLine = if counthhLine<=lookRight then phHH[lookRight] else na;
plot exthlLine = if counthlLine<=lookRight then phHL[lookRight] else na;
exthhLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
exthhLine.AssignValueColor(if close>exthhLine then GlobalColor("bullBorder") else GlobalColor("bearBorder"));
exthlLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
exthlLine.AssignValueColor(if close>exthhLine then GlobalColor("bullBorder") else GlobalColor("bearBorder"));
hhLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
hhLine.AssignValueColor(if BullColor then GlobalColor("bullBorder") else GlobalColor("bearBorder"));
hlLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
hlLine.AssignValueColor(if BullColor then GlobalColor("bullBorder") else GlobalColor("bearBorder"));


plot llLine =  if extend then na else plLL ;
plot lhLine =  if extend then na else plLH;
def  countllLine = if llLine==llLine[1] then countllLine[1] + 1 else 0;
def  countlhLine = if lhLine==lhLine[1] then countlhLine[1] + 1 else 0;
plot extllLine = if countllLine<=lookRight then plLL[lookRight] else na;
plot extlhLine = if countlhLine<=lookRight then plLH[lookRight] else na;
extllLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
extllLine.AssignValueColor(if close<extllLine then GlobalColor("bearBorder") else GlobalColor("bullBorder"));
extlhLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
extlhLine.AssignValueColor(if close<extllLine then GlobalColor("bearBorder") else GlobalColor("bullBorder"));
llLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
llLine.AssignValueColor(if BearColor then GlobalColor("bearBorder") else GlobalColor("bullBorder"));
lhLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
lhLine.AssignValueColor(if BearColor then GlobalColor("bearBorder") else GlobalColor("bullBorder"));

plot hhLine1 =  if extend or NumberOfPivots<2 then na else phHH1;
plot hlLine1 =  if extend or NumberOfPivots<2 then na else phHL1 ;
def counthhLine1 = if hhLine1==hhLine1[1] then counthhLine1[1] + 1 else 0;
def counthlLine1 = if hlLine1==hlLine1[1] then counthlLine1[1] + 1 else 0;
plot exthhLine1  = if counthhLine1<=lookRight then phHH1[lookRight] else na;
plot exthlLine1  = if counthlLine1<=lookRight then phHL1[lookRight] else na;
exthhLine1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
exthhLine1.AssignValueColor(if close>exthhLine1 then GlobalColor("bullBorder") else GlobalColor("bearBorder"));
exthlLine1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
exthlLine1.AssignValueColor(if close>exthhLine1 then GlobalColor("bullBorder") else GlobalColor("bearBorder"));
hhLine1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
hhLine1.AssignValueColor(if BullColor1 then GlobalColor("bullBorder") else GlobalColor("bearBorder"));
hlLine1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
hlLine1.AssignValueColor(if BullColor1 then GlobalColor("bullBorder") else GlobalColor("bearBorder"));

plot llLine1 =  if extend or NumberOfPivots<2 then na else plLL1;
plot lhLine1 =  if extend or NumberOfPivots<2 then na else plLH1;
def  countllLine1 = if llLine1==llLine1[1] then countllLine1[1] + 1 else 0;
def  countlhLine1 = if lhLine1==lhLine1[1] then countlhLine1[1] + 1 else 0;
plot extllLine1 = if countllLine1<=lookRight then plLL1[lookRight] else na;
plot extlhLine1 = if countlhLine1<=lookRight then plLH1[lookRight] else na;
extllLine1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
extllLine1.AssignValueColor(if close<extllLine1 then GlobalColor("bearBorder") else GlobalColor("bullBorder"));
extlhLine1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
extlhLine1.AssignValueColor(if close<extllLine1 then GlobalColor("bearBorder") else GlobalColor("bullBorder"));
llLine1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
llLine1.AssignValueColor(if BearColor1 then GlobalColor("bearBorder") else GlobalColor("bullBorder"));
lhLine1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
lhLine1.AssignValueColor(if BearColor1 then GlobalColor("bearBorder") else GlobalColor("bullBorder"));

plot hhLine2 =  if extend or NumberOfPivots<3 then na else phHH2;
plot hlLine2 =  if extend or NumberOfPivots<3 then na else phHL2;
def counthhLine2 = if hhLine2==hhLine2[1] then counthhLine2[1] + 1 else 0;
def counthlLine2 = if hlLine2==hlLine2[1] then counthlLine2[1] + 1 else 0;
plot exthhLine2  = if counthhLine2<=lookRight then phHH2[lookRight] else na;
plot exthlLine2  = if counthlLine2<=lookRight then phHL2[lookRight] else na;
exthhLine2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
exthhLine2.AssignValueColor(if close>exthhLine2 then GlobalColor("bullBorder") else GlobalColor("bearBorder"));
exthlLine2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
exthlLine2.AssignValueColor(if close>exthhLine2 then GlobalColor("bullBorder") else GlobalColor("bearBorder"));
hhLine2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
hhLine2.AssignValueColor(if BullColor2 then GlobalColor("bullBorder") else GlobalColor("bearBorder"));
hlLine2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
hlLine2.AssignValueColor(if BullColor2 then GlobalColor("bullBorder") else GlobalColor("bearBorder"));

plot llLine2 =  if extend or NumberOfPivots<3 then na else plLL2;
plot lhLine2 =  if extend or NumberOfPivots<3 then na else plLH2;
def  countllLine2 = if llLine2==llLine2[1] then countllLine2[1] + 1 else 0;
def  countlhLine2 = if lhLine2==lhLine2[1] then countlhLine2[1] + 1 else 0;
plot extllLine2 = if countllLine2<=lookRight then plLL2[lookRight] else na;
plot extlhLine2 = if countlhLine2<=lookRight then plLH2[lookRight] else na;
extllLine2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
extllLine2.AssignValueColor(if close<extllLine2 then GlobalColor("bearBorder") else GlobalColor("bullBorder"));
extlhLine2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
extlhLine2.AssignValueColor(if close<extllLine2 then GlobalColor("bearBorder") else GlobalColor("bullBorder"));
llLine2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
llLine2.AssignValueColor(if BearColor2 then GlobalColor("bearBorder") else GlobalColor("bullBorder"));
lhLine2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
lhLine2.AssignValueColor(if BearColor2 then GlobalColor("bearBorder") else GlobalColor("bullBorder"));

plot hhLine3 =  if extend or NumberOfPivots<4 then na else phHH3;
plot hlLine3 =  if extend or NumberOfPivots<4 then na else phHL3;
def counthhLine3 = if hhLine3==hhLine3[1] then counthhLine3[1] + 1 else 0;
def counthlLine3 = if hlLine3==hlLine3[1] then counthlLine3[1] + 1 else 0;
plot exthhLine3  = if counthhLine3<=lookRight then phHH3[lookRight] else na;
plot exthlLine3  = if counthlLine3<=lookRight then phHL3[lookRight] else na;
exthhLine3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
exthhLine3.AssignValueColor(if close>exthhLine3 then GlobalColor("bullBorder") else GlobalColor("bearBorder"));
exthlLine3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
exthlLine3.AssignValueColor(if close>exthhLine3 then GlobalColor("bullBorder") else GlobalColor("bearBorder"));
hhLine3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
hhLine3.AssignValueColor(if BullColor3 then GlobalColor("bullBorder") else GlobalColor("bearBorder"));
hlLine3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
hlLine3.AssignValueColor(if BullColor3 then GlobalColor("bullBorder") else GlobalColor("bearBorder"));

plot llLine3 =  if extend or NumberOfPivots<4 then na else plLL3;
plot lhLine3 =  if extend or NumberOfPivots<4 then na else plLH3;
def  countllLine3 = if llLine3==llLine3[1] then countllLine3[1] + 1 else 0;
def  countlhLine3 = if lhLine3==lhLine3[1] then countlhLine3[1] + 1 else 0;
plot extllLine3 = if countllLine3<=lookRight then plLL3[lookRight] else na;
plot extlhLine3 = if countlhLine3<=lookRight then plLH3[lookRight] else na;
extllLine3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
extllLine3.AssignValueColor(if close<extllLine3 then GlobalColor("bearBorder") else GlobalColor("bullBorder"));
extlhLine3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
extlhLine3.AssignValueColor(if close<extllLine3 then GlobalColor("bearBorder") else GlobalColor("bullBorder"));
llLine3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
llLine3.AssignValueColor(if BearColor3 then GlobalColor("bearBorder") else GlobalColor("bullBorder"));
lhLine3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
lhLine3.AssignValueColor(if BearColor3 then GlobalColor("bearBorder") else GlobalColor("bullBorder"));

plot hhLine4 =  if extend or NumberOfPivots<5 then na else phHH4;
plot hlLine4 =  if extend or NumberOfPivots<5 then na else phHL4;
def counthhLine4 = if hhLine4==hhLine4[1] then counthhLine4[1] + 1 else 0;
def counthlLine4 = if hlLine4==hlLine4[1] then counthlLine4[1] + 1 else 0;
plot exthhLine4  = if counthhLine4<=lookRight then phHH4[lookRight] else na;
plot exthlLine4  = if counthlLine4<=lookRight then phHL4[lookRight] else na;
exthhLine4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
exthhLine4.AssignValueColor(if close>exthhLine4 then GlobalColor("bullBorder") else GlobalColor("bearBorder"));
exthlLine4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
exthlLine4.AssignValueColor(if close>exthhLine4 then GlobalColor("bullBorder") else GlobalColor("bearBorder"));
hhLine4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
hhLine4.AssignValueColor(if BullColor4 then GlobalColor("bullBorder") else GlobalColor("bearBorder"));
hlLine4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
hlLine4.AssignValueColor(if BullColor4 then GlobalColor("bullBorder") else GlobalColor("bearBorder"));

plot llLine4 =  if extend or NumberOfPivots<5 then na else plLL4;
plot lhLine4 =  if extend or NumberOfPivots<5 then na else plLH4;
def  countllLine4 = if llLine4==llLine4[1] then countllLine4[1] + 1 else 0;
def  countlhLine4 = if lhLine4==lhLine4[1] then countlhLine4[1] + 1 else 0;
plot extllLine4 = if countllLine4<=lookRight then plLL4[lookRight] else na;
plot extlhLine4 = if countlhLine4<=lookRight then plLH4[lookRight] else na;
extllLine4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
extllLine4.AssignValueColor(if close<extllLine4 then GlobalColor("bearBorder") else GlobalColor("bullBorder"));
extlhLine4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
extlhLine4.AssignValueColor(if close<extllLine4 then GlobalColor("bearBorder") else GlobalColor("bullBorder"));
llLine4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
llLine4.AssignValueColor(if BearColor4 then GlobalColor("bearBorder") else GlobalColor("bullBorder"));
lhLine4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
lhLine4.AssignValueColor(if BearColor4 then GlobalColor("bearBorder") else GlobalColor("bullBorder"));

#--
AddCloud(if hhLine==hhLine[1] and BullColor then hhLine else na, hlLine, GlobalColor("bearCloud"));
AddCloud(if hhLine==hhLine[1] and !BullColor then hhLine else na, hlLine, GlobalColor("bullCloud"));
AddCloud(if hhLine1==hhLine1[1] and BullColor1 then hhLine1 else na, hlLine1, GlobalColor("bearCloud"));
AddCloud(if hhLine1==hhLine1[1] and !BullColor1 then hhLine1 else na, hlLine1, GlobalColor("bullCloud"));
AddCloud(if hhLine2==hhLine2[1] and BullColor2 then hhLine2 else na, hlLine2, GlobalColor("bearCloud"));
AddCloud(if hhLine2==hhLine2[1] and !BullColor2 then hhLine2 else na, hlLine2, GlobalColor("bullCloud"));
AddCloud(if hhLine3==hhLine3[1] and BullColor3 then hhLine3 else na, hlLine3, GlobalColor("bearCloud"));
AddCloud(if hhLine3==hhLine3[1] and !BullColor3 then hhLine3 else na, hlLine3, GlobalColor("bullCloud"));
AddCloud(if hhLine4==hhLine4[1] and BullColor4 then hhLine4 else na, hlLine4, GlobalColor("bearCloud"));
AddCloud(if hhLine4==hhLine4[1] and !BullColor4 then hhLine4 else na, hlLine4, GlobalColor("bullCloud"));

AddCloud(if llLine==llLine[1] and BearColor then lhLine else na, llLine, GlobalColor("bullCloud"));
AddCloud(if llLine==llLine[1] and !BearColor then lhLine else na, llLine, GlobalColor("bearCloud"));
AddCloud(if llLine1==llLine1[1] and BearColor1 then lhLine1 else na, llLine1, GlobalColor("bullCloud"));
AddCloud(if llLine1==llLine1[1] and !BearColor1 then lhLine1 else na, llLine1, GlobalColor("bearCloud"));
AddCloud(if llLine2==llLine2[1] and BearColor2 then lhLine2 else na, llLine2, GlobalColor("bullCloud"));
AddCloud(if llLine2==llLine2[1] and !BearColor2 then lhLine2 else na, llLine2, GlobalColor("bearCloud"));
AddCloud(if llLine3==llLine3[1] and BearColor3 then lhLine3 else na, llLine3, GlobalColor("bullCloud"));
AddCloud(if llLine3==llLine3[1] and !BearColor3 then lhLine3 else na, llLine3, GlobalColor("bearCloud"));
AddCloud(if llLine4==llLine4[1] and BearColor3 then lhLine3 else na, llLine3, GlobalColor("bullCloud"));
AddCloud(if llLine4==llLine4[1] and !BearColor3 then lhLine3 else na, llLine3, GlobalColor("bearCloud"));
#--- Ext
AddCloud(if exthhLine==exthhLine[1] and close>exthhLine then exthhLine else na, exthlLine, GlobalColor("bearCloud"));
AddCloud(if exthhLine==exthhLine[1] and close<=exthhLine then exthhLine else na, exthlLine, GlobalColor("bullCloud"));
AddCloud(if exthhLine1==exthhLine1[1] and close>exthhLine1 then exthhLine1 else na, exthlLine1, GlobalColor("bearCloud"));
AddCloud(if exthhLine1==exthhLine1[1] and close<=exthhLine1 then exthhLine1 else na, exthlLine1, GlobalColor("bullCloud"));
AddCloud(if exthhLine2==exthhLine2[1] and close>exthhLine2 then exthhLine2 else na, exthlLine2, GlobalColor("bearCloud"));
AddCloud(if exthhLine2==exthhLine2[1] and close<=exthhLine2 then exthhLine2 else na, exthlLine2, GlobalColor("bullCloud"));
AddCloud(if exthhLine3==exthhLine3[1] and close>exthhLine3 then exthhLine3 else na, exthlLine3, GlobalColor("bearCloud"));
AddCloud(if exthhLine3==exthhLine3[1] and close<=exthhLine3 then exthhLine3 else na, exthlLine3, GlobalColor("bullCloud"));
AddCloud(if exthhLine4==exthhLine4[1] and close>exthhLine4 then exthhLine4 else na, exthlLine4, GlobalColor("bearCloud"));
AddCloud(if exthhLine4==exthhLine4[1] and close<=exthhLine4 then exthhLine4 else na, exthlLine4, GlobalColor("bullCloud"));

AddCloud(if extllLine==extllLine[1] and close<extllLine then extlhLine else na, extllLine, GlobalColor("bullCloud"));
AddCloud(if extllLine==extllLine[1] and close>=extllLine then extlhLine else na, extllLine, GlobalColor("bearCloud"));
AddCloud(if extllLine1==extllLine1[1] and close<extllLine1 then extlhLine1 else na, extllLine1, GlobalColor("bullCloud"));
AddCloud(if extllLine1==extllLine1[1] and close>=extllLine1 then extlhLine1 else na, extllLine1, GlobalColor("bearCloud"));
AddCloud(if extllLine2==extllLine2[1] and close<extllLine2 then extlhLine2 else na, extllLine2, GlobalColor("bullCloud"));
AddCloud(if extllLine2==extllLine2[1] and close>=extllLine2 then extlhLine2 else na, extllLine2, GlobalColor("bearCloud"));
AddCloud(if extllLine3==extllLine3[1] and close<extllLine3 then extlhLine3 else na, extllLine3, GlobalColor("bullCloud"));
AddCloud(if extllLine3==extllLine3[1] and close>=extllLine3 then extlhLine3 else na, extllLine3, GlobalColor("bearCloud"));
AddCloud(if extllLine4==extllLine4[1] and close<extllLine4 then extlhLine4 else na, extllLine4, GlobalColor("bullCloud"));
AddCloud(if extllLine4==extllLine4[1] and close>=extllLine4 then extlhLine4 else na, extllLine4, GlobalColor("bearCloud"));

#--- Break
AddChartBubble(ShowBreakUp and srcHigh crosses above hhLine and !BullColor[lookRight], low, "Break", Color.GREEN, no);
AddChartBubble(ShowBreakDn and srcLow crosses below llLine and !BearColor[lookRight], high, "Break", Color.RED, yes);

#Alerts
Alert(alert and ShowBreakUp and srcHigh crosses above hhLine and  low, "Break Long",  Alert.Bar, sound.Ring);
Alert(alert and ShowBreakDn and srcLow crosses below llLine and  high, "Break Short",  Alert.Bar, sound.Ring);

#--- END CODE
 
Last edited by a moderator:
View attachment 17188

I converted partially. pls check.
@samer800 This is a great script, thank you. Can you advise if it’s possible to modify the code so that with “input lookRight = 0;” it can still plot? I wanted to try that as a way of preventing it from repainting based on the look forward (I understand that of course it can still repaint based on high/low pivots changing). But it cannot plot unless that input is set to at least 1. I don’t see why the key levels couldn’t just be calculated from the lookback.

Edit: actually there seems to be almost no difference between lookright==1 and 2 or something and I am not seeing much evidence of significant repainting with the value set to 1. So it’s probably not worth the trouble of modifying.
 
Last edited:
@samer800 @METAL This looks like a great indicator for support/resistance. Is it possible to create watchlist code for Bjorgum Key Levels ?
This script is too complex for use in watchlists, scans, conditional orders and all other widgets that utilize the ToS Condition Wizard.
 
@samer800 , Is there a way to make this work as a MTF? What I would like to accomplish, if possible, is to trade on the 2 min. but have the level show as if they are on a higher time frame. I have tried changing the lookback to see if I can get the same levels to show but to no avail.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
248 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