frien_dd
frien_dd

Reputation: 164

Extending plot function

Afternoon all,

I am currently using the below to plot a line (using yesterdays data), plotting on today chart (only currently plots for 24 hours). What I would like to do is to have it continuously extend to the right.

Is this possible?

plot(pastLine, "Past Line", change(pastLine) ? na : color.purple, offset = 0)

enter image description here

@Bjorn thank you for your reply Extend is indeed wrong and it is track price I am after but for the previous plotted lines.

Thank you for your time Daniel

##Updated 31/01/2021 @ 1800 - As per Bjorn request##

Please see script I am using below. I would like to extend all previous purple lines to the price track.

    //@version=4
study(title="Points of Interest", shorttitle="Points of Interest", overlay=true)

//Session Rules
bartimeSess = time('D')
newbarSess = bartimeSess != bartimeSess[1]
offset_val = input(title="Label Offset", type=input.integer, defval=6)

va_percent = input(0.7, title = "Value Area", type = input.float, 
     minval = 0.1, maxval = 1, step = 0.1)
     

dtf = input("D", title = "Time Frame", type = input.resolution)
resolution = input(1, title = "Resolution", type = input.float)

is_new_bar(t) => 
    change(time(t)) != 0

round_to_nearest(v, x) => 
    round(v / x) * x


tick_size = max(syminfo.mintick, resolution)

var a = array.new_float(0)

a_min = 0.0, a_min := nz(a_min[1], round_to_nearest(low, tick_size))
a_max = 0.0, a_max := nz(a_max[1], round_to_nearest(high, tick_size))

d_switch = is_new_bar(dtf)

if d_switch
    a_min := low
    a_max := high
    array.clear(a)

// Scaled min max
v_min = int(round_to_nearest(low - a_min, tick_size) / tick_size)
v_max = int(round_to_nearest(high - a_min, tick_size) / tick_size)

// Scaled candle range
ticks = v_max - v_min

vol = volume / (ticks == 0 ? 1 : ticks)

for i = v_min to max(v_max - 1, v_min)
    
    // Insert new low value
    if i < 0
        array.insert(a, i - v_min, vol)
        continue
    
    // Adjust index
    offset = v_min < 0 ? abs(v_min) : 0
    index = int(i + offset)
    
    // Push new high value
    if index >= array.size(a)
        array.push(a, vol)
        continue
    
    // Update existing value
    v = array.get(a, index)
    array.set(a, index, v + vol)

// Array bounds
a_min := min(a_min, round_to_nearest(low, tick_size))
a_max := max(a_max, round_to_nearest(high, tick_size))
a_size = array.size(a)

// { POC

poc_index = -1
poc_prev = -1.0
sum_vol = 0.0

for i = 0 to a_size - 1
    
    poc_current = array.get(a, i)
    sum_vol := sum_vol + poc_current
    
    if poc_current > poc_prev
        poc_prev := poc_current
        poc_index := i

// }

// { VA

va_high_index = poc_index
va_low_index  = poc_index
    
va_vol_cap = sum_vol * va_percent
sum_va_vol = array.get(a, poc_index)

for i = 1 to a_size - 1
    
    above = 0.0
    if va_high_index + 1 < a_size - 1
        above := above + nz(array.get(a, (va_high_index + 1)), 0.0)
    if va_high_index + 2 < a_size - 1
        above := above + nz(array.get(a, (va_high_index + 2)), 0.0)
        
    below = 0.0
    if va_low_index - 1 > 0
        below := below + nz(array.get(a, (va_low_index - 1)), 0.0)
    if va_low_index - 2 > 0
        below := below + nz(array.get(a, (va_low_index - 2)), 0.0)
    
    if above > below
        va_high_index := min(va_high_index + 2, a_size - 1)
        sum_va_vol  := sum_va_vol + above
    else
        va_low_index := max(va_low_index - 2, 0)
        sum_va_vol := sum_va_vol + below
        
    if sum_va_vol >= va_vol_cap or (va_low_index <= 0 and va_high_index >= a_size - 1)
        break

// }

float p_poc = 0.0

float d_poc = 0.0
float b_poc = 0.0


d_poc  := poc_index * tick_size + a_min

if is_new_bar(dtf)
    p_poc  := d_poc[1]

else
    p_poc  := p_poc[1]


plot(p_poc, " pd_POC", change(p_poc) ? na : color.purple, offset = 0, trackprice=true)

plotshape(p_poc, style=shape.labeldown, location=location.absolute, color=color.purple,  textcolor=color.white, show_last=1, text="pdPOC",  offset = offset_val, transp=20, title="pdPOC")

Upvotes: 0

