starwithnobucks
Pine Script Rookie
Pine Script Rookie
Posts: 1
Joined: January 15th, 2024
Contact: TradingView Profile

Market Structure. Minor plotting issue.

Can anyone help me with a pine script code that needs some tweaking. It's related to market structure.

The problem is that after a BOS or CHoCH the New HH or LL label should be plotted. Once it is plotted the HH or LL is formed. After that the new LL or HH label should be plotted only after new BoS and CHoCH. But when it wicks the high it is then again checking the new HH LL label. It is not supposed to that. I guess flags are to be set to match the condition. But I just cannot figure it out.

Code: Select all

//@version=5
indicator("Structure", max_bars_back = 5000, max_labels_count = 500, max_lines_count = 500, overlay = true)

//#region variable declaration and flags
var bool mnUp = na
var bool mnDn = na
float mnStrc = na
var top = high
var bot = low
var puUp = high
var puDn = low
var L = low
var H = high
var scB = low
var scS = high
var lastH = high
var lastL = low
var lastHH = high
var lastLL = low
//bar indexes
var int puUpbar = na
var int puDnbar = na
var int scB_bar = na
var int scS_bar = na
var int Hbar = bar_index
var int Lbar = bar_index
var int lastHbar = bar_index
var int lastLbar = bar_index
var int lastHHbar = bar_index
var int lastLLbar = bar_index
//structure confirmaions
lastHL = math.max(Hbar, Lbar)
var bool isBosUp = false
var bool isBosDn = false
var bool isCocUp = true
var bool isCocDn = true
//Check Structure
var bool recentpro = false

//color
color transp = color.new(color.gray,100)

//drawing options
equalHL = false

showHL = input.bool(true, "Mark H/L", group = "H/L marking options")
HLcolor = input.color(color.gray, "H/L color", group = "H/L marking options")

showMn = input.bool(false, "Mark out internal structure", group = "Internal structue")
puUpco = input.color(color.red  , "High pivots", inline = "mnco", group = "Internal structue")
puDnco = input.color(color.green, "Low pivots" , inline = "mnco", group = "Internal structue")

showBC = input.bool(true, "Mark BoS/ChoCH", group = "BoS/Choch")
bull = input.color(color.green, "Bull color", inline = "BSclor" ,group = "BoS/Choch")
bear = input.color(color.red  , "Bear color", inline = "BSclor" ,group = "BoS/Choch")

showsc = input.bool(true,"Mark previous sc", group = "sc")
showlivesc = input.bool(true,"Mark live sc", group = "sc")
scColor = input.color(color.gray, "sc color", group = "sc")
var label lv_lbl = na
var line lv_line = na

showSw = input.bool(true, "Show H/L pro lines", group = "H/L pros")
markX = input.bool(true, 'Mark "X"', group = "H/L pros")
swColor  = input.color(color.gray, "pro line color", group = "H/L pros")

//max line length
maxlen = bar_index - 500

//#endregion

//#region drawing functions
mnMark(bool UD) =>
    if showMn 
        label.new(
         x = UD ? puUpbar : puDnbar,
         y = UD ? puUp : puDn,
         yloc = UD ? yloc.abovebar : yloc.belowbar,
         text = "",
         color = UD ? puUpco : puDnco,
         style = UD ? label.style_arrowdown : label.style_arrowup,
         size = size.tiny
         )

BoS_ChoCh(bool B_C, bool UpDn) =>
    [HLbarid, BCprc, BCcolor, BCtxt, BCstyle] = switch
        B_C == true and UpDn == true => [lastHHbar, lastHH, bull, "BoS"  , label.style_label_down]
        B_C == false and UpDn == true => [lastHbar , lastH , bull, "ChoCh", label.style_label_down]
        B_C == false and UpDn == false => [lastLbar , lastL , bear, "ChoCh", label.style_label_up  ]
        B_C == true and UpDn == false => [lastLLbar, lastLL, bear, "BoS"  , label.style_label_up  ]
    HLbarid := HLbarid < maxlen ? maxlen : HLbarid
    if showBC
        line.new(HLbarid, BCprc, bar_index, BCprc, color = BCcolor, style = line.style_dashed)
        label.new(
         int( math.avg(bar_index,HLbarid) ), BCprc
          ,BCtxt
          ,color = transp
          ,style = BCstyle
          ,textcolor = BCcolor
          )

cfHL(bool ifHL) =>
    [ifHLbarid, HLprc] = switch
        ifHL => 
            [Hbar, H]
        =>
            [Lbar, L]
    ifHL_txt = if ifHL
        if H == lastHH
            "HH"
        else
            "LH"
    else
        if L == lastLL
            "LL"
        else
            "HL"
    cfHLStyle = ifHL ? label.style_label_down : label.style_label_up
    if showHL
        label.new(ifHLbarid,HLprc,ifHL_txt, color = transp, textcolor = HLcolor, style = cfHLStyle)

