【发布时间】:2021-04-01 06:21:05
【问题描述】:
我目前正在编写一个新脚本,但会遇到一些奇怪的行为,具体取决于我目前正在处理的股票或指数。
我的新脚本似乎在某些股票上运行良好,但在其他股票、指数或外汇对上抛出以下错误:
Pine 无法确定系列的参考长度。尝试在研究或策略函数中使用 max_bars_back
我已经阅读了 Pine 文档中的条目,还尝试将“max_bars_back”添加到研究中,但没有任何结果。
因此,出于演示目的,我分解了我的脚本,现在有一个版本会在每只股票上引发此错误。
代码如下:
//@version=4
study("StackOverflow_Test", overlay = true, max_bars_back=100)
var int loopRange = 0
var int trendState = 2
var bool firstBreak = true
var float hp_last = low
var float hp_current = high
float tempHighest = 0
int tempHighPosition = 0
var bool waitForConfirmation = false
float source = close // Used Price
for i = loopRange to 0
if ( source[i] > tempHighest )
tempHighest := source[i]
tempHighPosition := i
if ( source > hp_current )
waitForConfirmation := true
hp_current := source
if ( waitForConfirmation )
if ( firstBreak )
firstBreak := false
if ( trendState == 1 )
trendState := 2
else
trendState := 0
hp_last := hp_current[1]
if ( tempHighPosition > 3 )
loopRange := 3
waitForConfirmation := false
firstBreak := true
loopRange := loopRange + 1
color trendLineColor = color.green
if ( trendState == 0 )
trendLineColor := color.green
else if ( trendState == 1 )
trendLineColor := color.red
else
trendLineColor := color.silver
// Plot trendline
plot(source, color = trendLineColor)
如果我删除第一个 for 循环或行:
if ( trendState == 1 )
trendState := 2
else
trendState := 0
不会发生错误。
我认为该错误意味着我尝试越界访问数据,但我看不到确切的位置...
你们中有人知道是什么线导致了这种行为吗?
【问题讨论】:
-
您能否编辑您的问题并解释您要达到的目标?我认为这将使社区能够帮助您。另外,我认为您使用 for 循环以错误的方式解决了您的问题。
-
感谢您的回答,我将更多信息放在下面作为安德烈回答下的评论。但我可能会在这里添加我使用 for 循环的原因。我想寻找新的高点,并将它们用作价值区域,并估计市场是否处于区间或上升趋势中。我不使用最高/最低方法,因为我不希望上升趋势中的每根新蜡烛都出现“突破”。我只想要上升趋势结束时的新高点,所以当一定数量的蜡烛收盘价低于左右的新高点时。出于这个原因,我使用循环来获得不仅是高点
-
值,但也是它在 loopRange 中的位置。 loopRange 始终是新高点的最后一个高点,并在新高点确认后更新
标签: pine-script