English Русский 中文 Español Deutsch Português 한국어 Français Italiano Türkçe
MQL5 ウィザード:任意の価格で指値注文をオープンすることを EA に教える方法

MQL5 ウィザード:任意の価格で指値注文をオープンすることを EA に教える方法

MetaTrader 5 | 25 11月 2015, 08:19
1 696 0
Vladimir Karputov
Vladimir Karputov

はじめに

MQL5 ウィザードを用いて作成されている Expert Advisor は現在価格から固定された距離にある指値注文しかオープンすることができません。これはマーケット状況が変化すれば(たとえばマーケット予想変動価格の変化)、 Expert Advisor は新しいパラメータで再び実行しなければならないということです。

これは多くのトレーディングシステムにとって適したものではありません。ほとんどの場合、指し値注文に対する価格レベルはトレーディングシステムによって動的に判断されます。また、現在価格からの距離は常に変化しています。本稿では現在価格から変化する距離で指値注文をオープンできるように MQL5 ウィザードを用いて作成されるExpert Advisor を変更する方法を述べていきます。


1. MQL5 ウィザードを用いて作成されるExpert Advisor で指値注文をオープンするメカニズム

作成される Expert Advisor は以下に提供されているヘッダ内のコードとおおよそ同じものです。

//+------------------------------------------------------------------+
//| Inputs                                                           |
//+------------------------------------------------------------------+
//--- inputs for expert
input string             Expert_Title="ExpertMySignalEnvelopes.mq5";      // Document name
ulong                    Expert_MagicNumber        =3915;        // 
bool                     Expert_EveryTick          =false;       // 
//--- inputs for main signal
input int                Signal_ThresholdOpen      =10;          // Signal threshold value to open [0...100]
input int                Signal_ThresholdClose     =10;          // Signal threshold value to close [0...100]
input double             Signal_PriceLevel         =0.0;         // Price level to execute a deal
input double             Signal_StopLevel          =85.0;        // Stop Loss level (in points)
input double             Signal_TakeLevel          =195.0;       // Take Profit level (in points)
input int                Signal_Expiration         =0;           // Expiration of pending orders (in bars)
input int                Signal_Envelopes_PeriodMA =13;          // Envelopes(13,0,MODE_SMA,...) Period of averaging
input int                Signal_Envelopes_Shift    =0;           // Envelopes(13,0,MODE_SMA,...) Time shift
input ENUM_MA_METHOD     Signal_Envelopes_Method   =MODE_SMA;    // Envelopes(13,0,MODE_SMA,...) Method of averaging
input ENUM_APPLIED_PRICE Signal_Envelopes_Applied  =PRICE_CLOSE; // Envelopes(13,0,MODE_SMA,...) Prices series
input double             Signal_Envelopes_Deviation=0.2;         // Envelopes(13,0,MODE_SMA,...) Deviation
input double             Signal_Envelopes_Weight   =1.0;         // Envelopes(13,0,MODE_SMA,...) Weight [0...1.0]
//--- inputs for money
input double             Money_FixLot_Percent      =10.0;        // Percent
input double             Money_FixLot_Lots         =0.1;         // Fixed volume
//+------------------------------------------------------------------+

Signal_PriceLevel パラメータに気づいてください。デフォルトでは Signal_PriceLevel=0を用いて作成されます。このパラメータは現在価格からの距離を決定します。それがゼロに等しければ、注文は現在のマーケット価格でオープンされます。指し値注文をオープンするには、 Signal_PriceLevel パラメータに対してゼロでない値を設定する必要があります。すなわち、Signal_PriceLevel は正でも負でもありえます。

Signal_PriceLevel の値は通常きわめて大きな数字です。以下に正の値と負の値の差を示します。

Signal_PriceLevel=-50:

図1 Signal_PriceLevel=-50

図1 Signal_PriceLevel=-50

Signal_PriceLevel=50:

図2 Signal_PriceLevel=50

図2 Signal_PriceLevel=50

このため Signal_PriceLevel=-50であれば、指値注文は現在価格よりも好ましくない価格でオープンすることとなります。またSignal_PriceLevel=50であれば指値注文は現在価格よりも良い価格でオープンされることとなります。

