MT5自動売買ソフトの作成7 パラメータを追加する

パラメータとは

前回までは移動平均RCIの期間は固定値でDLLに渡していました。

でも固定値だと使い勝手が悪いですよね。毎回数値を変更してビルドする必要があります。

MT5標準のインジケータでは入力パラメータで期間などが変更して表示できます。

今回は期間などをパラメータにしてみます。

dll_testインジケータをチャートにドラッグドロップした時に、「インプット」タブを選択すると期間とバーサイズが入力できるようにしましょう。

インジケータのプログラム

//+------------------------------------------------------------------+
//|                                                         rciChart.mq5 |
//|                                      Copyright 2023, MetaQuotes Ltd. |
//|                                                 https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_plots   2
//--- plot rciChart
#property indicator_label1  "rciChart"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRoyalBlue
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1

#property indicator_label2  "rciChart"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrOrange
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1

input int   middlePeriod = 24;      // 中期期間
input int   shortPeriod = 9;        // 短期期間

input int   barCount = 10000;       // バーサイズ

//--- indicator buffers
double  rciChartBuffer1[];
double  rciChartBuffer2[];

double  closeBuffer[];
double  middleBuffer[];
double  shortBuffer[];

#import  "D:\\mql5dll\\Dll_test\\x64\\Debug\\Dll_test.dll"
    void dllRciArray(const int size, const int period, const double& close[], double& out[]);
#import  

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
//--- indicator buffers mapping
    SetIndexBuffer(0, rciChartBuffer1, INDICATOR_DATA);
    SetIndexBuffer(1, rciChartBuffer2, INDICATOR_DATA);

    ArraySetAsSeries(rciChartBuffer1, true);
    ArraySetAsSeries(rciChartBuffer2, true);

    ArrayResize(closeBuffer, barCount, 0);
    ArrayResize(middleBuffer, barCount, 0);
    ArrayResize(shortBuffer, barCount, 0);
//---
    return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function                             |
//+------------------------------------------------------------------+
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[])
  {
//---
    ArraySetAsSeries(close, true);

    int size = rates_total - prev_calculated;
    size = (size > barCount) ? barCount : size;
    ArrayCopy(closeBuffer, close, 0, 0, size);

    dllRciArray(size, middlePeriod, closeBuffer, middleBuffer);
    ArrayCopy(rciChartBuffer1, middleBuffer, 0, 0, size);

    dllRciArray(size, shortPeriod, closeBuffer, shortBuffer);
    ArrayCopy(rciChartBuffer2, shortBuffer, 0, 0, size);

//--- return value of prev_calculated for next call
    return(rates_total);
}

//+------------------------------------------------------------------+

まずは入力パラメータを作ります。

input int middlePeriod = 24;      // 中期期間
input int   shortPeriod = 9;        // 短期期間

input int   barCount = 10000;       // バーサイズ

inputで宣言した行の変数ではなく「//」の後ろ、コメント部分がインプットパラメータ名称になります。

MQL5が特殊な言語ということが良くわかります。まさかコメントが!?と思いますが、本当です。

double    closeBuffer[];
double  middleBuffer[];
double  shortBuffer[];

次にバッファの宣言はサイズを指定しません。

OnInit関数で下記のようにバッファのエリアを確保します。

  ArrayResize(closeBuffer, barCount, 0);
    ArrayResize(middleBuffer, barCount, 0);
    ArrayResize(shortBuffer, barCount, 0);

MQL5のArrayResize関数を利用してバッファのリサイズをします。

そしてDLLのdllRciArray関数を呼び出すときに、inputパラメータの期間を指定します。

  dllRciArray(size, middlePeriod, closeBuffer, middleBuffer);
    ArrayCopy(rciChartBuffer1, middleBuffer, 0, 0, size);

    dllRciArray(size, shortPeriod, closeBuffer, shortBuffer);
    ArrayCopy(rciChartBuffer2, shortBuffer, 0, 0, size);

インプットの期間を変えてみましょう。インジケータのグラフが変化しますね。

次回

次回は、ついに自動売買ソフトウェアです。サンプルを見てみましょう。