简体   繁体   中英

Pinescript plotting last values of array(array of last candle time)

Good day!

I have a question - how can i use line.new from array for the last updated array values? When i use the array to plot line:

for x = 0 to (array.size(strong_levels_array) > 0 ? array.size(strong_levels_array) - 1 : na) by 1
    mid = array.get(strong_levels_array, x)
    array.push(sr_lines, line.new(x1=bar_index, y1=mid, x2=bar_index - 1, y2=mid, extend=extend.both, color=color.new(color.blue, 60),style = line.style_solid, width=2))

I have an array with the certain levels and i want to plot the last version of this array as lines. For example - I am getting the array to plot with 8 values - but there are much more lines on chart. most likely - it plots not the last but for several candles, and i do not know why.

Here is full code for pine editor. In the data window You can see 'power levels' - number of final array length. So i need to plot only these lines on chart(and update every candle). But as You can see - there are much more of them on chart. I tried to delete it, overwrite, else - anyway it plots not needed number of values

I need to plot lines according to the last length of array, not from any previus candle or other.

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Arivadis

//@version=5
indicator("Trend crossier",overlay = true)
step = input.int(100, minval = 1, title = "Length every step")
free_space = input.int(10, minval = 1, title = "Give some space to reach max/min")
close4 = request.security(syminfo.tickerid, "240", close)

float line_1_h = ta.highest(close4, step)[free_space]
float line_1_l = ta.lowest(close4, step)[free_space]
float line_2_h = ta.highest(close4, step)[step+free_space]
float line_2_l = ta.lowest(close4, step)[step]
float line_3_h = ta.highest(close4, step)[step*2 +free_space]
float line_3_l = ta.lowest(close4, step)[step*2 +free_space]
float line_4_h = ta.highest(close4, step)[step*3 +free_space]
float line_4_l = ta.lowest(close4, step)[step*3 +free_space]
float line_5_h = ta.highest(close4, step)[step*4 +free_space]
float line_5_l = ta.lowest(close4, step)[step*4 +free_space]
float line_6_h = ta.highest(close4, step)[step*5 +free_space]
float line_6_l = ta.lowest(close4, step)[step*5 +free_space]
float line_7_h = ta.highest(close4, step)[step*6 +free_space]
float line_7_l = ta.lowest(close4, step)[step*6 +free_space]
float line_8_h = ta.highest(close4, step)[step*7 +free_space]
float line_8_l = ta.lowest(close4, step)[step*7 +free_space]
float line_9_h = ta.highest(close4, step)[step*8 +free_space]
float line_9_l = ta.lowest(close4, step)[step*8 +free_space]
float line_10_h = ta.highest(close4, step)[step*9 +free_space]
float line_10_l = ta.lowest(close4, step)[step*9 +free_space]
line_above = line_1_h + (line_1_h * 0.010)
line_below = line_1_l + (line_1_l * 0.010)
SMA = ta.sma(close, 7)
plot(SMA, title = "SMA")

croosing_counter = 0.0

if SMA > line_1_h
    croosing_counter += 0.33
if SMA > line_1_l
    croosing_counter += 0.33
if SMA > line_2_h
    croosing_counter += 0.33
if SMA > line_2_l
    croosing_counter += 0.33
if SMA > line_3_h
    croosing_counter += 0.33
if SMA > line_3_l
    croosing_counter += 0.33
if SMA > line_4_h
    croosing_counter += 0.33
if SMA > line_4_l
    croosing_counter += 0.33
if SMA > line_5_h
    croosing_counter += 0.33
if SMA > line_5_l
    croosing_counter += 0.33
if SMA > line_6_h
    croosing_counter += 0.33
if SMA > line_6_l
    croosing_counter += 0.33
if SMA > line_7_h
    croosing_counter += 0.33
if SMA > line_7_l
    croosing_counter += 0.33
if SMA > line_8_h
    croosing_counter += 0.33
if SMA > line_8_l
    croosing_counter += 0.33
if SMA > line_9_h
    croosing_counter += 0.33
if SMA > line_9_l
    croosing_counter += 0.33
