EMA(指数平滑移動平均線)でDiscordに乖離通知を出す(トレーディングビュー×プログラミング)
この記事を読み終わると得られること
- 一定以上EMAからの乖離があったらDiscordに知らせるコード
- TradingViewでの設定方法
//@version=5
indicator("EMA + 乖離通知 + 複数Webhook(ローソク足選択可)", overlay=true)
// === EMA計算 ===
ema25 = ta.ema(close, 25)
ema50 = ta.ema(close, 50)
ema100 = ta.ema(close, 100)
ema200 = ta.ema(close, 200)
// EMAライン表示
plot(ema25, color=color.yellow, title="EMA25")
plot(ema50, color=color.blue, title="EMA50")
plot(ema100, color=color.white, title="EMA100")
plot(ema200, color=color.orange, title="EMA200")
// === EMA通知対象チェック ===
notify25 = input.bool(true, "EMA25通知")
notify50 = input.bool(false,"EMA50通知")
notify100 = input.bool(false,"EMA100通知")
notify200 = input.bool(false,"EMA200通知")
// === 通知閾値 ===
deviationAlert = input.float(5, "通知閾値 (%)")
// === Webhook & 通知対象ローソク足(最大8個) ===
webhook1 = input.string("", "Webhook 1")
tf1 = input.timeframe("60", "Webhook1 通知対象ローソク足")
webhook2 = input.string("", "Webhook 2")
tf2 = input.timeframe("60", "Webhook2 通知対象ローソク足")
webhook3 = input.string("", "Webhook 3")
tf3 = input.timeframe("60", "Webhook3 通知対象ローソク足")
webhook4 = input.string("", "Webhook 4")
tf4 = input.timeframe("60", "Webhook4 通知対象ローソク足")
webhook5 = input.string("", "Webhook 5")
tf5 = input.timeframe("60", "Webhook5 通知対象ローソク足")
webhook6 = input.string("", "Webhook 6")
tf6 = input.timeframe("60", "Webhook6 通知対象ローソク足")
webhook7 = input.string("", "Webhook 7")
tf7 = input.timeframe("60", "Webhook7 通知対象ローソク足")
webhook8 = input.string("", "Webhook 8")
tf8 = input.timeframe("60", "Webhook8 通知対象ローソク足")
webhooks = array.from(webhook1,webhook2,webhook3,webhook4,webhook5,webhook6,webhook7,webhook8)
timeframes = array.from(tf1,tf2,tf3,tf4,tf5,tf6,tf7,tf8)
// === 乖離率計算 ===
dev25 = (close - ema25) / ema25 * 100
dev50 = (close - ema50) / ema50 * 100
dev100 = (close - ema100) / ema100 * 100
dev200 = (close - ema200) / ema200 * 100
// === 小窓表示 ===
showDeviation = input.bool(true,"小窓表示")
tablePosition = input.string("top_right", "小窓位置", options=["top_right","top_left","bottom_right","bottom_left"])
getPos(opt) => opt == "top_right" ? position.top_right : opt == "top_left" ? position.top_left : opt == "bottom_right" ? position.bottom_right : position.bottom_left
pos = getPos(tablePosition)
var table info = table.new(pos,1,6,border_width=1,frame_color=color.gray)
colorDev(val) => val >= 0 ? color.new(color.green,0) : color.new(color.red,0)
if showDeviation and barstate.islast
table.cell(info,0,0,"EMA25: "+str.tostring(dev25,"#.##")+"%",text_color=colorDev(dev25))
table.cell(info,0,1,"EMA50: "+str.tostring(dev50,"#.##")+"%",text_color=colorDev(dev50))
table.cell(info,0,2,"EMA100:"+str.tostring(dev100,"#.##")+"%",text_color=colorDev(dev100))
table.cell(info,0,3,"EMA200:"+str.tostring(dev200,"#.##")+"%",text_color=colorDev(dev200))
table.cell(info,0,4,"通知閾値: ±"+str.tostring(deviationAlert)+"%",text_color=color.yellow)
table.cell(info,0,5,"Webhookは設定欄で確認",text_color=color.blue)
// === アラート条件(Webhookごとにローソク足を判定) ===
for i = 0 to 7
w = array.get(webhooks,i)
tf = array.get(timeframes,i)
if w != "" and timeframe.isintraday and timeframe.period == tf
if notify25 and math.abs(dev25)>=deviationAlert
alert(w+" EMA25乖離通知: "+str.tostring(dev25,"#.##")+"%", alert.freq_once_per_bar_close)
if notify50 and math.abs(dev50)>=deviationAlert
alert(w+" EMA50乖離通知: "+str.tostring(dev50,"#.##")+"%", alert.freq_once_per_bar_close)
if notify100 and math.abs(dev100)>=deviationAlert
alert(w+" EMA100乖離通知: "+str.tostring(dev100,"#.##")+"%", alert.freq_once_per_bar_close)
if notify200 and math.abs(dev200)>=deviationAlert
alert(w+" EMA200乖離通知: "+str.tostring(dev200,"#.##")+"%", alert.freq_once_per_bar_close)
・このTradingViewコードの使い方
1. TradingViewに無料登録
登録してチャートを開いたら画面左下のPine Editorを選択。

2. PineEditorにスクリプトを貼り付けてUpdateを押せば完了

EMAの機能だけ使いたかったら乖離の小窓を消す欄が設定にあるので適宜どうぞ。なお、通知はDiscordのWebhookURLを入力することによって乖離を通知できます。WebhookURLの設定方法は「仮想通貨BotやEA等の通知をディスコードのウェブフックURLで送る方法」から閲覧してください。

最後までお読みいただきありがとうございました。