Search code examples
arraysplotlabelpine-scriptincrement

Is it possible to add Incremental Labels?


I know it's possible to make this indicator faster and more efficient using arrays but the concept of being able to plot hundreds of lines and labels with incremental look backs sounds great at first... but there's no way to distinguish which lines and labels belong where (while extended to current bar index). The concept behind this indicator is you're able to extract H, L, O, C from three previous sessions with labels indicating which are which and while increasing int values, sessions are replaced with their next three sessions as an easy workaround for incremental values. I just haven't found any way to fit a function that will count the look backs and then increase value for the next sessions only.

"for" function works ever so slightly but perhaps i'm using it incorrectly so would anyone know of a possible solution for this?

EX:

Default look back value shows 

9 Lines and 9 Labels (Previous 1-3 Day High, Low, Open, Close)

Labels appear as (1 Day High, 2 Day Low, 3 Day Open)

Next look back value now shows

9 Lines and 9 Labels (Previous 4-6 Day High, Low, Open, Close)

Labels appear as (4 Day High, 5 Day Low, 6 Day Open)

I want to be able to have

18 Lines and 18 Labels with Previous 1-6 Day High, Low, Open, Close

with labels showing (1, 2, 3 , 4, 5, 6 Day High, Low, Open, Close)

using arrays would easily achieve this but labels would appear as

(6, 6, 6, 6, 6, 6) since it's just showing the total value

If anyone is able to help or needs further explanation, please lmk!


//@version=5
indicator('Lines & Labels', 'LL', overlay=true, max_lines_count=100, max_labels_count=100)

Levels=input(true, 'Show Levels?', group='Levels', tooltip='[✓] Show Levels?\nPlots Daily, Weekly, Monthly, Yearly\nMarket Session Levels\n———<[O, H, L, C | Levels | % Levels]\n\nDays Default Levels:\n1-3 Day | High | Low | Open / Close')
LL=input.int(0, 'Levels', minval=0, step=3, group='Levels', inline='2')
OC=input.string('None', '', options=['Open', 'Close', 'None'], group='Levels', tooltip='1st Default Option Value: [0]\nShows Six Previous Sessions (O, H, L, C)\n———<[1 Session Open | 455.00 | 1.50%]\n———<[1 Session High | 460.00 | 2.50%]\n———<[1 Session Low | 445.00 | -0.50%]\n———<[1 Session Close | 450.00 | 0.01%]\n\nNext Value: Shows Next Six (O, H, L, C)\n———<[4 Session Open | 465.00 | 2.50%]\n———<[4 Session High | 470.00 | 4.50%]\n———<[4 Session Low | 455.00 | -1.50%]\n———<[4 Session Close | 440.00 | 0.01%]\n\n2nd Default Option: [None]\nHides Open / Close Data On Charts\n[Open]: Shows Lines & Labels\nBased Off Six Previous Open\n[Close]: Shows Lines & Labels\nBased Off Six Previous Close', inline='2')
ShowBC=input(true, 'Swap Colors?', group='Levels', inline='3')
BC=input(color.rgb(0, 0, 0), '', group='Levels', tooltip='[✓] Swap Colors?\nText Color Is Shown Using\nBackground Color\n\n[   ] Swap Colors?\nBackground Color Is Shown\nUsing Text Color', inline='3')

T(X, T) => str.tostring(X, T)
PCT(Levels) => ' | ' + str.tostring(Levels / close * 100 - 100, '0.00') + '%'
SWAP(OPEN, CLOSE) => OC == 'Open' ? OPEN : OC == 'Close' ? CLOSE : na
TX(COLOR) => ShowBC ? BC : COLOR
BG(COLOR) => ShowBC ? COLOR : BC

L(VL) => 
    L=math.min(time - time[1], time[1] - time[2])
    time + L * (10 + VL * 4)

Lines(X1, X2, Y, COLOR) =>
    var LINE=line.new(x1=X1, x2=X2, y1=Y, y2=Y, style=line.style_solid, width=1, xloc=xloc.bar_time)
    line.set_color(LINE, COLOR)