if SMA > line_above
    croosing_counter += 0.33
if SMA > line_below
    croosing_counter += 0.33


float line_1_h_ = ta.highest(close, step)[free_space]
float line_1_l_ = ta.lowest(close, step)[free_space]
float line_2_h_ = ta.highest(close, step)[step+free_space]
float line_2_l_ = ta.lowest(close, step)[step]
float line_3_h_ = ta.highest(close, step)[step*2 +free_space]
float line_3_l_ = ta.lowest(close, step)[step*2 +free_space]
float line_4_h_ = ta.highest(close, step)[step*3 +free_space]
float line_4_l_ = ta.lowest(close, step)[step*3 +free_space]
float line_5_h_ = ta.highest(close, step)[step*4 +free_space]
float line_5_l_ = ta.lowest(close, step)[step*4 +free_space]
float line_6_h_ = ta.highest(close, step)[step*5 +free_space]
float line_6_l_ = ta.lowest(close, step)[step*5 +free_space]
float line_7_h_ = ta.highest(close, step)[step*6 +free_space]
float line_7_l_ = ta.lowest(close, step)[step*6 +free_space]
float line_8_h_ = ta.highest(close, step)[step*7 +free_space]
float line_8_l_ = ta.lowest(close, step)[step*7 +free_space]
float line_9_h_ = ta.highest(close, step)[step*8 +free_space]
float line_9_l_ = ta.lowest(close, step)[step*8 +free_space]
float line_10_h_ = ta.highest(close, step)[step*9 +free_space]
float line_10_l_ = ta.lowest(close, step)[step*9 +free_space]
line_above_ = line_1_h_ + (line_1_h_ * 0.010)
line_below_ = line_1_l_ + (line_1_l_ * 0.010)



if SMA > line_1_h_
    croosing_counter += 0.17
if SMA > line_1_l_
    croosing_counter += 0.17
if SMA > line_2_h_
    croosing_counter += 0.17
if SMA > line_2_l_
    croosing_counter += 0.17
if SMA > line_3_h_
    croosing_counter += 0.17
if SMA > line_3_l_
    croosing_counter += 0.17
if SMA > line_4_h_
    croosing_counter += 0.17
if SMA > line_4_l_
    croosing_counter += 0.17
if SMA > line_5_h_
    croosing_counter += 0.17
if SMA > line_5_l_
    croosing_counter += 0.17
if SMA > line_6_h_
    croosing_counter += 0.17
if SMA > line_6_l_
    croosing_counter += 0.17
if SMA > line_7_h_
    croosing_counter += 0.17
if SMA > line_7_l_
    croosing_counter += 0.17
if SMA > line_8_h_
    croosing_counter += 0.17
if SMA > line_8_l_
    croosing_counter += 0.17
if SMA > line_9_h_
    croosing_counter += 0.17
if SMA > line_9_l_
    croosing_counter += 0.17
if SMA > line_above_
    croosing_counter += 0.17
if SMA > line_below_
    croosing_counter += 0.17



var table atrDisplay = table.new(position.top_right, 1, 1, bgcolor = color.gray, frame_width = 2, frame_color = color.black)

table.cell(atrDisplay, 0, 0, str.tostring(croosing_counter, format.mintick), text_color = color.white)

// computing the strong lines of trend

levels = array.new_float(0)
array.push(levels, line_1_h)
array.push(levels, line_1_l)
array.push(levels, line_2_h)
array.push(levels, line_2_l)
array.push(levels, line_3_h)
array.push(levels, line_3_l)
array.push(levels, line_4_h)
array.push(levels, line_4_l)
array.push(levels, line_5_h)
array.push(levels, line_5_l)
array.push(levels, line_6_h)
array.push(levels, line_6_l)
array.push(levels, line_7_h)
array.push(levels, line_7_l)
array.push(levels, line_8_h)
array.push(levels, line_8_l)
array.push(levels, line_9_h)
array.push(levels, line_9_l)
array.push(levels, line_above)
array.push(levels, line_below)
array.push(levels, line_1_h_)
array.push(levels, line_1_l_)
array.push(levels, line_2_h_)
array.push(levels, line_2_l_)
array.push(levels, line_3_h_)
array.push(levels, line_3_l_)
array.push(levels, line_4_h_)
array.push(levels, line_4_l_)
array.push(levels, line_5_h_)
array.push(levels, line_5_l_)
array.push(levels, line_6_h_)
array.push(levels, line_6_l_)
array.push(levels, line_7_h_)
array.push(levels, line_7_l_)
array.push(levels, line_8_h_)
array.push(levels, line_8_l_)
array.push(levels, line_9_h_)
array.push(levels, line_9_l_)
array.push(levels, line_above_)
array.push(levels, line_below_)
array.sort(levels, order = order.ascending)
ADR = ta.sma(high - low, step * 10)



