Pine Compilation Errors

From TradingView Wiki
Jump to: navigation, search
Language: English  • 日本語 • Русский

Script requesting too many securities

The maximum number of securities in script is limited to 40. If you declare a variable as a security call and then use that variable as input for other variables and calculations, it will not result in multiple security calls. But if you will declare a function that calls security - every call to this function will count as security call.

It is not easy to say how many securities will be called looking at the source code. Following example have exactly 3 calls to security after compilation:

study("Securities count")
a = security(tickerid, '42', close) // (1) first unique security call
b = security(tickerid, '42', close) // same call as above, will not produce new security call after optimizations

plot(a + 2)

sym(p) => security(tickerid, p, close) // no security call on this line
plot(sym('D')) // (2) one indirect call to security
plot(sym('W')) // (3) another one indirect call to security

security(tickerid, period, open) // result of this line is never used, and will be optimized-out

Script could not be translated from: null


Usually this error occur in Version 1 pine scripts, and means that code is incorrect. Pine of Version 2 is better at explaining errors of this kind. So you can try to switch to version 2 by adding a special attribute in the first line. You'll get line 2: no viable alternative at character '$'

// @version=2

line 2: no viable alternative at character '$'

Now error message gives a hint on what is wrong. $ stands in place of string with script title.

// @version=2

Mismatched input <...> expect <???>

Same as "no viable alternative", but it is known what should be at that place.


study("My Script")
 line 3: mismatched input 'plot' expecting LEND

To fix this you should start line with 'plot' on a new line without indent

study("My Script")

Cannot call `operator ?:` with arguments (series, string, string)


study("Cannot call `operator ?:")
c = open > close ? '$' : '#'
plot(c == '$' ? 1 : 2)

The result of such ternary operator should be series of string. But there is no support for "series of string" in Pine Script. So this call is forbidden.

Loop is too long (> 200 ms)

We limit the computation time of loop on every historical bar and realtime tick to protect our servers from infinite or very long loops. This limit also fail-fast indicators that will take too long to compute. For example, if you'll have 5000 bars, and indicator takes 200ms to compute on each of bars, it would have result in more than 16 minutes of loading.

study("Loop is too long", max_bars_back=101)
s = 0
for i = 1 to 1e3 // to make it longer
    for j = 0 to 100
        if (timestamp(2017, 02, 23, 00, 00) <= time[j] and time[j] < timestamp(2017, 02, 23, 23, 59))
            s := s + 1

It might be possible to optimize algorithm to overcome this error. In this case, algorithm may be optimized like this:

study("Loop is too long", max_bars_back=101)
bar_back_at(t) =>
    i = 0
    step = 51
    for j = 1 to 100
        if i < 0
            i := 0
        if step == 0
        if time[i] >= t
            i := i + step
            i := i - step
        step := step / 2

s = 0
for i = 1 to 1e3 // to make it longer
    s := s - bar_back_at(timestamp(2017, 02, 23, 23, 59)) + bar_back_at(timestamp(2017, 02, 23, 00, 00))

Script has too many local variables

This error appears if the script is too large to be compiled. A statement var=expression creates a local variable for var. Apart from this, it is important to note, that auxiliary variables can be implicitly created during the process of a script compilation. The limit applies to variables created both explicitly and implicitly. The limitation of 1000 variables is applied to each function individually. In fact, the code placed in a "global" scope of a script also implicitly wrapped up into the main function and the limit of 1000 variables becomes applicable to it. There are few refactorings you can try to avoid this issue:

var1 = expr1
var2 = expr2
var3 = var1 + var2

can be сonverted into

var3 = expr1 + expr2

The study has too many output series

This error means that you have exceeded the maximum number of output series. These are the series whose values are plotted on the chart. There are several functions you can call to plot series - their names begin with plot: plot, plotcandle, plotbar, etc.
Please note:

  • plotbar and plotcandle functions actually plot 4 series (OHLC) at once;
  • serial (not constant) color in all plot functions is considered as an additional output series.

To avoid this error reduce the number of plot functions.

Previous: Indicator repainting, Next: Where can I get more information?, Up: Pine Script Tutorial