How to fix TradingView out of depth at index 540 e

2019-08-15 10:49发布

问题:

When working on a pine script in TradingView (tradingview.com), I kept seeing red text appear near the top of the chart saying "out of depth at index" 540, and my script would not execute. Being new to pine script, I wasn't really sure what it meant.

This issue was rather cryptic and difficult to google, so I'm posting the answer I found here. Hopefully it will be useful to someone.

回答1:

I found an answer via this detailed writeup regarding the issue. Since stackoverflow doesn't like link-rot, I'll copy enough here to get to the point.

Basically, TradingView cannot figure out how much historical data your script needs. So, you either have to change the organization of your script to help it, or manually enter the number of bars your script will need by passing the max_bars_back parameter to your study() or strategy() call.

Example: I create an SMA that runs over 200 bars. So, I need to pass strategy("example", max_bars_back=200) 200 is the minimum here, so you may need to make the value larger, depending on how much data you want to analyze.

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.

Side-note: 'max_bars_back' sounded like a poor name choice to me at first, but now I'm thinking it's called that because it's the "max" number among the indicators you're using. So, if I had a 15-day sma(), a 50-day sma() and a 200-day sma(), 200 would be the "max" bars needed among all my calculations.