カスタムインジケーターに基づくトレーディングシグナルジェネレーター
はじめに
この記事では、カスタムインジケーターに基づくトレーディングシグナルジェネレーターの作成方法を紹介いたします。カスタムインジケーターのためにどのように自身のトレーディングモデルを作成できるかを見ていきます。モデル0の目的を説明し、IS_PATTERN_USAGE(0)タイプのストラクチャーがなぜそのトレーディングシグナルモジュールで使用されているのかについて説明します。
この記事は、二つの種類のコードを使用します:修正予定のコードと、すでに修正されたコードの二つです。修正されたコードは、以下のようにハイライト が当てられます。
//+------------------------------------------------------------------+
//| MySignal.mqh |
//| Copyright © 2012, Vladimir Karputov |
//| http://wmua.ru/slesar/ |
//+------------------------------------------------------------------+
修正されたコードは、コピーされ、トレーディングシグナルジェネレーターに貼り付けられます。ハイライトの使用を通して、そのコードをより理解できることを願っています。
1. カスタムインジケーター
長い間使いたいと思っていた標準デリバリに含まれないインジケーターがあるはずです。そして、それは、トレーディングシグナルモジュールの作成の基盤となるインジケーターです。そのようなインジケーターとして標準ライブラリからMACDインジケーターを使用します。そのインジケーターの位置は以下の通りです:MQL5\Indicators\Examples\MACD.mq5.
個々のインジケーターは、一つかそれ以上のマーケットモデルを示しています。マーケットモデルは、インジケーター値と価格値の特定の組み合わせです。MACDインジケーターで使用可能なモデルは、メイン・シグナルラインの交差線と、0レベル分岐とダブル分岐の交差線です。
1.1 新しいインジケーターモデル.
そのインジケーターに使用できる特定のマーケットモデルに満足しおらず、独自のインジケーターモデルを導入したいとします。新しいインジケーターモデルの詳細:もしMACDインジケーターが0のラインを下回り、その値が増加している場合、さらなる成長を望むことができ、ロングポジションをオープンにすることができます。
図 1: 将来のインジケーターの成長モデル
もしMACDインジケーターが0のラインを超え、その値が減少している場合、さらなる減少を予想でき、ショートポジションを開きます。
図2:将来のインジケーター降下モデルl
カスタムインジケーターに決定し、インジケーターとその詳細のための新しいトレーディングモデルを思いつきました。コードの記述に進みましょう。
2. カスタムインジケーターに基づくトレーディングシグナルジェネレーターの記述
私たちのジェネレーターは、CExpertSignalベースクラスの派生クラスです。CExpertSignalベースクラスはトレーディングシグナルジェネレーター作成のためのクラスです。CExpertSignalクラスは、パブリックメソッドを含み、それはエキスパートアドバイザーがマーケットへのエントリの方向に関してトレーデイングシグナルジェネレーターの指示を見ることができるようにします。
独自のトレーディングシグナルジェネレーターに取り組んでいるため、関連した仮想メソッドの再定義された CExpertSignalクラスから継承される必要があります。
3. トレーディングシグナルジェネレーターのクラスの作成
トレーディングシグナルジェネレーターは標準として...MQL5\Include\Expert\Signalフォルダーに位置している必要があります。標準ライブラリの...\Signalフォルダーに情報を詰め込みすぎないように、...\Expert フォルダの下にあたらしいフォルダを作成し、\MySignalsと名付けましょう:
図3. 新しいMySignalsフォルダの作成
次に、MQL5ウィザードを使用しインクルードファイルを作成します。MetaEditorでは、Fileメニュー下の「New」を選択し、「Include File(*.mqh)」を選びます。
図4. MQL5 ウィザードインクルードファイルの作成
シグナルジェネレーターのクラス名は、MySignalです。それはInclude\Expert\MySignals\MySignalに格納されます。詳しく見てみましょう:
図5. MQL5 ウィザードインクルードファイルの位置
「Finish」をクリックした後、MQL5ウィザードは、空のテンプレートを作成します。ここからは全て手作業で、データのコピー・貼り付けを行います。標準ライブラリからの全てのシグナルは、ほとんど同一であるという事実に注意してください。トレーディングモデルを決定するアルゴリズムのみ異なります。
したがって、\Include\Expert\Signal folderからファイルを取得し、内容をコピーし、テンプレートに貼り付けることができます。トレーディングシグナルジェネレーターのファイルを編集し始めることができます。
4. トレーディングシグナルジェネレーターのクラスの詳細
テンプレートとして、\Include\Expert\Signal\SignalEnvelopes.mqhファイルを取得し、そこからヘッダーを除くすべてをコピーしました。//+------------------------------------------------------------------+ //| SignalEnvelopes.mqh | //| Copyright 2009-2013, MetaQuotes Software Corp. | //| https://www.mql5.com | //+------------------------------------------------------------------+
ほぼ何もないMySignal.mqhテンプレートにそれを貼り付けました。以下がそのファイルです:
//+------------------------------------------------------------------+ //| MySignal.mqh | //| Copyright © 2012, Vladimir Karputov | //| 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. | //| It 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 time series 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 time series 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 the 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 the 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); } //+------------------------------------------------------------------+
ノート行 6:
#include <Expert\ExpertSignal.mqh>
テンプレート内にトレーディングシグナルジェネレーターを作成するCExpertSignalベースクラスを含めるために、前処理プログラムに命令を与えました。
テンプレートの編集を続けます。テンプレートがMQL5ウィザードにその後見えることを保証するために、クラスの記述を変更する必要があります:
//+------------------------------------------------------------------+ //| 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 | //+------------------------------------------------------------------+
それでは見てみましょう。以下の行
//| Title=Signals of indicator 'Envelopes' |
MQL5ウィザードにて表示されるシグナルクラスの名前を示しています。この名前を以下のように変えます:
//| Title=Signal of the 'Custom Indicator' indicator |
次の行;
//| Name=Envelopes |
トレーディングシグナルクラスの変数を表す名前を示しています。これはMQL5ウィザードにて使用されます。これを以下のように修正しましょう。:
//| Name=MyCustomIndicator |
次の行;
//| ShortName=Envelopes |
このパラメーターには同じ名前を与えます:
//| ShortName=MyCustomIndicator |
以下の行はクラス名をセットします:
//| Class=CSignalEnvelopes |
このパラメーターの名前を変更します;
//| Class=CSignalMyCustInd |
次のパラメーターはそのままにしておきましょう。
//| Page=signal_envelopes |
以下のパラメーターグループは、トレーディングシグナルジェネレーターの根底にあるインジケーターのパラメータの記述を担います。以前述べた通り、...MQL5\Indicators\Examples\MACD.mq5をカスタムインジケーターとして使用します。以下のパラメーターを持っています:
//--- input parameters input int InpFastEMA=12; // Fast EMA period input int InpSlowEMA=26; // Slow EMA period input int InpSignalSMA=9; // Signal SMA period input ENUM_APPLIED_PRICE InpAppliedPrice=PRICE_CLOSE; // Applied price
4.1パラメーター詳細ブロック
上記のパラメーターは、MACD.mq5.にのみ適用され、カスタムインジケーターは全く異なるパラメーターを持つかもしれないことに注意してください。重要な点として、トレーディングシグアンルクラスの記述を持つインジケーターパラメーターをマッチさせることがあります。カスタムインジケーターのためのトレーディングシグナルクラス内のパラメーター記述ブロック、MACD.mq5 は以下です:
//| Parameter=PeriodFast,int,12,Period of fast EMA |
//| Parameter=PeriodSlow,int,24,Period of slow EMA |
//| Parameter=PeriodSignal,int,9,Period of averaging of difference |
//| Parameter=Applied,ENUM_APPLIED_PRICE,PRICE_CLOSE,Prices series |
そのインジケーターのパラメーターがいかにクラス記述ブロックの詳細と合致しているかみてください。全ての修正の後、そのクラスブロックは以下のようになります;
//+------------------------------------------------------------------+ //| Description of the class | //| Title=Signal of the 'Custom Indicator' indicator | //| Type=SignalAdvanced | //| Name=MyCustomIndicator | //| ShortName=MyCustomIndicator | //| Class=CSignalMyCustInd | //| Page=signal_envelopes | //| Parameter=PeriodFast,int,12,Period of fast EMA | //| Parameter=PeriodSlow,int,24,Period of slow EMA | //| Parameter=PeriodSignal,int,9,Period of averaging of difference | //| Parameter=Applied,ENUM_APPLIED_PRICE,PRICE_CLOSE,Prices series | //+------------------------------------------------------------------+
プログラミングにおいて、コードにコメントを残し、しばらく経ってその部分に戻った時にそのコードを理解しやすくすることが良いとされています。なので、以下のブロックを修正します;
//+------------------------------------------------------------------+ //| Class CSignalEnvelopes. | //| Purpose: Class of generator of trade signals based on | //| the 'Envelopes' indicator. | //| It is derived from the CExpertSignal class. | //+------------------------------------------------------------------+
クラスの詳細と合致させるため:
//+------------------------------------------------------------------+
//| Class CSignalMyCustInd. |
//| Purpose: Class of the trading signal generator based on |
//| the custom indicator. |
//| It is derived from the CExpertSignal class. |
//+------------------------------------------------------------------+
混乱をさけるため、「CSignalEnvelopes」値と「CSignalMyCusInd」を置き換える必要があります。
図6. CSignalEnvelopesとCSignalMyCustIndを置き換える
幾つかの理論的な側面を見てみましょう。
5. CiCustomクラス
カスタムインジケーターのトレーディングインジケータークラスのコードに取り組むために、CiCustomクラスが必要です。CiCustomクラスは、カスタムインジケータを扱うために特別に作成されました。CiCustomクラスは、カスタムインジケーターのデータへの生成やアクセスを提供します。
6. CIndicatorsクラス
CIndicatorsは、時系列のインスタンスとテクニカルインジケータークラスの収集のためのクラスです。CIndicatorsクラスは、テクニカルインンジケータークラスのインスタンスにおける(データの同期、メモリ管理などの)管理と作成、保存を提供します。
Createメソッドのために、CIndicators に特に興味があります。このメソッドは、特定のパラメーターを持つ種類のインジケーターを作成します。
7. トレーディングシグナルクラスの記述の続き
修正する次のコードは、(28 ~ 42行)以下のコードを修正します。class CSignalMyCustInd : 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"
8. トレーディングシグナルジェネレーターのカスタムインジケーターの作成
上記のコードを見てみましょう。以下の行
CiEnvelopes m_env; // object-indicator
オブジェクト- CiEnvelopesクラスインジケーターを宣言しています。CiEnvelopes は、標準ライブラリからのテクニカルインジケーターを扱うクラスです。CiEnvelopesクラスは、標準ライブラリからのテクニカルインジケーターに基づいて作成されました。しかし、カスタムインジケーターに基づいてジェネレーターのコードを記述していきます。したがって、標準ライブラリ内にカスタムインジケーターのためのすでに出来上がったクラスはありません。できることとしては、 CiCustomクラスを使用することです。
CiCustomクラスとしてインジケーターを宣言しましょう;
CiCustom m_mci; // indicator object "MyCustomIndicator"
8.14つの変数
パラメーター記述ブロックを覚えていますか?その記述の中に3つのパラメーターがありました。ジェネレータークラスのprotectedの区域に、4つのパラメータに値を渡すための4つの変数を淵源します。
//--- adjustable parameters
int m_period_fast; // "fast EMA period"
int m_period_slow; // "slow EMA period"
int m_period_signal; // "difference averaging period"
ENUM_APPLIED_PRICE m_applied; // "price type"
以下のコードブロックです:
//--- "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"
このコードは、トレーディングシグナルジェネレーターのトレーディングモデルに「重み」を付ける変数を宣言します。以下のコードと「重み」ブロックと置き換えましょう:
//--- "weights" of the market models (0-100)
int m_pattern_0; // model 0 "the oscillator has required direction"
int m_pattern_1; // model 1 "the indicator is gaining momentum - buy; the indicator is falling - sell"
9. モデル0
覚えているように、この記事の最初に、トレーディングシグナルジェネレーターによって生成される新しいモデル一つを記述することと決定していました。しかし、上記のコードでは、二つのマーケットモデルを明記しました(モデル0とモデル1)ここでは、モデル0は、重要な補助モデルです。未決注文とトレーディングする際に必要です。モデル0は、未決注文が価格とともに移動することを保証します。トレーディングシグナルジェネレーターと以下の条件を見てみましょう:
- MACDカスタムインジケーターは0の線以下にあります。
- そして、その値は増え続けます。
- バーオープン価格から50ポイントをセットされた未決注文とトレーディングします。
これらの条件は完璧に私たちのトレーディングモデルを描写します。こちらは、どのように移動するかを示します;トレーディングモデルの条件は、バーが現れてすぐにチェックされます。1. 持っているもの:MACDは、0線の下にあり、弾みを得ます。これは買いシグナルに一致します。したがって、未決注文の指値買い注文を行います。
図7. 指値買い未決注文の発行
次のバーNo.2が現れるとすぐにその条件がMACDが0より下にあり、降下していることをチェックします。私たちのトレーディングモデルによると、買い・売りの条件はありません。しかし、注意してください:CExpertSignalクラスのロジックにおいて、買いや売りにおいて条件がないので、すべての未決注文はDELTEDされる必要があります。この場合、もしその価格が突然急激に上昇した場合、未決注文がないので、利益のためマーケットロングに入るための機会を失います。
これは補助モデル0がとても役に立つケースです。補助モデル0は、以下の条件の際に適用されます:
- MACDカスタムインジケーターが0より下にある
指値未決注文を発行できます。バーオープン価格から50ポイントの注文を発行したため、価格の動きに沿って指値未決注文をシンプルに動かします。
図8. 指値注文を下に移動させる
したがって、補助モデル0を使用して、価格の動きに応じて未決注文を動かす機会を得ます。
10. テンプレードコードの修正
修正される次のコードブロックは以下です;public: CSignalMyCustInd(void); ~CSignalMyCustInd(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 time series virtual bool InitIndicators(CIndicators *indicators); //--- methods of checking if the market models are formed virtual int LongCondition(void); virtual int ShortCondition(void);
このブロックでは、調整できるパラメーターの設定用メソッド、トレーディングモデルの重みの調整用メソッド、設定の確認用メソッド、インジケーター初期化メソッド、マーケットモデルが生成されたかをチェックするメソッドを宣言します。
調整可能パラメーターの内の4つの変数を宣言したことを考慮すると、パラメーターの設定用メソッドのブロックは以下になります;
//--- methods of setting adjustable parameters
void PeriodFast(int value) { m_period_fast=value; }
void PeriodSlow(int value) { m_period_slow=value; }
void PeriodSignal(int value) { m_period_signal=value; }
void Applied(ENUM_APPLIED_PRICE value) { m_applied=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 time series virtual bool InitIndicators(CIndicators *indicators); //--- methods of checking if the market models are generated 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)); } };
このブロックは大きく修正されます。CIndicatorクラスのGetData メソッドを用いていることに注意してください。呼ばれたメソッドの名前は、コードに直接提供されます;
protected:
//--- indicator initialization method
bool InitMyCustomIndicator(CIndicators *indicators);
//--- methods for getting data
//- getting the indicator value
double Main(int ind) { return(m_mci.GetData(0,ind)); }
//- getting the signal line value
double Signal(int ind) { return(m_mci.GetData(1,ind)); }
//- difference between two successive indicator values
double DiffMain(int ind) { return(Main(ind)-Main(ind+1)); }
int StateMain(int ind);
double State(int ind) { return(Main(ind)-Signal(ind)); }
//- preparing data for the search
bool ExtState(int ind);
//- searching the market model with the specified parameters
bool CompareMaps(int map,int count,bool minimax=false,int start=0);
};
次のコードブロックは、コンストラクターです。
//+------------------------------------------------------------------+ //| Constructor | //+------------------------------------------------------------------+ CSignalMyCustInd::CSignalMyCustInd(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; }
そのコンストラクターでは、変数の名前を変更します。さrない、以下の二つの身を使用します;USE_SERIES_HIGH+USE_SERIES_LOW
//+------------------------------------------------------------------+
//| Constructor |
//+------------------------------------------------------------------+
CSignalMyCustInd::CSignalMyCustInd(void) : m_period_fast(12),
m_period_slow(24),
m_period_signal(9),
m_applied(PRICE_CLOSE),
m_pattern_0(10),
m_pattern_1(50)
{
//--- initialization of protected data
m_used_series=USE_SERIES_HIGH+USE_SERIES_LOW;
}
クラスのValidationSettingsメソッドを修正します。
//+------------------------------------------------------------------+ //| Validation settings protected data. | //+------------------------------------------------------------------+ bool CSignalMyCustInd::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); }
チェックボックス内にて、特定のカスタムインジケーターに置いて主な条件をチェックします;m_period_fast>=m_period_slow
//+------------------------------------------------------------------+
//| Checking parameters of protected data |
//+------------------------------------------------------------------+
bool CSignalMyCustInd::ValidationSettings(void)
{
//--- validation settings of additional filters
if(!CExpertSignal::ValidationSettings())
return(false);
//--- initial data checks
if(m_period_fast>=m_period_slow)
{
printf(__FUNCTION__+": slow period must be greater than fast period");
return(false);
}
//--- ok
return(true);
}
次のブロックは、インジケーターの生成を扱います:
//+------------------------------------------------------------------+ //| Create indicators. | //+------------------------------------------------------------------+ bool CSignalMyCustInd::InitIndicators(CIndicators *indicators) { //--- check pointer if(indicators==NULL) return(false); //--- initialization of indicators and time series of additional filters if(!CExpertSignal::InitIndicators(indicators)) return(false); //--- create and initialize MA indicator if(!InitMA(indicators)) return(false); //--- ok return(true); }
カスタムインジケーターに適用されます。
//+------------------------------------------------------------------+
//| Creation of indicators. |
//+------------------------------------------------------------------+
bool CSignalMyCustInd::InitIndicators(CIndicators *indicators)
{
//--- check of pointer is performed in the method of the parent class
//---
//--- initialization of indicators and time series of additional filters
if(!CExpertSignal::InitIndicators(indicators))
return(false);
//--- creation and initialization of the custom indicator
if(!InitMyCustomIndicator(indicators))
return(false);
//--- ok
return(true);
}
以下のブロックは、インジケーターの初期化ブロックです;
//+------------------------------------------------------------------+ //| Initialize MA indicators. | //+------------------------------------------------------------------+ bool CSignalMyCustInd::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); }
まずは、オブジェクトをコレクションに追加します。インジケーターのパラメーターをセットし、CIndicatorsクラスのCreate メソッドを用いてカスタムインジケーターを作成します。
//+------------------------------------------------------------------+
//| Initialization of indicators. |
//+------------------------------------------------------------------+
bool CSignalMyCustInd::InitMyCustomIndicator(CIndicators *indicators)
{
//--- add an object to the collection
if(!indicators.Add(GetPointer(m_mci)))
{
printf(__FUNCTION__+": error adding object");
return(false);
}
//--- set parameters of the indicator
MqlParam parameters[4];
//---
parameters[0].type=TYPE_STRING;
parameters[0].string_value="Examples\\MACD.ex5";
parameters[1].type=TYPE_INT;
parameters[1].integer_value=m_period_fast;
parameters[2].type=TYPE_INT;
parameters[2].integer_value=m_period_slow;
parameters[3].type=TYPE_INT;
parameters[3].integer_value=m_period_signal;
//--- object initialization
if(!m_mci.Create(m_symbol.Name(),0,IND_CUSTOM,4,parameters))
{
printf(__FUNCTION__+": error initializing object");
return(false);
}
//--- number of buffers
if(!m_mci.NumBuffers(4)) return(false);
//--- ok
return(true);
}
次のブロックは、買い条件をチェックします。
//+------------------------------------------------------------------+ //| "Voting" that the price will grow. | //+------------------------------------------------------------------+ int CSignalMyCustInd::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); }
モデル 0の実装によると、二つのモデルがチェックされます;
//+------------------------------------------------------------------+
//| "Voting" that the price will grow. |
//+------------------------------------------------------------------+
int CSignalMyCustInd::LongCondition(void)
{
int result=0;
int idx =StartIndex();
//--- check direction of the main line
if(DiffMain(idx)>0.0)
{
//--- the main line goes upwards, which confirms the possibility of the price growth
if(IS_PATTERN_USAGE(0))
result=m_pattern_0; // "confirming" signal number 0
//--- if the model 1 is used, look for a reverse of the main line
if(IS_PATTERN_USAGE(1) && DiffMain(idx+1)<0.0)
result=m_pattern_1; // signal number 1
}
//--- return the result
return(result);
}
以下のブロックは売り条件をチェックします;
//+------------------------------------------------------------------+ //| "Voting" that the price will fall. | //+------------------------------------------------------------------+ int CSignalMyCustInd::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); }
モデル0の実装によると、二つのモデルがチェックされます;
//+------------------------------------------------------------------+
//| "Voting" that the price will fall. |
//+------------------------------------------------------------------+
int CSignalMyCustInd::ShortCondition(void)
{
int result=0;
int idx =StartIndex();
//--- check direction of the main line
if(DiffMain(idx)<0.0)
{
//--- the main line gown downwards, which confirms the possibility of the price fall
if(IS_PATTERN_USAGE(0))
result=m_pattern_0; // "confirming" signal number 0
//--- if the model 1 is used, look for a reverse of the main line
if(IS_PATTERN_USAGE(1) && DiffMain(idx+1)>0.0)
result=m_pattern_1; // signal number 1
}
//--- return the result
return(result);
}
結論
この記事がどのようにカスタムインジケーターに基づいてトレーディングシグナルジェネレーターを作成できるかについての理解を助けることができることを願っています。
MetaQuotes Ltdによってロシア語から翻訳されました。
元の記事: https://www.mql5.com/ru/articles/691
- 無料取引アプリ
- 8千を超えるシグナルをコピー
- 金融ニュースで金融マーケットを探索