Repaints Higher high Lower Low For ThinkOrSwim

Repaints

Orca

New member
The author states
Generally HH and HL shows up-trend, LL and LH shows down-trend.

If price breaks resistance levels it means the trend is up or if price breaks support level it means the trend is down, so the script changes bar color blue or black. if there is up-trend then bar color is blue, or if down-trend then bar color is black. also as you can see support and resistance levels change dynamically.

usQjUqM.png



The original Tradingview code:
https://www.tradingview.com/script/vcg3prja/

The new ThinkOrSwim code can be found below.
 
Last edited by a moderator:

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

Hey @samer800
do you have a indicator that shows higher highs and lower lows?
If not could you convert this for TOS
https://www.tradingview.com/script/vcg3prja/


//@version=4
study("Higher High Lower Low Strategy", overlay =true, max_lines_count = 500)
lb = input(5, title="Left Bars", minval = 1)
rb = input(5, title="Right Bars", minval = 1)
showsupres = input(true, title="Support/Resistance", inline = "srcol")
supcol = input(color.lime, title ="", inline = "srcol")
rescol = input(color.red, title ="", inline = "srcol")
srlinestyle = input(line.style_dotted, title = "Line Style/Width", options = [line.style_solid, line.style_dashed, line.style_dotted], inline ="style")
srlinewidth = input(3, title = "", minval = 1, maxval = 5, inline ="style")
changebarcol = input(true, title="Change Bar Color", inline = "bcol")
bcolup = input(color.blue, title ="", inline = "bcol")
bcoldn = input(color.black, title ="", inline = "bcol")

ph = pivothigh(lb, rb)
pl = pivotlow(lb, rb)

hl = iff(ph, 1, iff(pl, -1, na)) // Trend direction
zz = iff(ph, ph, iff(pl, pl, na)) // similar to zigzag but may have multiple highs/lows
zz :=iff(pl and hl == -1 and valuewhen(hl, hl, 1) == -1 and pl > valuewhen(zz, zz, 1), na, zz)
zz :=iff(ph and hl == 1 and valuewhen(hl, hl, 1) == 1 and ph < valuewhen(zz, zz, 1), na, zz)

hl := iff(hl==-1 and valuewhen(hl, hl, 1)==1 and zz > valuewhen(zz, zz, 1), na, hl)
hl := iff(hl==1 and valuewhen(hl, hl, 1)==-1 and zz < valuewhen(zz, zz, 1), na, hl)
zz := iff(na(hl), na, zz)

findprevious()=> // finds previous three points (b, c, d, e)
ehl = iff(hl==1, -1, 1)
loc1 = 0.0, loc2 = 0.0, loc3 = 0.0, loc4 = 0.0
xx = 0
for x=1 to 1000
if hl[x]==ehl and not na(zz[x])
loc1 := zz[x]
xx := x + 1
break
ehl := hl
for x=xx to 1000
if hl[x]==ehl and not na(zz[x])
loc2 := zz[x]
xx := x + 1
break
ehl := iff(hl==1, -1, 1)
for x=xx to 1000
if hl[x]==ehl and not na(zz[x])
loc3 := zz[x]
xx := x + 1
break
ehl := hl
for x=xx to 1000
if hl[x]==ehl and not na(zz[x])
loc4 := zz[x]
break
[loc1, loc2, loc3, loc4]

float a = na, float b = na, float c = na, float d = na, float e = na
if not na(hl)
[loc1, loc2, loc3, loc4] = findprevious()
a := zz
b := loc1
c := loc2
d := loc3
e := loc4

_hh = zz and (a > b and a > c and c > b and c > d)
_ll = zz and (a < b and a < c and c < b and c < d)
_hl = zz and ((a >= c and (b > c and b > d and d > c and d > e)) or (a < b and a > c and b < d))
_lh = zz and ((a <= c and (b < c and b < d and d < c and d < e)) or (a > b and a < c and b > d))

plotshape(_hl, text="HL", title="Higher Low", style=shape.labelup, color=color.lime, textcolor=color.black, location=location.belowbar, offset = -rb)
plotshape(_hh, text="HH", title="Higher High", style=shape.labeldown, color=color.lime, textcolor=color.black, location=location.abovebar, offset = -rb)
plotshape(_ll, text="LL", title="Lower Low", style=shape.labelup, color=color.red, textcolor=color.white, location=location.belowbar, offset = -rb)
plotshape(_lh, text="LH", title="Lower High", style=shape.labeldown, color=color.red, textcolor=color.white, location=location.abovebar, offset = -rb)

