← ポートフォリオに戻る

チャートプレビュー

MA Cross Signal チャートプレビュー

機能一覧

使い方

  1. TradingViewでチャートを開く
  2. Pine Editorにコードを貼り付け
  3. 「チャートに追加」をクリック
  4. 設定パネルでパラメータを調整

パラメータ設定

パラメータ デフォルト 説明
短期MA 5 短期移動平均の期間
中期MA 25 中期移動平均の期間
長期MA 75 長期移動平均の期間
MA種類 SMA 移動平均の種類(SMA / EMA)

ソースコード

// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Portfolio Sample - MA Cross Signal

//@version=6
indicator("MA Cross Signal [3本MA + 売買サイン + アラート]", overlay=true)

// ===== 入力パラメータ =====
fast_len   = input.int(5,   "短期MA", minval=1)
mid_len    = input.int(25,  "中期MA", minval=1)
slow_len   = input.int(75,  "長期MA", minval=1)
ma_type    = input.string("SMA", "MA種類", options=["SMA", "EMA"])

// ===== MA計算 =====
f_ma(src, len) =>
    ma_type == "EMA" ? ta.ema(src, len) : ta.sma(src, len)

fast = f_ma(close, fast_len)
mid  = f_ma(close, mid_len)
slow = f_ma(close, slow_len)

// ===== MA描画 =====
plot(fast, "短期MA", color.new(#2962FF, 0), 2)
plot(mid,  "中期MA", color.new(#FF6D00, 0), 2)
plot(slow, "長期MA", color.new(#E91E63, 0), 2)

// ===== パーフェクトオーダー判定 =====
bull_perfect = fast > mid and mid > slow
bear_perfect = fast < mid and mid < slow

// ===== 背景色(パーフェクトオーダー時) =====
bgcolor(bull_perfect ? color.new(#2962FF, 92) : bear_perfect ? color.new(#E91E63, 92) : na)

// ===== ゴールデンクロス / デッドクロス =====
bull_cross = ta.crossover(fast, mid) and mid > slow
bear_cross = ta.crossunder(fast, mid) and mid < slow

// ===== 売買サイン描画 =====
plotshape(bull_cross, "買いサイン", shape.triangleup,   location.belowbar, color.new(#2962FF, 0), size=size.small)
plotshape(bear_cross, "売りサイン", shape.triangledown, location.abovebar, color.new(#E91E63, 0), size=size.small)

// ===== アラート =====
alertcondition(bull_cross, "ゴールデンクロス", "GC: 短期MAが中期MAを上抜け(上昇トレンド中)")
alertcondition(bear_cross, "デッドクロス",     "DC: 短期MAが中期MAを下抜け(下降トレンド中)")

// ===== 情報テーブル =====
var table info = table.new(position.top_right, 2, 4, bgcolor=color.new(#1E293B, 10), border_width=1, border_color=color.new(#475569, 50))

if barstate.islast
    table.cell(info, 0, 0, "MA Cross Signal", text_color=color.white, text_size=size.small, bgcolor=color.new(#2962FF, 0))
    table.cell(info, 1, 0, "",                 bgcolor=color.new(#2962FF, 0))
    table.cell(info, 0, 1, "短期 " + str.tostring(fast_len), text_color=color.white, text_size=size.tiny)
    table.cell(info, 1, 1, str.tostring(fast, "#.0"), text_color=color.new(#2962FF, 0), text_size=size.tiny)
    table.cell(info, 0, 2, "中期 " + str.tostring(mid_len),  text_color=color.white, text_size=size.tiny)
    table.cell(info, 1, 2, str.tostring(mid, "#.0"),  text_color=color.new(#FF6D00, 0), text_size=size.tiny)
    table.cell(info, 0, 3, "長期 " + str.tostring(slow_len), text_color=color.white, text_size=size.tiny)
    table.cell(info, 1, 3, str.tostring(slow, "#.0"), text_color=color.new(#E91E63, 0), text_size=size.tiny)