Expert Advisor のこのバージョンは「売り止まり」注文と「買い止まり」注文をオープンします。


2. 指し値注文をオープンする価格からの距離データをどこに格納するのか?

まず以下の図を見て、コメントを進めていきます。

図3 現在価格からの距離データの格納

図3 現在価格からの距離データの格納

上図の解釈

Expert Advisor MQL5 ウィザードを用いて作成されるExpert Advisor です。

  • CExpert クラスのExtExpert オブジェクトは Expert Advisor 内でグローバルレベルで宣言されます。 
  • そして Expert AdvisorのOnInit() 関数で、 CExpertSignal クラスの シグナルオブジェクトに対する ポインターを宣言します。 新規オペレータを用いてシグナルオブジェクトが即作成されます。
  • OnInit() 関数内で ExtExpert オブジェクトのInitSignal 関数を呼びシグナルオブジェクトを初期化します。
  • OnInit() 関数内でSignal_PriceLevelパラメータを取得するシグナルオブジェクトのPriceLevel 関数を呼びます。

よって現在価格からの距離が格納され、Expert Advisor で宣言された Signal_PriceLevel パラメータがCExpertSignal クラスの シグナル オブジェクトに渡されます。

CExpertSignal クラスは保護されたクラススコープによって宣言される m_price_level 変数内の現在価格からの距離の値を格納します。

