基本構文

?: #

三項条件演算子。

expr1 ? expr2 : expr3
戻り値 #

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

#
// Draw circles at the bars where open crosses close
s2 = ta.cross(open, close) ? math.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’ の様なステートメントと同等の機能を実現できます(上記の例をご参照ください)。
演算子は数値だけでなく系列変数でも使用できます。 系列を使用する場合は演算子は要素ごとに適用されます。

関連 #

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
関連 #

math.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
#
//@version=5
indicator("My sma")
my_sma(price, length) =>
    sum = price
    for i = 1 to length-1
        sum := sum + price[i]
    sum / length
plot(my_sma(close,14))
備考 #

Variable sum’ is a mutable variable and a new value can be given to it by an operator := in body of the loop. Also note that we recommend to use a built-in function ta.sma for Moving Average as it calculates faster.

関連 #

math.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
#
// This code compiles
x = if close > open
    close
else
    open

// This code doesn’t compile
x = if close > open
    close
else
    "open"
x = if close > open
    close
// If current close > current open, then x = close.
// Otherwise the x = na.
x = if open > close
    5
else if high > low
    close
else
    open
if (ta.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")
float x = na
if close > open
    if close > close[1]
        x := close
    else
        x := close[1]
else
    x := open

not #

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

not expr1
戻り値 #

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

or #

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

expr1 or expr2
戻り値 #

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

var #

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

var variable_name = expression
#
//@version=5
indicator("Var keyword example")
var a = close
var b = 0.0
var c = 0.0
var green_bars_count = 0
if close > open
	var x = close
	b := x
	green_bars_count := green_bars_count + 1
	if green_bars_count >= 10
		var y = close
		c := y
plot(a)
plot(b)
plot(c)

varip #

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

varip variable_name = expression
#
//@version=5
indicator('varip')
varip int v = -1
v := v + 1
plot(v)
備考 #

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(source)  series float
fixnan(source)  series int
fixnan(source)  series bool
fixnan(source)  series color
戻り値 #

naギャップのない形式。

関連 #

na, na, nz

na #

NaNの場合はテスト値。

na(x)  simple bool
na(x)  series bool
戻り値 #

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

関連 #

na, fixnan, nz

nz #

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

nz(source, replacement)  simple int
nz(source, replacement)  simple float
nz(source, replacement)  simple color
nz(source, replacement)  simple bool
nz(source, replacement)  series int
nz(source, replacement)  series float
nz(source, replacement)  series color
nz(source, replacement)  series bool
nz(source)  simple int
nz(source)  simple float
nz(source)  simple color
nz(source)  simple bool
nz(source)  series int
nz(source)  series float
nz(source)  series color
nz(source)  series bool
戻り値 #

The value of source if it is not na. If the value of source is na, returns zero, or the replacement argument when one is used.

引数 #
source (series int/float/bool/color)処理する値の系列。
replacement (series int/float/bool/color)Value that will replace all ‘na’ values in the source series.
#
nz(ta.sma(close, 100))
関連 #

na, na, fixnan

© - 2021 - TradingViewの教科書