Labels(X, Y, TEXT, TEXTCOLOR, COLOR) =>
    var LABEL=label.new(x=X, y=Y, text=TEXT, textcolor=TEXTCOLOR, color=COLOR, 
  style=label.style_label_left, size=size.small, textalign=text.align_center, tooltip=TEXT, xloc=xloc.bar_time)

LVL(Levels, HLO, Time, Text, LineColor, TextColor, LabelColor) =>
    Lines(L(Levels), Time, HLO, LineColor)
    Labels(L(Levels), HLO, Text, TextColor, LabelColor)

TOHLC() => [
  time[1+LL], open[1+LL], high[1+LL], low[1+LL], close[1+LL], 
  time[2+LL], open[2+LL], high[2+LL], low[2+LL], close[2+LL], 
  time[3+LL], open[3+LL], high[3+LL], low[3+LL], close[3+LL]]

var DHColor=input(color.rgb(0, 150, 136), 'DH', group='Daily', inline='D')
var DLColor=input(color.rgb(244, 67, 54), 'DL', group='Daily', inline='D')
var DOColor=input(color.rgb(209, 212, 220), 'DO / DC', group='Daily', inline='D')

[DT1, DO1, DH1, DL1, DC1, DT2, DO2, DH2, DL2, DC2, DT3, DO3, DH3, DL3, DC3]
  =request.security(syminfo.tickerid, 'D', TOHLC(), lookahead=barmerge.lookahead_off)

if Levels and barstate.islast
    LVL(0, DH1, DT1, T(1+LL, '0') + 'DH | ' + T(DH1, '0.00'), DHColor, TX(DHColor), BG(DHColor))
    LVL(0, DH2, DT2, T(2+LL, '0') + 'DH | ' + T(DH2, '0.00'), DHColor, TX(DHColor), BG(DHColor))
    LVL(0, DH3, DT3, T(3+LL, '0') + 'DH | ' + T(DH3, '0.00'), DHColor, TX(DHColor), BG(DHColor))
    LVL(0, DL1, DT1, T(1+LL, '0') + 'DL | ' + T(DL1, '0.00'), DLColor, TX(DLColor), BG(DLColor))
    LVL(0, DL2, DT2, T(2+LL, '0') + 'DL | ' + T(DL2, '0.00'), DLColor, TX(DLColor), BG(DLColor))
    LVL(0, DL3, DT3, T(3+LL, '0') + 'DL | ' + T(DL3, '0.00'), DLColor, TX(DLColor), BG(DLColor))
    if OC == 'Open' or OC == 'Close'
        LVL(0, SWAP(DO1, DC1), DT1, T(1+LL, '0') + SWAP('DO | ', 'DC | ') + T(SWAP(DO1, DC1), '0.00'), DOColor, TX(DOColor), BG(DOColor))
        LVL(0, SWAP(DO2, DC2), DT2, T(2+LL, '0') + SWAP('DO | ', 'DC | ') + T(SWAP(DO2, DC2), '0.00'), DOColor, TX(DOColor), BG(DOColor))
        LVL(0, SWAP(DO3, DC3), DT3, T(3+LL, '0') + SWAP('DO | ', 'DC | ') + T(SWAP(DO3, DC3), '0.00'), DOColor, TX(DOColor), BG(DOColor))

