Pineスクリプト作成代行はこちら

Pineスクリプトのinput関数完全解説|ユーザー設定パネルの作り方

Pineスクリプトでインジケーターを作ると、パラメータの変更のたびにコードを書き換えることになる。EMAの期間を20から50に変えたいだけなのに、エディタを開いてコードを修正して再保存。これでは使い物にならない。

input関数を使えば、TradingViewの設定パネルにパラメータを表示し、UIからワンクリックで変更できるようになる。期間、色、ソース、時間足、ON/OFFスイッチ。あらゆるパラメータをユーザーが自由にカスタマイズできる。

ただし、v6ではinput()という汎用関数ではなく、input.int()input.float()input.bool()のように型別の専用関数を使うのが標準だ。v4/v5時代のinput()とは書き方が変わっている。

この記事では、v6のinput関数を型別にすべて解説し、groupinlinetooltipを使った設定パネルの設計まで、実用的なコード付きで網羅する。

Pineスクリプトの実践テクニックを見る

input関数の基本 — なぜ型別関数を使うのか

v4時代はinput(defval=12, type=input.integer)のように1つのinput()関数で型を指定していた。v6ではこの書き方は非推奨になり、型ごとに専用の関数が用意されている。

//@version=6
indicator("input基本", overlay=true)

// v6の書き方(推奨)
length = input.int(20, "EMA期間")
src = input.source(close, "ソース")
showLine = input.bool(true, "ライン表示")

// v4の書き方(非推奨・互換用)
// length = input(20, type=input.integer)

plot(showLine ? ta.ema(src, length) : na, "EMA", color.blue, 2)

型別関数を使うメリットは3つ。コードの可読性が上がる。型の不一致エラーをコンパイル時に検出できる。各型に特化した引数(minvalmaxvaloptionsなど)が使える。

v6で使えるinput関数の一覧はこうだ。input.int() — 整数。input.float() — 小数。input.bool() — 真偽値。input.string() — 文字列。input.source() — 価格ソース。input.timeframe() — 時間足。input.color() — 色。input.time() — 日時。input.price() — 価格(ドラッグ可能)。input.text_area() — 複数行テキスト。

input.int() — 整数の入力

最も使用頻度が高いinput関数。移動平均の期間、RSIの閾値、バーの本数など、整数パラメータに使う。

//@version=6
indicator("input.int", overlay=true)

// 基本
length = input.int(20, "期間")

// 範囲制限(minval/maxval)
rsiLen = input.int(14, "RSI期間", minval=1, maxval=200)

// ステップ値(スライダーのような操作感)
thickness = input.int(2, "線の太さ", minval=1, maxval=5, step=1)

plot(ta.sma(close, length), "SMA", color.blue, thickness)

主要引数:

defval — デフォルト値(第1引数)。title — 設定パネルに表示される名前(第2引数)。minval — 最小値。これ未満の値を入力するとエラーになる。maxval — 最大値。step — 増減ボタンのステップ幅。tooltip — ホバー時に表示されるヒントテキスト。group — 設定パネル内のグループ名(後述)。

minvalmaxval必ず設定すべきだ。期間に0やマイナス値を入れるとランタイムエラーになる。minval=1を付けるだけで防げる。

input.float() — 小数の入力

ATRの倍率、フィボナッチ比率、損益比率など、小数を扱うパラメータに使う。

//@version=6
indicator("input.float", overlay=true)

atrMulti = input.float(1.5, "ATR倍率", minval=0.1, maxval=10.0, step=0.1)
atrLen = input.int(14, "ATR期間", minval=1)

atrValue = ta.atr(atrLen)
upper = close + atrValue * atrMulti
lower = close - atrValue * atrMulti

plot(upper, "上限", color.red)
plot(lower, "下限", color.green)

step=0.1を指定すると、設定パネルの上下ボタンで0.1刻みに値を変更できる。小数の入力をinput.int()で受けるとコンパイルエラーになるので、必ずinput.float()を使うこと。

input.bool() — ON/OFFスイッチ

チェックボックスとして表示される。機能のON/OFF、表示の切り替えに使う。

//@version=6
indicator("input.bool", overlay=true)

showSMA = input.bool(true, "SMA表示")
showEMA = input.bool(true, "EMA表示")
fillBetween = input.bool(false, "間を塗りつぶす")
length = input.int(20, "期間")

smaLine = ta.sma(close, length)
emaLine = ta.ema(close, length)

