Search code examples
timelabelpine-scriptincrementpine-script-v5

Count Previous Days Excluding Weekends


This script has two incremental issues,

  1. [ ] Swap Increments?: Counts all previous days including weekends yet increments are incorrect as labels are shown with higher increments. (7 DH 450.00), (6 DH 440.00), (5 DH 420.00), (4 DH 410.00), (1 DH 430.00)

  2. [✓] Swap Increments?: Counts all previous market session days yet prices are incorrect as labels are all shown with the same price. (4 DH 430.00), (3 DH 430.00), (2 DH 430.00), (1 DH 430.00), (0 DH 430.00)

Here's a visual difference between both increments https://imgur.com/a/MNX7p7p

//@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(5, '', 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')
DHColor=input(color.rgb(0, 150, 136), '', group='Levels', inline='2')
Extra=input(false, 'Swap Increments?', group='Levels')
T(X, T) => str.tostring(X, T)
TX(COLOR) => ShowBC ? BC : COLOR
BG(COLOR) => ShowBC ? COLOR : BC
DD=str.format_time(last_bar_index - time, 'd ')
[DT, DH, DB]=request.security(syminfo.tickerid, 'D', [time, high, barstate.islast], lookahead=barmerge.lookahead_off)
DTC=DT != DT[1]
L() => bar_index + 10
var HIGH=array.new_float(1)
LVL(bool YES, int LookBack, float Highs, color HighColor) => 
    var line H1=na
    var label H2=na
    var H3=array.new_line()
    var H4=array.new_label()
    END=true ? L() : bar_index
    if YES
        line.set_x2(H1, END)
        label.set_x(H2, END)
        if Extra
            ExtraCount=array.size(H4)
            for i = 0 to ExtraCount > 0 ? ExtraCount -1 : na
                Count=ExtraCount -1 -i
                label.set_text(array.get(H4, i), T(Count, '0') + ' DH | ' + T(Highs, '0.00'))
        H1 := line.new(bar_index, Highs, bar_index, Highs, color=HighColor, style=line.style_solid, width=1)
        H2 := label.new(na, Highs, text=DD + 'DH | ' + T(Highs, '0.00'), color=BG(HighColor), textcolor=TX(HighColor), style=label.style_label_left, size=size.small)
        array.push(H3, H1)
        array.push(H4, H2)
        if array.size(H3) > LookBack + 1
            line.delete(array.shift(H3))
            label.delete(array.shift(H4))
    if Levels and barstate.islast and array.size(H3) > 1
        for i = 0 to array.size(H3) - 2
            line.set_x2(array.get(H3, i), L())
            label.set_x(array.get(H4, i), L())
if DTC and not DB
    array.set(HIGH, 1, DH)
if Extra == false
    LVL(DTC, LL, DH, DHColor)
if Extra
    LVL(DTC, LL, DH, DHColor)


The simple fix would be fixing the 1st question but I've alternated between every single time variable available and still no solution.


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(5, '', 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')
    T(X, T) => str.tostring(X, T)
    TX(COLOR) => ShowBC ? BC : COLOR
    BG(COLOR) => ShowBC ? COLOR : BC
    [DT, DH]=request.security(syminfo.tickerid, 'D', [time, high], lookahead=barmerge.lookahead_off)
    DTC=DT != DT[1]
    L() => bar_index + 10
    LVL(bool YES, int LookBack, float Highs, color HighColor) => 
        var line H1=na
        var label H2=na
        var H3=array.new_line()
        var H4=array.new_label()
        if YES and Levels
            line.set_x2(H1, L())
            label.set_x(H2, 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'))
            H1 := line.new(bar_index, Highs, bar_index, Highs, color=HighColor, 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)
            array.push(H3, H1)
            array.push(H4, H2)
            if array.size(H3) > LookBack + 1
                line.delete(array.shift(H3))
                label.delete(array.shift(H4))
        if barstate.islast and array.size(H3) > 1
            for i = 0 to array.size(H3) - 2
                line.set_x2(array.get(H3, i), L())
                label.set_x(array.get(H4, i), L())
    LVL(DTC, LL, DH, DHColor)
    
    
    
    

    Price was constant since it required "label.get_y".