Views: 1911

Answers (1)

Bjorn Mistiaen
Bjorn Mistiaen

Reputation: 6865

You cannot extend a plot. You could try to use the trackprice parameter for that purpose.

plot(pastLine, "Past Line", change(pastLine) ? na : color.purple, offset = 0, trackprice=true)

Update 31/01/2021: Using lines instead of plot.

//@version=4
study(title="Points of Interest", shorttitle="Points of Interest", overlay=true, max_lines_count=500)

//Session Rules
bartimeSess = time('D')
newbarSess = bartimeSess != bartimeSess[1]
offset_val = input(title="Label Offset", type=input.integer, defval=6)

va_percent = input(0.7, title = "Value Area", type = input.float, 
     minval = 0.1, maxval = 1, step = 0.1)
     

dtf = input("D", title = "Time Frame", type = input.resolution)
resolution = input(1, title = "Resolution", type = input.float)

is_new_bar(t) => change(time(t)) != 0
round_to_nearest(v, x) => round(v / x) * x

tick_size = max(syminfo.mintick, resolution)

var a = array.new_float(0)

a_min = 0.0, a_min := nz(a_min[1], round_to_nearest(low, tick_size))
a_max = 0.0, a_max := nz(a_max[1], round_to_nearest(high, tick_size))

d_switch = is_new_bar(dtf)

if d_switch
    a_min := low
    a_max := high
    array.clear(a)

// Scaled min max
v_min = int(round_to_nearest(low - a_min, tick_size) / tick_size)
v_max = int(round_to_nearest(high - a_min, tick_size) / tick_size)

// Scaled candle range
ticks = v_max - v_min

vol = volume / (ticks == 0 ? 1 : ticks)

for i = v_min to max(v_max - 1, v_min)
    // Insert new low value
    if i < 0
        array.insert(a, i - v_min, vol)
        continue
    
    // Adjust index
    offset = v_min < 0 ? abs(v_min) : 0
    index = int(i + offset)
    
    // Push new high value
    if index >= array.size(a)
        array.push(a, vol)
        continue
    
    // Update existing value
    v = array.get(a, index)
    array.set(a, index, v + vol)

// Array bounds
a_min := min(a_min, round_to_nearest(low, tick_size))
a_max := max(a_max, round_to_nearest(high, tick_size))
a_size = array.size(a)

// { POC

poc_index = -1
poc_prev = -1.0
sum_vol = 0.0

for i = 0 to a_size - 1
    poc_current = array.get(a, i)
    sum_vol := sum_vol + poc_current
    
    if poc_current > poc_prev
        poc_prev := poc_current
        poc_index := i

// }

// { VA

va_high_index = poc_index
va_low_index  = poc_index
    
va_vol_cap = sum_vol * va_percent
sum_va_vol = array.get(a, poc_index)

for i = 1 to a_size - 1
    
    above = 0.0
    if va_high_index + 1 < a_size - 1
        above := above + nz(array.get(a, (va_high_index + 1)), 0.0)
    if va_high_index + 2 < a_size - 1
        above := above + nz(array.get(a, (va_high_index + 2)), 0.0)
        
    below = 0.0
    if va_low_index - 1 > 0
        below := below + nz(array.get(a, (va_low_index - 1)), 0.0)
    if va_low_index - 2 > 0
        below := below + nz(array.get(a, (va_low_index - 2)), 0.0)
    
    if above > below
        va_high_index := min(va_high_index + 2, a_size - 1)
        sum_va_vol  := sum_va_vol + above
    else
        va_low_index := max(va_low_index - 2, 0)
        sum_va_vol := sum_va_vol + below
        
    if sum_va_vol >= va_vol_cap or (va_low_index <= 0 and va_high_index >= a_size - 1)
        break

// }

float p_poc = 0.0
float d_poc = 0.0
float b_poc = 0.0

d_poc  := poc_index * tick_size + a_min

// if is_new_bar(dtf)
//     p_poc  := d_poc[1]
// else
//     p_poc  := p_poc[1]

p_poc := is_new_bar(dtf) ? d_poc[1] : p_poc[1]

if is_new_bar(dtf)
    line.new(time, p_poc, time+1, p_poc, xloc=xloc.bar_time, color=color.purple, extend=extend.right)

// plot(p_poc, " pd_POC", change(p_poc) ? na : color.purple, offset = 0, trackprice=true)

plotshape(p_poc, style=shape.labeldown, location=location.absolute, color=color.purple,  textcolor=color.white, show_last=1, text="pdPOC",  offset = offset_val, transp=20, title="pdPOC")

Yields this (max 500 lines) enter image description here

