我正在尝试测试我的脚本,我想绘制一些我在它上面的变量。问题是,在for语句之前绘制的所有内容都可以工作,但在此之后就没有任何工作了。我做了一个测试来绘制'bar'变量,当我这样做时,它弹出一个错误:未声明的标识符'bar'。当我尝试绘制'bar'变量时,就会发生这种情况。
'SwingIndex'变量包含我最终想要在指标中绘制的结果,但是当我试图绘制它时,它会在指标窗口上出现一个错误,上面写着'Study error Negative index -1'。for语句中的某个地方出现了问题。
我个人认为,由于'bar'变量不工作(在我的代码的第39行声明),没有其他的。但我不知道为什么不工作。
谁能帮我找出我的错误在哪里?我把代码留在这里。// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © TD_DaFen
//@version=5
// Indicator name
indicator("DAF_Swing_Index", shorttitle= 'DAF_SwInd', overlay=false)
// Input
T = input.int(30000, title = 'Ratio de escala', minval = 1000, maxval = 150000)
Shift = input.int(0, title = 'Desplazamiento horizontal', minval = 0, maxval = 100)
// Other variable
var float SwingIndex = 0
var int StartBars = 1
Prev_calculated = bar_index
Rates_total = bar_index + 1
var float SH1 = 0
var float SI = 0
var float R = 0
// Array
Open = array.new_float(Rates_total)
Open1 = array.new_float(Rates_total)
Close = array.new_float(Rates_total)
Close1 = array.new_float(Rates_total)
High = array.new_float(Rates_total)
Low = array.new_float(Rates_total)
// Initial bar verification
if Rates_total < StartBars
SwingIndex := 0
Primero = 1
if Prev_calculated > Rates_total or Prev_calculated <= 0
Primero := 1
else
Primero := Prev_calculated -1
// Main process
for bar= Primero to Rates_total-1
array.push(Open, high[bar])
array.push(Open1, open[bar-1])
array.push(Close, close[bar])
array.push(Close1, close[bar-1])
array.push(High, high[bar])
array.push(Low, low[bar])
K = math.max(math.abs(array.get(High, bar)- array.get(Close1, bar)), math.abs(array.get(Low, bar) - array.get(Close1, bar)))
TR = math.max(math.max(math.abs(array.get(High, bar) - array.get(Close1, bar)), math.abs(array.get(Low, bar) - array.get(Close1, bar))), math.abs(array.get(High, bar) - array.get(Low, bar)))
ER = 0.0
if array.get(Close1, bar) > array.get(High, bar)
ER := math.abs(array.get(High, bar) - array.get(Close1, bar))
if array.get(Close1, bar) < array.get(Low, bar)
ER := math.abs(array.get(Low, bar) - array.get(Close1, bar))
SH1 := math.abs(array.get(Close1, bar) - array.get(Open1, bar))
R := TR - 0.5 * ER + 0.25 * SH1
SI := 0.0
if R != 0
SI := 50 * ((array.get(Close, bar) - array.get(Close1, bar)) + 0.5 * (array.get(Close, bar) - array.get(Open1, bar))) * (K / T) / R
SwingIndex := SI
// ploting partial results to see if its working
plot(SwingIndex, title = 'Swing Index', style = plot.style_line, color = color.rgb(193, 150, 51, 10))
问题出在for循环中。错误消息告诉您,您正在尝试访问索引为-1的元素。
以下两个array.push
调用导致问题:
for bar= Primero to Rates_total-1
array.push(Open1, open[bar-1])
array.push(Close1, close[bar-1])
这是因为在图表的第二个条形处,Primero
变为零,使得for循环从零开始(for bar = Primero
)。然后从0中减去1,然后尝试访问下标为-1的元素。
我们怎么知道的?
请记住,您的脚本将对每个栏执行。让我们分析以下代码片段的第一个和第二个栏。
Prev_calculated = bar_index
Rates_total = bar_index + 1
Primero = 1
if Prev_calculated > Rates_total or Prev_calculated <= 0
Primero := 1
else
Primero := Prev_calculated -1
Very first bar (bar_index = 0)
Prev_calculated = 0
Rates_total = 1
Primero = 1 (if branch -> Prev_calculated <= 0 is true)
第二栏(bar_index = 1)
Prev_calculated = 1
Rates_total = 2
Primero = 0 (else branch)
同样,这个Prev_calculated > Rates_total
检查将始终是false
。