`alert()` 関数を利用した新しいスクリプトアラートは、ストラテジーとインジケーターの双方で動作し、アラートのトリガー時に完全に動的なメッセージを生成する事ができます。
新しいアラートは、最近のストラテジーアラートに似たモデルで動作します。チャートのユーザーインターフェースで作成された1つのアラートだけで、単一のストラテジーアラートがすべてのブローカーイベントのトリガーを集約するのと同じように、スクリプト内の任意の数の `alert()` の呼び出しによって生成されたすべてのトリガーを集約することができます。
新しいアラートを作成するには:
- スクリプトに必要な数だけ `alert()` の呼び出しを含めて、それぞれをトリガー条件を定義する `if` ブロックで囲みます。
- チャートの「アラート作成」ダイアログボックスを使用して、スクリプトに1つアラートを設定し、「alert() 関数の呼び出し」を含むアラートタイプを選択します。
ストラテジーで使用する場合、ユーザーはアラートを作成する際に、`alert()` イベントのみをトリガーするか、約定イベントのみをトリガーするか、それら双方をトリガーするかを選択できます。また、動的なアラートメッセージに使用できる変数の数に制限はなく、スクリプトで使用される変数は、文字列形式であれば、新しい `alert()` 関数の呼び出しでも利用できますので、もはやプレースホルダーは必要ありません。
以下は `alert()` 関数の3つの例です:
1) スクリプトのアラートは各バーでトリガーされ、メッセージにはバーの終値が含まれます:
//@version=4
study("Simple alert() example")
plot(close)
alert("Close = " + tostring(close), alert.freq_once_per_bar_close)
2) RSIとSMA、モメンタムの3つのインジケーターの値をチェックして、いずれかのインジケーターが指定したレベル(または価格)を交差した場合にスクリプトアラートがトリガーされ、メッセージにはインジケーター名とその値が含まれます。
//@version=4
study("alert() with multiple indicators", overlay=true)
f_triggerSma()=>
_s = sma(close, 14)
_co = crossover(close, _s)
_cu = crossunder(close, _s)
if _co
alert("Price (" + tostring(close) + ") crossing up SMA (" + tostring(_s) + ")", alert.freq_once_per_bar)
else if _cu
alert("Price (" + tostring(close) + ") crossing down SMA (" + tostring(_s) + ")", alert.freq_once_per_bar)
f_triggerRsi()=>
_r = rsi(close, 7)
_co = crossover(_r, 70)
_cu = crossunder(_r, 30)
if _co
alert("RSI (" + tostring(_r) + ") crossing up 70 level", alert.freq_once_per_bar)
else if _cu
alert("RSI (" + tostring(_r) + ") crossing down 30 level", alert.freq_once_per_bar)
f_triggerMom()=>
_m = mom(close, 14)
_co = crossover(_m, 0)
_cu = crossunder(_m, 0)
if _co
alert("Momentum (" + tostring(_m) + ") crossing up 0 level", alert.freq_once_per_bar)
else if _cu
alert("Momentum (" + tostring(_m) + ") crossing down 0 level", alert.freq_once_per_bar)
plot(sma(close, 14), "SMA")
f_triggerSma()
f_triggerRsi()
f_triggerMom()
3) この例では、利用するインジケーターはRSIのみですが、同時に5つのシンボルの値をチェックします。スクリプトアラートは、RSIがいずれかのシンボルで指定したレベルを交差した場合にトリガーされ、メッセージにはシンボルと交差した時のインジケーターの値が含まれます。
//@version=4
study("alert() with multiple symbols")
f_triggerRsi(_ticker)=>
_r = rsi(close, 7)
_x = crossover(_r,70)
_y = crossunder(_r,30)
_rt = barstate.isrealtime
[_rsi, _co, _cu, _rt_bar] = security(_ticker, timeframe.period, [_r, _x, _y, _rt])
_msg = _ticker + ", " + timeframe.period + ": "
if _co and _rt_bar
_msg := _msg + "RSI (" + tostring(_rsi) + ") crossing up 70 level"
alert(_msg, alert.freq_once_per_bar_close)
else if _cu and _rt_bar
_msg := _msg + "RSI (" + tostring(_rsi) + ") crossing down 30 level"
alert(_msg, alert.freq_once_per_bar_close)
plot(rsi(close, 7), "RSI", color=#8E1599)
band1 = hline(70, "Upper Band", color=#C0C0C0)
band0 = hline(30, "Lower Band", color=#C0C0C0)
fill(band1, band0, color=color.new(#9915FF,90), title="Background")
f_triggerRsi(syminfo.tickerid)
f_triggerRsi("NASDAQ:MSFT")
f_triggerRsi("FX:EURUSD")
f_triggerRsi("NASDAQ:TSLA")
f_triggerRsi("NASDAQ:PYPL")
スクリプトを利用される方は、ヘルプセンターでアラートの作成方法をご確認頂けます。Pineプログラマーの方は、Pineのユーザーマニュアルとリファレンスマニュアルで新しいアラートについてご確認ください。
Pineのすべての更新については、ユーザーマニュアルのリリースノートをご覧ください。