【MQL5】陽線が3連続・陰線が3連続でサインを出すサインツールのコードを公開

陽線3連続サインツール

【MQL5】陽線が3連続・陰線が3連続でサインを出すサインツールのコードを公開しています。サインツール作成の学習用に是非ご活用下さい。

サインツール(MQL5)の内容は以下の様になります。

  • 陽線もしくは陰線が3連続で現れた時にサインが表示
  • 4連続になった場合は、サインが出ない
  • 新しいサインが出た時はアラームが発生

アラーム

陽線が3連続・陰線が3連続でサインを出すサインツールのコード



#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   2

#property indicator_label1 "Buy"
#property indicator_type1   DRAW_ARROW
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  2

#property indicator_label2 "Sell"
#property indicator_type2   DRAW_ARROW
#property indicator_color2  clrBlue
#property indicator_style2  STYLE_SOLID
#property indicator_width2  2

double buy[];
double sell[];
datetime lastBuy = 0;
datetime lastSell = 0;

int OnInit()
{
    SetIndexBuffer(0, buy, INDICATOR_DATA);
    PlotIndexSetInteger(0, PLOT_ARROW, 233);
    
    SetIndexBuffer(1, sell, INDICATOR_DATA);
    PlotIndexSetInteger(1, PLOT_ARROW, 234);
    
    return INIT_SUCCEEDED;
}

int OnCalculate(const int total,
                const int prev,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_vol[],
                const long &vol[],
                const int &spread[])
{
    if (total < 4) return 0;
    int start = MathMax(prev - 1, 3);

    if (prev == 0) {
        ArrayInitialize(buy, EMPTY_VALUE);
        ArrayInitialize(sell, EMPTY_VALUE);
    }

    for (int i = start; i <= total - 2; i++) {
        bool b1 = close[i] > open[i];
        bool b2 = close[i - 1] > open[i - 1];
        bool b3 = close[i - 2] > open[i - 2];
        bool b4 = close[i - 3] > open[i - 3];

        bool s1 = close[i] < open[i];
        bool s2 = close[i - 1] < open[i - 1];
        bool s3 = close[i - 2] < open[i - 2];
        bool s4 = close[i - 3] < open[i - 3];

        // Buy signal
        if (b1 && b2 && b3 && !b4) {
            buy[i] = low[i] - Point() * 10;

            if (i == total - 2 && lastBuy != time[i]) {
                Alert(StringFormat("【アラート】3連続陽線!%s", TimeToString(time[i], TIME_DATE | TIME_MINUTES)));
                PlaySound("alert.wav");
                lastBuy = time[i];
            }
        } else {
            buy[i] = EMPTY_VALUE;
        }

        // Sell signal
        if (s1 && s2 && s3 && !s4) {
            sell[i] = high[i] + Point() * 10;

            if (i == total - 2 && lastSell != time[i]) {
                Alert(StringFormat("【アラート】3連続陰線!%s", TimeToString(time[i], TIME_DATE | TIME_MINUTES)));
                PlaySound("alert2.wav");
                lastSell = time[i];
            }
        } else {
            sell[i] = EMPTY_VALUE;
        }
    }

    return total;
}


色の変更方法

色の変更

色に関しては「カラー」タブより変更が可能です。背景色が白の場合赤や青は見やすのですが黒背景の場合は少し見づらいので、状況に合わせて色の変更をお願いします。