Solution

  • 
    //@version=5
    indicator('Lines & Labels', 'LL', overlay=true, max_lines_count=100, max_labels_count=100)
    
    Levels=input(true, 'Show Levels?', group='Levels', inline='1')
    LL=input.int(1, '', minval=1, step=1, group='Levels', inline='1')
    ShowBC=input(true, 'Swap Colors?', group='Levels', inline='2')
    BC=input(color.rgb(0, 0, 0), '', group='Levels', inline='2')
    var DHColor=input(color.rgb(0, 150, 136), 'DH', group='Daily', inline='D')
    var DLColor=input(color.rgb(244, 67, 54), 'DL', group='Daily', inline='D')
    var DOColor=input(color.rgb(209, 212, 220), 'DO', group='Daily', inline='D')
    var DCColor=input(color.rgb(178, 181, 190), 'DC', group='Daily', inline='D')
    T(X, T) => str.tostring(X, T)
    TX(COLOR) => ShowBC ? BC : COLOR
    BG(COLOR) => ShowBC ? COLOR : BC
    [DT, DO, DH, DL, DC]=request.security(syminfo.tickerid, 'D', [time, open, high, low, close], lookahead=barmerge.lookahead_off)
    DTC=DT != DT[1]
    L() => bar_index + 10
    LVL(bool YES, int LookBack, float Highs, float Lows, float Opens, float Closes, color HighColor, color LowColor, color OpenColor, color CloseColor) => 
        var line H1=na
        var line L1=na
        var line O1=na
        var line C1=na
        var label H2=na
        var label L2=na
        var label O2=na
        var label C2=na
        var H3=array.new_line()
        var L3=array.new_line()
        var O3=array.new_line()
        var C3=array.new_line()
        var H4=array.new_label()
        var L4=array.new_label()
        var O4=array.new_label()
        var C4=array.new_label()
        if YES and Levels
            line.set_x2(H1, L())
            line.set_x2(L1, L())
            line.set_x2(O1, L())
            line.set_x2(C1, L())
            label.set_x(H2, L())
            label.set_x(L2, L())
            label.set_x(O2, L())
            label.set_x(C2, L())
            for i = 0 to array.size(H4) > 0 ? array.size(H4) -1 : na
                Count=array.size(H4) -1 -i
                label.set_text(array.get(H4, i), T(Count, '0') + ' DH | ' + T(label.get_y(array.get(H4, i)), '0.00'))
                label.set_text(array.get(L4, i), T(Count, '0') + ' DL | ' + T(label.get_y(array.get(L4, i)), '0.00'))
                label.set_text(array.get(O4, i), T(Count, '0') + ' DO | ' + T(label.get_y(array.get(O4, i)), '0.00'))
                label.set_text(array.get(C4, i), T(Count, '0') + ' DC | ' + T(label.get_y(array.get(C4, i)), '0.00'))
            H1 := line.new(bar_index, Highs, bar_index, Highs, color=HighColor, style=line.style_solid, width=1)
            L1 := line.new(bar_index, Lows, bar_index, Lows, color=LowColor, style=line.style_solid, width=1)
            O1 := line.new(bar_index, Opens, bar_index, Opens, color=OpenColor, style=line.style_solid, width=1)
            C1 := line.new(bar_index, Closes, bar_index, Closes, color=CloseColor, style=line.style_solid, width=1)
            H2 := label.new(na, Highs, color=BG(HighColor), textcolor=TX(HighColor), style=label.style_label_left, size=size.small)
            L2 := label.new(na, Lows, color=BG(LowColor), textcolor=TX(LowColor), style=label.style_label_left, size=size.small)
            O2 := label.new(na, Opens, color=BG(OpenColor), textcolor=TX(OpenColor), style=label.style_label_left, size=size.small)
            C2 := label.new(na, Closes, color=BG(CloseColor), textcolor=TX(CloseColor), style=label.style_label_left, size=size.small)
            array.push(H3, H1)
            array.push(L3, L1)
            array.push(O3, O1)
            array.push(C3, C1)
            array.push(H4, H2)
            array.push(L4, L2)
            array.push(O4, O2)
            array.push(C4, C2)
            if array.size(H3) > LookBack + 1
                line.delete(array.shift(H3))
                line.delete(array.shift(L3))
                line.delete(array.shift(O3))
                line.delete(array.shift(C3))
                label.delete(array.shift(H4))
                label.delete(array.shift(L4))
                label.delete(array.shift(O4))
                label.delete(array.shift(C4))
        if barstate.islast and array.size(H3) > 1
            for i = 0 to array.size(H3) - 2
                line.set_x2(array.get(H3, i), L())
                line.set_x2(array.get(L3, i), L())
                line.set_x2(array.get(O3, i), L())
                line.set_x2(array.get(C3, i), L())
                label.set_x(array.get(H4, i), L())
                label.set_x(array.get(L4, i), L())
                label.set_x(array.get(O4, i), L())
                label.set_x(array.get(C4, i), L())
    LVL(DTC, LL, DH, DL, DO, DC, DHColor, DLColor, DOColor, DCColor)
    
    

    Here's the solution while using arrays.