Edit 1 in response to this comment
Keep non-broken poc's

//@version=4
study(title="Points of Interest", shorttitle="Points of Interest", overlay=true, max_lines_count=500)

//Session Rules
bartimeSess = time('D')
newbarSess = bartimeSess != bartimeSess[1]
offset_val = input(title="Label Offset", type=input.integer, defval=6)

va_percent = input(0.7, title = "Value Area", type = input.float, 
     minval = 0.1, maxval = 1, step = 0.1)
     

dtf = input("D", title = "Time Frame", type = input.resolution)
resolution = input(1, title = "Resolution", type = input.float)

is_new_bar(t) => change(time(t)) != 0
round_to_nearest(v, x) => round(v / x) * x

tick_size = max(syminfo.mintick, resolution)

var a = array.new_float(0)

a_min = 0.0, a_min := nz(a_min[1], round_to_nearest(low, tick_size))
a_max = 0.0, a_max := nz(a_max[1], round_to_nearest(high, tick_size))

d_switch = is_new_bar(dtf)

if d_switch
    a_min := low
    a_max := high
    array.clear(a)

// Scaled min max
v_min = int(round_to_nearest(low - a_min, tick_size) / tick_size)
v_max = int(round_to_nearest(high - a_min, tick_size) / tick_size)

// Scaled candle range
ticks = v_max - v_min

vol = volume / (ticks == 0 ? 1 : ticks)

for i = v_min to max(v_max - 1, v_min)
    // Insert new low value
    if i < 0
        array.insert(a, i - v_min, vol)
        continue
    
    // Adjust index
    offset = v_min < 0 ? abs(v_min) : 0
    index = int(i + offset)
    
    // Push new high value
    if index >= array.size(a)
        array.push(a, vol)
        continue
    
    // Update existing value
    v = array.get(a, index)
    array.set(a, index, v + vol)

// Array bounds
a_min := min(a_min, round_to_nearest(low, tick_size))
a_max := max(a_max, round_to_nearest(high, tick_size))
a_size = array.size(a)

// { POC

poc_index = -1
poc_prev = -1.0
sum_vol = 0.0

for i = 0 to a_size - 1
    poc_current = array.get(a, i)
    sum_vol := sum_vol + poc_current
    
    if poc_current > poc_prev
        poc_prev := poc_current
        poc_index := i

// }

// { VA

va_high_index = poc_index
va_low_index  = poc_index
    
va_vol_cap = sum_vol * va_percent
sum_va_vol = array.get(a, poc_index)

for i = 1 to a_size - 1
    
    above = 0.0
    if va_high_index + 1 < a_size - 1
        above := above + nz(array.get(a, (va_high_index + 1)), 0.0)
    if va_high_index + 2 < a_size - 1
        above := above + nz(array.get(a, (va_high_index + 2)), 0.0)
        
    below = 0.0
    if va_low_index - 1 > 0
        below := below + nz(array.get(a, (va_low_index - 1)), 0.0)
    if va_low_index - 2 > 0
        below := below + nz(array.get(a, (va_low_index - 2)), 0.0)
    
    if above > below
        va_high_index := min(va_high_index + 2, a_size - 1)
        sum_va_vol  := sum_va_vol + above
    else
        va_low_index := max(va_low_index - 2, 0)
        sum_va_vol := sum_va_vol + below
        
    if sum_va_vol >= va_vol_cap or (va_low_index <= 0 and va_high_index >= a_size - 1)
        break

// }

float p_poc = 0.0
float d_poc = 0.0
float b_poc = 0.0

d_poc  := poc_index * tick_size + a_min

// if is_new_bar(dtf)
//     p_poc  := d_poc[1]
// else
//     p_poc  := p_poc[1]

p_poc := is_new_bar(dtf) ? d_poc[1] : p_poc[1]

var line[]  lines = array.new_line()
var float[] pocs  = array.new_float()

if is_new_bar(dtf)
    array.push(lines, line.new(time, p_poc, time+1, p_poc, xloc=xloc.bar_time, color=color.purple, extend=extend.right))
    array.push(pocs, p_poc)

if array.size(pocs) > 0
    for i = 0 to array.size(pocs)-1
        mypoc = array.get(pocs,i)
        if mypoc > low
            line.delete(array.get(lines,i))

// plot(p_poc, " pd_POC", change(p_poc) ? na : color.purple, offset = 0, trackprice=true)

plotshape(p_poc, style=shape.labeldown, location=location.absolute, color=color.purple,  textcolor=color.white, show_last=1, text="pdPOC",  offset = offset_val, transp=20, title="pdPOC")

Upvotes: 2

Related Questions