class CExpertSignal : public CExpertBase
  {
protected:
   //--- variables
   double            m_base_price;     // base price for detection of level of entering (and/or exit?)
   //--- variables for working with additional filters
   CArrayObj         m_filters;        // array of additional filters (maximum number of fileter is 64)
   //--- Adjusted parameters
   double            m_weight;         // "weight" of a signal in a combined filter
   int               m_patterns_usage; // bit mask of  using of the market models of signals
   int               m_general;        // index of the "main" signal (-1 - no)
   long              m_ignore;         // bit mask of "ignoring" the additional filter
   long              m_invert;         // bit mask of "inverting" the additional filter
   int               m_threshold_open; // threshold value for opening
   int               m_threshold_close;// threshold level for closing
   double            m_price_level;    // level of placing a pending orders relatively to the base price
   double            m_stop_level;     // level of placing of the "stop loss" order relatively to the open price
   double            m_take_level;     // level of placing of the "take profit" order relatively to the open price
   int               m_expiration;     // time of expiration of a pending order in bars


3. MQL5 ウィザードを用いて作成される Expert Advisor のストラクチャ

Expert Advisor は異なる機能性を持つ複数のブロックで構成されています。


図4 Expert Advisor のストラクチャ 

図4 Expert Advisor のストラクチャ

上図の実装

  • Expert Advisor はe MQL5 ウィザードを用いて作成される Expert Advisor です。
  • CExpert はトレーディング戦略の実装用の基本クラスです。
  • CExpertSignal はトレードシグナルジェネレータを作成するための基本クラスです。
  • filter0 ... filtern はトレードシグナルジェネレータで CExpertSignal クラスの派生クラスです。われわれのトレーディングシステムエンベロープインディケータのトレードシグナルジェネレータを基にしていますが、ジェネレータ内のシグナルは変更されていることに注意が必要です。その変更については7節でお話します。


4. 変更にAdvisableなExpert Advisorブロック

MQL5 ウィザードを用いて作成される Expert Advisorのストラクチャから判るように、基本クラスブロックがあります。基本クラスは標準ライブラリの一部です。

そのままのクラスは別の基本クラスの派生クラスで、それ自体は複数の基本クラスで構成されています。以下で2つのクラス:CExpert および CExpertSignalの初めの数行を確認することができます。

//+------------------------------------------------------------------+
//|                                                       Expert.mqh |
//|                   Copyright 2009-2013, MetaQuotes Software Corp. |
//|                                              https://www.mql5.com |
//+------------------------------------------------------------------+
#include "ExpertBase.mqh"
#include "ExpertTrade.mqh"
#include "ExpertSignal.mqh"
#include "ExpertMoney.mqh"
#include "ExpertTrailing.mqh"
//+------------------------------------------------------------------+
.
.
.
class CExpert : public CExpertBase

//+------------------------------------------------------------------+
//|                                                 ExpertSignal.mqh |
//|                   Copyright 2009-2013, MetaQuotes Software Corp. |
//|                                              https://www.mql5.com |
//+------------------------------------------------------------------+
#include "ExpertBase.mqh"
.
.
.
class CExpertSignal : public CExpertBase

私は基本クラスを変更することには強く反対を唱えます。

  1. MetaEditor が更新されるとき、基本クラスに加える変更はすべて上書きされ、基本クラスは初期状態に戻ります。
  2. この場合継承がより適していることでしょう。しかしそれから「標準ライブラリ」を『全体に』変更する必要が出ます。

代わりに Expert Advisor とトレードシグナルジェネレータのブロックを変更するのが一番よい方法です。特にわれわれのトレーディングシステムはすでに変更されたモジュールを一つ使用しているからです。それはエンベロープインディケータのトレードシグナルジェネレータです

それで解決です: Expert Advisor のブロックとレードシグナルジェネレータのブロックに変更を加えます。


5. 実装ロジック

Expert Advisor からレードシグナルジェネレータにポインターが渡されます。

このために、保護スコープを持つ変数を追加で宣言し、Expert Advisor から内部変数にポインターを格納するメソッドを書く必要があります。


  図5 実装ロジック

図5 実装ロジック


6. トレーディングシステム

チャートの時間枠は D1です。使用するインディケータは平均期間13で指数平均メソッドを持つエンベロープです。Expert Advisor がオープンすることのできるオーダータイプは売り止めBuy Stopです。

前回バーがブルならば 「売りストップ」 オーダーを設定します。前回バーがベアならば 「買いストップ」オーダーを設定します。すなわちわれわれは引き戻しを望んでいるのです。

図6 トレーディングシステム

図6 トレーディングシステム

トレーディングシステムが要望するようにトレードシグナルを作成するために、トレードシグナルジェネレータの標準モジュール SignalEnvelopes.mqh が変更されています。

ここで「標準ライブラリ」からあらゆるトレードシグナルを使用することができることに留意してください。


7. トレードシグナルジェネレータの変更バー価格の取得

では始めます。私はプログラムをMQL5 ストレージに保存することを好むとお伝えする必要があります。

トレードシグナルジェネレータを変更するため最初に行うべきことは、空のインクルードファイルを作成し、そこからすべてを削除して、エンベロープインディケータの標準トレードシグナルジェネレータの内容をすべてペーストすることです。

デフォルトではトレードシグナルジェネレータはMQL5\Include\Expert\Signalの下に入っているべきです。情報が多すぎて「標準ライブラリ」の\Signal フォルダをオーバーロードしないために、\Expert フォルダの下に新しいフォルダを作成し、それに\MySignalsと名前を付けます。

図7 MySignals フォルダの作成

図7 MySignals フォルダの作成

次に MQL5 ウィザードを用いてインクルードファイルを作成します。

MetaEditorで「ファイル」メニューの下で「新規」を選択し、「インクルードファイル(*.mqh)」を選びます。

図8 MQL5 ウィザードインクルードファイルの作成

図8 MQL5 ウィザードインクルードファイルの作成

われわれのシグナルジェネレータクラスの名前はMySignalEnvelopesです。

そしてそれは Include\Expert\MySignals\MySignalEnvelopes下に入ります。それを指定します。

図9 MQL5 ウィザードインクルードファイルの位置

図9 MQL5 ウィザードインクルードファイルの位置

「終了」をクリックしたら MQL5 ウィザードは空のテンプレートを作成します。

作成された MySignalEnvelopes.mqh ファイルはそれから MQL5 ストレージに追加されます。

図10 MQL5 ストレージファイルの追加

図10 MQL5 ストレージファイルの追加

ファイルが追加されると MQL5 ストレージへの変更をコミットする必要があります。

図11 MQL5 ストレージ変更のコミット

図11 MQL5 ストレージ変更のコミット

上記ステップが完了すれば、レードシグナルジェネレータの変更に進むことができます。

ジェネレータは \Include\Expert\Signal\SignalEnvelopes.mqh ファイルを基にしているので、ファイルの内容全体をジェネレータファイルにコピー&ペーストし、その際元のヘッダのみ残しておきます。

//+------------------------------------------------------------------+
//|                                            MySignalEnvelopes.mqh |
//|                              Copyright © 2013, Vladimir Karputov |
//|                                           http://wmua.ru/slesar/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2013, Vladimir Karputov"
#property link      "http://wmua.ru/slesar/"
#include <Expert\ExpertSignal.mqh>
// wizard description start
//+------------------------------------------------------------------+
//| Description of the class                                         |
//| Title=Signals of indicator 'Envelopes'                           |
//| Type=SignalAdvanced                                              |
//| Name=Envelopes                                                   |
//| ShortName=Envelopes                                              |
//| Class=CSignalEnvelopes                                           |
//| Page=signal_envelopes                                            |
//| Parameter=PeriodMA,int,45,Period of averaging                    |
//| Parameter=Shift,int,0,Time shift                                 |
//| Parameter=Method,ENUM_MA_METHOD,MODE_SMA,Method of averaging     |
//| Parameter=Applied,ENUM_APPLIED_PRICE,PRICE_CLOSE,Prices series   |
//| Parameter=Deviation,double,0.15,Deviation                        |
//+------------------------------------------------------------------+
// wizard description end
//+------------------------------------------------------------------+
//| Class CSignalEnvelopes.                                          |
//| Purpose: Class of generator of trade signals based on            |
//|          the 'Envelopes' indicator.                              |
//| Is derived from the CExpertSignal class.                         |
//+------------------------------------------------------------------+
class CSignalEnvelopes : public CExpertSignal
  {
protected:
   CiEnvelopes       m_env;            // object-indicator
   //--- adjusted parameters
   int               m_ma_period;      // the "period of averaging" parameter of the indicator
   int               m_ma_shift;       // the "time shift" parameter of the indicator
   ENUM_MA_METHOD    m_ma_method;      // the "method of averaging" parameter of the indicator
   ENUM_APPLIED_PRICE m_ma_applied;    // the "object of averaging" parameter of the indicator
   double            m_deviation;      // the "deviation" parameter of the indicator
   double            m_limit_in;       // threshold sensitivity of the 'rollback zone'
   double            m_limit_out;      // threshold sensitivity of the 'break through zone'
   //--- "weights" of market models (0-100)
   int               m_pattern_0;      // model 0 "price is near the necessary border of the envelope"
   int               m_pattern_1;      // model 1 "price crossed a border of the envelope"

public:
                     CSignalEnvelopes(void);
                    ~CSignalEnvelopes(void);
   //--- methods of setting adjustable parameters
   void              PeriodMA(int value)                 { m_ma_period=value;        }
   void              Shift(int value)                    { m_ma_shift=value;         }
   void              Method(ENUM_MA_METHOD value)        { m_ma_method=value;        }
   void              Applied(ENUM_APPLIED_PRICE value)   { m_ma_applied=value;       }
   void              Deviation(double value)             { m_deviation=value;        }
   void              LimitIn(double value)               { m_limit_in=value;         }
   void              LimitOut(double value)              { m_limit_out=value;        }
   //--- methods of adjusting "weights" of market models
   void              Pattern_0(int value)                { m_pattern_0=value;        }
   void              Pattern_1(int value)                { m_pattern_1=value;        }
   //--- method of verification of settings
   virtual bool      ValidationSettings(void);
   //--- method of creating the indicator and timeseries
   virtual bool      InitIndicators(CIndicators *indicators);
   //--- methods of checking if the market models are formed
   virtual int       LongCondition(void);
   virtual int       ShortCondition(void);

protected:
   //--- method of initialization of the indicator
   bool              InitMA(CIndicators *indicators);
   //--- methods of getting data
   double            Upper(int ind)                      { return(m_env.Upper(ind)); }
   double            Lower(int ind)                      { return(m_env.Lower(ind)); }
  };
//+------------------------------------------------------------------+
//| Constructor                                                      |
//+------------------------------------------------------------------+
CSignalEnvelopes::CSignalEnvelopes(void) : m_ma_period(45),
                                           m_ma_shift(0),
                                           m_ma_method(MODE_SMA),
                                           m_ma_applied(PRICE_CLOSE),
                                           m_deviation(0.15),
                                           m_limit_in(0.2),
                                           m_limit_out(0.2),
                                           m_pattern_0(90),
                                           m_pattern_1(70)
  {
//--- initialization of protected data
   m_used_series=USE_SERIES_OPEN+USE_SERIES_HIGH+USE_SERIES_LOW+USE_SERIES_CLOSE;
  }
//+------------------------------------------------------------------+
//| Destructor                                                       |
//+------------------------------------------------------------------+
CSignalEnvelopes::~CSignalEnvelopes(void)
  {
  }
//+------------------------------------------------------------------+
//| Validation settings protected data.                              |
//+------------------------------------------------------------------+
bool CSignalEnvelopes::ValidationSettings(void)
  {
//--- validation settings of additional filters
   if(!CExpertSignal::ValidationSettings())
      return(false);
//--- initial data checks
   if(m_ma_period<=0)
     {
      printf(__FUNCTION__+": period MA must be greater than 0");
      return(false);
     }
//--- ok
   return(true);
  }
//+------------------------------------------------------------------+
//| Create indicators.                                               |
//+------------------------------------------------------------------+
bool CSignalEnvelopes::InitIndicators(CIndicators *indicators)
  {
//--- check pointer
   if(indicators==NULL)
      return(false);
//--- initialization of indicators and timeseries of additional filters
   if(!CExpertSignal::InitIndicators(indicators))
      return(false);
//--- create and initialize MA indicator
   if(!InitMA(indicators))
      return(false);
//--- ok
   return(true);
  }
//+------------------------------------------------------------------+
//| Initialize MA indicators.                                        |
//+------------------------------------------------------------------+
bool CSignalEnvelopes::InitMA(CIndicators *indicators)
  {
//--- check pointer
   if(indicators==NULL)
      return(false);
//--- add object to collection
   if(!indicators.Add(GetPointer(m_env)))
     {
      printf(__FUNCTION__+": error adding object");
      return(false);
     }
//--- initialize object
   if(!m_env.Create(m_symbol.Name(),m_period,m_ma_period,m_ma_shift,m_ma_method,m_ma_applied,m_deviation))
     {
      printf(__FUNCTION__+": error initializing object");
      return(false);
     }
//--- ok
   return(true);
  }
//+------------------------------------------------------------------+
//| "Voting" that price will grow.                                   |
//+------------------------------------------------------------------+
int CSignalEnvelopes::LongCondition(void)
  {
   int result=0;
   int idx   =StartIndex();
   double close=Close(idx);
   double upper=Upper(idx);
   double lower=Lower(idx);
   double width=upper-lower;
//--- if the model 0 is used and price is in the rollback zone, then there is a condition for buying
   if(IS_PATTERN_USAGE(0) && close<lower+m_limit_in*width && close>lower-m_limit_out*width)
      result=m_pattern_0;
//--- if the model 1 is used and price is above the rollback zone, then there is a condition for buying
   if(IS_PATTERN_USAGE(1) && close>upper+m_limit_out*width)
      result=m_pattern_1;
//--- return the result
   return(result);
  }
//+------------------------------------------------------------------+
//| "Voting" that price will fall.                                   |
//+------------------------------------------------------------------+
int CSignalEnvelopes::ShortCondition(void)
  {
   int result  =0;
   int idx     =StartIndex();
   double close=Close(idx);
   double upper=Upper(idx);
   double lower=Lower(idx);
   double width=upper-lower;
//--- if the model 0 is used and price is in the rollback zone, then there is a condition for selling
   if(IS_PATTERN_USAGE(0) && close>upper-m_limit_in*width && close<upper+m_limit_out*width)
      result=m_pattern_0;
//--- if the model 1 is used and price is above the rollback zone, then there is a condition for selling
   if(IS_PATTERN_USAGE(1) && close<lower-m_limit_out*width)
      result=m_pattern_1;
//--- return the result
   return(result);
  }
//+------------------------------------------------------------------+

ここでコードのいくつかのぱーたう変更に取り組みます。

混乱を避けるため、変更したコードを 強調表示します

//+------------------------------------------------------------------+
//|                                                     MySignal.mqh |
//|                              Copyright © 2013, Vladimir Karputov |
//|                                           http://wmua.ru/slesar/ |
//+------------------------------------------------------------------+

変更されたコードはレードシグナルジェネレータにコピー&ペーストする必要のあるコードです。そのように強調表示することでみなさんがコードを理解するのに役立てばよいと思います。.

独自のレードシグナルジェネレータのクラスを書いているのでその名前は基本クラスの名前とは異なります。よってコード全体にわたり CSignalEnvelopes を CMySignalEnvelopes と置き換えます。

図12 Renaming クラスの保持

図12 Renaming クラスの保持

レードシグナルジェネレータクラスがその名前でMQL5 ウィザードに表示されるのを確認するため、記述ブロック内でクラス名を変更します。

//| Title=Signals of indicator 'Envelopes'                           |

次へ

//| Title=Signals of indicator 'MySignalEnvelopes'                   |

MA 期間値を変更します。

//| Parameter=PeriodMA,int,45,Period of averaging                    |

13へ(これは私の提案に過ぎません。みなさんはお好きな値に設定できます。)

//| Parameter=PeriodMA,int,13,Period of averaging                    |

また偏差Deviationパラメータも変更します。

//| Parameter=Deviation,double,0.15,Deviation                        |

もっと大きな値を設定します。

//| Parameter=Deviation,double,1.15,Deviation                        |

われわれの実装ロジックによると、ポインターをメインシグナルに格納する内部変数も宣言する必要があります。

これは内部変数(トレードシグナルジェネレータクラススコープ内でのみ)であるため次のコードブロックに追加されます。

protected:
   CiEnvelopes       m_env;          // object-indicator
   //--- adjusted parameters
   int               m_ma_period;    // the "period of averaging" parameter of the indicator
   int               m_ma_shift;     // the "time shift" parameter of the indicator
   ENUM_MA_METHOD    m_ma_method;     // the "method of averaging" parameter of the indicator
   ENUM_APPLIED_PRICE m_ma_applied;    // the "object of averaging" parameter of the indicator
   double            m_deviation;    // the "deviation" parameter of the indicator
   //--- "weights" of market models (0-100)
   int               m_pattern_0;      // model 0
   CExpertSignal    *m_signal;         // storing the pointer to the main signal

私がコードから不要な変数を削除したことにも注意ください。 

メインシグナルにポインターを格納する方法は別のコードブロックで宣言します。それは「メインシグナルにポインターを格納する方法」です。ここでも私はいくつか不適切なメソッドを削除しました。

public:
                     CMySignalEnvelopes(void);
                    ~CMySignalEnvelopes(void);
   //--- methods of setting adjustable parameters
   void              PeriodMA(int value)                 { m_ma_period=value;        }
   void              Shift(int value)                    { m_ma_shift=value;         }
   void              Method(ENUM_MA_METHOD value)        { m_ma_method=value;        }
   void              Applied(ENUM_APPLIED_PRICE value)   { m_ma_applied=value;       }
   void              Deviation(double value)             { m_deviation=value;        }
   //--- methods of adjusting "weights" of market models
   void              Pattern_0(int value)                { m_pattern_0=value;        }
   //--- method of verification of settings
   virtual bool      ValidationSettings(void);
   //--- method of creating the indicator and timeseries
   virtual bool      InitIndicators(CIndicators *indicators);
   //--- methods of checking if the market models are formed
   virtual int       LongCondition(void);
   virtual int       ShortCondition(void);
   //--- method of setting the pointer to the main signal
   virtual bool      InitSignal(CExpertSignal *signal=NULL);

コンストラクタ内で変更されたパラメータを指定し、もう必要ではない変数を削除します。

//+------------------------------------------------------------------+
//| Constructor                                                      |
//+------------------------------------------------------------------+
CMySignalEnvelopes::CMySignalEnvelopes(void) : m_ma_period(13),
                                               m_ma_shift(0),
                                               m_ma_method(MODE_SMA),
                                               m_ma_applied(PRICE_CLOSE),
                                               m_deviation(1.15),
                                               m_pattern_0(50)  

この点で われわれのトレーディングシステムに従いレードシグナル作成ロジックの変更に進むことができます。

以下は買いシグナルに関わるコードブロックです。

int CMySignalEnvelopes::LongCondition(void)
  {
   int result=0;
   int idx   =StartIndex();
   double close=Close(idx);
   double upper=Upper(idx);
   double lower=Lower(idx);
   double width=upper-lower;
//--- if the model 0 is used and price is in the rollback zone, then there is a condition for buying
   if(IS_PATTERN_USAGE(0) && close<lower+m_limit_in*width && close>lower-m_limit_out*width)
      result=m_pattern_0;
//--- if the model 1 is used and price is above the rollback zone, then there is a condition for buying
   if(IS_PATTERN_USAGE(1) && close>upper+m_limit_out*width)
      result=m_pattern_1;
//--- return the result
   return(result);
  }

必要な変更の後以下のように表示されます。

int CMySignalEnvelopes::LongCondition(void) //---buy
  {
   int result=0;
   int idx   =StartIndex();
   double open=Open(idx);
   double close=Close(idx);
   double prlevel;
      if(IS_PATTERN_USAGE(0) && close<open)
        {
         prlevel=GetPriceLevelStopp(open,Open(0));
         m_signal.PriceLevel(prlevel);
         result=m_pattern_0;
        }
//--- return the result
   return(result);
  }

以下は売りシグナルに関わるコードブロックです。

int CMySignalEnvelopes::ShortCondition(void)
  {
   int result  =0;
   int idx     =StartIndex();
   double close=Close(idx);
   double upper=Upper(idx);
   double lower=Lower(idx);
   double width=upper-lower;
//--- if the model 0 is used and price is in the rollback zone, then there is a condition for selling
   if(IS_PATTERN_USAGE(0) && close>upper-m_limit_in*width && close<upper+m_limit_out*width)
      result=m_pattern_0;
//--- if the model 1 is used and price is above the rollback zone, then there is a condition for selling
   if(IS_PATTERN_USAGE(1) && close<lower-m_limit_out*width)
      result=m_pattern_1;
//--- return the result
   return(result);
  }

必要な変更の後以下のように表示されます。

int CMySignalEnvelopes::ShortCondition(void) //---sell
  {
   int result  =0;
   int idx     =StartIndex();
   double open=Open(idx);
   double close=Close(idx);
   double prlevel;
      if(IS_PATTERN_USAGE(0) && close>open)
        {
         prlevel=GetPriceLevelStopp(Open(0),open);
         m_signal.PriceLevel(prlevel);
         result=m_pattern_0;
        }
//--- return the result
   return(result);
  }


8. シグナルコードブロックに関するコメント

特定のシグナルに対する要件が満たされると、現在価格からの距離値である "20" や "15" といった番号を返す GetPriceLevelStopp メソッドを呼びます。

この次に(指値注文レベル価格に対する距離を設定する)m_signal オブジェクトの PriceLevel メソッドを呼びます。m_signal はポインターをメインシグナルに格納する CExpertSignal クラスオブジェクトであることを思い出す必要があります。

以下が GetPriceLevelStopp メソッドのコードです。

double CMySignalEnvelopes::GetPriceLevelStopp(double price_0,double min)
  {
   double level;
   double temp;
   temp-=(price_0-min)/PriceLevelUnit();
   level=NormalizeDouble(temp,0);
   return(level);
  }

このメソッドはクラスヘッダで宣言する必要があります。

protected:
   //--- method of initialization of the indicator
   bool              InitMA(CIndicators *indicators);
   //--- methods of getting data
   double            Upper(int ind)                      { return(m_env.Upper(ind)); }
   double            Lower(int ind)                      { return(m_env.Lower(ind)); }
   double            GetPriceLevelStopp(double price,double min);
  };

