基本構文

?: #

三項条件演算子。

expr1 ? expr2 : expr3
戻り値 #

もしexpr1がtrueであればexpr2、そうでなければexpr3。 ゼロ値(0またはNaN、+ Infinity、-Infinity)はfalseとみなされ、他の値はtrueです。

#
// Draw circles at the bars where open crosses close
s2 = cross(open, close) ? avg(open,close) : na
plot(s2, style=plot.style_circles, linewidth=2, color=color.red)

// Combination of ?: operators for 'switch'-like logic
c = timeframe.isintraday ? color.red : timeframe.isdaily ? color.green : timeframe.isweekly ? color.blue : color.gray
plot(hl2, color=c)
備考 #

必要がない場合は、 ‘else’分岐にnaを使用してください。
2つ以上の ?: 演算子を組み合わせて、‘switch’ の様なステートメントと同等の機能を実現できます(上記の例をご参照ください)。
演算子は数値だけでなく系列変数でも使用できます。 系列を使用する場合は演算子は要素ごとに適用されます。

関連 #

iff, na

[] #

サブスクリプト形式。 expr1系列の以前の値へのアクセスを提供します。 expr2は過去バーの数であり、数値でなければなりません。 小数点は切り捨てられます。

expr1[expr2]
戻り値 #

一連の値

#
// [] can be used to "save" variable value between bars
a = 0.0 // declare `a`
a := a[1] // immediately set current value to the same as previous. `na` in the beginning of history
if high == low // if some condition - change `a` value to another
    a := low
関連 #

floor

and #

論理AND。 ブーリアン式に適用できます。

expr1 and expr2
戻り値 #

ブーリアン値、もしくはブーリアン値の系列

for #

Forステートメントは、複数の命令を繰り返し実行することを可能にします。

var_declarationX = for counter = from_num to to_num [by step_num]  var_decl0  var_decl1    continue    break    var_declN  return_expression
備考 #

変数 ‘sum’は可変変数であり、ループ本体中の演算子「=」によって新しい値が与えられます。また、より高速に計算されるため、移動平均の組み込み関数smaを使用することをお勧めします。

関連 #

sum

if #

If文は、式の条件が満たされたときに実行されなければならない文のブロックを定義します。

var_declarationX = if condition  var_decl_then0  var_decl_then1    var_decl_thenNelse if [optional block]  var_decl_else0  var_decl_else1    var_decl_elseNelse  var_decl_else0  var_decl_else1    var_decl_elseN  return_expression_else

not #

論理否定(NOT)。ブーリアン式に適用できます。

not expr1
戻り値 #

ブーリアン値、もしくはブーリアン値の系列

or #

論理OR。 ブーリアン式に適用できます。

expr1 or expr2
戻り値 #

ブーリアン値、もしくはブーリアン値の系列

var #

varは、変数の1度限りの初期化や代入に用いられるキーワードです。

var variable_name = expression

varip #

varip (var intrabar persist) は、変数の割当と一度だけの初期化に使用するキーワードです。varキーワードと似ていますが、varipで宣言された変数は、リアルタイムバーの更新の間も値を保持します。

varip variable_name = expression
備考 #

float, int, bool, string などのシンブルな型や,これらの型の配列でのみ使用できます。

na #

Double。NaN値(数値ではない)。

#
bar_index < 10 ? na : close    // CORRECT

close == na ? close[1] : close    // INCORRECT!
na(close) ? close[1] : close    // CORRECT
備考 #

戻り値にのみ使用してください。比較には使用できません!値が NaN かどうかを確認する必要がある場合は、ビルトイン関数 na を使用します。

関連 #

na

fixnan #

指定されたシリーズでは、NaN値を直近の非NaN値に置き換える。

fixnan(x)  series[float]
fixnan(x)  series[integer]
fixnan(x)  series[bool]
fixnan(x)  series[color]
戻り値 #

naギャップのない形式。

関連 #

na, na, nz

iff #

もし…なら…他に…

iff(condition, then, _else)  bool
iff(condition, then, _else)  integer
iff(condition, then, _else)  float
iff(condition, then, _else)  color
iff(condition, then, _else)  series[float]
iff(condition, then, _else)  series[integer]
iff(condition, then, _else)  series[color]
iff(condition, then, _else)  string
iff(condition, then, _else)  input bool
iff(condition, then, _else)  input integer
iff(condition, then, _else)  input float
iff(condition, then, _else)  input string
iff(condition, then, _else)  input color
iff(condition, then, _else)  const bool
iff(condition, then, _else)  const integer
iff(condition, then, _else)  const float
iff(condition, then, _else)  const string
iff(condition, then, _else)  const color
iff(condition, then, _else)  series[bool]
iff(condition, then, _else)  series[string]
iff(condition, then, _else)  series[line]
iff(condition, then, _else)  series[label]
iff(condition, then, _else)  array[]
戻り値 #

yまたはz形式。

引数 #
condition (series)条件値を持つ形式。 ゼロ値(0やNaN、+ Infinity、-Infinity)はfalseとみなされ、他の値はtrueです。
then (series)条件がtrueの場合に返される値の形式。
_else (series)conditionがfalseの場合に返される値を持つ系列。 ‘else’分岐が必要ない場合は、 _else引数にnaを使用してください。
#
// Draw circles at the bars where open crosses close
s1 = iff(cross(open, close), avg(open,close), na)
plot(s1, style=plot.style_circles, linewidth=4, color=color.green)
備考 #

iffは三項条件演算子{@op?:}と全く同じことをしますが、機能形式では同じです。 また、iffは演算子{@op?:}よりやや効率が劣ります。

関連 #

?:, na

na #

NaNの場合はテスト値。

na(x)  bool
na(x)  series[bool]
戻り値 #

xが有効な数値でない場合はtrue(xはNaN)、そうでない場合はfalse。

関連 #

na, fixnan, nz

nz #

NaN値を直列の0(または指定された値)に置き換えます。

nz(x, y)  integer
nz(x, y)  float
nz(x, y)  color
nz(x, y)  bool
nz(x, y)  series[integer]
nz(x, y)  series[float]
nz(x, y)  series[color]
nz(x, y)  series[bool]
nz(x)  integer
nz(x)  float
nz(x)  color
nz(x)  bool
nz(x)  series[integer]
nz(x)  series[float]
nz(x)  series[color]
nz(x)  series[bool]
戻り値 #

2つの引数形式:有効な(NaNではない)番号の場合はxを返し、それ以外の場合はyを返す。 ,1つの引数の型:有効な(NaNではない)番号の場合はxを返し、それ以外の場合は0を返します。

引数 #
x (series)処理する値の系列。
y (float)x系列においてすべてのNaN値の代わりに挿入される値。
#
nz(sma(close, 100))
関連 #

na, na, fixnan

© - 2021 - TradingViewの教科書