p1 = plot(showSMA ? smaLine : na, "SMA", color.blue, 2)
p2 = plot(showEMA ? emaLine : na, "EMA", color.red, 2)

fill(p1, p2, color=fillBetween ? color.new(color.purple, 85) : na)

input.bool()の戻り値はtrueまたはfalse。三項演算子condition ? A : Bと組み合わせて、描画の表示/非表示を切り替えるのが定番パターンだ。

input.string() — 文字列・ドロップダウン選択

options引数を付けるとドロップダウンメニューになる。MA種類の切り替えなどに使う。

//@version=6
indicator("input.string", overlay=true)

maType = input.string("EMA", "MA種類", options=["SMA", "EMA", "WMA", "VWMA", "RMA"])
length = input.int(20, "期間")

ma = switch maType
    "SMA"  => ta.sma(close, length)
    "EMA"  => ta.ema(close, length)
    "WMA"  => ta.wma(close, length)
    "VWMA" => ta.vwma(close, length)
    "RMA"  => ta.rma(close, length)
    => ta.ema(close, length)

plot(ma, maType, color.blue, 2)

optionsを省略すると自由入力のテキストフィールドになるが、ユーザーの入力ミスを防ぐためにドロップダウン(options指定)を使うのが推奨だ。switch文と組み合わせると、選択された文字列に応じて処理を分岐できる。

input.source() — 価格ソースの選択

closeopenhighlowhl2hlc3ohlc4などの価格ソースをユーザーが選択できる。さらに、チャートに表示されている他のインジケーターの出力も選択肢に含まれる。

//@version=6
indicator("input.source", overlay=true)

src = input.source(close, "ソース")
length = input.int(14, "期間")

plot(ta.rsi(src, length))

input.source()のドロップダウンには、他のインジケーターのplot()出力も表示される。つまり、インジケーターAの出力をインジケーターBの入力として使える。これがPineスクリプトの「インジケーターonインジケーター」機能だ。

input.timeframe() — 時間足の選択

マルチタイムフレーム(MTF)インジケーターで上位足を選択させるときに使う。

//@version=6
indicator("input.timeframe", overlay=true)

tf = input.timeframe("D", "上位足")
length = input.int(20, "期間")

htfMA = request.security(syminfo.tickerid, tf, ta.ema(close, length))
plot(htfMA, "HTF EMA", color.orange, 2)

ドロップダウンに表示される時間足は、"1"(1分)、"5"(5分)、"15"(15分)、"60"(1時間)、"240"(4時間)、"D"(日足)、"W"(週足)、"M"(月足)など。空文字""を指定すると「チャートの時間足と同じ」という意味になる。

input.color() — 色の選択

カラーピッカーとして表示される。ラインやエリアの色をユーザーがカスタマイズできる。

//@version=6
indicator("input.color", overlay=true)

bullColor = input.color(color.green, "上昇色")
bearColor = input.color(color.red, "下降色")
length = input.int(20, "期間")

ma = ta.ema(close, length)
maColor = close > ma ? bullColor : bearColor

plot(ma, "EMA", maColor, 2)

カラーピッカーでは色だけでなく透明度も設定できる。color.new(color.red, 50)のようにコードで透明度を固定することもできるが、input.color()を使えばユーザーが自由に調整できる。

input.time() — 日時の選択

カレンダーUIで特定の日時を選択できる。バックテストの開始日・終了日の指定に使う。

//@version=6
strategy("input.time", overlay=true)

startDate = input.time(timestamp("2024-01-01"), "開始日")
endDate = input.time(timestamp("2024-12-31"), "終了日")
inRange = time >= startDate and time <= endDate

fast = ta.ema(close, 12)
slow = ta.ema(close, 26)

if inRange and ta.crossover(fast, slow)
    strategy.entry("Long", strategy.long)
if inRange and ta.crossunder(fast, slow)
    strategy.close("Long")

timestamp()でデフォルト値をISO形式の文字列で指定する。設定パネルにはカレンダーUIが表示され、マウスで日付を選択できる。

input.price() — ドラッグ可能な価格ライン

設定パネルに値を入力できるだけでなく、チャート上でラインをドラッグして価格を変更できる。TP/SLラインの設定に便利だ。

//@version=6
indicator("input.price", overlay=true)

tp = input.price(defval=0, title="利確ライン", confirm=true)
sl = input.price(defval=0, title="損切ライン", confirm=true)

plot(tp != 0 ? tp : na, "TP", color.green, 2, plot.style_linebr)
plot(sl != 0 ? sl : na, "SL", color.red, 2, plot.style_linebr)

confirm=trueを付けると、インジケーターをチャートに追加する際にチャートをクリックして価格を指定するモードになる。ユーザーがクリックした位置の価格が初期値として設定される。

input.text_area() — 複数行テキスト

Webhook用のJSONテンプレートや、カスタムメッセージの入力に使う。

//@version=6
indicator("input.text_area", overlay=true)
fast = ta.ema(close, 12)
slow = ta.ema(close, 26)

msgTemplate = input.text_area('{"action":"buy","price":{{close}}}', "Webhookメッセージ")

if ta.crossover(fast, slow)
    alert(msgTemplate, alert.freq_once_per_bar_close)

通常のinput.string()は1行テキストだが、input.text_area()は複数行のテキストを入力できる。改行を含むJSON文字列やマルチライン文字列に使う。

公式リファレンスで各input関数の全引数を確認したい方はこちら。

Pineスクリプトリファレンスの読み方|公式マニュアルを使いこなすコツ

Pineスクリプトの全テクニックを探す

group — 設定パネルをセクション分けする

入力項目が10個を超えると、設定パネルが縦に長くなり見づらくなる。group引数でセクション分けすると、折りたたみ可能なグループに整理できる。

//@version=6
indicator("groupの例", overlay=true)

// EMAグループ
showEMA = input.bool(true, "表示", group="EMA設定")
emaLen = input.int(20, "期間", minval=1, group="EMA設定")
emaSrc = input.source(close, "ソース", group="EMA設定")
emaColor = input.color(color.blue, "色", group="EMA設定")

// BBグループ
showBB = input.bool(true, "表示", group="ボリンジャーバンド設定")
bbLen = input.int(20, "期間", minval=1, group="ボリンジャーバンド設定")
bbMult = input.float(2.0, "偏差", minval=0.1, step=0.1, group="ボリンジャーバンド設定")

// EMA描画
ema = ta.ema(emaSrc, emaLen)
plot(showEMA ? ema : na, "EMA", emaColor, 2)

// BB描画
[bbMid, bbUp, bbLow] = ta.bb(close, bbLen, bbMult)
plot(showBB ? bbUp : na, "BB Upper", color.gray)
plot(showBB ? bbMid : na, "BB Mid", color.gray, 1, plot.style_stepline)
plot(showBB ? bbLow : na, "BB Lower", color.gray)

同じgroup文字列を持つinputが1つのセクションにまとまる。セクション名が設定パネルに見出しとして表示され、クリックで折りたたみ/展開できる。

groupの命名ルールとして、機能単位でグループ化するのが見やすい。「EMA設定」「RSI設定」「表示設定」「アラート設定」のように分けるとプロ品質のUIになる。

inline — 複数の入力を1行にまとめる

色と有効/無効のスイッチを横並びにしたり、期間とソースを1行に収めたりするのに使う。

//@version=6
indicator("inlineの例", overlay=true)

// 色とON/OFFスイッチを1行にまとめる
showMA1 = input.bool(true, "MA1", inline="ma1", group="移動平均線")
ma1Color = input.color(color.blue, "", inline="ma1", group="移動平均線")
ma1Len = input.int(20, "期間", inline="ma1", group="移動平均線")

showMA2 = input.bool(true, "MA2", inline="ma2", group="移動平均線")
ma2Color = input.color(color.red, "", inline="ma2", group="移動平均線")
ma2Len = input.int(50, "期間", inline="ma2", group="移動平均線")

showMA3 = input.bool(false, "MA3", inline="ma3", group="移動平均線")
ma3Color = input.color(color.orange, "", inline="ma3", group="移動平均線")
ma3Len = input.int(200, "期間", inline="ma3", group="移動平均線")

plot(showMA1 ? ta.ema(close, ma1Len) : na, "MA1", ma1Color, 2)
plot(showMA2 ? ta.ema(close, ma2Len) : na, "MA2", ma2Color, 2)
plot(showMA3 ? ta.ema(close, ma3Len) : na, "MA3", ma3Color, 2)

同じinline文字列を持つinputが横一列に並ぶ。2番目以降のinputのtitleを空文字""にすると、ラベルなしでコンパクトに表示される。

上の例では、1行に「チェックボックス → カラーピッカー → 期間入力」が並ぶ。TradingView公式のインジケーターと同じ見た目になる。

tooltip — ホバーヒントを付ける

設定項目にマウスを重ねたときにヒントテキストを表示する。パラメータの意味やおすすめ値を補足できる。

//@version=6
indicator("tooltipの例", overlay=true)

length = input.int(20, "EMA期間", minval=1, maxval=500,
    tooltip="短期: 5-20、中期: 20-50、長期: 50-200 が一般的です")

atrMulti = input.float(1.5, "ATR倍率", minval=0.1, step=0.1,
    tooltip="1.0=ATR幅、1.5=ATR×1.5。大きいほどバンドが広がります")

maType = input.string("EMA", "MA種類", options=["SMA", "EMA", "WMA"],
    tooltip="SMA=単純移動平均、EMA=指数移動平均、WMA=加重移動平均")

ma = switch maType
    "SMA" => ta.sma(close, length)
    "EMA" => ta.ema(close, length)
    "WMA" => ta.wma(close, length)
    => ta.ema(close, length)

plot(ma, maType, color.blue, 2)

tooltipは日本語で書ける。スクリプトを公開する場合や、自分以外のトレーダーが使う場合に、パラメータの意味を説明するのに役立つ。

confirm — チャートクリックで初期値を設定する

confirm=trueを付けると、インジケーターをチャートに追加する際に、チャート上をクリックして初期値を設定するモードになる。

//@version=6
indicator("confirmの例", overlay=true)

// チャートクリックで価格を指定
entryPrice = input.price(defval=0, title="エントリー価格", confirm=true)
tpPrice = input.price(defval=0, title="利確価格", confirm=true)
slPrice = input.price(defval=0, title="損切価格", confirm=true)

// ライン描画
plot(entryPrice != 0 ? entryPrice : na, "Entry", color.white, 2, plot.style_linebr)
plot(tpPrice != 0 ? tpPrice : na, "TP", color.green, 2, plot.style_linebr)
plot(slPrice != 0 ? slPrice : na, "SL", color.red, 2, plot.style_linebr)

// 損益比を計算
if barstate.islast and entryPrice != 0 and tpPrice != 0 and slPrice != 0
    rr = math.abs(tpPrice - entryPrice) / math.abs(entryPrice - slPrice)
    var label rrLabel = na
    label.delete(rrLabel)
    rrLabel := label.new(bar_index + 5, entryPrice, "RR: " + str.tostring(rr, "#.##"), style=label.style_label_left, color=color.new(color.white, 80), textcolor=color.white)

input.price()confirm=trueの組み合わせは、手動でTP/SLラインを引くツールに最適だ。input.time()confirm=trueを付ければ、チャートのバーをクリックして日時を指定することもできる。

実用例: プロ品質の設定パネル

ここまでの知識を組み合わせた、実用レベルのインジケーター設定パネルの実装例。

//@version=6
indicator("プロ設定パネル", overlay=true)

// ============ MA設定 ============
maType = input.string("EMA", "種類", options=["SMA", "EMA", "WMA", "VWMA"], group="移動平均線")
maLen = input.int(20, "期間", minval=1, maxval=500, group="移動平均線", tooltip="推奨: 短期5-20, 中期20-50, 長期50-200")
maSrc = input.source(close, "ソース", group="移動平均線")
maColor = input.color(color.blue, "色", group="移動平均線")
maWidth = input.int(2, "太さ", minval=1, maxval=5, group="移動平均線")

// ============ RSI設定 ============
showRSI = input.bool(false, "RSI背景色を表示", group="RSI")
rsiLen = input.int(14, "RSI期間", minval=1, group="RSI")
rsiOB = input.int(70, "買われすぎ", minval=50, maxval=100, group="RSI")
rsiOS = input.int(30, "売られすぎ", minval=0, maxval=50, group="RSI")

// ============ アラート設定 ============
alertOnCross = input.bool(true, "MA×価格クロスでアラート", group="アラート")
alertOnRSI = input.bool(false, "RSI過熱でアラート", group="アラート")

// ============ 計算 ============
ma = switch maType
    "SMA"  => ta.sma(maSrc, maLen)
    "EMA"  => ta.ema(maSrc, maLen)
    "WMA"  => ta.wma(maSrc, maLen)
    "VWMA" => ta.vwma(maSrc, maLen)
    => ta.ema(maSrc, maLen)

rsi = ta.rsi(close, rsiLen)

// ============ 描画 ============
plot(ma, maType + "(" + str.tostring(maLen) + ")", maColor, maWidth)

// RSI背景色
rsiBG = showRSI and rsi > rsiOB ? color.new(color.red, 90) : showRSI and rsi < rsiOS ? color.new(color.green, 90) : na
bgcolor(rsiBG)

// ============ アラート ============
crossUp = ta.crossover(close, ma)
crossDown = ta.crossunder(close, ma)

if alertOnCross and crossUp
    alert(syminfo.ticker + " 価格がMA上抜け", alert.freq_once_per_bar_close)
if alertOnCross and crossDown
    alert(syminfo.ticker + " 価格がMA下抜け", alert.freq_once_per_bar_close)
if alertOnRSI and ta.crossover(rsi, rsiOB)
    alert(syminfo.ticker + " RSI買われすぎゾーン突入", alert.freq_once_per_bar_close)

alertcondition(crossUp, "MA上抜け", "{{ticker}}: 価格がMA上抜け")
alertcondition(crossDown, "MA下抜け", "{{ticker}}: 価格がMA下抜け")

設定パネルには「移動平均線」「RSI」「アラート」の3つのグループが表示される。各グループは折りたたみ可能で、必要なセクションだけを展開して設定できる。

Pineスクリプトの基本構文から復習したい方はこちら。

Pineスクリプトの書き方|基本構文をコード付きで完全解説

input関数一覧表

v6で使えるinput関数の引数を一覧でまとめる。

input.int(defval, title, minval, maxval, step, tooltip, inline, group, confirm) — 整数。設定パネルに数値入力フィールドを表示。

input.float(defval, title, minval, maxval, step, tooltip, inline, group, confirm) — 小数。数値入力フィールド。stepで小数点以下の刻みを制御。

input.bool(defval, title, tooltip, inline, group, confirm) — 真偽値。チェックボックスを表示。

input.string(defval, title, options, tooltip, inline, group, confirm) — 文字列。options指定でドロップダウン、省略でテキストフィールド。

input.source(defval, title, tooltip, inline, group) — 価格ソース。ドロップダウンにclose, open, high, low, hl2, hlc3, ohlc4と他インジケーターの出力が表示。

input.timeframe(defval, title, tooltip, inline, group) — 時間足。ドロップダウンに時間足一覧を表示。空文字""で「チャートと同じ」。

input.color(defval, title, tooltip, inline, group) — 色。カラーピッカー(透明度調整付き)を表示。

input.time(defval, title, tooltip, inline, group, confirm) — 日時。カレンダーUIを表示。timestamp()でデフォルト値を指定。

input.price(defval, title, tooltip, inline, group, confirm) — 価格。confirm=trueでチャートクリック入力対応。

input.text_area(defval, title, tooltip, group) — 複数行テキスト。テキストエリアを表示。

よくあるミスと対処法

ミス①: input.int()に小数を渡してエラーinput.int(1.5, "倍率")はコンパイルエラー。小数を使うならinput.float()を使う。

ミス②: minvalを設定せず期間に0が入るta.sma(close, 0)はランタイムエラー。input.int()には必ずminval=1を付ける。

ミス③: groupの文字列が微妙に異なり別グループになるgroup="EMA設定"group="EMA 設定"(半角スペース)は別グループとして認識される。文字列を定数に切り出すのが安全だ。

ミス④: inlineを使ったのに横並びにならないinline文字列が完全一致しないと同じ行にまとまらない。groupも同じでなければならない。

ミス⑤: input()の戻り値をグローバルスコープ以外で使おうとするinput関数はスクリプトのトップレベル(グローバルスコープ)でのみ呼び出せる。if文やローカルスコープ内では使えない。

まとめ

v6のinput関数は型別に10種類。input.int()input.float()input.bool()input.string()の4つが基本で、ここにinput.source()input.timeframe()input.color()を加えれば大半のインジケーターに対応できる。

設定パネルの見た目はgroup(セクション分け)、inline(横並び)、tooltip(ヒント)の3つで制御する。この3つを使いこなせば、TradingView公式インジケーターと同等のプロ品質UIを実現できる。

Pineスクリプトの実践ノウハウをもっと見る

※当サイトの内容は投資助言を目的としたものではありません。FX取引にはリスクが伴い、投資元本を失う可能性があります。投資判断はご自身の責任でお願いいたします。