如何在索引 540 错误处修复 TradingView 超出深度

How to fix TradingView out of depth at index 540 error

在 TradingView (tradingview.com) 中处理 pine 脚本时,我一直看到图表顶部附近出现红色文本 "out of depth at index" 540,我的脚本无法执行。作为 pine 脚本的新手,我不太确定它是什么意思。

这个问题相当晦涩难懂 google,所以我把找到的答案贴在这里。希望对某人有用。

我通过 this detailed writeup 找到了有关此问题的答案。由于 Whosebug 不喜欢 link-rot,我将在此处复制足够的内容以达到目的。

基本上,TradingView 无法确定您的脚本需要多少历史数据。因此,您要么必须更改脚本的组织来帮助它,要么通过将 max_bars_back 参数传递给您的 study()strategy() 调用来手动输入您的脚本需要的柱数.

示例: 我创建了一个运行超过 200 个柱的 SMA。所以,我要传strategy("example", max_bars_back=200)这里200是最小的,所以你可能需要把这个值调大一些,看你要分析多少数据。

Practically all TradingView indicator and strategy scripts use historical data for their calculations. How much data we use affects how long the script ‘waits’ before calculating. An indicator that plots 20-bar highs needs 20 bars of price data to do so. And a strategy that trades the 9-bar SMA needs 9 price bars before it can send orders.

TradingView is quite good in estimating how many price bars our script needs and hold off calculations until there’s enough data. But sometimes even a good estimation can be wrong. When that happens TradingView triggers the ‘out of depth at index’ error message.

[...]

The ‘out of depth at index’ error message can be intimidating because it sounds abstract. But there are just three steps to fix the error:

  1. Open in the Pine Editor the indicator or strategy script that triggers the ‘out of depth at index’ error.
    1. Look through the code to get an idea of many historical bars your script uses for its calculations. Also consider the range of values that input options can have.
    2. Now add the max_bars_back argument to the study() or strategy() function in your script. Set that argument’s value to your estimation of how many bars the script uses in its calculations.
      • Did you already add the max_bars_back argument to the study() or strategy() function but still got the ‘out of depth at index’ error? Then increase the value of max_bars_back and save your script to try again.

旁注: 'max_bars_back' 起初对我来说听起来像是一个糟糕的名字选择,但现在我认为它被称为是因为它是 "max" 您正在使用的指标中的数量。所以,如果我有一个 15 天的 sma()、一个 50 天的 sma() 和一个 200 天的 sma(),200 将是我所有计算中需要的 "max" 个柱。