float res = na, float sup = na
res := iff(_lh, zz, res[1])
sup := iff(_hl, zz, sup[1])

int trend = na
trend := iff(close > res, 1, iff(close < sup, -1, nz(trend[1])))

res := iff((trend == 1 and _hh) or (trend == -1 and _lh), zz, res)
sup := iff((trend == 1 and _hl) or (trend == -1 and _ll), zz, sup)
rechange = res != res[1]
suchange = sup != sup[1]

var line resline = na
var line supline = na
if showsupres
if rechange
line.set_x2(resline, bar_index)
line.set_extend(resline, extend = extend.none)
resline := line.new(x1 = bar_index - rb, y1 = res, x2 = bar_index, y2 = res, color = rescol, extend = extend.right, style = srlinestyle, width = srlinewidth)

if suchange
line.set_x2(supline, bar_index)
line.set_extend(supline, extend = extend.none)
supline := line.new(x1 = bar_index - rb, y1 = sup, x2 = bar_index, y2 = sup, color = supcol, extend = extend.right, style = srlinestyle, width = srlinewidth)

barcolor(color = iff(changebarcol, iff(trend == 1, bcolup, bcoldn), na))
check the below:

Rich (BB code):
#// Indicator for TOS
#// © LonesomeThecolor.blue
#study("Higher High Lower Low Strategy", overlay =true, max_lines_count = 500)
# Converted by Sam4Cok@Samer800    - 10/2024

input showLines = yes;      # "Support/Resistance"
input changeBarColor = no;  # Change Bar Color"
input LeftBars = 5;         # "Left Bars"
input RightBars = 5;        # "Right Bars"

def na = Double.NaN;
def last = IsNaN(close);
def lb = LeftBars;
def rb = RightBars;

script Pivot {
    input series    = close;
    input leftBars  = 10;
    input rightBars = 10;
    input isHigh = yes;
    def na = Double.NaN;
    def HH = series == Highest(series, leftBars + 1);
    def LL = series == Lowest(series, leftBars + 1);
    def pivotRange = (leftBars + rightBars + 1);
    def leftEdgeValue = if series[pivotRange] == 0 then na else series[pivotRange];
    def pvtCond = !IsNaN(series) and leftBars > 0 and rightBars > 0 and !IsNaN(leftEdgeValue);
    def barIndexH = if pvtCond then
                    fold i = 1 to rightBars + 1 with p=1 while p do
                    series > GetValue(series, - i) else na;
    def barIndexL = if pvtCond then
                    fold j = 1 to rightBars + 1 with q=1 while q do
                    series < GetValue(series, - j) else na;
    def PivotPoint;
    if isHigh {
        PivotPoint = if HH and barIndexH then series else na;
    } else {
        PivotPoint = if LL and barIndexL then series else na;
    }
    plot pvt = PivotPoint;
}

def ph_ = pivot(high[rb], lb, rb, yes);
def pl_ = pivot(low[rb] , lb, rb, no);
def phz = !IsNaN(ph_);
def plz = !IsNaN(pl_);
def ph = high[rb];
def pl = low[rb];
def pvt = (!IsNaN(ph_) or !IsNaN(pl_));

def hl0; def zz0; def hl00;
if phz {
    hl0  = 1;
    hl00 = 1;
    zz0  = ph;
} else if plz {
    hl0  = -1;
    hl00 = -1;
    zz0  = pl;
    } else {
    hl0  = na;
    hl00 = hl00[1];
    zz0  = zz0[1];
}
def zz1  = if (plz and hl00[1] ==-1 and pl > zz0[1]) then na else zz0;
def zz11 = if !isNaN(zz1) then zz1 else zz11[1];

def zz2  = if (phz and hl00[1] == 1 and ph < zz11[1]) then na else zz1;
def zz22 = if !isNaN(zz2) then zz2 else zz22[1];

def hl1  = if (plz and hl00[1]== 1 and zz2 > zz22[1]) then na else hl0;
def hl11 = if !isNaN(hl1) then hl1 else hl11[1];

def hl   = if (!isNaN(hl1) and hl1==1 and hl11[1]==-1 and zz2 < zz22[1]) then na else hl1;
def zz   = if isNaN(hl) then na else zz2;

def a; def b; def c; def d; def e;
if !isNaN(zz) {
    e = d[1];
    d = c[1];
    c = b[1];
    b = a[1];
    a = zz;
    } else {
    a = a[1];
    b = b[1];
    c = c[1];
    d = d[1];
    e = e[1];
}

