if文

if

If文は、式の条件が満たされたときに実行されなければならない文のブロックを定義します。if文にアクセスして使用するには、コードの最初の行にPine Script言語のバージョン2以上を指定する必要があります。
例:
//@version=4
The 4th version of Pine Script Language allows you to use “else if” syntax.

一般コードフォーム:
var_declarationX = if condition
  var_decl_then0
  var_decl_then1
  …
  var_decl_thenN
else if [optional block]
  var_decl_else0
  var_decl_else1
  …
  var_decl_elseN
else
  var_decl_else0
  var_decl_else1
  …
  var_decl_elseN
  return_expression_else

解説:
var_declarationX — この変数はifステートメントの値を取得します
condition — if the condition is true, the logic from the block ‘then’ (var_decl_then0, var_decl_then1, etc.) is used.If the condition is false, the logic from the block ‘else’ (var_decl_else0, var_decl_else1, etc.) is used.
return_expression_thenreturn_expression_else — 「thenブロック」または「elseブロック」の最後の式は、ステートメントの最終値を返します。変数の宣言が最後にある場合、その値が結果の値になります。if文の戻り値の型は、return_expression_then型かreturn_expression_else型かによって異なります(型が一致する必要があります。つまり、elseブロックに文字列の値がある間は整数値を返すことはできません)。

// This code compiles
x = if close > open
    close
else
    open
// This code doesn’t compile
else
x = if close > open
    close
else
    "open"

else ブロックを省略することが可能です。この場合、条件が偽の場合、”空” の値 (na、false、または “”) がvar_declarationX変数に代入されます。

x = if close > open
    close
// If current close > current open, then x = close.
// Otherwise the x = na.

複数の “else if” ブロックを使用することも可能です。

x = if open > close
    5
else if high > low
    close
else
    open

ブロック “then” “else if”、”else” は 4 つのスペースだけシフトされます。ステートメントに相互に組み合える場合は、+4 個のスペースを指定します。

x = if close > open
    b = if close > close[1]
        close
    else
        close[1]
    b
else
    open

結果として得られるif文の値を無視することができます(“var_declarationX =”は省略可能です)。式の副次効果が必要な場合、例えばストラテジートレードの場合などに便利です。

if (crossover(source, lower))
    strategy.entry("BBandLE", strategy.long, stop=lower,
        oca_name="BollingerBands",
    oca_type=strategy.oca.cancel, comment="BBandLE")
else
    strategy.cancel(id="BBandLE")

\ 最新情報をチェック /

コメントを残す

Pineスクリプト

前の記事

forループ
Pineスクリプト

次の記事

accdist