【问题标题】:Argument types problem ('float', 'const int') in array数组中的参数类型问题('float'、'const int')
【发布时间】:2021-12-15 05:50:42
【问题描述】:

我一直在尝试使用 pine 脚本编写我的第一个代码。问题是这样的。我创建了一些 array.new_float 用作“for”语句中的缓冲区。问题是我需要对数据做一些数学运算。现在,一旦 'for' 完成,就会弹出一个错误:'Cannot call 'operator -' with argument 'expr0' = 'High'.An argument of 'float[]' type was used but a 'const int' is expected '。

拜托,如果有人知道我做错了什么,我会感谢你。

编辑:我将在此处留下我正在尝试做的脚本

//@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)

// Array
SWINGINDEX = array.new_float(200)
Open = array.new_float(200)
Open1 = array.new_float(200)
Close = array.new_float(200)
Close1 = array.new_float(200)
High = array.new_float(200)
Low = array.new_float(200)

// 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

// 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
    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(High - Close1), math.abs(Low - Close1))
    TR = math.max(math.max(math.abs(High-Close1), math.abs(Low-Close1)), math.abs(High-Low))
    
    ER = 0.0
    
    if Close1 > High
        ER := math.abs(High - Close1)
    if Close1 < Low
        ER := math.abs(Low - Close1)
    
    
    SH1 := math.abs(Close1 - Open1)
    R := TR - 0.5 * ER + 0.25 * SH1
    SI := 0.0
    if R != 0
        SI := 50 * ((Close - Close1) + 0.5 * (Close - Open1)) * (K / T) / R
    
    SwingIndex := SI
// ploting result

plot(SwingIndex, title = 'Swing Index', style = plot.style_line,  color = color.rgb(193, 255, 51, 10))

【问题讨论】:

  • 请不要上传您的代码截图。相反,请在此处复制并粘贴您的代码,然后使用代码示例 {} 按钮来格式化您的代码。
  • 你能分享你的整个代码或最小可重复的代码吗?
  • @BarisYakut 我刚刚添加了脚本。

标签: arrays for-loop math compiler-errors pine-script


【解决方案1】:

因此,错误消息告诉您的是,您正在传递一个数组,它需要一个 const 值。

喜欢这里:

K = math.max(math.abs(High - Close1), math.abs(Low - Close1))

所有这些变量(HighClose1Low)都是数组。它根本无法从另一个数组中减去一个数组。但是,您可以从另一个元素中减去一个元素。

所以对于那条线,我相信你想要这样的东西:

K = math.max(math.abs(array.get(High, bar) - array.get(Close1, bar)), math.abs(array.get(Low, bar) - array.get(Close1, bar)))

使用array.get(),可以获取指定索引处元素的值。

您应该在所有其他情况下解决此问题。

【讨论】:

  • 非常感谢@BarisYakut。那就是我的错误所在。非常感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 2023-02-07
  • 2023-02-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多