Pineスクリプトで規模の大きいシステムを実装する際に関係する代表的なコンパイラ制限値を調査した。
- Pineスクリプトで利用できるローカル変数の数は最大999個まで
- Pineスクリプトでコンパイル可能なローカルスコープの数は最大499個まで
Pineスクリプトで利用できるローカル変数の数は最大999個まで
Pineスクリプトで利用できるローカル変数の最大数を調べてみたところ、変数が1000個に達した時点で下記のエラーメッセージが出た。
Script has too many local variables (1001) in "#f1": #loc_0, #loc_1, #loc_2, #loc_3, #loc_4... The limit is 1000
1000に達するとエラーとなるので、実際に利用できる変数の最大数は999個までとなる。
※open
、close
等のビルトイン変数は、この数に含まれない。
変数の最大数を確かめるために、下記のようなコードを利用した。長いので途中は省略している。
//@version=4
study("max limit num of variables")
x1 = open + high + low + close
x2 = x1
x3 = x2
x4 = x3
//
//省略...
//
x997 = x996
x998 = x997
x999 = x998
//x1000 = x999 // Script has too many local variables...The limit is 1000
plot(x999)
//plot(x1000)
また、mutable
変数の場合の制限についても調べてみた。
※変数に :=
演算子を使うと、その変数は mutable
(変更可能な)変数としてPineコンパイラに認識される。
mutable
変数の場合、値を変更する度に別の一つの変数として数えられるようで、998回まで :=
演算子を使った値の変更が可能だが、999回で下記の "The limit is 1000"
エラーが出た。
Script has too many local variables (1002) in "#f1": #mut_0, #mut_0, #mut_0, #mut_0, #mut_0... The limit is 1000
mutable
変数の制限の確認には、下記のようなコードを使用した。長いので途中は省略している。
//@version=4
study("max limit num of variables")
x = close
x := x[1]
x := x[1] - x[2]
x := x[2] - x[3]
//
//省略...
//
x := x[996] - x[997]
x := x[997] - x[998]
//x := x[998] - x[999] // Script has too many local variables...The limit is 1000
plot(x)
Pineスクリプトでコンパイル可能なローカルスコープの数は最大499個まで
Pineスクリプトの関数、if
文、for
文などはそれぞれローカルスコープを持つ。そのローカルスコープを最大どれだけ持てるのか、別の言い方をすると、関数をどれだけ定義できるか、if
文やfor
文をどれだけ書けるか、その最大数を調べてみた。
簡単なコードで確認したところ、ローカルスコープが500個できるようにした時点でエラーが出た。
Script has too many local scopes: 501. The limit is 500.
500個でエラーが出るので、実際可能なのは499個まで。関数、if
、for
それぞれの確認コードを作ったが、ローカルスコープの最大数は関数、if
、for
など合わせて全体で499個までとなる。例えば、関数を499個定義したら、それ以上 if
や for
など一つも使うことはできないことになる。
//@version=4
study("max limit num of scopes")
x = close
func1(src) => src[1]
x := func1(close)
func2(src) => src[2]
x := func2(close)
func3(src) => src[3]
x := func3(close)
//
//省略...
//
func499(src) => src[499]
x := func499(close)
//func500(src) => src[500] // Script has too many local scopes: 501. The limit is 500.
//x := func500(close)
plot(x)
//@version=4
study("max limit num of scopes")
x = close
if x > close[1]
x := open[1]
if x > close[2]
x := open[2]
if x > close[3]
x := open[3]
//
//省略...
//
if x > close[499]
x := open[499]
//if x > close[500] // Script has too many local scopes: 501. The limit is 500.
// x := open[500]
plot(x)
//@version=4
study("max limit num of scopes")
x = close
for i = 1 to 1
x := close[1]
for i = 2 to 2
x := close[2]
for i = 3 to 3
x := close[3]
//
//省略...
//
for i = 499 to 499
x := close[499]
//for i = 500 to 500 // Script has too many local scopes: 501. The limit is 500.
// x := close[500]
plot(x)