我在编译 Tradingview Pine 脚本 5 时遇到此错误
我知道问题出在这两行中,因为我将数组乘以一个不正确的值,并且我需要将浮点转换为系列,但我不知道做什么以及如何做到这一点:
# forward_error += autocorrelation[i] * array.get(ar_coefficients, i - iter)
# backward_error += autocorrelation[i - iter] * array.get(ar_coefficients, i)
// Autoregressive function using Burg method
source = input.source(close, "Source")
order = input.int(2, "Order")
lookback = input.int(100, "Lookback", minval = 2)
// Step 1: Calculate Autocorrelation Function (ACF)
autocorrelation = array.new_float(lookback)
for lag = 0 to lookback - 1
correlation = ta.correlation(close, source[lag], lengthBB)
array.push(autocorrelation, correlation)
// Step 2: Initialize Coefficients
ar_coefficients = array.new_float(order + 1)
array.push(ar_coefficients, 1.0) // Initialize first coefficient as 1
// Step 3: Iterative Calculation (Burg algorithm)
for iter = 1 to order
forward_error = 1.0
backward_error = -1.0
for i = iter to lookback - 1
forward_error += autocorrelation[i] * array.get(ar_coefficients, i - iter)
backward_error += autocorrelation[i - iter] * array.get(ar_coefficients, i)
// Avoid division by zero
if backward_error != 0.0
reflection_coefficient = -2.0 * forward_error / backward_error
// Update coefficients using Burg recursion formula
temp_coefficients = array.new_float(order + 1)
array.push(temp_coefficients, 1.0) // First coefficient remains 1
for i = 1 to iter
coefficient = array.get(ar_coefficients, i) + reflection_coefficient * array.get(ar_coefficients, iter - i + 1)
array.push(temp_coefficients, coefficient)
ar_coefficients := temp_coefficients
// Calculate autoregressive values
hlc3_series = (hlc3)
autoregressive_values = array.new_float(lookback)
for i = 0 to lookback - 1
value = 0.0
for j = 1 to order
// Perform element-wise multiplication and addition
value += array.get(ar_coefficients, j) * hlc3_series[i - j]
array.push(autoregressive_values, value)
// Plot autoregressive values
for i = 0 to lookback - 1
// Plot individual autocorrelation values
plot(array.get(autocorrelation, i), color=color.purple, title="Autocorrelation Values")
我尝试将浮点更改为系列,但无论我输入什么语法,我总是会遇到更多错误 如何将浮点数组转换为系列?`
autocorrelation
是一个数组。您无法通过 autocorrelation[i]
来访问其元素。
您需要像在其他地方一样使用
array.get()
函数。