def _hh = !isNaN(zz) and phz and (a >= b and a >= c and a >= d and a > e);
def _ll = !isNaN(zz) and plz and (a <= b and a <= c and a <= d and a < e);
def _hl = !isNaN(zz) and plz and ((a >= c and (b > c and b > d and d > c and d > e)) or (a < b and a > c and b < d));
def _lh = !isNaN(zz) and phz and ((a <= c and (b < c and b < d and d < c and d < e)) or (a > b and a < c and b > d));
def noT = !_hh and !_ll and !_hl and !_lh;

def _hh2 = !isNaN(zz) and noT and phz and (a > b and b < c and c > d and a > c);
def _ll2 = !isNaN(zz) and noT and plz and (a < b and b > c and c < d and a < c);
def _hl2 = !isNaN(zz) and noT and plz and !_ll2 and ((a <= b and b > c ) or (a <= b and b < c and (a > d or a > e)));
def _lh2 = !isNaN(zz) and noT and phz and !_hh2 and ((a >= b and b < c ) or (a >= b and b > c and (a < d or a < e)));

AddChartBubble(_hh[-rb], high, "HH", Color.GREEN);
AddChartBubble(_hl[-rb], low,  "HL", Color.DARK_GREEN, no);
AddChartBubble(_ll[-rb], low,  "LL", Color.RED, no);
AddChartBubble(_lh[-rb], high, "LH", Color.DARK_RED);

AddChartBubble(_hh2[-rb], high, "HH", Color.GREEN);
AddChartBubble(_ll2[-rb], low,  "LL", Color.RED, no);
AddChartBubble(_hl2[-rb], low,  "HL", Color.DARK_GREEN, no);
AddChartBubble(_lh2[-rb], high, "LH", Color.DARK_RED);


def res1; def sup1;
def res = if _lh then zz else if _lh2 then zz else res1[1];
def sup = if _hl then zz else if _hl2 then zz else sup1[1];

def trend = if close > res then 1 else if close < sup then -1 else trend[1];

res1 = if ((trend == 1 and _hh)  or (trend == -1 and _lh))  then zz else
       if ((trend == 1 and _hh2) or (trend == -1 and _lh2)) then zz else res;
sup1 = if ((trend == 1 and _hl)  or (trend == -1 and _ll))  then zz else
       if ((trend == 1 and _hl2) or (trend == -1 and _ll2)) then zz else sup;

def rechange = if !showLines then na else res1 != res1[1];
def suchange = if !showLines then na else sup1 != sup1[1];


def supLine0; def supline1; def supCnt;
if suchange {
    supCnt   = if supCnt[1] > rb then 0 else 1;
    supline1 = if (isNaN(supline1[1]) or !supCnt) then supLine0[1] else supline1[1];
    supLine0 = supLine0[1];
    } else {
    supCnt = if supCnt[1] > rb then 0 else supCnt[1] + 1;
    supline1 = if supCnt > rb then na else supline1[1];
    supLine0 = if !last and showLines then sup1 else supLine0[1];

    }

def resLine0; def resline1; def resCnt;
if rechange {
    resCnt   = if resCnt[1] > rb then 0 else 1;
    resline1 = if (isNaN(resline1[1]) or !resCnt) then resLine0[1] else resline1[1];
    resLine0 = resLine0[1];
    } else {
    resCnt = if resCnt[1] > rb then 0 else resCnt[1] + 1;
    resline1 = if resCnt > rb then na else resline1[1];
    resLine0 = if !last and showLines then res1 else resLine0[1];
    }

plot resLine = if !last[rb] and resLine0[-rb] then resLine0[-rb] else na;
plot res1Line = if resline1[-rb] then resline1[-rb] else na;

plot supLine = if !last[rb] and supLine0[-rb] then supLine0[-rb] else na;
plot sup1Line = if supline1[-rb] then supline1[-rb] else na;

resLine.SetDefaultColor(Color.MAGENTA);
res1Line.SetDefaultColor(Color.MAGENTA);
supLine.SetDefaultColor(Color.CYAN);
sup1Line.SetDefaultColor(Color.CYAN);
resLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
res1Line.SetPaintingStrategy(PaintingStrategy.DASHES);
supLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
sup1Line.SetPaintingStrategy(PaintingStrategy.DASHES);

AssignPriceColor(if !changeBarColor then Color.CURRENT else
                 if trend==1 then Color.GREEN else Color.RED);

#-- END of CODE
 

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
311 Online
Create Post

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