もう一つ必要なのは内部変数に対するメインシグナルにポインターを渡すメソッドです。

bool CMySignalEnvelopes::InitSignal(CExpertSignal *signal)
  {
   m_signal=signal;
   return(true);
  }

 その後、MQL5 ウィザードに Expert Advisor を作成し、そこにシグナルモジュール 'MySignalEnvelopes'をインクルードします。

MQL5 ウィザードを用いて作成される Expert Advisorのコードに InitSignal メソッド呼び出しを追加する必要もあります。

//--- Set filter parameters
   filter0.PeriodMA(Signal_Envelopes_PeriodMA);
   filter0.Shift(Signal_Envelopes_Shift);
   filter0.Method(Signal_Envelopes_Method);
   filter0.Applied(Signal_Envelopes_Applied);
   filter0.Deviation(Signal_Envelopes_Deviation);
   filter0.Weight(Signal_Envelopes_Weight);
   filter0.InitSignal(signal);
//...

Expert Advisorのオペレーションを目で見てよりよく解るよう短いビデオをご準備しました。

MQL5 ウィザードを用いて作成される Expert Advisor のコードはシグナルモジュールのコードと共に本稿に添付しております。

以下で Expert Advisorの検証結果を確認いただけます。EURUSD およびUSDJPY に関して次のパラメータについて行われた検証です。:検証期間 2013.01.01 ~ 2013.09.01、時間枠-D1、ストップロスレベル= 85、テイクプロフィットレベルl = 195。