sr_lines = array.new_line(0)
if array.size(sr_lines) > 0
    for i = array.size(sr_lines) - 1 to 0
        line.delete(array.get(sr_lines, i))


for i = (array.size(levels) == 0 ? na : array.size(levels) - 2) to 1
    levels_i = array.get(levels, i)
    below = array.get(levels, i - 1)
    above = array.get(levels, i + 1)
    if levels_i > below + ADR and levels_i < above - ADR
        array.remove(levels, i)


var the_level_array = array.new_int(0)
array.clear(the_level_array)



counter = 1
for i = 0 to array.size(levels) - 2 by 1
    val = array.get(levels, i)
    val2 = array.get(levels, i+1)

    if val > val2 - ADR
        array.push(the_level_array, counter)
    else
        counter += 1


var strong_levels_array = array.new_float(0)
array.clear(strong_levels_array)
var counter_of_level_power = array.new_int(0)
array.clear(counter_of_level_power)

for i = 0 to array.get(the_level_array, array.size(the_level_array) > 0 ? array.size(the_level_array) - 1 : na) by 1
    counter_ = 0
    summ_of_levels = 0.0
    for j = 1 to array.size(the_level_array) - 1
        if array.get(the_level_array, j) == i
            counter_ += 1
            summ_of_levels += array.get(levels, j)
    if counter > 3
        array.push(strong_levels_array, summ_of_levels / counter_)
        array.push(counter_of_level_power, counter)
    counter_ := 0
    summ_of_levels := 0.0

val = array.size(strong_levels_array) > 0 ? array.size(strong_levels_array) - 1 : na
plot(val, title = 'power levels')


for x = 0 to (array.size(strong_levels_array) > 0 ? array.size(strong_levels_array) - 1 : na) by 1
    mid = array.get(strong_levels_array, x)
    array.push(sr_lines, line.new(x1=bar_index, y1=mid, x2=bar_index - 1, y2=mid, extend=extend.both, color=color.new(color.blue, 60),style = line.style_solid, width=2))

// for x = 0 to (array.size(levels) > 0 ? array.size(levels) - 1 : na) by 1
//     mid = array.get(levels, x)
//     array.push(sr_lines, line.new(x1=bar_index, y1=mid, x2=bar_index - 1, y2=mid, extend=extend.both, color=color.new(color.blue, 60),style = line.style_solid, width=2))


plot(ADR, title = "ADR")

Once you draw a line , it stays static on the screen unless you use line.delete() function. That's also true if you draw it by adding it as an element to an array . For example, this code will draw multiple lines although the array has only 1 element on every given bar:

lineArray = array.new_line(1)
array.set(lineArray, 0, line.new(bar_index, high, bar_index - 1, high, extend = extend.both))

That is caused due to the fact that on each bar a line is drawn, and if you wish to delete on the next bar, you'll need to reference previous line (even if it's inside an array) and not just clearing the array on current bar.

I'm guessing there are multiple ways to address this issue, but the easiest one I could thought of is by keeping the reference to previous lines using the var keyword on the array and then looping over the lines in that array using the line.delete function before adding the new lines to that array :

var lineArray = array.new_line(1)

for _line in lineArray 
    line.delete(_line)

array.set(lineArray, 0, line.new(bar_index, high, bar_index - 1, high, extend = extend.both))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM