利益確定で有用なトレーリングストップの機能について
株価チャートで視覚的に分析しやすいように、TradingViewのストラテジーとして
岩瀬式トレーリングストップを作成しました。
通常のトレーリングストップとしても使えますが、
実践で使えるように、以下の条件でトレーリングストップを発動させる設定もできます。
①利益率が基準値を超えた時
②移動平均乖離率が基準値を超えた時
(基準値はご自身で設定可能です)
ぜひ、利益確定のタイミングなどの分析や検証で参考にして下さい。
Trading Viewのコード
下記のコードをTradingViewのPineエディタに入れて、インジケーターを作成してください。
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © M_iwase
//@version=6
strategy('Trigger & Trail Strategy', overlay = true, process_orders_on_close = true)
// === ユーザー入力(パラメータ設定) ===
// エントリー日を timestamp に変換
entry_date = input.time(defval = timestamp('2025-01-01 00:00 +0000'), title = 'エントリー日付')
//exitCondition = input(1, title="決済条件を選択 (1=利益率, 2=乖離率)")
exitConditionStr = input.string("利益率", title="トレーリングストップ条件を選択", options=["利益率", "乖離率"])
trigger_threshold_pct = input.int(50, title = 'トレーリングストップ発動閾値 (%)',minval=0,group="利益率条件") // 50% 上昇を変更可能に
// === ユーザーの選択を数値に変換(1=利益率, 2=乖離率) ===
exitCondition = exitConditionStr == "利益率" ? 1 : 2
maLength = input.int(20, title="移動平均の期間",minval=1,group="乖離率条件")
maDeviationLimit = input.int(10, title="移動平均乖離率 (%)",minval=0,group="乖離率条件")
trailing_stop_pct = input.int(5, title = 'トレーリングストップ (%)',minval=0)
// === エントリー管理フラグ ===
var bool entered = false // 初期値はfalse(エントリーしていない)
var float highest_since_entry = 0 // エントリー後の最高値
var float entry_price = na // 実際のエントリー価格
var bool trailing_stop_activated = false // トレーリングストップ発動フラグ
// 検証用 1行2列のテーブルを作成
var table log_table = table.new(position = position.top_right, columns = 2, rows = 5)
// === 移動平均の計算 ===
ma = ta.sma(close, maLength)
plot(ma, title="移動平均", color=color.blue, linewidth=2)
// === 乖離率の計算 ===
deviation = ((close - ma) / ma) * 100
bgcolor(deviation > maDeviationLimit ? color.new(color.red, 85) : na)
// エントリー条件(エントリー日以降、ポジションなし、かつ未エントリー)
entry_condition = time >= entry_date and strategy.position_size == 0 and not entered
if strategy.position_size != 0
entered := true // エントリー済みフラグをON
if entry_condition
highest_since_entry := close // エントリー直後の価格を基準にする
entry_price := close
strategy.entry('Long', strategy.long, comment = 'エントリー')
entry_date_str = str.tostring(year(entry_date)) + '-' + str.tostring(month(entry_date)) + '-' + str.tostring(dayofmonth(entry_date))
// テーブルにデータを追加
if bar_index == bar_index
table.cell(log_table, 0, 0, '項目', text_color = color.white, bgcolor = color.gray)
table.cell(log_table, 1, 0, '値', text_color = color.white, bgcolor = color.gray)
table.cell(log_table, 0, 1, '日付')
table.cell(log_table, 1, 1, str.tostring(entry_date_str))
table.cell(log_table, 0, 2, 'エントリー価格')
table.cell(log_table, 1, 2, str.tostring(entry_price))
// === 決済条件(トレーリングストップ) ===
var float trail_stop = na
if strategy.position_size > 0
if high >= highest_since_entry
highest_since_entry := high // エントリー後の最高値を更新
time_str = str.tostring(year(time)) + '-' + str.tostring(month(time)) + '-' + str.tostring(dayofmonth(time))
table.cell(log_table, 0, 3, '最高値日付')
table.cell(log_table, 1, 3, str.tostring(time_str))
table.cell(log_table, 0, 4, '最高値')
table.cell(log_table, 1, 4, str.tostring(highest_since_entry))
// トレーリングストップの発動条件(エントリー価格 + 50% 以上に達したら)
if ( highest_since_entry >= entry_price * (1 + trigger_threshold_pct / 100) and exitCondition ==1) or (deviation >= maDeviationLimit and exitCondition ==2)
trailing_stop_activated := true // トレーリングストップ発動
if exitCondition ==2
time_str = str.tostring(year(time)) + '-' + str.tostring(month(time)) + '-' + str.tostring(dayofmonth(time))
table.cell(log_table, 0, 3, '乖離率ヒット')
table.cell(log_table, 1, 3, str.tostring(time_str))
// トレーリングストップが発動したら、最高値から指定割合下落で決済
if trailing_stop_activated
trail_stop := highest_since_entry * (1 - trailing_stop_pct / 100) // 最高値から10%下がったら決済
strategy.exit('Exit', from_entry = 'Long', stop = trail_stop, comment = 'トレーリングストップ')
plot_price = entry_price * (1 + trigger_threshold_pct / 100)
// === エントリー価格のラインを `plotshape()` で表示 ===
plotshape(not na(plot_price) ? plot_price : na, location=location.absolute, color=color.orange, style=shape.cross, title="エントリー価格")
TradingViewのインジケーターの登録について