図13 D1で EURUSD に対する検証

図13 D1で EURUSD に対する検証

図14 D1で USDJPY に対する検証

図14 D1で USDJPY に対する検証


おわりに

本稿は現在価格から任意の距離で指値注文を設定することができるようになる機能性の実装のためのトレードシグナルモジュールコードを変更するメソッドについて見てきました。それは前回バーの価格クローズまたはオープンであり、あるいは移動平均の値の可能性があります。オプションは数多くあります。重要なことは指値注文に対して任意の始値を設定することができるということです。

本稿はメインシグナルに対するポインターにアクセスする方法、CExpertSignalクラスメソッドを示しました。  指値注文でトレードを行うトレーダーにとっては有用なものとなることを信じています。


MetaQuotes Ltdによってロシア語から翻訳されました。
元の記事: https://www.mql5.com/ru/articles/723

添付されたファイル |
自動ニューストレーダーのバインディング 自動ニューストレーダーのバインディング
これは一からシンプルなオブジェクト指向 EA を構築する方法を述べ、オブジェクト指向プログラミングのアドバイスを提供したもう一つ別の MQL5 OOP クラス記事の続編です。本稿では、ニュースをトレードすることのできる EA を開発するのに必要とされる技術の基本をお話します。目標は OOP に関する考え方を提示し続け、ファイルシステムと関連づけながらこのシリーズにおける新しいトピックを取り上げることです。
資金管理について 資金管理について
様々な資金管理システムの使用について詳細に説明します。また、それらを使用した取引の結果もご覧いただけます。
MQL5 ウィザードおよび Hlaiman EA ジェネレータを用いたニューラルネットワーク EA の作成 MQL5 ウィザードおよび Hlaiman EA ジェネレータを用いたニューラルネットワーク EA の作成
本稿はMQL5 ウィザードおよび Hlaiman EA ジェネレータを用いたニューラルネットワーク EA の自動作成について述べます。理論的な情報をすべて学習する必要なくニューラルネットワークと簡単に連携し、ご自身のコードを書く方法をお伝えします。
有用なテクノロジーカクテルでYour MQL5 顧客を驚嘆させる! 有用なテクノロジーカクテルでYour MQL5 顧客を驚嘆させる!
MQL5 はプログラマーに関数の完全セットとオブジェクト指向API を提供します。それらのお陰でプログラマーは MetaTrader 環境内で願うことを行うことができるのです。ただ「ウェブテクノロジー」は今日ひじょに特殊なことをしてなにか違ったもので顧客を驚かせる必要があったり、ただ MT5 「標準ライブラリ」の特定箇所をマスターする十分な時間がないなんらかの状況で救助にきてくれる極端に多才なツールです。今回の例題によりご自身の開発時間管理の仕方と同時にすばらしいテクノロジーカクテルを作成する方法を実用例をご紹介します。