从数组中删除 "NaN" 值时出现问题
Problem with removing "NaN" values from an array
我无法从数组 (SRprices) 中删除“NaN”值并且只保存数字。 (第 173 行)
我不知道我的代码是否有效,因为我无法像在第一个数组中那样在 table 中打印新数组。 (只检查最后一节)
- 我有两个 table,第一个用于包含 NaN 值的基本数组
第二个 table 是打印只有数字但不起作用的新数组!
我认为问题出在 array.size 但为什么呢?!我不知道
如果有人帮助我,我将不胜感激
Array values in table
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © MoriFX
//@version=5
indicator('Dynamic Sup/Res lines Indicator', overlay=true, precision=4)
///////// Parameters Input
pivotVal = input.int(10, title='Period for Pivot Points', minval=10)
loopbkPeriod = input.int(284, title='Loopback Period', minval=100, maxval=500)
SRstrength = input.int(3, title='S/R strength', minval=1)
ChannelW = input.int(10, title='Channel Width %', minval=5)
label_location = input.int(10, title='Label Location +-', tooltip='0 means last bar. for example if you set it -5 then label is shown on last 5. bar. + means future bars')
linestyle = input.string('Dotted', title='Line Style', options=['Solid', 'Dotted', 'Dashed'])
LineColor = input(color.blue, title='Line Color')
drawhl = input(true, title='Draw Highest/Lowest Pivots in Period')
showpp = input(false, title='Show Point Points')
///////// Pivot points
ph = ta.pivothigh(pivotVal, pivotVal)
pl = ta.pivotlow(pivotVal, pivotVal)
plotshape(ph and showpp, text='PH', style=shape.labeldown, color=color.new(color.white, 100), textcolor=color.new(color.red, 0), location=location.abovebar, offset=-pivotVal)
plotshape(pl and showpp, text='PL', style=shape.labelup, color=color.new(color.white, 100), textcolor=color.new(color.lime, 0), location=location.belowbar, offset=-pivotVal)
///////// Support / Ressitant levels
sr_levels = array.new_float(21, na)
loopbkPeriodhighest = ta.highest(loopbkPeriod)
loopbkPeriodlowest = ta.lowest(loopbkPeriod)
cwidth = (loopbkPeriodhighest - loopbkPeriodlowest) * ChannelW / 100
aas = array.new_bool(41, true) //availability of the PPs
// last privot points have more priority to be support/resistance, so we start from them
// if we met new Pivot Point then we calculate all supports/resistances again
u1 = 0.0
u1 := nz(u1[1])
d1 = 0.0
d1 := nz(d1[1])
highestph = 0.0
lowestpl = 0.0
highestph := highestph[1]
lowestpl := lowestpl[1]
if ph or pl
//old S/Rs not valid anymore
for x = 0 to array.size(sr_levels) - 1 by 1
array.set(sr_levels, x, na)
highestph := loopbkPeriodlowest
lowestpl := loopbkPeriodhighest
countpp = 0 // keep position of the PP
for x = 0 to loopbkPeriod by 1
if na(close[x])
break
if not na(ph[x]) or not na(pl[x]) // is it PP?
highestph := math.max(highestph, nz(ph[x], loopbkPeriodlowest), nz(pl[x], loopbkPeriodlowest))
lowestpl := math.min(lowestpl, nz(ph[x], loopbkPeriodhighest), nz(pl[x], loopbkPeriodhighest))
countpp += 1
if countpp > 40
break
if array.get(aas, countpp) // if PP is not used in a channel
upl = (ph[x] ? high[x + pivotVal] : low[x + pivotVal]) + cwidth
dnl = (ph[x] ? high[x + pivotVal] : low[x + pivotVal]) - cwidth
u1 := countpp == 1 ? upl : u1
d1 := countpp == 1 ? dnl : d1
// to keep the PPs which will be in current channel
tmp = array.new_bool(41, true)
cnt = 0 // keep which pivot point we are on
tpoint = 0 // number of PPs in the channel
for xx = 0 to loopbkPeriod by 1
if na(close[xx])
break
if not na(ph[xx]) or not na(pl[xx])
chg = false
cnt += 1
if cnt > 40
break
if array.get(aas, cnt) // if PP not used in other channels
if not na(ph[xx])
if high[xx + pivotVal] <= upl and high[xx + pivotVal] >= dnl // PP is in the channel?
tpoint += 1
chg := true
chg
if not na(pl[xx])
if low[xx + pivotVal] <= upl and low[xx + pivotVal] >= dnl // PP is in the channel?
tpoint += 1
chg := true
chg
// set if PP is used in the channel
if chg and cnt < 41
array.set(tmp, cnt, false)
if tpoint >= SRstrength // met enough PP in the channel? mark the PP as used for a channel and set the SR level
for g = 0 to 40 by 1
if not array.get(tmp, g)
array.set(aas, g, false)
if ph[x] and countpp < 21
array.set(sr_levels, countpp, high[x + pivotVal])
if pl[x] and countpp < 21
array.set(sr_levels, countpp, low[x + pivotVal])
setline(level) =>
LineStyle = linestyle == 'Solid' ? line.style_solid : linestyle == 'Dotted' ? line.style_dotted : line.style_dashed
_ret = line.new(bar_index - 1, level, bar_index, level, color=LineColor, width=2, style=LineStyle, extend=extend.both)
_ret
if ph or pl
var line highest_ = na
var line lowest_ = na
line.delete(highest_)
line.delete(lowest_)
if drawhl ////////// Highest PH/PL price line
highest_ := line.new(bar_index - 1, highestph, bar_index, highestph, color=color.purple, style=line.style_dashed, width=1, extend=extend.both)
lowest_ := line.new(bar_index - 1, lowestpl, bar_index, lowestpl, color=color.purple, style=line.style_dashed, width=1, extend=extend.both)
lowest_
var sr_lines = array.new_line(21, na)
for x = 0 to array.size(sr_lines) - 1 by 1
line.delete(array.get(sr_lines, x))
if array.get(sr_levels, x)
array.set(sr_lines, x, setline(array.get(sr_levels, x)))
// set new labels if changed
var sr_levs = array.new_float(21, na)
if ph or pl
for x = 0 to array.size(sr_levs) - 1 by 1
array.set(sr_levs, x, array.get(sr_levels, x))
///////////////////// define and delete old labels
label hlabel = na
label llabel = na
label.delete(hlabel[1])
label.delete(llabel[1])
var sr_labels = array.new_label(21, na)
bool resistance_broken = false
bool support_broken = false
float r_s_level = na
/////////////////// set labels
for x = 0 to array.size(sr_labels) - 1 by 1
label.delete(array.get(sr_labels, x))
if array.get(sr_levs, x)
if close[1] <= array.get(sr_levs, x) and close > array.get(sr_levs, x)
resistance_broken := true
r_s_level := array.get(sr_levs, x)
r_s_level
if close[1] >= array.get(sr_levs, x) and close < array.get(sr_levs, x)
support_broken := true
r_s_level := array.get(sr_levs, x)
r_s_level
lab_loc = close >= array.get(sr_levs, x) ? label.style_label_up : label.style_label_down
array.set(sr_labels, x, label.new(x=bar_index + label_location, y=array.get(sr_levs, x), text=str.tostring(math.round_to_mintick(array.get(sr_levs, x))), color=color.lime, textcolor=color.black, style=lab_loc))
hlabel := drawhl ? label.new(x=bar_index + label_location + math.round(math.sign(label_location)) * 20, y=highestph, text='Highest PH : ' + str.tostring(highestph), color=color.silver, textcolor=color.black, style=label.style_label_down) : na
llabel := drawhl ? label.new(x=bar_index + label_location + math.round(math.sign(label_location)) * 20, y=lowestpl, text='Lowest PL : ' + str.tostring(lowestpl), color=color.silver, textcolor=color.black, style=label.style_label_up) : na
//////////////// table for printing S/R Levels
var table SRlevs = table.new(position.top_right, columns = 1, rows = array.size(sr_levs),
bgcolor = color.rgb(210,210,210), border_width = 2, border_color = color.white, frame_color = color.black, frame_width = 2)
for xii = 0 to array.size(sr_levs) - 1
table.cell(table_id = SRlevs, column = 0, row = xii , text_size=size.small,
text =str.tostring(array.get(sr_levs, xii)))
SRprices = array.new_float(array.size(sr_levs))
for xi = 0 to array.size(sr_levs) - 1
array.set(SRprices, xi, array.get(sr_levs, xi))
for k = 0 to array.size(sr_levs) - 1
if (na(array.get(SRprices, k)))
array.remove(SRprices, k)
/// code for printing new array in table which not work!!
// var table SRtable = table.new(position.top_right, columns = 1, rows = array.size(SRprices),
// bgcolor = color.rgb(210,210,210), border_width = 2, border_color = color.white, frame_color = color.black, frame_width = 2)
// for xii = 0 to array.size(SRprices) - 1
// table.cell(table_id = SRtable, column = 0, row = xii , text_size=size.small,
// text =str.tostring(array.get(SRprices, xii)))
您可以只使用 array.copy()
而不是循环来复制您的数组。
你必须从相反的方向遍历数组,否则当你删除一个数组元素时,循环的大小将最终尝试引用一个现在不存在的数组元素,因为数组的大小将不再存在匹配循环的迭代次数。
您有一个声明为 table 的变量,它使用 SRprices
的数组大小作为行数。在第一根柱上,这可能为零,因此在后续柱上,当有更多值时,它不会被修改。要么将大小设置为所需的最大值,要么使用您声明的大小(21)的 array.size(sr_levs)
。
//////////////// table for printing S/R Levels
var table SRlevs = table.new(position.top_right, columns = 1, rows = array.size(sr_levs),
bgcolor = color.rgb(210,210,210), border_width = 2, border_color = color.white, frame_color = color.black, frame_width = 2)
for xii = 0 to array.size(sr_levs) - 1
table.cell(table_id = SRlevs, column = 0, row = xii , text_size=size.small,
text =str.tostring(array.get(sr_levs, xii)))
SRprices = array.copy(sr_levs)
for k = array.size(SRprices) -1 to 0
if (na(array.get(SRprices, k)))
array.remove(SRprices, k)
var table SRtable = table.new(position.bottom_right, columns = 1, rows = array.size(sr_levs),
bgcolor = color.rgb(210,210,210), border_width = 2, border_color = color.white, frame_color = color.black, frame_width = 2)
table.clear(SRtable, 0, 0, 0, 20)
if array.size(SRprices) > 0
for xii = 0 to array.size(SRprices) - 1
table.cell(table_id = SRtable, column = 0, row = xii , text_size=size.small,
text =str.tostring(array.get(SRprices, xii)))
我无法从数组 (SRprices) 中删除“NaN”值并且只保存数字。 (第 173 行) 我不知道我的代码是否有效,因为我无法像在第一个数组中那样在 table 中打印新数组。 (只检查最后一节)
- 我有两个 table,第一个用于包含 NaN 值的基本数组 第二个 table 是打印只有数字但不起作用的新数组!
我认为问题出在 array.size 但为什么呢?!我不知道 如果有人帮助我,我将不胜感激
Array values in table
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © MoriFX
//@version=5
indicator('Dynamic Sup/Res lines Indicator', overlay=true, precision=4)
///////// Parameters Input
pivotVal = input.int(10, title='Period for Pivot Points', minval=10)
loopbkPeriod = input.int(284, title='Loopback Period', minval=100, maxval=500)
SRstrength = input.int(3, title='S/R strength', minval=1)
ChannelW = input.int(10, title='Channel Width %', minval=5)
label_location = input.int(10, title='Label Location +-', tooltip='0 means last bar. for example if you set it -5 then label is shown on last 5. bar. + means future bars')
linestyle = input.string('Dotted', title='Line Style', options=['Solid', 'Dotted', 'Dashed'])
LineColor = input(color.blue, title='Line Color')
drawhl = input(true, title='Draw Highest/Lowest Pivots in Period')
showpp = input(false, title='Show Point Points')
///////// Pivot points
ph = ta.pivothigh(pivotVal, pivotVal)
pl = ta.pivotlow(pivotVal, pivotVal)
plotshape(ph and showpp, text='PH', style=shape.labeldown, color=color.new(color.white, 100), textcolor=color.new(color.red, 0), location=location.abovebar, offset=-pivotVal)
plotshape(pl and showpp, text='PL', style=shape.labelup, color=color.new(color.white, 100), textcolor=color.new(color.lime, 0), location=location.belowbar, offset=-pivotVal)
///////// Support / Ressitant levels
sr_levels = array.new_float(21, na)
loopbkPeriodhighest = ta.highest(loopbkPeriod)
loopbkPeriodlowest = ta.lowest(loopbkPeriod)
cwidth = (loopbkPeriodhighest - loopbkPeriodlowest) * ChannelW / 100
aas = array.new_bool(41, true) //availability of the PPs
// last privot points have more priority to be support/resistance, so we start from them
// if we met new Pivot Point then we calculate all supports/resistances again
u1 = 0.0
u1 := nz(u1[1])
d1 = 0.0
d1 := nz(d1[1])
highestph = 0.0
lowestpl = 0.0
highestph := highestph[1]
lowestpl := lowestpl[1]
if ph or pl
//old S/Rs not valid anymore
for x = 0 to array.size(sr_levels) - 1 by 1
array.set(sr_levels, x, na)
highestph := loopbkPeriodlowest
lowestpl := loopbkPeriodhighest
countpp = 0 // keep position of the PP
for x = 0 to loopbkPeriod by 1
if na(close[x])
break
if not na(ph[x]) or not na(pl[x]) // is it PP?
highestph := math.max(highestph, nz(ph[x], loopbkPeriodlowest), nz(pl[x], loopbkPeriodlowest))
lowestpl := math.min(lowestpl, nz(ph[x], loopbkPeriodhighest), nz(pl[x], loopbkPeriodhighest))
countpp += 1
if countpp > 40
break
if array.get(aas, countpp) // if PP is not used in a channel
upl = (ph[x] ? high[x + pivotVal] : low[x + pivotVal]) + cwidth
dnl = (ph[x] ? high[x + pivotVal] : low[x + pivotVal]) - cwidth
u1 := countpp == 1 ? upl : u1
d1 := countpp == 1 ? dnl : d1
// to keep the PPs which will be in current channel
tmp = array.new_bool(41, true)
cnt = 0 // keep which pivot point we are on
tpoint = 0 // number of PPs in the channel
for xx = 0 to loopbkPeriod by 1
if na(close[xx])
break
if not na(ph[xx]) or not na(pl[xx])
chg = false
cnt += 1
if cnt > 40
break
if array.get(aas, cnt) // if PP not used in other channels
if not na(ph[xx])
if high[xx + pivotVal] <= upl and high[xx + pivotVal] >= dnl // PP is in the channel?
tpoint += 1
chg := true
chg
if not na(pl[xx])
if low[xx + pivotVal] <= upl and low[xx + pivotVal] >= dnl // PP is in the channel?
tpoint += 1
chg := true
chg
// set if PP is used in the channel
if chg and cnt < 41
array.set(tmp, cnt, false)
if tpoint >= SRstrength // met enough PP in the channel? mark the PP as used for a channel and set the SR level
for g = 0 to 40 by 1
if not array.get(tmp, g)
array.set(aas, g, false)
if ph[x] and countpp < 21
array.set(sr_levels, countpp, high[x + pivotVal])
if pl[x] and countpp < 21
array.set(sr_levels, countpp, low[x + pivotVal])
setline(level) =>
LineStyle = linestyle == 'Solid' ? line.style_solid : linestyle == 'Dotted' ? line.style_dotted : line.style_dashed
_ret = line.new(bar_index - 1, level, bar_index, level, color=LineColor, width=2, style=LineStyle, extend=extend.both)
_ret
if ph or pl
var line highest_ = na
var line lowest_ = na
line.delete(highest_)
line.delete(lowest_)
if drawhl ////////// Highest PH/PL price line
highest_ := line.new(bar_index - 1, highestph, bar_index, highestph, color=color.purple, style=line.style_dashed, width=1, extend=extend.both)
lowest_ := line.new(bar_index - 1, lowestpl, bar_index, lowestpl, color=color.purple, style=line.style_dashed, width=1, extend=extend.both)
lowest_
var sr_lines = array.new_line(21, na)
for x = 0 to array.size(sr_lines) - 1 by 1
line.delete(array.get(sr_lines, x))
if array.get(sr_levels, x)
array.set(sr_lines, x, setline(array.get(sr_levels, x)))
// set new labels if changed
var sr_levs = array.new_float(21, na)
if ph or pl
for x = 0 to array.size(sr_levs) - 1 by 1
array.set(sr_levs, x, array.get(sr_levels, x))
///////////////////// define and delete old labels
label hlabel = na
label llabel = na
label.delete(hlabel[1])
label.delete(llabel[1])
var sr_labels = array.new_label(21, na)
bool resistance_broken = false
bool support_broken = false
float r_s_level = na
/////////////////// set labels
for x = 0 to array.size(sr_labels) - 1 by 1
label.delete(array.get(sr_labels, x))
if array.get(sr_levs, x)
if close[1] <= array.get(sr_levs, x) and close > array.get(sr_levs, x)
resistance_broken := true
r_s_level := array.get(sr_levs, x)
r_s_level
if close[1] >= array.get(sr_levs, x) and close < array.get(sr_levs, x)
support_broken := true
r_s_level := array.get(sr_levs, x)
r_s_level
lab_loc = close >= array.get(sr_levs, x) ? label.style_label_up : label.style_label_down
array.set(sr_labels, x, label.new(x=bar_index + label_location, y=array.get(sr_levs, x), text=str.tostring(math.round_to_mintick(array.get(sr_levs, x))), color=color.lime, textcolor=color.black, style=lab_loc))
hlabel := drawhl ? label.new(x=bar_index + label_location + math.round(math.sign(label_location)) * 20, y=highestph, text='Highest PH : ' + str.tostring(highestph), color=color.silver, textcolor=color.black, style=label.style_label_down) : na
llabel := drawhl ? label.new(x=bar_index + label_location + math.round(math.sign(label_location)) * 20, y=lowestpl, text='Lowest PL : ' + str.tostring(lowestpl), color=color.silver, textcolor=color.black, style=label.style_label_up) : na
//////////////// table for printing S/R Levels
var table SRlevs = table.new(position.top_right, columns = 1, rows = array.size(sr_levs),
bgcolor = color.rgb(210,210,210), border_width = 2, border_color = color.white, frame_color = color.black, frame_width = 2)
for xii = 0 to array.size(sr_levs) - 1
table.cell(table_id = SRlevs, column = 0, row = xii , text_size=size.small,
text =str.tostring(array.get(sr_levs, xii)))
SRprices = array.new_float(array.size(sr_levs))
for xi = 0 to array.size(sr_levs) - 1
array.set(SRprices, xi, array.get(sr_levs, xi))
for k = 0 to array.size(sr_levs) - 1
if (na(array.get(SRprices, k)))
array.remove(SRprices, k)
/// code for printing new array in table which not work!!
// var table SRtable = table.new(position.top_right, columns = 1, rows = array.size(SRprices),
// bgcolor = color.rgb(210,210,210), border_width = 2, border_color = color.white, frame_color = color.black, frame_width = 2)
// for xii = 0 to array.size(SRprices) - 1
// table.cell(table_id = SRtable, column = 0, row = xii , text_size=size.small,
// text =str.tostring(array.get(SRprices, xii)))
您可以只使用 array.copy()
而不是循环来复制您的数组。
你必须从相反的方向遍历数组,否则当你删除一个数组元素时,循环的大小将最终尝试引用一个现在不存在的数组元素,因为数组的大小将不再存在匹配循环的迭代次数。
您有一个声明为 table 的变量,它使用 SRprices
的数组大小作为行数。在第一根柱上,这可能为零,因此在后续柱上,当有更多值时,它不会被修改。要么将大小设置为所需的最大值,要么使用您声明的大小(21)的 array.size(sr_levs)
。
//////////////// table for printing S/R Levels
var table SRlevs = table.new(position.top_right, columns = 1, rows = array.size(sr_levs),
bgcolor = color.rgb(210,210,210), border_width = 2, border_color = color.white, frame_color = color.black, frame_width = 2)
for xii = 0 to array.size(sr_levs) - 1
table.cell(table_id = SRlevs, column = 0, row = xii , text_size=size.small,
text =str.tostring(array.get(sr_levs, xii)))
SRprices = array.copy(sr_levs)
for k = array.size(SRprices) -1 to 0
if (na(array.get(SRprices, k)))
array.remove(SRprices, k)
var table SRtable = table.new(position.bottom_right, columns = 1, rows = array.size(sr_levs),
bgcolor = color.rgb(210,210,210), border_width = 2, border_color = color.white, frame_color = color.black, frame_width = 2)
table.clear(SRtable, 0, 0, 0, 20)
if array.size(SRprices) > 0
for xii = 0 to array.size(SRprices) - 1
table.cell(table_id = SRtable, column = 0, row = xii , text_size=size.small,
text =str.tostring(array.get(SRprices, xii)))