pro(bool swHL, bool swHrLr) =>
    swStyle = swHL ? label.style_label_down : label.style_label_up
    [swHLbarid, swprc, swHL_txt] = switch
        swHL == true and swHrLr == true => [lastHHbar, lastHH, "HH"]
        swHL == true and swHrLr == false => [lastHbar , lastH , "LH"]
        swHL == false and swHrLr == true => [lastLbar , lastL , "HL"]
        swHL == false and swHrLr == false => [lastLLbar, lastLL, "LL"]
    swHLbarid := swHLbarid < maxlen ? maxlen : swHLbarid
    //draw pro line
    if showSw
        line.new(swHLbarid,swprc,bar_index,swprc,color = swColor,style = line.style_dotted)
        if markX
            label.new(int(math.avg(bar_index,swHLbarid)),swprc, "X", color = transp, textcolor = swColor, style = swStyle, size = size.small)
    true

sc(bool BS) =>
    scid = BS ? scB_bar : scS_bar
    scprc = BS ? scB : scS
    scStyle = BS ? label.style_label_up : label.style_label_down
    scid := scid < maxlen ? maxlen : scid
    //draw sc takeout line
    if showsc
        line.new(scid,scprc,bar_index,scprc,color = scColor,style = line.style_dotted)
        label.new(int(math.avg(bar_index,scid)),scprc, "SC", color = transp, textcolor = scColor, style = scStyle,size = size.small)
        

//#endregion

//#region structure mapping
//update sc
if high > lastH 
    if low < bot and close > open 
        if low < scB and lastHL != Lbar 
            sc(true)
            cfHL(true)
            isBosUp := false
            L := low
            Lbar := bar_index
        scB := low
        scB_bar := bar_index
    else 
        scB := puDn
        scB_bar := puDnbar

if low < lastL
    if high > top and close < open
        if high > scS and lastHL != Hbar 
            sc(false)
            cfHL(false)
            isBosDn := false
            H := high
            Hbar := bar_index
        scS := high
        scS_bar := bar_index
    else
        scS := puUp
        scS_bar := puUpbar

//Check for sc and ChoCh
if isCocUp and lastHL != Lbar
    if low < scB
        if scB != lastL
            sc(true)
        isBosUp := false
        if high > lastH and close < open
            H := high
            Hbar := bar_index
        lastH := H
        lastHbar := Hbar
        lastHH := H
        lastHHbar := Hbar
        cfHL(true)
        L := low
        Lbar := bar_index
else if lastH != lastHH and high > lastH
    if close > lastH
        BoS_ChoCh(false, true)
        cfHL(false)
        isCocDn := false
        isBosDn := false
        isCocUp := true
    else
        recentpro := pro(true,false)
        

if isCocDn and lastHL != Hbar
    if high > scS
        if scS != lastH
            sc(false)
        isBosDn := false
        if low < lastL and close > open
            L := low
            Lbar := bar_index
        lastL := L
        lastLbar := Lbar
        lastLL := L
        lastLLbar := Lbar
        cfHL(false)
        H := high
        Hbar := bar_index
else if low < lastL and lastL != lastLL
    if close < lastL
        BoS_ChoCh(false,false)
        cfHL(true)
        isCocDn := true
        isCocUp := false
        isBosUp := false
    else
        recentpro := pro(false,true)

//Check for BoS
if isBosUp == false
    if high > lastHH
        if close > lastHH
            BoS_ChoCh(true,true)
            cfHL(false)
            lastL := L
            lastLbar := Lbar
            isCocUp := true
            isBosUp := true
            isCocDn := false
            isBosDn := false
        else
            recentpro := pro(true,true)
if isBosDn == false
    if low < lastLL
        if close < lastLL
            BoS_ChoCh(true,false)
            cfHL(true)
            lastH := H
            lastHbar := Hbar
            isCocUp := false
            isBosUp := false
            isCocDn := true
            isBosDn := true
        else
            recentpro := pro(false,false)
//#endregion

//#region internal structure
if high > top        
    if low > bot
        mnDn :=false
    mnUp := true
if low < bot
    if high < top
        mnUp := false
    mnDn := true
if mnUp[1] and not mnUp
    mnMark(true)
if mnDn[1] and not mnDn
    mnMark(false)

if high > top
    puUp := high
    puUpbar := bar_index
if low < bot
    puDn := low
    puDnbar := bar_index
    top := high
    bot := low
if high > top
    top := high
    bot := low
//#endregion

//#region auto update High and Low vars
if high > H
    H := high
    Hbar := bar_index

if high > lastH
    lastH := high
    lastHbar := bar_index
if high > lastHH
    lastHH := high
    lastHHbar := bar_index

if low < L
    L := low
    Lbar := bar_index
if low < scB
    scB := low
    scB_bar := bar_index
if low < lastL
    lastL := low
    lastLbar := bar_index
if low < lastLL
    lastLL := low
    lastLLbar := bar_index

//#endregion
Error: https://ibb.co/sqtkVdX
It should be like this: https://ibb.co/0cTmryC

Return to “Pine Script Q&A”