FX Trading Tools

移動平均線クロスアラート - MT5カスタムインジケーター

移動平均線のクロスは、トレンド転換を捉える最もポピュラーなシグナルの一つです。 このインジケーターは、短期MAと長期MAのクロスを検知し、チャート上に矢印を表示してアラートを発信します。

MT5に対応した国内FX業者であれば、どの業者のMT5でもそのまま利用できます。

インジケーターの機能

  • ゴールデンクロス検知: 短期MAが長期MAを上抜けした時に上向き矢印を表示
  • デッドクロス検知: 短期MAが長期MAを下抜けした時に下向き矢印を表示
  • アラート通知: クロス発生時にポップアップ・サウンド・プッシュ通知
  • パラメータ調整: MA期間・種類をカスタマイズ可能

パラメータ設定

パラメータデフォルト値説明
FastMAPeriod20短期MA期間
SlowMAPeriod50長期MA期間
MAMethodMODE_EMAMA種類(SMA/EMA/SMMA/LWMA)
AppliedPricePRICE_CLOSE適用価格
EnableAlerttrueアラート有効/無効
EnablePushfalseプッシュ通知有効/無効

MQ5コード

以下のコードをコピーして、MT5に追加してください。 導入手順の詳細はMT5にカスタムインジケーターを追加する方法を参照してください。

MACrossAlert.mq5 mq5
//+------------------------------------------------------------------+
//|                                               MACrossAlert.mq5   |
//|                                          FX Trading Tools        |
//+------------------------------------------------------------------+
#property copyright "FX Trading Tools"
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 4
#property indicator_plots   2

#property indicator_label1  "Buy Signal"
#property indicator_type1   DRAW_ARROW
#property indicator_color1  clrDodgerBlue
#property indicator_width1  3

#property indicator_label2  "Sell Signal"
#property indicator_type2   DRAW_ARROW
#property indicator_color2  clrOrangeRed
#property indicator_width2  3

input int            FastMAPeriod  = 20;
input int            SlowMAPeriod  = 50;
input ENUM_MA_METHOD MAMethod      = MODE_EMA;
input ENUM_APPLIED_PRICE AppliedPrice = PRICE_CLOSE;
input bool           EnableAlert   = true;
input bool           EnablePush    = false;

double BuyBuffer[];
double SellBuffer[];
double FastMABuffer[];
double SlowMABuffer[];

int fastMA_handle;
int slowMA_handle;

//+------------------------------------------------------------------+
int OnInit()
{
 SetIndexBuffer(0, BuyBuffer, INDICATOR_DATA);
 SetIndexBuffer(1, SellBuffer, INDICATOR_DATA);
 SetIndexBuffer(2, FastMABuffer, INDICATOR_CALCULATIONS);
 SetIndexBuffer(3, SlowMABuffer, INDICATOR_CALCULATIONS);

 PlotIndexSetInteger(0, PLOT_ARROW, 233);
 PlotIndexSetInteger(1, PLOT_ARROW, 234);

 PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, 0.0);
 PlotIndexSetDouble(1, PLOT_EMPTY_VALUE, 0.0);

 fastMA_handle = iMA(_Symbol, PERIOD_CURRENT, FastMAPeriod, 0, MAMethod, AppliedPrice);
 slowMA_handle = iMA(_Symbol, PERIOD_CURRENT, SlowMAPeriod, 0, MAMethod, AppliedPrice);

 if(fastMA_handle == INVALID_HANDLE || slowMA_handle == INVALID_HANDLE)
 {
    Print("Failed to create MA handles");
    return INIT_FAILED;
 }

 IndicatorSetString(INDICATOR_SHORTNAME,
    "MA Cross Alert (" +
    IntegerToString(FastMAPeriod) + "/" +
    IntegerToString(SlowMAPeriod) + ")");

 return INIT_SUCCEEDED;
}

//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
              const int prev_calculated,
              const datetime &time[],
              const double &open[],
              const double &high[],
              const double &low[],
              const double &close[],
              const long &tick_volume[],
              const long &volume[],
              const int &spread[])
{
 if(rates_total < SlowMAPeriod) return 0;

 int copied1 = CopyBuffer(fastMA_handle, 0, 0, rates_total, FastMABuffer);
 int copied2 = CopyBuffer(slowMA_handle, 0, 0, rates_total, SlowMABuffer);

 if(copied1 <= 0 || copied2 <= 0) return 0;

 int start = (prev_calculated > 1) ? prev_calculated - 1 : SlowMAPeriod;

 for(int i = start; i < rates_total; i++)
 {
    BuyBuffer[i]  = 0.0;
    SellBuffer[i] = 0.0;

    // Golden Cross
    if(FastMABuffer[i] > SlowMABuffer[i] &&
       FastMABuffer[i-1] <= SlowMABuffer[i-1])
    {
       BuyBuffer[i] = low[i] - _Point * 20;

       if(i == rates_total - 1 && EnableAlert)
       {
          Alert(_Symbol + ": Golden Cross detected!");
          if(EnablePush)
             SendNotification(_Symbol + ": Golden Cross detected!");
       }
    }

    // Dead Cross
    if(FastMABuffer[i] < SlowMABuffer[i] &&
       FastMABuffer[i-1] >= SlowMABuffer[i-1])
    {
       SellBuffer[i] = high[i] + _Point * 20;

       if(i == rates_total - 1 && EnableAlert)
       {
          Alert(_Symbol + ": Dead Cross detected!");
          if(EnablePush)
             SendNotification(_Symbol + ": Dead Cross detected!");
       }
    }
 }

 return rates_total;
}
//+------------------------------------------------------------------+

導入方法

上記のコードをコピーして、MetaEditorで新規ファイルに貼り付け→コンパイルしてください。 詳しい手順はMT5にカスタムインジケーターを追加する方法を参照してください。

パラメータの調整

  • スキャルピング: FastMA=5, SlowMA=20
  • デイトレード: FastMA=20, SlowMA=50(デフォルト)
  • スイングトレード: FastMA=50, SlowMA=200

使用上の注意

  • 移動平均線クロスは遅行指標です。エントリーは必ず他の指標と組み合わせて判断してください
  • レンジ相場ではダマシが多くなります。トレンドの有無を確認してから使いましょう
  • バックテストを十分に行ってからリアルトレードに使用してください

まとめ

このインジケーターは、MAクロスの基本的な検知を行うシンプルなツールです。 MT5対応のFX業者であればどこでも利用できますので、ぜひ試してみてください。

AIを使ってこのインジケーターを改良する方法も合わせてご覧ください。

rakkosan

rakkosan

FXトレーダー・インジケーター開発者。MT5やTradingView向けのカスタムインジケーターを開発・販売しています。

noteで記事・インジケーターを公開中