DoEasyライブラリの時系列(第45部): 複数期間指標バッファ
内容
概念
前の記事では、指標バッファコレクションクラスの開発を開始しました。今日は作業を再開し、指標バッファを作成してそのデータにアクセスするメソッドを調整します。バッファオブジェクトに設定されている期間プロパティ(データ時間枠)に応じた現在の銘柄/期間チャートにバッファ値の表示を実装します。現在のチャートに対応していない時間枠の値がバッファに設定されている場合、すべてのバッファデータがチャートに正しく表示されます。たとえば、現在のチャートのデータ期間がM15で、バッファオブジェクトチャートがH1に設定されている場合、バッファデータは、時刻がH1チャートバー内にある現在のM15チャートの各バーに表示されます。現在の例では、現在のチャートの4つのバーには、チャート期間がH1のバーからリクエストされた値に対応する同じ値が入力されています。
これらの改善により、バッファオブジェクトの時間枠を設定するだけでよいため、複数期間の指標を快適に作成できます。残りはライブラリがおこないます。また、指標バッファの作成を徐々に容易にしていくつもりです。これは単一のコード文字列に縮小され、必要なプロパティを持つ必要な指標バッファオブジェクトがそこで作成されるようになります。指標バッファには、特定の描画タイプのバッファインデックスを介してアクセスする必要があります。
「バッファインデックス」の概念は次のとおりです。まず矢印バッファが作成され、次に1行目が作成され、次に矢印バッファが再度作成される場合、バッファオブジェクトインデックスは作成順に配置されます。各描画スタイルには、独自のインデックスシーケンスがあります。提供されている例では、インデックスは次のとおりです。最初に作成された矢印バッファのインデックスは0、2番目の矢印バッファのインデックスは1、最初の矢印バッファの直後に作成されたラインバッファは「ライン」描画スタイルを特徴とする最初のバッファであるため、2番目に作成されても、インデックスは0です。
バッファオブジェクトインデックスの概念を説明します。
- 矢印バッファの作成(インデックス0)
- ラインバッファの作成(インデックス0)
- 矢印バッファの作成(インデックス1)
- ジグザグバッファの作成(インデックス0)
- ジグザグバッファの作成(インデックス1)
- 矢印バッファの作成(インデックス2)
- 矢印バッファの作成(インデックス3)
- ラインバッファの作成(インデックス1)
- ローソク足バッファの作成(インデックス0)
- 矢印バッファの作成(インデックス4)
複数期間モードで指標バッファを使用するためのクラスの改善
MetaTrader 5の[気配値表示]ウィンドウに配置できる銘柄の数はビルド2430以降増えており、ビルド2430の前の1000から5000になっています。銘柄リストを使用するために、Defines.mqhファイルに新しいマクロ置換を導入します。
//+------------------------------------------------------------------+ //| Macro substitutions | //+------------------------------------------------------------------+ //--- Describe the function with the error line number .... //--- Symbol parameters #define CLR_DEFAULT (0xFF000000) // Default symbol background color in the navigator #ifdef __MQL5__ #define SYMBOLS_COMMON_TOTAL (TerminalInfoInteger(TERMINAL_BUILD)<2430 ? 1000 : 5000) // Total number of MQL5 working symbols #else #define SYMBOLS_COMMON_TOTAL (1000) // Total number of MQL4 working symbols #endif //--- Pending request type IDs #define PENDING_REQUEST_ID_TYPE_ERR (1) // Type of a pending request created based on the server return code #define PENDING_REQUEST_ID_TYPE_REQ (2) // Type of a pending request created by request //--- Timeseries parameters #define SERIES_DEFAULT_BARS_COUNT (1000) // Required default amount of timeseries data #define PAUSE_FOR_SYNC_ATTEMPTS (16) // Amount of pause milliseconds between synchronization attempts #define ATTEMPTS_FOR_SYNC (5) // Number of attempts to receive synchronization with the server //+------------------------------------------------------------------+ //| Enumerations | //+------------------------------------------------------------------+
\MQL5\Include\DoEasy\Collections\SymbolsCollection.mqhの使用する銘柄のリストを設定するメソッドで、予約済みの銘柄配列サイズを1000から新しいマクロ置換値に置き換えます。
//+------------------------------------------------------------------+ //| Set the list of used symbols | //+------------------------------------------------------------------+ bool CSymbolsCollection::SetUsedSymbols(const string &symbol_used_array[]) { ::ArrayResize(this.m_array_symbols,0,SYMBOLS_COMMON_TOTAL); ::ArrayCopy(this.m_array_symbols,symbol_used_array);
銘柄リストを作成するメソッドで、ループインデックスの参照値1000を新しいマクロ置換に置き換えます。
//+------------------------------------------------------------------+ //| Creating the symbol list (Market Watch or the complete list) | //+------------------------------------------------------------------+ bool CSymbolsCollection::CreateSymbolsList(const bool flag) { bool res=true; int total=::SymbolsTotal(flag); for(int i=0;i<total && i<SYMBOLS_COMMON_TOTAL;i++) {
同様に、 \MQL5\Include\DoEasy\Engine.mqhの使用されているすべての銘柄と時間枠を書き込むメソッドで、1000をマクロ置換に置き換えます。
//+------------------------------------------------------------------+ //| Write all used symbols and timeframes | //| to the ArrayUsedSymbols and ArrayUsedTimeframes arrays | //+------------------------------------------------------------------+ void CEngine::WriteSymbolsPeriodsToArrays(void) { //--- Get the list of all created timeseries (created by the number of used symbols) CArrayObj *list_timeseries=this.GetListTimeSeries(); if(list_timeseries==NULL) return; //--- Get the total number of created timeseries int total_timeseries=list_timeseries.Total(); if(total_timeseries==0) return; //--- Set the size of the array of used symbols equal to the number of created timeseries, while //--- the size of the array of used timeframes is set equal to the maximum possible number of timeframes in the terminal if(::ArrayResize(ArrayUsedSymbols,total_timeseries,SYMBOLS_COMMON_TOTAL)!=total_timeseries || ::ArrayResize(ArrayUsedTimeframes,21,21)!=21) return; //--- Set both arrays to zero ::ZeroMemory(ArrayUsedSymbols); ::ZeroMemory(ArrayUsedTimeframes); //--- Reset the number of added symbols and timeframes to zero and, //--- in a loop by the total number of timeseries, int num_symbols=0,num_periods=0; for(int i=0;i<total_timeseries;i++) { //--- get the next object of all timeseries of a single symbol CTimeSeriesDE *timeseries=list_timeseries.At(i); if(timeseries==NULL || this.IsExistSymbol(timeseries.Symbol())) continue; //--- increase the number of used symbols and (num_symbols variable), and //--- write the timeseries symbol name to the array of used symbols by the num_symbols-1 index num_symbols++; ArrayUsedSymbols[num_symbols-1]=timeseries.Symbol(); //--- Get the list of all its timeseries from the object of all symbol timeseries CArrayObj *list_series=timeseries.GetListSeries(); if(list_series==NULL) continue; //--- In the loop by the total number of symbol timeseries, int total_series=list_series.Total(); for(int j=0;j<total_series;j++) { //--- get the next timeseries object CSeriesDE *series=list_series.At(j); if(series==NULL || this.IsExistTimeframe(series.Timeframe())) continue; //--- increase the number of used timeframes and (num_periods variable), and //--- write the timeseries timeframe value to the array of used timeframes by num_periods-1 index num_periods++; ArrayUsedTimeframes[num_periods-1]=series.Timeframe(); } } //--- Upon the loop completion, change the size of both arrays to match the exact number of added symbols and timeframes ::ArrayResize(ArrayUsedSymbols,num_symbols,SYMBOLS_COMMON_TOTAL); ::ArrayResize(ArrayUsedTimeframes,num_periods,21); } //+------------------------------------------------------------------+
バッファオブジェクトパラメータは、次の指標バッファに割り当てられる配列インデックスを指定するパラメータを備えています。これにより、単一のバッファを作成してそれに必要な数の銘柄を追加するときに、次の配列インデックスを次のバッファの最初の(基本)配列にすぐに設定できます。逆に、指標バッファ(グラフィック構築)インデックスの場合、この値は、PlotIndexSetDouble()、 PlotIndexSetInteger()、PlotIndexSetString()関数を使用してバッファに必要な値を設定するときに使用されます。バッファオブジェクトパラメータにはプロパティがありません。次のグラフィカル構造のインデックスを便利に計算するには、Defines.mqhファイルのバッファオブジェクトの整数プロパティの列挙に新しいプロパティを追加します。また、次のバッファオブジェクトの基本配列のインデックスを格納するプロパティの名前を変更します。
//+------------------------------------------------------------------+ //| Buffer integer properties | //+------------------------------------------------------------------+ enum ENUM_BUFFER_PROP_INTEGER { BUFFER_PROP_INDEX_PLOT = 0, // Plotted buffer serial number BUFFER_PROP_STATUS, // Buffer status (by drawing style) from the ENUM_BUFFER_STATUS enumeration BUFFER_PROP_TYPE, // Buffer type (from the ENUM_BUFFER_TYPE enumeration) BUFFER_PROP_TIMEFRAME, // Buffer period data (timeframe) BUFFER_PROP_ACTIVE, // Buffer usage flag BUFFER_PROP_DRAW_TYPE, // Graphical construction type (from the ENUM_DRAW_TYPE enumeration) BUFFER_PROP_ARROW_CODE, // Arrow code for DRAW_ARROW style BUFFER_PROP_ARROW_SHIFT, // The vertical shift of the arrows for DRAW_ARROW style BUFFER_PROP_LINE_STYLE, // Line style BUFFER_PROP_LINE_WIDTH, // Line width BUFFER_PROP_DRAW_BEGIN, // The number of initial bars that are not drawn and values in DataWindow BUFFER_PROP_SHOW_DATA, // Flag of displaying construction values in DataWindow BUFFER_PROP_SHIFT, // Indicator graphical construction shift by time axis in bars BUFFER_PROP_COLOR_INDEXES, // Number of colors BUFFER_PROP_COLOR, // Drawing color BUFFER_PROP_INDEX_BASE, // Base data buffer index BUFFER_PROP_INDEX_NEXT_BASE, // Index of the array to be assigned as the next indicator buffer BUFFER_PROP_INDEX_NEXT_PLOT, // Index of the next drawn buffer BUFFER_PROP_NUM_DATAS, // Number of data buffers BUFFER_PROP_INDEX_COLOR, // Color buffer index }; #define BUFFER_PROP_INTEGER_TOTAL (20) // Total number of integer bar properties #define BUFFER_PROP_INTEGER_SKIP (2) // Number of buffer properties not used in sorting //+------------------------------------------------------------------+
バッファオブジェクトの整数プロパティの数を19から20に増やします。
可能なバッファ並び替え基準を列挙しながら、新しく追加されたプロパティによる並び替えを追加し、次の基本バッファ配列として割り当てるために、配列インデックスによる並び替えの定数名を変更します。
//+------------------------------------------------------------------+ //| Possible buffer sorting criteria | //+------------------------------------------------------------------+ #define FIRST_BUFFER_DBL_PROP (BUFFER_PROP_INTEGER_TOTAL-BUFFER_PROP_INTEGER_SKIP) #define FIRST_BUFFER_STR_PROP (BUFFER_PROP_INTEGER_TOTAL-BUFFER_PROP_INTEGER_SKIP+BUFFER_PROP_DOUBLE_TOTAL-BUFFER_PROP_DOUBLE_SKIP) enum ENUM_SORT_BUFFER_MODE { //--- Sort by integer properties SORT_BY_BUFFER_INDEX_PLOT = 0, // Sort by the plotted buffer serial number SORT_BY_BUFFER_STATUS, // Sort by buffer drawing style (status) from the ENUM_BUFFER_STATUS enumeration SORT_BY_BUFFER_TYPE, // Sort by buffer type (from the ENUM_BUFFER_TYPE enumeration) SORT_BY_BUFFER_TIMEFRAME, // Sort by the buffer data period (timeframe) SORT_BY_BUFFER_ACTIVE, // Sort by the buffer usage flag SORT_BY_BUFFER_DRAW_TYPE, // Sort by graphical construction type (from the ENUM_DRAW_TYPE enumeration) SORT_BY_BUFFER_ARROW_CODE, // Sort by the arrow code for DRAW_ARROW style SORT_BY_BUFFER_ARROW_SHIFT, // Sort by the vertical shift of the arrows for DRAW_ARROW style SORT_BY_BUFFER_LINE_STYLE, // Sort by the line style SORT_BY_BUFFER_LINE_WIDTH, // Sort by the line width SORT_BY_BUFFER_DRAW_BEGIN, // Sort by the number of initial bars that are not drawn and values in DataWindow SORT_BY_BUFFER_SHOW_DATA, // Sort by the flag of displaying construction values in DataWindow SORT_BY_BUFFER_SHIFT, // Sort by the indicator graphical construction shift by time axis in bars SORT_BY_BUFFER_COLOR_INDEXES, // Sort by a number of attempts SORT_BY_BUFFER_COLOR, // Sort by the drawing color SORT_BY_BUFFER_INDEX_BASE, // Sort by the basic data buffer index SORT_BY_BUFFER_INDEX_NEXT_BASE, // Sort by the index of the array to be assigned as the next indicator buffer SORT_BY_BUFFER_INDEX_NEXT_PLOT, // Sort by the index of the next drawn buffer //--- Sort by real properties SORT_BY_BUFFER_EMPTY_VALUE = FIRST_BUFFER_DBL_PROP, // Sort by the empty value for plotting where nothing will be drawn //--- Sort by string properties SORT_BY_BUFFER_SYMBOL = FIRST_BUFFER_STR_PROP, // Sort by the buffer symbol SORT_BY_BUFFER_LABEL, // Sort by the name of the graphical indicator series displayed in DataWindow }; //+------------------------------------------------------------------+
ここで、バッファオブジェクトを作成するときに、基本的な配列インデックスと次のグラフィカルシリーズのインデックスを現在のバッファパラメータに書き込みます。したがって、新しく作成されたバッファオブジェクトに設定する必要があるインデックスを見つけるには、前のインデックス(最後に作成されたバッファオブジェクト)にアクセスして、必要な値を取得するだけで済みます。
各バッファには1〜5個の配列が割り当てられている可能性があり、新しい指標バッファに必要な配列インデックスのすべての値を再計算する必要がないため、これはより簡単で優れています。すべては、最後に作成されたバッファオブジェクトのプロパティで直接設定されます。 これらの値は、使用される配列の数に応じて、新しく追加された指標バッファごとに再計算されます。
\MQL5\Include\DoEasy\Datas.mqhで、新しく追加されたバッファプロパティの新しいメッセージインデックスを追加し、指標バッファの次の配列のプロパティについてのメッセージのインデックス定数名を変更します。
//--- CBuffer MSG_LIB_TEXT_BUFFER_TEXT_INDEX_BASE, // Base data buffer index MSG_LIB_TEXT_BUFFER_TEXT_INDEX_PLOT, // Plotted buffer serial number MSG_LIB_TEXT_BUFFER_TEXT_INDEX_COLOR, // Color buffer index MSG_LIB_TEXT_BUFFER_TEXT_NUM_DATAS, // Number of data buffers MSG_LIB_TEXT_BUFFER_TEXT_INDEX_NEXT_BASE, // Index of the array to be assigned as the next indicator buffer MSG_LIB_TEXT_BUFFER_TEXT_INDEX_NEXT_PLOT, // Index of the next drawn buffer MSG_LIB_TEXT_BUFFER_TEXT_TIMEFRAME, // Buffer (timeframe) data period
また、新しく追加したインデックスに対応するメッセージテキストを追加します。
{"Индекс базового буфера данных","Index of Base data buffer"}, {"Порядковый номер рисуемого буфера","Plot buffer sequence number"}, {"Индекс буфера цвета","Color buffer index"}, {"Количество буферов данных","Number of data buffers"}, {"Индекс массива для назначения следующим индикаторным буфером","Array index for assignment as the next indicator buffer"}, {"Индекс следующего по счёту рисуемого буфера","Index of the next drawable buffer"}, {"Период данных буфера (таймфрейм)","Buffer data Period (Timeframe)"},
次の基本バッファオブジェクト配列のインデックスを格納する定数の名前を変更したため、バッファオブジェクトクラスのすべてのファイル(BufferArrow.mqh、BufferBars.mqh、BufferCandles.mqh、BufferFilling.mqh、BufferHistogram.mqh、BufferHistogram2.mqh、BufferLine.mqh、BufferArrow.mqh、BufferSection.mqh、BufferZigZag.mqh )で、BUFFER_PROP_INDEX_NEXT文字列のすべてのインスタンスをBUFFER_PROP_INDEX_NEXT_BASEに置き換え、操作ログで短いバッファオブジェクトの説明を表示する仮想メソッドを改善します。
例として、 \MQL5\Include\DoEasy\Objects\Indicators\BufferArrow.mqhでCBufferArrowクラスを使用するメソッドの変更を検討してみましょう。
//+------------------------------------------------------------------+ //| Display short buffer description in the journal | //+------------------------------------------------------------------+ void CBufferArrow::PrintShort(void) { ::Print ( CMessage::Text(MSG_LIB_TEXT_BUFFER_TEXT_BUFFER),"(P",(string)this.IndexPlot(),"/B",(string)this.IndexBase(),"/C",(string)this.IndexColor(),"): ", this.GetStatusDescription(true)," ",this.Symbol()," ",TimeframeDescription(this.Timeframe()) ); } //+------------------------------------------------------------------+
メッセージテキストは可能な限り有益なものになっています。たとえば、改善されたメソッドによって表示される矢印バッファクラスの簡単な説明は次のようになります。
Buffer(P0/B0/C1): EURUSD H1を矢印で描画
ここで
- 「P」は「Plot(プロット)」 — グラフィカルな構築インデックス
- 「B」は「Base(ベース)」 — バッファに最初に割り当てられる基本配列のインデックス(配列の残りのバッファは、基本配列インデックスから始まるインデックスの増加に配置されます)
- 「C」は「Color(色)」 — バッファに割り当てられたカラー配列のインデックス
したがって、可能なすべてのバッファオブジェクトを作成し、それらの簡単な説明を操作ログに表示すると、そこに次のエントリが表示されます。
Buffer(P0/B0/C1): Drawing with arrows EURUSD H1 Buffer(P1/B2/C3): EURUSD H1 line Buffer(P2/B4/C5): EURUSD H1 sections Buffer(P3/B6/C7): Histogram from the zero line EURUSD H1 Buffer(P4/B8/C10): Histogram on two indicator buffers EURUSD H1 Buffer(P5/B11/C13): EURUSD H1 zigzag Buffer(P6/B14/C16): Color filling between two levels EURUSD H1 Buffer(P7/B16/C20): Display as EURUSD H1 bars Buffer(P8/B21/C25): Display as EURUSD H1 candles Buffer[P8/B26/C27]: Calculated buffer
これで、インデックス(BとC)が割り当てられた指標バッファと配列、およびバッファ(P)に割り当てられたグラフィカルな系列インデックスを明確に確認できます。
ここで、計算バッファ(この記事の後半で紹介)に割り当てられたグラフィカルシリーズインデックスが、前のバッファに割り当てられたものと一致していることがわかります。実際、計算バッファには、グラフィカルなシリーズとカラーバッファ配列がありません。現在、デバッグデータのみが含まれています。P8とC27は、グラフィカルシリーズとカラーバッファ配列のどのインデックスに、計算バッファの後に次のバッファを作成する必要があるかを示しています。
指標バッファを作成した後、データをたとえばPN、CN、PX、CXに変更します。これは、計算バッファにグラフィカルな系列とカラーバッファの配列がないことを意味します。または、B(バッファに割り当てられた配列のインデックス)のみを残して削除します。
基本抽象バッファオブジェクトの子孫オブジェクトがあり、作成された指標バッファのタイプを描画タイプで明確にします。ただし、さらに別のバッファオブジェクトがありません。計算されたオブジェクトは、配列を必要とする計算を実行するために使用されますが、チャートには何も表示されません。このような配列は指標バッファです。ターミナルサブシステムは、描画された指標バッファ配列とともにその配布を担当します。
CBufferCalculateクラス名を使用してこのような子孫オブジェクトを作成します。\MQL5\Include\DoEasy\Objects\Indicators\で、新しいBufferCalculate.mqhファイルを作成し、必要なコンテンツをすぐに入力します。
//+------------------------------------------------------------------+ //| BufferCalculate.mqh | //| Copyright 2020, MetaQuotes Software Corp. | //| https://mql5.com/en/users/artmedia70 | //+------------------------------------------------------------------+ #property copyright "Copyright 2020, MetaQuotes Software Corp." #property link "https://mql5.com/en/users/artmedia70" #property version "1.00" //+------------------------------------------------------------------+ //| Include files | //+------------------------------------------------------------------+ #include "Buffer.mqh" //+------------------------------------------------------------------+ //| Calculated buffer | //+------------------------------------------------------------------+ class CBufferCalculate : public CBuffer { private: public: //--- Constructor CBufferCalculate(const uint index_plot,const uint index_array) : CBuffer(BUFFER_STATUS_NONE,BUFFER_TYPE_CALCULATE,index_plot,index_array,1,0,"Calculate") {} //--- Supported integer properties of a buffer virtual bool SupportProperty(ENUM_BUFFER_PROP_INTEGER property); //--- Supported real properties of a buffer virtual bool SupportProperty(ENUM_BUFFER_PROP_DOUBLE property); //--- Supported string properties of a buffer virtual bool SupportProperty(ENUM_BUFFER_PROP_STRING property); //--- Display a short buffer description in the journal virtual void PrintShort(void); //--- Set the value to the data buffer array void SetData(const uint series_index,const double value) { this.SetBufferValue(0,series_index,value); } //--- Return the value from the data buffer array double GetData(const uint series_index) const { return this.GetDataBufferValue(0,series_index); } }; //+------------------------------------------------------------------+ //| Return 'true' if a buffer supports a passed | //| integer property, otherwise return 'false' | //+------------------------------------------------------------------+ bool CBufferCalculate::SupportProperty(ENUM_BUFFER_PROP_INTEGER property) { if( property==BUFFER_PROP_INDEX_PLOT || property==BUFFER_PROP_STATUS || property==BUFFER_PROP_TYPE || property==BUFFER_PROP_INDEX_BASE || property==BUFFER_PROP_INDEX_NEXT_BASE ) return true; return false; } //+------------------------------------------------------------------+ //| Return 'true' if a buffer supports a passed | //| real property, otherwise return 'false' | //+------------------------------------------------------------------+ bool CBufferCalculate::SupportProperty(ENUM_BUFFER_PROP_DOUBLE property) { return false; } //+------------------------------------------------------------------+ //| Return 'true' if a buffer supports a passed | //| string property, otherwise return 'false' | //+------------------------------------------------------------------+ bool CBufferCalculate::SupportProperty(ENUM_BUFFER_PROP_STRING property) { return false; } //+------------------------------------------------------------------+ //| Display short buffer description in the journal | //+------------------------------------------------------------------+ void CBufferCalculate::PrintShort(void) { ::Print ( CMessage::Text(MSG_LIB_TEXT_BUFFER_TEXT_BUFFER),"[P",(string)this.IndexPlot(),"/B",(string)this.IndexBase(),"/C",(string)this.IndexColor(),"]: ", this.GetTypeBufferDescription() ); } //+------------------------------------------------------------------+
基本抽象バッファの子孫クラスの構造と動作原理は、第43部ですでに検討しました。これらは、ここで説明されているものと似ています。
基本バッファオブジェクト(CBuffer)のすべての子孫クラスの完全なセットができたので、それも変更してみましょう。
場合によっては、要求または条件によって、指標バッファを使用して1回限りのアクションを実行する必要があります。たとえば、ボタンを押してバッファデータを消去または更新したい場合があります。これを行うために、必要なアクションがまだ実行されていないことを指定するフラグを導入しましょう。
クラスのprivateセクションでそのようなフラグを格納するためのクラスメンバー変数を宣言し、publicセクションでは、値の変数を設定するメソッドと取得するメソッドを宣言します。
//+------------------------------------------------------------------+ //| Abstract indicator buffer class | //+------------------------------------------------------------------+ class CBuffer : public CBaseObj { private: long m_long_prop[BUFFER_PROP_INTEGER_TOTAL]; // Integer properties double m_double_prop[BUFFER_PROP_DOUBLE_TOTAL]; // Real properties string m_string_prop[BUFFER_PROP_STRING_TOTAL]; // String properties bool m_act_state_trigger; // Auxiliary buffer status switch flag //--- Return the index of the array the buffer's (1) double and (2) string properties are located at int IndexProp(ENUM_BUFFER_PROP_DOUBLE property) const { return(int)property-BUFFER_PROP_INTEGER_TOTAL; } int IndexProp(ENUM_BUFFER_PROP_STRING property) const { return(int)property-BUFFER_PROP_INTEGER_TOTAL-BUFFER_PROP_DOUBLE_TOTAL; } //--- Set the graphical construction type by buffer type and status void SetDrawType(void); //--- Return the adjusted buffer array index int GetCorrectIndexBuffer(const uint buffer_index) const; protected: struct SDataBuffer { double Array[]; }; // Structure for storing buffer object buffer arrays SDataBuffer DataBuffer[]; // Array of all object indicator buffers double ColorBufferArray[]; // Color buffer array int ArrayColors[]; // Array for storing colors public: //--- Set buffer's (1) integer, (2) real and (3) string properties void SetProperty(ENUM_BUFFER_PROP_INTEGER property,long value) { this.m_long_prop[property]=value; } void SetProperty(ENUM_BUFFER_PROP_DOUBLE property,double value) { this.m_double_prop[this.IndexProp(property)]=value; } void SetProperty(ENUM_BUFFER_PROP_STRING property,string value) { this.m_string_prop[this.IndexProp(property)]=value; } //--- Return (1) integer, (2) real and (3) string buffer properties from the properties array long GetProperty(ENUM_BUFFER_PROP_INTEGER property) const { return this.m_long_prop[property]; } double GetProperty(ENUM_BUFFER_PROP_DOUBLE property) const { return this.m_double_prop[this.IndexProp(property)]; } string GetProperty(ENUM_BUFFER_PROP_STRING property) const { return this.m_string_prop[this.IndexProp(property)]; } //--- Get description of buffer's (1) integer, (2) real and (3) string properties string GetPropertyDescription(ENUM_BUFFER_PROP_INTEGER property); string GetPropertyDescription(ENUM_BUFFER_PROP_DOUBLE property); string GetPropertyDescription(ENUM_BUFFER_PROP_STRING property); //--- Return the flag of the buffer supporting the property virtual bool SupportProperty(ENUM_BUFFER_PROP_INTEGER property) { return true; } virtual bool SupportProperty(ENUM_BUFFER_PROP_DOUBLE property) { return true; } virtual bool SupportProperty(ENUM_BUFFER_PROP_STRING property) { return true; } //--- Compare CBuffer objects by all possible properties (for sorting the lists by a specified buffer object property) virtual int Compare(const CObject *node,const int mode=0) const; //--- Compare CBuffer objects by all properties (to search for equal buffer objects) bool IsEqual(CBuffer* compared_obj) const; //--- Set the buffer name void SetName(const string name) { this.m_name=name; } //--- (1) Set and (2) return the buffer status switch flag void SetActStateFlag(const bool flag) { this.m_act_state_trigger=flag; } bool GetActStateFlag(void) const { return this.m_act_state_trigger; } //--- Default constructor CBuffer(void){;}
たとえば、ボタンを押してバッファ表示を無効にすると、すべての配列データを消去するだけでなく、バッファの描画スタイルをDRAW_NONEに設定することもできます。これを実現するために、クラスのpublicセクションで描画タイプをバッファオブジェクトに設定する追加のメソッドを導入します。
public: //--- Send description of buffer properties to the journal (full_prop=true - all properties, false - only supported ones) void Print(const bool full_prop=false); //--- Display a short buffer description in the journal (implementation in the descendants) virtual void PrintShort(void) {;} //--- Set (1) the arrow code, (2) vertical shift of arrows, (3) symbol, (4) timeframe, (5) buffer activity flag //--- (6) drawing type, (7) number of initial bars without drawing, (8) flag of displaying construction values in DataWindow, //--- (9) shift of the indicator graphical construction along the time axis, (10) line style, (11) line width, //--- (12) total number of colors, (13) one drawing color, (14) color of drawing in the specified color index, //--- (15) drawing colors from the color array, (16) empty value, (17) name of the graphical series displayed in DataWindow virtual void SetArrowCode(const uchar code) { return; } virtual void SetArrowShift(const int shift) { return; } void SetSymbol(const string symbol) { this.SetProperty(BUFFER_PROP_SYMBOL,symbol); } void SetTimeframe(const ENUM_TIMEFRAMES timeframe) { this.SetProperty(BUFFER_PROP_TIMEFRAME,timeframe); } void SetActive(const bool flag) { this.SetProperty(BUFFER_PROP_ACTIVE,flag); } void SetDrawType(const ENUM_DRAW_TYPE draw_type); void SetDrawBegin(const int value); void SetShowData(const bool flag); void SetShift(const int shift); void SetStyle(const ENUM_LINE_STYLE style); void SetWidth(const int width); void SetColorNumbers(const int number); void SetColor(const color colour); void SetColor(const color colour,const uchar index); void SetColors(const color &array_colors[]); void SetEmptyValue(const double value); virtual void SetLabel(const string label);
クラス本体の外側で実装しましょう。
//+------------------------------------------------------------------+ //| Set the passed graphical construction type | //+------------------------------------------------------------------+ void CBuffer::SetDrawType(const ENUM_DRAW_TYPE draw_type) { if(this.TypeBuffer()==BUFFER_TYPE_CALCULATE) return; this.SetProperty(BUFFER_PROP_DRAW_TYPE,draw_type); ::PlotIndexSetInteger((int)this.GetProperty(BUFFER_PROP_INDEX_PLOT),PLOT_DRAW_TYPE,draw_type); } //+------------------------------------------------------------------+
ここで、バッファタイプがCalculationの場合、レンダリングはないので、メソッドを終了します。
描画タイプ(メソッドに渡される)をオブジェクトプロパティに設定し、バッファのスタイルを設定します。
BUFFER_PROP_INDEX_NEXT定数をBUFFER_PROP_INDEX_NEXT_BASEに置き換えたので、適切なIndexNextBuffer()メソッドの名前をIndexNextBaseBuffer()に変更し、次に描画されるバッファのインデックスを返す別のメソッドを追加します。
//--- Return (1) the serial number of the drawn buffer, (2) bound array index, (3) color buffer index, //--- (4) index of the first free bound array, (5) index of the next drawn buffer, (6) buffer data period, (7) buffer status, //--- (8) buffer type, (9) buffer usage flag, (10) arrow code, (11) arrow shift for DRAW_ARROW style, //--- (12) number of initial bars that are not drawn and values in DataWindow, (13) graphical construction type, //--- (14) flag of displaying construction values in DataWindow, (15) indicator graphical construction shift along the time axis, //--- (16) drawing line style, (17) drawing line width, (18) number of colors, (19) drawing color, number of buffers for construction //--- (20) set empty value, (21) buffer symbol, (22) name of the indicator graphical series displayed in DataWindow int IndexPlot(void) const { return (int)this.GetProperty(BUFFER_PROP_INDEX_PLOT); } int IndexBase(void) const { return (int)this.GetProperty(BUFFER_PROP_INDEX_BASE); } int IndexColor(void) const { return (int)this.GetProperty(BUFFER_PROP_INDEX_COLOR); } int IndexNextBaseBuffer(void) const { return (int)this.GetProperty(BUFFER_PROP_INDEX_NEXT_BASE); } int IndexNextPlotBuffer(void) const { return (int)this.GetProperty(BUFFER_PROP_INDEX_NEXT_PLOT); } ENUM_TIMEFRAMES Timeframe(void) const { return (ENUM_TIMEFRAMES)this.GetProperty(BUFFER_PROP_TIMEFRAME); }
最後に、クラス本体の最後に4つのメソッド(オブジェクト配列を初期化するための2つのメソッドとそれらに入力するための2つのメソッド)を追加します。
//--- Return the size of the data buffer array virtual int GetDataTotal(const uint buffer_index=0) const; //--- Return the value from the specified index of the specified (1) data, (2) color index and (3) color buffer arrays double GetDataBufferValue(const uint buffer_index,const uint series_index) const; int GetColorBufferValueIndex(const uint series_index) const; color GetColorBufferValueColor(const uint series_index) const; //--- Set the value to the specified index of the specified (1) data and (2) color buffer arrays void SetBufferValue(const uint buffer_index,const uint series_index,const double value); void SetBufferColorIndex(const uint series_index,const uchar color_index); //--- Initialize all object buffers by (1) a specified value, (2) the empty value specified for the object void InitializeAll(const double value,const uchar color_index); void InitializeAll(void); //--- Fill all data buffers with empty values in the specified timeseries index void ClearData(const int series_index); //--- Fill the specified buffer with data from (1) the passed timeseries and (2) the array void FillAsSeries(const int buffer_index,CSeriesDE *series,const ENUM_SORT_BAR_MODE property); void FillAsSeries(const int buffer_index,const double &array[]); }; //+------------------------------------------------------------------+
実際、これらは、さまざまな状況でさまざまなパラメータのセットを受け入れる2つのオーバーロードされたメソッドです。
すべてのオブジェクトバッファをパラメータとして初期化するパラメトリックメソッドは、指標バッファによって割り当てられたすべてのバッファオブジェクト配列を初期化するために使用される値と、バッファオブジェクトのカラーバッファによって割り当てられた配列を初期化するために使用されるカラーインデックスを取ります。
入力のないメソッドは、バッファオブジェクトのプロパティで「空」に設定された値ですべてのバッファオブジェクト配列を初期化しますが、カラー配列はゼロで初期化されます。これは、バッファオブジェクトに設定された最初の色(または存在する色が1色の場合は唯一の色)です。
クラス本体の外にメソッドを実装しましょう。
//+------------------------------------------------------------------+ //| Initialize all object buffers by the specified value | //+------------------------------------------------------------------+ void CBuffer::InitializeAll(const double value,const uchar color_index) { for(int i=0;i<this.GetProperty(BUFFER_PROP_NUM_DATAS);i++) ::ArrayInitialize(this.DataBuffer[i].Array,value); if(this.Status()!=BUFFER_STATUS_FILLING && this.TypeBuffer()!=BUFFER_TYPE_CALCULATE) ::ArrayInitialize(this.ColorBufferArray,(color_index>this.ColorsTotal()-1 ? 0 : color_index)); } //+------------------------------------------------------------------+ //| Initialize all object buffers | //| by the empty value set for the object | //+------------------------------------------------------------------+ void CBuffer::InitializeAll(void) { for(int i=0;i<this.GetProperty(BUFFER_PROP_NUM_DATAS);i++) ::ArrayInitialize(this.DataBuffer[i].Array,this.EmptyValue()); if(this.Status()!=BUFFER_STATUS_FILLING && this.TypeBuffer()!=BUFFER_TYPE_CALCULATE) ::ArrayInitialize(this.ColorBufferArray,0); } //+------------------------------------------------------------------+
まず、指標バッファによって割り当てられたバッファオブジェクト配列の数による反復処理で、最初のメソッドに渡された値と2番目のメソッドのバッファに設定された値を使用して次の各配列を初期化します。次に、バッファオブジェクトが「2つのレベル間のカラー塗りつぶし」描画スタイルではなく、計算バッファでもない場合(バッファにカラーバッファがない)、カラー配列は最初のメソッドのメソッドに渡された値を使用して初期化され、 2番目のメソッドでゼロを使用します。
+//+-----------------------------------------------------------------------+ //| Fill the specified buffer with data from the passed timeseries' bars | //+-----------------------------------------------------------------------+ void CBuffer::FillAsSeries(const int buffer_index,CSeriesDE *series,const ENUM_SORT_BAR_MODE property) { //--- If an empty timeseries is passed or a property to fill the array is a string one, leave the method if(series==NULL || property>FIRST_BAR_STR_PROP-1) return; //--- Get the list of all its timeseries from the passed timeseries object CArrayObj *list=series.GetList(); if(list==NULL || list.Total()==0) return; //--- Get the number of bars in the timeseries list int total=list.Total(); //--- In the loop from the very last timeseries list element (from the current bar) int n=0; for(int i=total-1;i>WRONG_VALUE && !::IsStopped();i--) { //--- get the next bar object by the loop index, CBar *bar=list.At(i); //--- Set the value of the copied property double value= (bar==NULL ?this.EmptyValue() : property<FIRST_BAR_DBL_PROP ?bar.GetProperty((ENUM_BAR_PROP_INTEGER)property) : property<FIRST_BAR_STR_PROP ?bar.GetProperty((ENUM_BAR_PROP_DOUBLE)property) : this.EmptyValue() ); //--- calculate the index, based on which the bar property is saved to the buffer, and //--- write the value of the obtained bar property to the buffer array using the calculated index n=total-1-i; this.SetBufferValue(buffer_index,n,value); } } //+------------------------------------------------------------------+ //| Fill the specified buffer with data from the passed array | //+------------------------------------------------------------------+ void CBuffer::FillAsSeries(const int buffer_index,const double &array[]) { //--- Get the copied array size int total=::ArraySize(array); if(total==0) return; //--- In the loop from the very last array element (from the current bar) int n=0; for(int i=total-1;i>WRONG_VALUE && !::IsStopped();i--) { //--- calculate the index, based on which the array value is saved to the buffer, and //--- write the value of the array cell by i index using the calculated index n=total-1-i; this.SetBufferValue(buffer_index,n,array[i]); } } //+------------------------------------------------------------------+
これらの2つのメソッドは、コードリストで詳細にコメントされています。最初のメソッドは、指標バッファを埋めるためにデータが使用される時系列オブジェクトへのポインタを受け取り、2番目のメソッドはdouble配列を受け取ります。そのデータは、指標バッファを埋めるためにも使用されます。
クラスコンストラクタを調整します。
//+------------------------------------------------------------------+ //| Closed parametric constructor | //+------------------------------------------------------------------+ CBuffer::CBuffer(ENUM_BUFFER_STATUS buffer_status, ENUM_BUFFER_TYPE buffer_type, const uint index_plot, const uint index_base_array, const int num_datas, const int width, const string label) { this.m_type=COLLECTION_BUFFERS_ID; this.m_act_state_trigger=true; //--- Save integer properties this.m_long_prop[BUFFER_PROP_STATUS] = buffer_status; this.m_long_prop[BUFFER_PROP_TYPE] = buffer_type; ENUM_DRAW_TYPE type= ( !this.TypeBuffer() || !this.Status() ? DRAW_NONE : this.Status()==BUFFER_STATUS_FILLING ? DRAW_FILLING : ENUM_DRAW_TYPE(this.Status()+8) ); this.m_long_prop[BUFFER_PROP_DRAW_TYPE] = type; this.m_long_prop[BUFFER_PROP_TIMEFRAME] = PERIOD_CURRENT; this.m_long_prop[BUFFER_PROP_ACTIVE] = true; this.m_long_prop[BUFFER_PROP_ARROW_CODE] = 0x9F; this.m_long_prop[BUFFER_PROP_ARROW_SHIFT] = 0; this.m_long_prop[BUFFER_PROP_DRAW_BEGIN] = 0; this.m_long_prop[BUFFER_PROP_SHOW_DATA] = (buffer_type>BUFFER_TYPE_CALCULATE ? true : false); this.m_long_prop[BUFFER_PROP_SHIFT] = 0; this.m_long_prop[BUFFER_PROP_LINE_STYLE] = STYLE_SOLID; this.m_long_prop[BUFFER_PROP_LINE_WIDTH] = width; this.m_long_prop[BUFFER_PROP_COLOR_INDEXES] = (this.Status()>BUFFER_STATUS_NONE ? (this.Status()!=BUFFER_STATUS_FILLING ? 1 : 2) : 0); this.m_long_prop[BUFFER_PROP_COLOR] = clrRed; this.m_long_prop[BUFFER_PROP_NUM_DATAS] = num_datas; this.m_long_prop[BUFFER_PROP_INDEX_PLOT] = index_plot; this.m_long_prop[BUFFER_PROP_INDEX_BASE] = index_base_array; this.m_long_prop[BUFFER_PROP_INDEX_COLOR] = this.GetProperty(BUFFER_PROP_INDEX_BASE)+this.GetProperty(BUFFER_PROP_NUM_DATAS); this.m_long_prop[BUFFER_PROP_INDEX_NEXT_BASE] = this.GetProperty(BUFFER_PROP_INDEX_COLOR)+ (this.Status()==BUFFER_STATUS_FILLING || this.TypeBuffer()==BUFFER_TYPE_CALCULATE ? 0 : 1); this.m_long_prop[BUFFER_PROP_INDEX_NEXT_PLOT] = (this.TypeBuffer()>BUFFER_TYPE_CALCULATE ? index_plot+1 : index_plot); //--- Save real properties this.m_double_prop[this.IndexProp(BUFFER_PROP_EMPTY_VALUE)] = (this.TypeBuffer()>BUFFER_TYPE_CALCULATE ? EMPTY_VALUE : 0); //--- Save string properties this.m_string_prop[this.IndexProp(BUFFER_PROP_SYMBOL)] = ::Symbol(); this.m_string_prop[this.IndexProp(BUFFER_PROP_LABEL)] = (this.TypeBuffer()>BUFFER_TYPE_CALCULATE ? label : NULL); //--- If failed to change the size of the indicator buffer array, display the appropriate message indicating the string if(::ArrayResize(this.DataBuffer,(int)this.GetProperty(BUFFER_PROP_NUM_DATAS))==WRONG_VALUE) ::Print(DFUN_ERR_LINE,CMessage::Text(MSG_LIB_SYS_FAILED_DRAWING_ARRAY_RESIZE),". ",CMessage::Text(MSG_LIB_SYS_ERROR),": ",(string)::GetLastError()); //--- If failed to change the size of the color array (only for a non-calculated buffer), display the appropriate message indicating the string if(this.TypeBuffer()>BUFFER_TYPE_CALCULATE) if(::ArrayResize(this.ArrayColors,(int)this.ColorsTotal())==WRONG_VALUE) ::Print(DFUN_ERR_LINE,CMessage::Text(MSG_LIB_SYS_FAILED_COLORS_ARRAY_RESIZE),". ",CMessage::Text(MSG_LIB_SYS_ERROR),": ",(string)::GetLastError()); //--- For DRAW_FILLING, fill in the color array with two default colors if(this.Status()==BUFFER_STATUS_FILLING) { this.SetColor(clrBlue,0); this.SetColor(clrRed,1); } //--- Bind indicator buffers with arrays //--- In a loop by the number of indicator buffers int total=::ArraySize(DataBuffer); for(int i=0;i<total;i++) { //--- calculate the index of the next array and //--- bind the indicator buffer by the calculated index with the dynamic array //--- located by the i loop index in the DataBuffer array int index=(int)this.GetProperty(BUFFER_PROP_INDEX_BASE)+i; ::SetIndexBuffer(index,this.DataBuffer[i].Array,(this.TypeBuffer()==BUFFER_TYPE_DATA ? INDICATOR_DATA : INDICATOR_CALCULATIONS)); //--- Set indexation flag as in the timeseries to all buffer arrays ::ArraySetAsSeries(this.DataBuffer[i].Array,true); } //--- Bind the color buffer with the array (only for a non-calculated buffer and not for the filling buffer) if(this.Status()!=BUFFER_STATUS_FILLING && this.TypeBuffer()!=BUFFER_TYPE_CALCULATE) { ::SetIndexBuffer((int)this.GetProperty(BUFFER_PROP_INDEX_COLOR),this.ColorBufferArray,INDICATOR_COLOR_INDEX); ::ArraySetAsSeries(this.ColorBufferArray,true); } //--- If this is a calculated buffer, all is done if(this.TypeBuffer()==BUFFER_TYPE_CALCULATE) return; //--- Set integer parameters of the drawn buffer ::PlotIndexSetInteger((int)this.GetProperty(BUFFER_PROP_INDEX_PLOT),PLOT_DRAW_TYPE,(ENUM_PLOT_PROPERTY_INTEGER)this.GetProperty(BUFFER_PROP_DRAW_TYPE)); ::PlotIndexSetInteger((int)this.GetProperty(BUFFER_PROP_INDEX_PLOT),PLOT_ARROW,(ENUM_PLOT_PROPERTY_INTEGER)this.GetProperty(BUFFER_PROP_ARROW_CODE)); ::PlotIndexSetInteger((int)this.GetProperty(BUFFER_PROP_INDEX_PLOT),PLOT_ARROW_SHIFT,(ENUM_PLOT_PROPERTY_INTEGER)this.GetProperty(BUFFER_PROP_ARROW_SHIFT)); ::PlotIndexSetInteger((int)this.GetProperty(BUFFER_PROP_INDEX_PLOT),PLOT_DRAW_BEGIN,(ENUM_PLOT_PROPERTY_INTEGER)this.GetProperty(BUFFER_PROP_DRAW_BEGIN)); ::PlotIndexSetInteger((int)this.GetProperty(BUFFER_PROP_INDEX_PLOT),PLOT_SHOW_DATA,(ENUM_PLOT_PROPERTY_INTEGER)this.GetProperty(BUFFER_PROP_SHOW_DATA)); ::PlotIndexSetInteger((int)this.GetProperty(BUFFER_PROP_INDEX_PLOT),PLOT_SHIFT,(ENUM_PLOT_PROPERTY_INTEGER)this.GetProperty(BUFFER_PROP_SHIFT)); ::PlotIndexSetInteger((int)this.GetProperty(BUFFER_PROP_INDEX_PLOT),PLOT_LINE_STYLE,(ENUM_PLOT_PROPERTY_INTEGER)this.GetProperty(BUFFER_PROP_LINE_STYLE)); ::PlotIndexSetInteger((int)this.GetProperty(BUFFER_PROP_INDEX_PLOT),PLOT_LINE_WIDTH,(ENUM_PLOT_PROPERTY_INTEGER)this.GetProperty(BUFFER_PROP_LINE_WIDTH)); this.SetColor((color)this.GetProperty(BUFFER_PROP_COLOR)); //--- Set real buffer parameters ::PlotIndexSetDouble((int)this.GetProperty(BUFFER_PROP_INDEX_PLOT),PLOT_EMPTY_VALUE,this.GetProperty(BUFFER_PROP_EMPTY_VALUE)); //--- Set string buffer parameters ::PlotIndexSetString((int)this.GetProperty(BUFFER_PROP_INDEX_PLOT),PLOT_LABEL,this.GetProperty(BUFFER_PROP_LABEL)); } //+------------------------------------------------------------------+
この変更は、m_act_state_trigger変数をtrueに設定することに影響しました。これは、作成中にバッファがすでに処理されていることを意味します(たとえば、ボタンを押すと、「必要なアクションがバッファオブジェクトで実行されたかどうか」をチェックし、 実行されていない場合は実行する必要があります。設定されたフラグは、誤ったアクションの実行を許可しません)。
残りの改善点は、カラー配列を備えていない計算バッファがすでにあるという事実に関連しています。したがって、グラフィカルシリーズプロパティを設定する必要はありません。したがって、配列インデックスを計算するときに、バッファオブジェクトタイプが考慮されるようになりました。これが計算バッファである場合、適切な調整がインデックス計算に導入されます。指標バッファのグラフィカルシリーズ値を設定する前に、バッファオブジェクトタイプを確認します。これが計算バッファである場合は、メソッドを終了します。計算バッファにはグラフィカルな系列がありません。
バッファオブジェクトのグラフィカルシリーズプロパティを設定するメソッドについては、バッファタイプのチェックを導入します。これが計算バッファである場合は、メソッドをすぐに終了します。
//+------------------------------------------------------------------+ //| Set the number of initial bars | //| without drawing and values in DataWindow | //+------------------------------------------------------------------+ void CBuffer::SetDrawBegin(const int value) { if(this.TypeBuffer()==BUFFER_TYPE_CALCULATE) return; this.SetProperty(BUFFER_PROP_DRAW_BEGIN,value); ::PlotIndexSetInteger((int)this.GetProperty(BUFFER_PROP_INDEX_PLOT),PLOT_DRAW_BEGIN,value); } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| Set the number of colors | //+------------------------------------------------------------------+ void CBuffer::SetColorNumbers(const int number) { if(number>IND_COLORS_TOTAL || this.TypeBuffer()==BUFFER_TYPE_CALCULATE) return; int n=(this.Status()!=BUFFER_STATUS_FILLING ? number : 2); this.SetProperty(BUFFER_PROP_COLOR_INDEXES,n); ::ArrayResize(this.ArrayColors,n); ::PlotIndexSetInteger((int)this.GetProperty(BUFFER_PROP_INDEX_PLOT),PLOT_COLOR_INDEXES,n); } //+------------------------------------------------------------------+
計算バッファに必要のないすべてのメソッドに同様のチェックが導入されましたが、ここでは考慮しません。
バーインデックスを計算するときに指標バッファ値を設定する必要がある場合、結果の計算値がゼロ未満になることがあります。これは、現在のチャートと等しくないチャート期間のデータが表示されているときに、現在のチャートのバーインデックスを計算するときに発生します。計算されたインデックスの有効性に関する追加のチェックを回避するために、渡されたインデックスの負の値をチェックし、指定されたインデックスによって配列に値を書き込む責任があるメソッドでメソッドを終了する方がはるかに簡単です。
//+------------------------------------------------------------------+ //| Set the value to the specified timeseries index | //| for the specified data buffer array | //+------------------------------------------------------------------+ void CBuffer::SetBufferValue(const uint buffer_index,const uint series_index,const double value) { if(this.GetDataTotal(buffer_index)==0) return; int correct_buff_index=this.GetCorrectIndexBuffer(buffer_index); int data_total=this.GetDataTotal(buffer_index); int data_index=((int)series_index<data_total ? (int)series_index : data_total-1); if(data_index<0) return; this.DataBuffer[correct_buff_index].Array[data_index]=value; } //+------------------------------------------------------------------+ //| Set the color index to the specified timeseries index | //| of the color buffer array | //+------------------------------------------------------------------+ void CBuffer::SetBufferColorIndex(const uint series_index,const uchar color_index) { if(this.GetDataTotal(0)==0 || color_index>this.ColorsTotal()-1 || this.Status()==BUFFER_STATUS_FILLING || this.Status()==BUFFER_STATUS_NONE) return; int data_total=this.GetDataTotal(0); int data_index=((int)series_index<data_total ? (int)series_index : data_total-1); if(::ArraySize(this.ColorBufferArray)==0) ::Print(DFUN,CMessage::Text(MSG_LIB_SYS_ERROR),": ",CMessage::Text(MSG_LIB_TEXT_BUFFER_TEXT_INVALID_PROPERTY_BUFF)); if(data_index<0) return; this.ColorBufferArray[data_index]=color_index; } //+------------------------------------------------------------------+
また、クラスリストにいくつかの小さな変更を導入しました。コードのパフォーマンスに影響を与えないため、ここで説明しても意味がありません。以下に添付されているファイルですべての変更を確認できます。
\MQL5\Include\DoEasy\Collections\BuffersCollection.mqhの指標バッファのCBuffersCollectionコレクションクラスに追加します。
現在のチャート期間とは異なるチャート期間を処理する機能を導入するため、バッファコレクションクラスは時系列コレクションクラスにアクセスできる必要があります。時系列コレクションから、他の期間チャートのデータを使用して、現在のチャートの指標バッファ構築計算に必要なすべての時系列を取得します。
バッファコレクションクラスが時系列コレクションクラスにアクセスできるようにするには、時系列コレクションへのポインタをインジケータバッファのコレクションに渡すだけです。このポインタを使用して、クラスで受け取るために必要なデータとメソッドを選択しましょう。
すべてのバッファオブジェクトクラスファイルの定数名を変更するのと同様に、ここではBUFFER_PROP_INDEX_PLOT文字列のすべてのインスタンスがすでにBUFFER_PROP_INDEX_NEXT_PLOTに置き換えられていますが、IIndexNextBuffer()メソッドへのアクセスは名前が変更されたバージョンのIndexNextBaseBuffer()に置き換えられています。
計算バッファオブジェクトクラスファイルと時系列コレクションクラスファイルを指標バッファコレクションクラスファイルに含め、時系列コレクションクラスオブジェクトポインタを格納するためのprivateクラスメンバー変数を定義します。
//+------------------------------------------------------------------+ //| Include files | //+------------------------------------------------------------------+ #include "ListObj.mqh" #include "..\Objects\Indicators\BufferArrow.mqh" #include "..\Objects\Indicators\BufferLine.mqh" #include "..\Objects\Indicators\BufferSection.mqh" #include "..\Objects\Indicators\BufferHistogram.mqh" #include "..\Objects\Indicators\BufferHistogram2.mqh" #include "..\Objects\Indicators\BufferZigZag.mqh" #include "..\Objects\Indicators\BufferFilling.mqh" #include "..\Objects\Indicators\BufferBars.mqh" #include "..\Objects\Indicators\BufferCandles.mqh" #include "..\Objects\Indicators\BufferCalculate.mqh" #include "TimeSeriesCollection.mqh" //+------------------------------------------------------------------+ //| Collection of indicator buffers | //+------------------------------------------------------------------+ class CBuffersCollection : public CObject { private: CListObj m_list; // Buffer object list CTimeSeriesCollection *m_timeseries; // Pointer to the timeseries collection object
追加するメソッドは複数ありますが、それらのほとんどは、異なるバッファオブジェクトに対して同様の機能を備えています。したがって、各メソッドの説明を避けるために(特に、すべて適切なコメントで説明されているため)、クラス本体ですべての新しいメソッドを宣言して、それらの構造と原則を選択的に分析しましょう。
//+------------------------------------------------------------------+ //| Collection of indicator buffers | //+------------------------------------------------------------------+ class CBuffersCollection : public CObject { private: CListObj m_list; // Buffer object list CTimeSeriesCollection *m_timeseries; // Pointer to the timeseries collection object //--- Return the index of the (1) last, (2) next drawn and (3) basic buffer int GetIndexLastPlot(void); int GetIndexNextPlot(void); int GetIndexNextBase(void); //--- Create a new buffer object and place it to the collection list bool CreateBuffer(ENUM_BUFFER_STATUS status); //--- Get data of the necessary timeseries and bars for working with a single buffer bar, and return the number of bars int GetBarsData(CBuffer *buffer,const int series_index,int &index_bar_period); public: //--- Return (1) oneself and (2) the timeseries list CBuffersCollection *GetObject(void) { return &this; } CArrayObj *GetList(void) { return &this.m_list; } //--- Return the number of (1) drawn buffers, (2) all arrays used to build all buffers in the collection int PropertyPlotsTotal(void); int PropertyBuffersTotal(void); //--- Create the new buffer (1) "Drawing with arrows", (2) "Line", (3) "Sections", (4) "Histogram from the zero line", //--- (5) "Histogram on two indicator buffers", (6) "Zigzag", (7) "Color filling between two levels", //--- (8) "Display as bars", (9) "Display as candles", calculated buffer bool CreateArrow(void) { return this.CreateBuffer(BUFFER_STATUS_ARROW); } bool CreateLine(void) { return this.CreateBuffer(BUFFER_STATUS_LINE); } bool CreateSection(void) { return this.CreateBuffer(BUFFER_STATUS_SECTION); } bool CreateHistogram(void) { return this.CreateBuffer(BUFFER_STATUS_HISTOGRAM); } bool CreateHistogram2(void) { return this.CreateBuffer(BUFFER_STATUS_HISTOGRAM2); } bool CreateZigZag(void) { return this.CreateBuffer(BUFFER_STATUS_ZIGZAG); } bool CreateFilling(void) { return this.CreateBuffer(BUFFER_STATUS_FILLING); } bool CreateBars(void) { return this.CreateBuffer(BUFFER_STATUS_BARS); } bool CreateCandles(void) { return this.CreateBuffer(BUFFER_STATUS_CANDLES); } bool CreateCalculate(void) { return this.CreateBuffer(BUFFER_STATUS_NONE); } //--- Return the buffer by (1) the graphical series name, (2) timeframe, (2) Plot index and (3) collection list object CBuffer *GetBufferByLabel(const string plot_label); CBuffer *GetBufferByTimeframe(const ENUM_TIMEFRAMES timeframe); CBuffer *GetBufferByPlot(const int plot_index); CBuffer *GetBufferByListIndex(const int index_list); //--- Return buffers by their status by the specified serial number //--- (0 - the very first created buffer with the ХХХ drawing style, 1,2,N - subsequent ones) CBufferArrow *GetBufferArrow(const int number); CBufferLine *GetBufferLine(const int number); CBufferSection *GetBufferSection(const int number); CBufferHistogram *GetBufferHistogram(const int number); CBufferHistogram2 *GetBufferHistogram2(const int number); CBufferZigZag *GetBufferZigZag(const int number); CBufferFilling *GetBufferFilling(const int number); CBufferBars *GetBufferBars(const int number); CBufferCandles *GetBufferCandles(const int number); CBufferCalculate *GetBufferCalculate(const int number); //--- Initialize all drawn buffers by a (1) specified value, (2) empty value set for the buffer object void InitializePlots(const double value,const uchar color_index); void InitializePlots(void); //--- Initialize all calculated buffers by a (1) specified value, (2) empty value set for the buffer object void InitializeCalculates(const double value); void InitializeCalculates(void); //--- Set color values from the passed color array for all indicator buffers within the collection void SetColors(const color &array_colors[]); //--- Set the value by the timeseries index for the (1) arrow, (2) line, (3) section, (4) zero line histogram, //--- (5) two buffer histogram, (6) zigzag, (7) filling, (8) bar, (9) candle, (10) calculated buffer void SetBufferArrowValue(const int number,const int series_index,const double value,const uchar color_index,bool as_current=false); void SetBufferLineValue(const int number,const int series_index,const double value,const uchar color_index,bool as_current=false); void SetBufferSectionValue(const int number,const int series_index,const double value,const uchar color_index,bool as_current=false); void SetBufferHistogramValue(const int number,const int series_index,const double value,const uchar color_index,bool as_current=false); void SetBufferHistogram2Value(const int number,const int series_index,const double value1,const double value2,const uchar color_index,bool as_current=false); void SetBufferZigZagValue(const int number,const int series_index,const double value1,const double value2,const uchar color_index,bool as_current=false); void SetBufferFillingValue(const int number,const int series_index,const double value1,const double value2,bool as_current=false); void SetBufferBarsValue(const int number,const int series_index,const double open,const double high,const double low,const double close,const uchar color_index,bool as_current=false); void SetBufferCandlesValue(const int number,const int series_index,const double open,const double high,const double low,const double close,const uchar color_index,bool as_current=false); void SetBufferCalculateValue(const int number,const int series_index,const double value); //--- Set the color index to the color buffer by its serial number of //--- (1) arrows, (2) lines, (3) sections, (4) zero histogram //--- (5) histogram on two buffers, (6) zigzag, (7) filling, (8) bars and (9) candles //--- (0 - the very first created buffer with the ХХХ drawing style, 1,2,N - subsequent ones) void SetBufferArrowColorIndex(const int number,const int series_index,const uchar color_index); void SetBufferLineColorIndex(const int number,const int series_index,const uchar color_index); void SetBufferSectionColorIndex(const int number,const int series_index,const uchar color_index); void SetBufferHistogramColorIndex(const int number,const int series_index,const uchar color_index); void SetBufferHistogram2ColorIndex(const int number,const int series_index,const uchar color_index); void SetBufferZigZagColorIndex(const int number,const int series_index,const uchar color_index); void SetBufferFillingColorIndex(const int number,const int series_index,const uchar color_index); void SetBufferBarsColorIndex(const int number,const int series_index,const uchar color_index); void SetBufferCandlesColorIndex(const int number,const int series_index,const uchar color_index); //--- Clear buffer data by its index in the list in the specified timeseries bar void Clear(const int buffer_list_index,const int series_index); //--- Clear data by the timeseries index for the (1) arrow, (2) line, (3) section, (4) zero line histogram, //--- (5) histogram on two buffers, (6) zigzag, (7) filling, (8) bars and (9) candles void ClearBufferArrow(const int number,const int series_index); void ClearBufferLine(const int number,const int series_index); void ClearBufferSection(const int number,const int series_index); void ClearBufferHistogram(const int number,const int series_index); void ClearBufferHistogram2(const int number,const int series_index); void ClearBufferZigZag(const int number,const int series_index); void ClearBufferFilling(const int number,const int series_index); void ClearBufferBars(const int number,const int series_index); void ClearBufferCandles(const int number,const int series_index); //--- Constructor CBuffersCollection(); //--- Get pointers to the timeseries collection (the method is called in the CollectionOnInit() method of the CEngine object) void OnInit(CTimeSeriesCollection *timeseries) { this.m_timeseries=timeseries; } }; //+------------------------------------------------------------------+
新しく追加されたメソッドの割り当てについて考えてみましょう。メソッドの割り当てについて説明している間、ライブラリクラスのほとんどのメソッドと同様に、クラス本体の外部に移動された実装についてすぐに検討します。
GetIndexLastPlot()は、最後に作成されたバッファオブジェクトの一連のグラフィカルなインデックスを返します。
//+------------------------------------------------------------------+ //| Return the index of the last drawn buffer | //+------------------------------------------------------------------+ int CBuffersCollection::GetIndexLastPlot(void) { //--- Return the pointer to the list. If the list is not created for some reason, return -1 CArrayObj *list=this.GetList(); if(list==NULL) return WRONG_VALUE; //--- Get the index of the drawn buffer with the highest value. If the FindBufferMax() method returns -1, //--- the list is empty, return index 0 for the very first buffer in the list int index=CSelect::FindBufferMax(list,BUFFER_PROP_INDEX_PLOT); if(index==WRONG_VALUE) return 0; //--- if the index is not -1, //--- get the buffer object from the list by its index CBuffer *buffer=this.m_list.At(index); if(buffer==NULL) return WRONG_VALUE; //--- Return the Plot index of the buffer object return buffer.IndexPlot(); } //+------------------------------------------------------------------+
このメソッドは非常に特殊であり、返される値はバッファリストのサイズに関連付けられているため、主に新しいバッファを作成するメソッドを対象としています(リストが空の場合、-1ではなくゼロが返されます )。このメソッドが後で他の目的に必要であることが判明した場合は、少し修正します。現在のアプリケーション以外で不要になった場合、メソッドはprivateになります。
GetBarsData() privateメソッドは、他の時系列(バッファオブジェクトに対して非ネイティブ)からデータを取得するために使用され、バッファオブジェクトチャート期間の1つのバーに含まれる現在の時間枠のバーの数を返します。
//+------------------------------------------------------------------+ //| Get data of the necessary timeseries and bars | //| for working with a single bar of the buffer | //+------------------------------------------------------------------+ int CBuffersCollection::GetBarsData(CBuffer *buffer,const int series_index,int &index_bar_period) { //--- Get timeseries of the current chart and the chart of the buffer timeframe CSeriesDE *series_current=this.m_timeseries.GetSeries(buffer.Symbol(),PERIOD_CURRENT); CSeriesDE *series_period=this.m_timeseries.GetSeries(buffer.Symbol(),buffer.Timeframe()); if(series_current==NULL || series_period==NULL) return WRONG_VALUE; //--- Get the bar object of the current timeseries corresponding to the required timeseries index CBar *bar_current=series_current.GetBar(series_index); if(bar_current==NULL) return WRONG_VALUE; //--- Get the timeseries bar object of the buffer chart period corresponding to the time the timeseries bar of the current chart falls into CBar *bar_period=m_timeseries.GetBarSeriesFirstFromSeriesSecond(NULL,PERIOD_CURRENT,bar_current.Time(),NULL,series_period.Timeframe()); if(bar_period==NULL) return WRONG_VALUE; //--- Write down the bar index on the current timeframe which falls into the bar start time of the buffer object chart index_bar_period=bar_period.Index(PERIOD_CURRENT); //--- Calculate the amount of bars of the current timeframe included into one bar of the buffer object chart period //--- and return this value (1 if the result is 0) int num_bars=::PeriodSeconds(bar_period.Timeframe())/::PeriodSeconds(bar_current.Timeframe()); return(num_bars>0 ? num_bars : 1); } //+------------------------------------------------------------------+
このメソッドは、現在のチャートのバーを計算するために広く使用されます。この場合、現在の時間枠とは異なるチャート期間のデータを使用して指標バッファの値を書き込む必要があります。
以前のPlotsTotal()メソッドとBuffersTotal()メソッドの名前が PropertyPlotsTotal()とPropertyBuffersTotal()に変更され、わかりやすくなっています。指標プログラムのプロパティとして指定するために、#propertyindicator_plotsと#propertyindicator_buffersに正しい値を返します。
PropertyBuffersTotal()メソッドでは、定数名も名前が変更された名前に置き換えられました(BUFFER_PROP_INDEX_NEXTはBUFFER_PROP_INDEX_NEXT_BASEに置き換えられました)。
計算バッファの出現により、PropertyPlotsTotal()メソッドが書き直されました。したがって、データは別の方法で取得する必要があります。
//+------------------------------------------------------------------+ //| Return the number of drawn buffers | //+------------------------------------------------------------------+ int CBuffersCollection::PropertyPlotsTotal(void) { CArrayObj *list=CSelect::ByBufferProperty(this.GetList(),BUFFER_PROP_TYPE,BUFFER_TYPE_DATA,EQUAL); return(list!=NULL ? list.Total() : WRONG_VALUE); } //+------------------------------------------------------------------+
描画のみのバッファオブジェクトのリストを取得し、そのサイズを返します。リストが空の場合、-1が返されます。
CreateCalculate()は計算された(描画されていない)バッファのオブジェクトを作成するのに役立ちます。
このメソッドはクラス本体に直接実装され、新しいバッファを作成するためのメソッドの結果を返します(前の記事で考察済み)。計算バッファステータスは
bool CreateCalculate(void) { return this.CreateBuffer(BUFFER_STATUS_NONE); }
に渡されます。GetBufferByLabel()メソッドは、グラフィカルなシリーズ名でバッファオブジェクトへのポインタを返します。
//+------------------------------------------------------------------+ //| Return the buffer by the graphical series name | //+------------------------------------------------------------------+ CBuffer *CBuffersCollection::GetBufferByLabel(const string plot_label) { CArrayObj *list=CSelect::ByBufferProperty(this.GetList(),BUFFER_PROP_LABEL,plot_label,EQUAL); return(list!=NULL && list.Total()>0 ? list.At(list.Total()-1) : NULL); } //+------------------------------------------------------------------+
「グラフィカルシリーズ名」がメソッドに渡された値に対応するバッファオブジェクトのリストを取得し、リストの最後のオブジェクトへのポインタを返します。リストが空の場合は、NULLを返します。同じグラフィカルシリーズ名を持つバッファオブジェクトが複数ある場合、メソッドはそのような名前で作成された最後のバッファオブジェクトを返します。
GetBufferByTimeframe()メソッドは、設定された時間枠でバッファオブジェクトへのポインタを返します。
//+------------------------------------------------------------------+ //| Return the buffer by timeframe | //+------------------------------------------------------------------+ CBuffer *CBuffersCollection::GetBufferByTimeframe(const ENUM_TIMEFRAMES timeframe) { CArrayObj *list=CSelect::ByBufferProperty(this.GetList(),BUFFER_PROP_TIMEFRAME,timeframe,EQUAL); return(list!=NULL && list.Total()>0 ? list.At(list.Total()-1) : NULL); } //+------------------------------------------------------------------+
「時間枠」がメソッドに渡された値に対応するバッファオブジェクトのリストを取得し、リストの最後のオブジェクトへのポインタを返します。リストが空の場合は、NULLを返します。同じ時間枠のバッファオブジェクトが複数ある場合、メソッドは同じグラフ期間で作成された最後のバッファオブジェクトを返します。
GetBufferByListIndex()メソッドは、コレクションリスト内のインデックスによってバッファオブジェクトへのポインタを返します。
//+------------------------------------------------------------------+ //| Return the buffer by the collection list index | //+------------------------------------------------------------------+ CBuffer *CBuffersCollection::GetBufferByListIndex(const int index_list) { return this.m_list.At(index_list); } //+------------------------------------------------------------------+
バッファコレクションリストの最後のオブジェクトへのポインタを返すだけです。リストが空の場合、At()メソッドはNULLを返します。
GetBufferCalculate()メソッドは、シリアル番号(計算バッファを作成する順序)でバッファオブジェクトへのポインタを返します。
//+------------------------------------------------------------------+ //|Return the calculated buffer by serial number | //| (0 - the very first candle buffer, 1,2,N - subsequent ones) | //+------------------------------------------------------------------+ CBufferCalculate *CBuffersCollection::GetBufferCalculate(const int number) { CArrayObj *list=CSelect::ByBufferProperty(this.GetList(),BUFFER_PROP_TYPE,BUFFER_TYPE_CALCULATE,EQUAL); return(list!=NULL && list.Total()>0 ? list.At(number) : NULL); } //+------------------------------------------------------------------+
バッファオブジェクトコレクションリストから、「計算バッファ」タイプのバッファのみを取得し、取得したリストからメソッドに渡されたインデックスによってオブジェクトへのポインタを返します。結果の並び替えられたリストが空であるか、メソッドに渡された必要なオブジェクトのインデックスが取得されたリストを超える場合、At()メソッドはNULLを返します。
InitializePlots()でオーバーロードされたメソッドは、コレクション内のすべての描画バッファを初期化するために使用されます。
//+------------------------------------------------------------------+ //| Initialize all drawn buffers by a specified empty value | //+------------------------------------------------------------------+ void CBuffersCollection::InitializePlots(const double value,const uchar color_index) { CArrayObj *list=CSelect::ByBufferProperty(this.GetList(),BUFFER_PROP_TYPE,BUFFER_TYPE_DATA,EQUAL); if(list==NULL || list.Total()==0) return; int total=list.Total(); for(int i=0;i<total;i++) { CBuffer *buff=list.At(i); if(buff==NULL) continue; buff.InitializeAll(value,color_index); } } //+------------------------------------------------------------------+
このメソッドは、パラメータで渡されたバッファとカラーインデックスの値を使用して、描画されたすべてのコレクションバッファを初期化します。
最初に、描画されたバッファのみがリストから選択されます。 次に、取得したリストによってループ内の次のバッファオブジェクトを取得し、InitializeAll()バッファオブジェクトクラスメソッドを使用してそのすべての配列を初期化し、メソッド入力を渡します。
//+------------------------------------------------------------------+ //| Initialize all drawn buffers using | //| the empty value set for the buffer object | //+------------------------------------------------------------------+ void CBuffersCollection::InitializePlots(void) { CArrayObj *list=CSelect::ByBufferProperty(this.GetList(),BUFFER_PROP_TYPE,BUFFER_TYPE_DATA,EQUAL); if(list==NULL || list.Total()==0) return; int total=list.Total(); for(int i=0;i<total;i++) { CBuffer *buff=list.At(i); if(buff==NULL) continue; buff.InitializeAll(); } } //+------------------------------------------------------------------+
このメソッドは、各バッファオブジェクトに設定されたバッファ値とゼロに等しいカラーインデックス値を使用して、描画されたすべてのコレクションバッファを初期化します。
最初に、描画されたバッファのみがリストから選択されます。次に、取得したリストによってループ内の次のバッファオブジェクトを取得し、パラメータなしでInitializeAll()バッファオブジェクトクラスメソッドを使用してすべての配列を初期化します。
InitializeCalculates()でオーバーロードされたメソッドは、コレクション内のすべての計算バッファを初期化するために使用されます。
//+------------------------------------------------------------------+ //| Initialize all calculated buffers by a specified empty value | //+------------------------------------------------------------------+ void CBuffersCollection::InitializeCalculates(const double value) { CArrayObj *list=CSelect::ByBufferProperty(this.GetList(),BUFFER_PROP_TYPE,BUFFER_TYPE_CALCULATE,EQUAL); if(list==NULL || list.Total()==0) return; int total=list.Total(); for(int i=0;i<total;i++) { CBuffer *buff=list.At(i); if(buff==NULL) continue; buff.InitializeAll(value,0); } } //+------------------------------------------------------------------+
このメソッドは、パラメータで渡されたバッファ値を使用して、計算されたすべてのコレクションバッファを初期化します。
計算されたバッファのみが最初にリストに選択されます。次に、取得したリストによってループ内の次のバッファオブジェクトを取得し、InitializeAll()バッファオブジェクトクラスメソッドを使用してそのすべての配列を初期化し、メソッド入力を渡します。
//+------------------------------------------------------------------+ //| Initialize all calculated buffers using | //| the empty value set for the buffer object | //+------------------------------------------------------------------+ void CBuffersCollection::InitializeCalculates(void) { CArrayObj *list=CSelect::ByBufferProperty(this.GetList(),BUFFER_PROP_TYPE,BUFFER_TYPE_CALCULATE,EQUAL); if(list==NULL || list.Total()==0) return; int total=list.Total(); for(int i=0;i<total;i++) { CBuffer *buff=list.At(i); if(buff==NULL) continue; buff.InitializeAll(); } } //+------------------------------------------------------------------+
このメソッドは、渡されたバッファ値を使用して、計算されたすべてのコレクションバッファを初期化します。
計算されたバッファのみが最初にリストに選択されます。次に、取得したリストによってループ内の次のバッファオブジェクトを取得し、パラメータなしでInitializeAll() バッファオブジェクトクラスメソッドを使用してその配列を初期化します。
SetColors()メソッドは、描画されたすべてのコレクションバッファのカラー配列からカラー値を設定します。
//+------------------------------------------------------------------+ //| Set color values from the passed color array | //| for all collection indicator buffers | //+------------------------------------------------------------------+ void CBuffersCollection::SetColors(const color &array_colors[]) { CArrayObj *list=CSelect::ByBufferProperty(this.GetList(),BUFFER_PROP_TYPE,BUFFER_TYPE_DATA,EQUAL); if(list==NULL || list.Total()==0) return; int total=list.Total(); for(int i=0;i<total;i++) { CBuffer *buff=list.At(i); if(buff==NULL) continue; buff.SetColors(array_colors); } } //+------------------------------------------------------------------+
このメソッドは、描画されたすべてのコレクションバッファの配列パラメータで渡される色の値を設定します。
最初に、描画されたバッファのみがリストから選択されます。次に、取得したリストによってループ内の次のバッファオブジェクトを取得し、メソッド入力に送信された配列を渡すSetColors()バッファオブジェクトクラスメソッドを使用して、その色のセットを指定します。
Clear()メソッドは、指定された時系列バーのコレクションリスト内のインデックスによってバッファデータを消去します。
//+------------------------------------------------------------------+ //| Clear buffer data by its index in the list | //| in the specified timeseries bar | //+------------------------------------------------------------------+ void CBuffersCollection::Clear(const int buffer_list_index,const int series_index) { CBuffer *buff=this.GetBufferByListIndex(buffer_list_index); if(buff==NULL) return; buff.ClearData(series_index); } //+------------------------------------------------------------------+
まず、上記で検討したGetBufferByListIndex()メソッドを使用して、リスト内のインデックスによってバッファオブジェクトへのポインタを取得します。次に、取得したClearData()バッファオブジェクトのメソッドを使用して、そのすべての配列を消去します。
他の時間枠のデータを使用して現在のチャートに指標バッファラインを描画するときに必要なバーインデックスデータを計算できるようにするには、時系列コレクションクラスにあるすべての使用されている時系列のコレクションリストへのポインタをコレクションクラスに渡す必要があります。
これを行う最も簡単な方法は、ライブラリクラスのすべてのオブジェクトがすでに作成されているプログラムの初期化中です。
OnInit()クラスメソッドは、渡された時系列コレクションクラスへのポインタをm_timeseries変数に割り当てます。
void OnInit(CTimeSeriesCollection *timeseries) { this.m_timeseries=timeseries; }
さらに、プログラム内のライブラリ初期化関数でメソッドを呼び出します。私はすでにそのために必要なすべての機能を持っています。以下では、必要なCEngineクラスメソッドにメソッドの呼び出しを追加するだけです。
このクラスには、バッファタイプ(矢印、線など)ごとにインジケーターバッファの値を設定するメソッド、およびこれらのバッファのカラーインデックスを設定し、すべてのバッファ配列を消去するメソッドが既に備わっています。全部で28個のメソッドを追加しました。ここでそれぞれを説明する意味はありません—すべての方法は以下に添付されたファイルに示されています。それらは論理的には互いに同一です。唯一の違いは、異なるタイプのバッファが持つ配列の数です。したがって、ここでは矢印バッファメソッドのみを検討します。
以下は、矢印バッファ時系列インデックスで値を設定するメソッドです。
//+------------------------------------------------------------------+ //| Set the value to the arrow buffer by the timeseries index | //+------------------------------------------------------------------+ void CBuffersCollection::SetBufferArrowValue(const int number,const int series_index,const double value,const uchar color_index,bool as_current=false) { //--- Get the arrow buffer object CBufferArrow *buff=this.GetBufferArrow(number); if(buff==NULL) return; //--- If the buffer usage flag is set only as for the current timeframe, //--- write the passed value to the current buffer bar and exit (the color is not used) if(as_current) { buff.SetBufferValue(0,series_index,value); return; } //--- Get data on the necessary timeseries and bars, and calculate the amount of bars of the current timeframe included into one bar of the buffer object chart period int index_bar_period=series_index; int num_bars=this.GetBarsData(buff,series_index,index_bar_period); if(num_bars==WRONG_VALUE) return; //--- Calculate the index of the next bar for the current chart in the loop by the number of bars and //--- set the value and color passed to the method by the calculated index for(int i=0;i<num_bars;i++) { int index=index_bar_period-i; if(index<0) break; buff.SetBufferValue(0,index,value); buff.SetBufferColorIndex(index,color_index); } } //+------------------------------------------------------------------+
メソッドのロジックは、コードのコメントで説明されています。ただし、いくつかのニュアンスを明確にしましょう。
このメソッドは、矢印バッファオブジェクトの番号を受け取ります。番号は、作成されたすべての矢印バッファで構成されるシリアル番号を意味します。カスタムプログラムで作成した最初の矢印バッファの数は0、2番目の矢印バッファは1、3番目の矢印バッファは2などです。作成されたバッファオブジェクトの数を、コレクションリスト内のバッファオブジェクトインデックスと混同しないでください。新しく追加されたバッファのインデックスは常に昇順で表示され、バッファオブジェクトタイプに依存しません。バッファオブジェクトの数は、バッファタイプによって異なります。この概念については、記事の冒頭で詳しく説明しました。
このメソッドは、次のパラメータによっても渡される値を受け入れる時系列バーインデックス、指定された時系列インデックスによって指標バッファラインを描画するのに使用される色のインデックス、および「as for the current timeseries」追加パラメータを受け取ります。
メソッドは、バッファオブジェクトの「timeframe」プロパティ値が現在のチャート期間と異なる場合、メソッドに渡される時系列インデックスに関係なく、バッファオブジェクトのタイムフレームデータを考慮して現在のチャートにインジケータラインが描画されるように配置されます。つまりas_currentフラグの値がfalse(デフォルト)の場合、このメソッドは現在のチャート期間の他のチャート期間のデータを正しく表示します。フラグがtrueに設定されている場合、このメソッドは、バッファオブジェクトのプロパティに別の指定されたグラフ期間があるかどうかに関係なく、現在の時間枠データでのみ機能します。
以下は、矢印バッファオブジェクトのカラーインデックスをシリアル番号によってカラーバッファ配列に設定するメソッドです。
//+------------------------------------------------------------------+ //| Set the color index to the color buffer | //| of the arrow buffer by the timeseries index | //+------------------------------------------------------------------+ void CBuffersCollection::SetBufferArrowColorIndex(const int number,const int series_index,const uchar color_index) { CBufferArrow *buff=this.GetBufferArrow(number); if(buff==NULL) return; buff.SetBufferColorIndex(series_index,color_index); } //+------------------------------------------------------------------+
ここではすべてが簡単です。矢印バッファオブジェクトをそのインデックスで取得し、指定されたカラーインデックスを渡された時系列インデックスに設定します。
以下は、矢印バッファ時系列インデックスによってデータを消去するメソッドです。
//+------------------------------------------------------------------+ //| Clear the arrow buffer data by the timeseries index | //+------------------------------------------------------------------+ void CBuffersCollection::ClearBufferArrow(const int number,const int series_index) { CBufferArrow *buff=this.GetBufferArrow(number); if(buff==NULL) return; buff.SetBufferValue(0,series_index,buff.EmptyValue()); } //+------------------------------------------------------------------+
ここでは、矢印バッファオブジェクトをそのインデックスで取得し、バッファオブジェクトに指定された「空の」値を渡された時系列インデックスに設定します。
他のバッファオブジェクトタイプを操作するための残りのすべてのメソッドは、配列の数とバッファオブジェクトタイプを除いて、上記のメソッドと基本的に異なりません。ご自分でお調べください。
これで、バッファオブジェクトコレクションクラスの改善は完了です。
以下に添付されているファイルですべての変更を含む完全なコードを確認できます。
\MQL5\Include\DoEasy\Engine.mqhでCEngine ライブラリのメインオブジェクトクラスを補完および改善しましょう。バッファコレクションクラスの改善点を説明するときと同じように進めます。最初に、クラス本体の追加と変更を確認してから、各メソッドを分析します。
完全なコードは非常に大量であるため、ここに表示しても意味がありません。代わりに、変更と追加が実装された抜粋を表示します。
//+------------------------------------------------------------------+ //| Library basis class | //+------------------------------------------------------------------+ class CEngine { private: //--- The code has been removed for the sake of space //--- ... public: //--- The code has been removed for the sake of space //--- ... //--- Return the bar type of the specified timeframe's symbol by (1) index and (2) time ENUM_BAR_BODY_TYPE SeriesBarType(const string symbol,const ENUM_TIMEFRAMES timeframe,const int index); ENUM_BAR_BODY_TYPE SeriesBarType(const string symbol,const ENUM_TIMEFRAMES timeframe,const datetime time); //--- Copy the specified double property of the specified timeseries of the specified symbol to the array //--- Regardless of the array indexing direction, copying is performed the same way as copying to a timeseries array bool SeriesCopyToBufferAsSeries(const string symbol,const ENUM_TIMEFRAMES timeframe,const ENUM_BAR_PROP_DOUBLE property, double &array[],const double empty=EMPTY_VALUE) { return this.m_time_series.CopyToBufferAsSeries(symbol,timeframe,property,array,empty);} //--- Return (1) the buffer collection and (2) the buffer list from the collection CBuffersCollection *GetBuffersCollection(void) { return &this.m_buffers; } CArrayObj *GetListBuffers(void) { return this.m_buffers.GetList(); } //--- Return the buffer by (1) the graphical series name, (2) timeframe, (3) Plot index, (4) collection list and (5) the last one in the list CBuffer *GetBufferByLabel(const string plot_label) { return this.m_buffers.GetBufferByLabel(plot_label); } CBuffer *GetBufferByTimeframe(const ENUM_TIMEFRAMES timeframe) { return this.m_buffers.GetBufferByTimeframe(timeframe);} CBuffer *GetBufferByPlot(const int plot_index) { return this.m_buffers.GetBufferByPlot(plot_index); } CBuffer *GetBufferByListIndex(const int index_list) { return this.m_buffers.GetBufferByListIndex(index_list);} CBuffer *GetLastBuffer(void); //--- Return buffers by drawing style by a serial number //--- (0 - the very first created buffer with the ХХХ drawing style, 1,2,N - subsequent ones) CBufferArrow *GetBufferArrow(const int number) { return this.m_buffers.GetBufferArrow(number); } CBufferLine *GetBufferLine(const int number) { return this.m_buffers.GetBufferLine(number); } CBufferSection *GetBufferSection(const int number) { return this.m_buffers.GetBufferSection(number); } CBufferHistogram *GetBufferHistogram(const int number) { return this.m_buffers.GetBufferHistogram(number); } CBufferHistogram2 *GetBufferHistogram2(const int number) { return this.m_buffers.GetBufferHistogram2(number); } CBufferZigZag *GetBufferZigZag(const int number) { return this.m_buffers.GetBufferZigZag(number); } CBufferFilling *GetBufferFilling(const int number) { return this.m_buffers.GetBufferFilling(number); } CBufferBars *GetBufferBars(const int number) { return this.m_buffers.GetBufferBars(number); } CBufferCandles *GetBufferCandles(const int number) { return this.m_buffers.GetBufferCandles(number); } CBufferCalculate *GetBufferCalculate(const int number) { return this.m_buffers.GetBufferCalculate(number); } //--- Return the number of (1) drawn buffers and (2) all indicator arrays int BuffersPropertyPlotsTotal(void) { return this.m_buffers.PropertyPlotsTotal(); } int BuffersPropertyBuffersTotal(void) { return this.m_buffers.PropertyBuffersTotal(); } //--- Create the new buffer (1) "Drawing with arrows", (2) "Line", (3) "Sections", (4) "Histogram from the zero line", //--- (5) "Histogram on two indicator buffers", (6) "Zigzag", (7) "Color filling between two levels", //--- (8) "Display as bars", (9) "Display as candles", calculated buffer bool BufferCreateArrow(void) { return this.m_buffers.CreateArrow(); } bool BufferCreateLine(void) { return this.m_buffers.CreateLine(); } bool BufferCreateSection(void) { return this.m_buffers.CreateSection(); } bool BufferCreateHistogram(void) { return this.m_buffers.CreateHistogram(); } bool BufferCreateHistogram2(void) { return this.m_buffers.CreateHistogram2(); } bool BufferCreateZigZag(void) { return this.m_buffers.CreateZigZag(); } bool BufferCreateFilling(void) { return this.m_buffers.CreateFilling(); } bool BufferCreateBars(void) { return this.m_buffers.CreateBars(); } bool BufferCreateCandles(void) { return this.m_buffers.CreateCandles(); } bool BufferCreateCalculate(void) { return this.m_buffers.CreateCalculate(); } //--- Initialize all drawn buffers by a (1) specified value, (2) empty value set for the buffer object void BuffersInitPlots(const double value,const uchar color_index) { this.m_buffers.InitializePlots(value,color_index); } void BuffersInitPlots(void) { this.m_buffers.InitializePlots(); } //--- Initialize all calculated buffers by a (1) specified value, (2) empty value set for the buffer object void BuffersInitCalculates(const double value) { this.m_buffers.InitializeCalculates(value); } void BuffersInitCalculates(void) { this.m_buffers.InitializeCalculates(); } //--- Return buffer data by its serial number of (1) arrows, (2) line, (3) sections and (4) histogram from zero //--- (0 - the very first created buffer with the ХХХ drawing style, 1,2,N - subsequent ones) double BufferDataArrow(const int number,const int series_index); double BufferDataLine(const int number,const int series_index); double BufferDataSection(const int number,const int series_index); double BufferDataHistogram(const int number,const int series_index); //--- Return buffer data by its serial number of (1) the zero and (2) the first histogram buffer on two buffers //--- (0 - the very first created buffer with the ХХХ drawing style, 1,2,N - subsequent ones) double BufferDataHistogram20(const int number,const int series_index); double BufferDataHistogram21(const int number,const int series_index); //--- Return buffer data by its serial number of (1) the zero and (2) the first zigzag buffer //--- (0 - the very first created buffer with the ХХХ drawing style, 1,2,N - subsequent ones) double BufferDataZigZag0(const int number,const int series_index); double BufferDataZigZag1(const int number,const int series_index); //--- Return buffer data by its serial number of (1) the zero and (2) the first filling buffer //--- (0 - the very first created buffer with the ХХХ drawing style, 1,2,N - subsequent ones) double BufferDataFilling0(const int number,const int series_index); double BufferDataFilling1(const int number,const int series_index); //--- Return buffer data by its serial number of (1) Open, (2) High, (3) Low and (4) Close bar buffers //--- (0 - the very first created buffer with the ХХХ drawing style, 1,2,N - subsequent ones) double BufferDataBarsOpen(const int number,const int series_index); double BufferDataBarsHigh(const int number,const int series_index); double BufferDataBarsLow(const int number,const int series_index); double BufferDataBarsClose(const int number,const int series_index); //--- Return buffer data by its serial number of (1) Open, (2) High, (3) Low and (4) Close candle buffers //--- (0 - the very first created buffer with the ХХХ drawing style, 1,2,N - subsequent ones) double BufferDataCandlesOpen(const int number,const int series_index); double BufferDataCandlesHigh(const int number,const int series_index); double BufferDataCandlesLow(const int number,const int series_index); double BufferDataCandlesClose(const int number,const int series_index); //--- Set buffer data by its serial number of (1) arrows, (2) line, (3) sections, (4) histogram from zero and the (5) calculated buffer //--- (0 - the very first created buffer with the ХХХ drawing style, 1,2,N - subsequent ones) void BufferSetDataArrow(const int number,const int series_index,const double value,const uchar color_index,bool as_current=false); void BufferSetDataLine(const int number,const int series_index,const double value,const uchar color_index,bool as_current=false); void BufferSetDataSection(const int number,const int series_index,const double value,const uchar color_index,bool as_current=false); void BufferSetDataHistogram(const int number,const int series_index,const double value,const uchar color_index,bool as_current=false); void BufferSetDataCalculate(const int number,const int series_index,const double value); //--- Set data of the (1) zero, (2) first and (3) all histogram buffers on two buffers by a serial number of a created buffer //--- (0 - the very first created buffer with the HISTOGRAM2 drawing style, 1,2,N - subsequent ones) void BufferSetDataHistogram20(const int number,const int series_index,const double value); void BufferSetDataHistogram21(const int number,const int series_index,const double value); void BufferSetDataHistogram2(const int number,const int series_index,const double value0,const double value1,const uchar color_index,bool as_current=false); //--- Set data of the (1) zero, (2) first and (3) all zigzag buffers by a serial number of a created buffer //--- (0 - the very first created buffer with the ZIGZAG drawing style, 1,2,N - subsequent ones) void BufferSetDataZigZag0(const int number,const int series_index,const double value); void BufferSetDataZigZag1(const int number,const int series_index,const double value); void BufferSetDataZigZag(const int number,const int series_index,const double value0,const double value1,const uchar color_index,bool as_current=false); //--- Set data of the (1) zero, (2) first and (3) all filling buffers by a serial number of a created buffer //--- (0 - the very first created buffer with the FILLING drawing style, 1,2,N - subsequent ones) void BufferSetDataFilling0(const int number,const int series_index,const double value); void BufferSetDataFilling1(const int number,const int series_index,const double value); void BufferSetDataFilling(const int number,const int series_index,const double value0,const double value1,bool as_current=false); //--- Set data of the (1) Open, (2) High, (3) Low, (4) Close and (5) all bar buffers by a serial number of a created buffer //--- (0 - the very first created buffer with the BARS drawing style, 1,2,N - subsequent ones) void BufferSetDataBarsOpen(const int number,const int series_index,const double value); void BufferSetDataBarsHigh(const int number,const int series_index,const double value); void BufferSetDataBarsLow(const int number,const int series_index,const double value); void BufferSetDataBarsClose(const int number,const int series_index,const double value); void BufferSetDataBars(const int number,const int series_index,const double open,const double high,const double low,const double close,const uchar color_index,bool as_current=false); //--- Set data of the (1) Open, (2) High, (3) Low, (4) Close and (5) all candle buffers by a serial number of a created buffer //--- (0 - the very first created buffer with the CANDLES drawing style, 1,2,N - subsequent ones) void BufferSetDataCandlesOpen(const int number,const int series_index,const double value); void BufferSetDataCandlesHigh(const int number,const int series_index,const double value); void BufferSetDataCandlesLow(const int number,const int series_index,const double value); void BufferSetDataCandlesClose(const int number,const int series_index,const double value); void BufferSetDataCandles(const int number,const int series_index,const double open,const double high,const double low,const double close,const uchar color_index,bool as_current=false); //--- Return buffer color by its serial number of (1) arrows, (2) line, (3) sections, (4) histogram from zero //--- (5) histogram on two buffers, (6) zigzag, (7) filling, (8) bars and (9) candles //--- (0 - the very first created buffer with the ХХХ drawing style, 1,2,N - subsequent ones) color BufferArrowColor(const int number,const int series_index); color BufferLineColor(const int number,const int series_index); color BufferSectionColor(const int number,const int series_index); color BufferHistogramColor(const int number,const int series_index); color BufferHistogram2Color(const int number,const int series_index); color BufferZigZagColor(const int number,const int series_index); color BufferFillingColor(const int number,const int series_index); color BufferBarsColor(const int number,const int series_index); color BufferCandlesColor(const int number,const int series_index); //--- Return buffer color index by its serial number of (1) arrows, (2) line, (3) sections, (4) histogram from zero //--- (5) histogram on two buffers, (6) zigzag, (7) filling, (8) bars and (9) candles //--- (0 - the very first created buffer with the ХХХ drawing style, 1,2,N - subsequent ones) int BufferArrowColorIndex(const int number,const int series_index); int BufferLineColorIndex(const int number,const int series_index); int BufferSectionColorIndex(const int number,const int series_index); int BufferHistogramColorIndex(const int number,const int series_index); int BufferHistogram2ColorIndex(const int number,const int series_index); int BufferZigZagColorIndex(const int number,const int series_index); int BufferFillingColorIndex(const int number,const int series_index); int BufferBarsColorIndex(const int number,const int series_index); int BufferCandlesColorIndex(const int number,const int series_index); //--- Set color values from the passed color array for all indicator buffers within the collection void BuffersSetColors(const color &array_colors[]) { this.m_buffers.SetColors(array_colors); } //--- Set the color index to the color buffer by its serial number of (1) arrows, (2) line, (3) sections, (4) histogram from zero //--- (5) histogram on two buffers, (6) zigzag, (7) filling, (8) bars and (9) candles //--- (0 - the very first created buffer with the ХХХ drawing style, 1,2,N - subsequent ones) void BufferArrowSetColorIndex(const int number,const int series_index,const uchar color_index) { this.m_buffers.SetBufferArrowColorIndex(number,series_index,color_index); } void BufferLineSetColorIndex(const int number,const int series_index,const uchar color_index) { this.m_buffers.SetBufferLineColorIndex(number,series_index,color_index); } void BufferSectionSetColorIndex(const int number,const int series_index,const uchar color_index) { this.m_buffers.SetBufferSectionColorIndex(number,series_index,color_index); } void BufferHistogramSetColorIndex(const int number,const int series_index,const uchar color_index) { this.m_buffers.SetBufferHistogramColorIndex(number,series_index,color_index); } void BufferHistogram2SetColorIndex(const int number,const int series_index,const uchar color_index) { this.m_buffers.SetBufferHistogram2ColorIndex(number,series_index,color_index); } void BufferZigZagSetColorIndex(const int number,const int series_index,const uchar color_index) { this.m_buffers.SetBufferZigZagColorIndex(number,series_index,color_index); } void BufferFillingSetColorIndex(const int number,const int series_index,const uchar color_index) { this.m_buffers.SetBufferFillingColorIndex(number,series_index,color_index); } void BufferBarsSetColorIndex(const int number,const int series_index,const uchar color_index) { this.m_buffers.SetBufferBarsColorIndex(number,series_index,color_index); } void BufferCandlesSetColorIndex(const int number,const int series_index,const uchar color_index) { this.m_buffers.SetBufferCandlesColorIndex(number,series_index,color_index); } //--- Clear buffer data by its index in the list in the specified timeseries bar void BufferClear(const int buffer_list_index,const int series_index) { this.m_buffers.Clear(buffer_list_index,series_index); } //--- Clear data by the timeseries index for the (1) arrow, (2) line, (3) section, (4) zero line histogram, //--- (5) histogram on two buffers, (6) zigzag, (7) filling, (8) bars and (9) candles void BufferArrowClear(const int number,const int series_index) { this.m_buffers.ClearBufferArrow(number,series_index); } void BufferLineClear(const int number,const int series_index) { this.m_buffers.ClearBufferLine(number,series_index); } void BufferSectionClear(const int number,const int series_index) { this.m_buffers.ClearBufferSection(number,series_index); } void BufferHistogramClear(const int number,const int series_index) { this.m_buffers.ClearBufferHistogram(number,series_index); } void BufferHistogram2Clear(const int number,const int series_index) { this.m_buffers.ClearBufferHistogram2(number,series_index);} void BufferZigZagClear(const int number,const int series_index) { this.m_buffers.ClearBufferZigZag(number,series_index); } void BufferFillingClear(const int number,const int series_index) { this.m_buffers.ClearBufferFilling(number,series_index); } void BufferBarsClear(const int number,const int series_index) { this.m_buffers.ClearBufferBars(number,series_index); } void BufferCandlesClear(const int number,const int series_index) { this.m_buffers.ClearBufferCandles(number,series_index); } //--- Display short description of all indicator buffers of the buffer collection void BuffersPrintShort(void); //--- Set the following for the trading classes: //--- (1) correct filling policy, (2) filling policy, //--- (3) correct order expiration type, (4) order expiration type, //--- (5) magic number, (6) comment, (7) slippage, (8) volume, (9) order expiration date, //--- (10) the flag of asynchronous sending of a trading request, (11) logging level, (12) number of trading attempts void TradingSetCorrectTypeFilling(const ENUM_ORDER_TYPE_FILLING type=ORDER_FILLING_FOK,const string symbol_name=NULL); void TradingSetTypeFilling(const ENUM_ORDER_TYPE_FILLING type=ORDER_FILLING_FOK,const string symbol_name=NULL); void TradingSetCorrectTypeExpiration(const ENUM_ORDER_TYPE_TIME type=ORDER_TIME_GTC,const string symbol_name=NULL); void TradingSetTypeExpiration(const ENUM_ORDER_TYPE_TIME type=ORDER_TIME_GTC,const string symbol_name=NULL); void TradingSetMagic(const uint magic,const string symbol_name=NULL); void TradingSetComment(const string comment,const string symbol_name=NULL); void TradingSetDeviation(const ulong deviation,const string symbol_name=NULL); void TradingSetVolume(const double volume=0,const string symbol_name=NULL); void TradingSetExpiration(const datetime expiration=0,const string symbol_name=NULL); void TradingSetAsyncMode(const bool async_mode=false,const string symbol_name=NULL); void TradingSetLogLevel(const ENUM_LOG_LEVEL log_level=LOG_LEVEL_ERROR_MSG,const string symbol_name=NULL); void TradingSetTotalTry(const uchar attempts) { this.m_trading.SetTotalTry(attempts); } //--- Return the logging level of a trading class symbol trading object ENUM_LOG_LEVEL TradingGetLogLevel(const string symbol_name) { return this.m_trading.GetTradeObjLogLevel(symbol_name); } //--- Set standard sounds (symbol==NULL) for a symbol trading object, (symbol!=NULL) for trading objects of all symbols void SetSoundsStandart(const string symbol=NULL) { this.m_trading.SetSoundsStandart(symbol); } //--- Set the flag of using sounds void SetUseSounds(const bool flag) { this.m_trading.SetUseSounds(flag); } //--- Set a sound for a specified order/position type and symbol. 'mode' specifies an event a sound is set for //--- (symbol=NULL) for trading objects of all symbols, (symbol!=NULL) for a trading object of a specified symbol void SetSound(const ENUM_MODE_SET_SOUND mode,const ENUM_ORDER_TYPE action,const string sound,const string symbol=NULL) { this.m_trading.SetSound(mode,action,sound,symbol); } //--- Play a sound by its description bool PlaySoundByDescription(const string sound_description); //--- Pass the pointers to all the necessary collections to the trading class and the indicator buffer collection class void CollectionOnInit(void) { this.m_trading.OnInit(this.GetAccountCurrent(),m_symbols.GetObject(),m_market.GetObject(),m_history.GetObject(),m_events.GetObject()); this.m_buffers.OnInit(this.m_time_series.GetObject()); }
SeriesBarType()でオーバーロードされたメソッドは、指定された銘柄とチャート期間の指定されたバーのタイプを、て適切な時系列の指定されたインデックスによって返します。
//+------------------------------------------------------------------+ //| Return the bar type of the specified symbol | //| of the specified timeframe by index | //+------------------------------------------------------------------+ ENUM_BAR_BODY_TYPE CEngine::SeriesBarType(const string symbol,const ENUM_TIMEFRAMES timeframe,const int index) { CBar *bar=this.m_time_series.GetBar(symbol,timeframe,index); return(bar!=NULL ? bar.TypeBody() : (ENUM_BAR_BODY_TYPE)WRONG_VALUE); } //+------------------------------------------------------------------+
指定された時系列インデックスによって時系列コレクションから必要なバーオブジェクトを取得し、そのタイプ(強気/弱気/ゼロ/ゼロボディ)を返します。バーが取得できなかった場合は-1を返します。
時間によって:
//+------------------------------------------------------------------+ //| Return the bar type of the specified symbol | //| of the specified timeframe by time | //+------------------------------------------------------------------+ ENUM_BAR_BODY_TYPE CEngine::SeriesBarType(const string symbol,const ENUM_TIMEFRAMES timeframe,const datetime time) { CBar *bar=this.m_time_series.GetBar(symbol,timeframe,time); return(bar!=NULL ? bar.TypeBody() : (ENUM_BAR_BODY_TYPE)WRONG_VALUE); } //+------------------------------------------------------------------+
指定された時間までに時系列コレクションから必要なバーオブジェクトを取得し、そのタイプ(強気/弱気/ゼロ/ゼロボディ)を返します。バーが取得できなかった場合は-1を返します。
使用のメソッドを使用して、必要なバー(または他の時系列からデータを描画するときに必要なバー)の色を決定すると便利です。
GetBufferByLabel()メソッドは、グラフィカルシリーズの名前でバッファオブジェクトへのポインタを返します。
CBuffer *GetBufferByLabel(const string plot_label) { return this.m_buffers.GetBufferByLabel(plot_label); }
上記で説明した同じ名前のバッファコレクションクラスメソッドの結果を返します。
GetBufferByTimeframe()メソッドは、「timeframe」プロパティによってバッファオブジェクトへのポインタを返します。
CBuffer *GetBufferByTimeframe(const ENUM_TIMEFRAMES timeframe) { return this.m_buffers.GetBufferByTimeframe(timeframe);}
上記で説明した同じ名前のバッファコレクションクラスメソッドの結果を返します。
GetBufferByListIndex()メソッドは、バッファコレクションクラスのバッファオブジェクトコレクションリスト内のインデックスによって、バッファオブジェクトへのポインタを返します。
CBuffer *GetBufferByListIndex(const int index_list) { return this.m_buffers.GetBufferByListIndex(index_list);}
上記で説明した同じ名前のバッファコレクションクラスメソッドの結果を返します。
GetLastBuffer()メソッドは、最後に作成されたバッファオブジェクトへのポインタを返します。
//+------------------------------------------------------------------+ //| Return the last indicator buffer | //| in the indicator buffer collection list | //+------------------------------------------------------------------+ CBuffer *CEngine::GetLastBuffer(void) { CArrayObj *list=this.GetListBuffers(); if(list==NULL) return NULL; return list.At(list.Total()-1); } //+------------------------------------------------------------------+
バッファコレクションリストへのポインタを取得し、リストから最後のバッファオブジェクトを返します。
GetBufferCalculate()メソッドは、上記で説明した同じ名前のバッファコレクションクラスメソッドの結果を返します。
CBufferCalculate *GetBufferCalculate(const int number) { return this.m_buffers.GetBufferCalculate(number); }
BuffersPropertyPlotsTotal()メソッドとBuffersPropertyBuffersTotal()メソッドはそれぞれBufferPlotsTotal()とBuffersTotal()から名前変更されました。これらは、上記で検討したバッファコレクションクラスのPropertyBuffersTotal()メソッドとPropertyBuffersTotal()メソッドの結果を返します。
int BuffersPropertyPlotsTotal(void) { return this.m_buffers.PropertyPlotsTotal(); } int BuffersPropertyBuffersTotal(void) { return this.m_buffers.PropertyBuffersTotal(); }
BufferCreateCalculate()メソッドは、上記で検討したバッファコレクションクラスのCreateCalculate()メソッドの結果を返します。
bool BufferCreateCalculate(void) { return this.m_buffers.CreateCalculate(); }
現在、計算バッファの作成方法の使用には1つの制限があります。必要なすべての描画バッファがすでに存在する場合は、すべての計算バッファを作成する必要があります。複数の描画されたバッファの間に計算バッファを作成すると、指標バッファの配列を設定するシーケンスに違反します。その結果、計算されたバッファの後に作成されたすべての描画指標バッファは表示されません。この動作の理由はまだわかりませんが、後で修正するつもりです。
オーバーロードされたBuffersInitPlots()メソッドは、上記で検討したバッファコレクションクラスのInitializePlots()メソッドを使用して、描画されたすべてのバッファを初期化します。
void BuffersInitPlots(const double value,const uchar color_index) { this.m_buffers.InitializePlots(value,color_index); } void BuffersInitPlots(void) { this.m_buffers.InitializePlots(); }
オーバーロードされたBuffersInitCalculates()メソッドは、上記で検討したバッファコレクションクラスのInitializeCalculates()メソッドを使用して、計算されたすべてのバッファを初期化します。
void BuffersInitCalculates(const double value) { this.m_buffers.InitializeCalculates(value); } void BuffersInitCalculates(void) { this.m_buffers.InitializeCalculates(); }
BufferSetDataCalculate()メソッドは、上記で検討したバッファオブジェクトコレクションクラスのSetBufferCalculateValue()メソッドを使用して、計算されたバッファのデータを設定します。
//+------------------------------------------------------------------+ //| Set the calculated buffer data by its serial number | //| (0 - the very first buffer, 1,2,N - subsequent ones) | //+------------------------------------------------------------------+ void CEngine::BufferSetDataCalculate(const int number,const int series_index,const double value) { this.m_buffers.SetBufferCalculateValue(number,series_index,value); } //+------------------------------------------------------------------+
BufferSetDataArrow()、BufferSetDataLine()、BufferSetDataSection()、 BufferSetDataHistogram()、BufferSetDataHistogram2()、BufferSetDataZigZag()、 BufferSetDataFilling()、BufferSetDataBars()、BufferSetDataCandles()各メソッドが変更され、上記で検討したバッファオブジェクトコレクションクラスメソッドを使用して、適切なバッファのデータを設定するようになりました。
//+------------------------------------------------------------------+ //| Set arrow buffer data by its serial number | //| (0 - the very first arrow buffer, 1,2,N - subsequent ones) | //+------------------------------------------------------------------+ void CEngine::BufferSetDataArrow(const int number,const int series_index,const double value,const uchar color_index,bool as_current=false) { this.m_buffers.SetBufferArrowValue(number,series_index,value,color_index,as_current); } //+------------------------------------------------------------------+ //| Set line buffer data by its serial number | //| (0 - the very first line buffer, 1,2,N - subsequent ones) | //+------------------------------------------------------------------+ void CEngine::BufferSetDataLine(const int number,const int series_index,const double value,const uchar color_index,bool as_current=false) { this.m_buffers.SetBufferLineValue(number,series_index,value,color_index,as_current); } //+------------------------------------------------------------------+ //| Set section buffer data by its serial number | //| (0 - the very first sections buffer, 1,2,N - subsequent ones) | //+------------------------------------------------------------------+ void CEngine::BufferSetDataSection(const int number,const int series_index,const double value,const uchar color_index,bool as_current=false) { this.m_buffers.SetBufferSectionValue(number,series_index,value,color_index,as_current); } //+------------------------------------------------------------------+ //| Set histogram buffer data from zero | //| by its serial number | //| (0 - the very first buffer, 1,2,N - subsequent ones) | //+------------------------------------------------------------------+ void CEngine::BufferSetDataHistogram(const int number,const int series_index,const double value,const uchar color_index,bool as_current=false) { this.m_buffers.SetBufferHistogramValue(number,series_index,value,color_index,as_current); } //+------------------------------------------------------------------+
...
//+------------------------------------------------------------------+ //| Set data of all histogram buffers on two buffers | //| by its serial number | //| (0 - the very first buffer, 1,2,N - subsequent ones) | //+------------------------------------------------------------------+ void CEngine::BufferSetDataHistogram2(const int number,const int series_index,const double value1,const double value2,const uchar color_index,bool as_current=false) { this.m_buffers.SetBufferHistogram2Value(number,series_index,value1,value2,color_index,as_current); } //+------------------------------------------------------------------+
...
//+------------------------------------------------------------------+ //| Set data of all zizag buffers | //| by its serial number | //| (0 - the very first zigzag buffer, 1,2,N - subsequent ones) | //+------------------------------------------------------------------+ void CEngine::BufferSetDataZigZag(const int number,const int series_index,const double value1,const double value2,const uchar color_index,bool as_current=false) { this.m_buffers.SetBufferZigZagValue(number,series_index,value1,value2,color_index,as_current); } //+------------------------------------------------------------------+
...
//+------------------------------------------------------------------+ //| Set data of all filling buffers | //| by its serial number | //| (0 - the very first filling buffer, 1,2,N - subsequent ones) | //+------------------------------------------------------------------+ void CEngine::BufferSetDataFilling(const int number,const int series_index,const double value1,const double value2,bool as_current=false) { this.m_buffers.SetBufferFillingValue(number,series_index,value1,value2,as_current); } //+------------------------------------------------------------------+
...
//+------------------------------------------------------------------+ //| Set data of all bar buffers | //| by its serial number | //| (0 - the very first bar buffer, 1,2,N - subsequent ones) | //+------------------------------------------------------------------+ void CEngine::BufferSetDataBars(const int number,const int series_index,const double open,const double high,const double low,const double close,const uchar color_index,bool as_current=false) { CBufferBars *buff=this.m_buffers.GetBufferBars(number); if(buff==NULL) return; this.m_buffers.SetBufferBarsValue(number,series_index,open,high,low,close,color_index,as_current); } //+------------------------------------------------------------------+
...
//+------------------------------------------------------------------+ //| Set data of all candle buffers | //| by its serial number | //| (0 - the very first candle buffer, 1,2,N - subsequent ones) | //+------------------------------------------------------------------+ void CEngine::BufferSetDataCandles(const int number,const int series_index,const double open,const double high,const double low,const double close,const uchar color_index,bool as_current=false) { CBufferCandles *buff=this.m_buffers.GetBufferCandles(number); if(buff==NULL) return; this.m_buffers.SetBufferCandlesValue(number,series_index,open,high,low,close,color_index,as_current); } //+------------------------------------------------------------------+
これらのメソッドはすべて複数期間であるため、他の期間から要求されたデータを1回の呼び出しで現在のチャートに表示できます。
プログラム内で読みやすくするために、BufferArrowColor()、BufferLineColor()、BufferSectionColor()、BufferHistogramColor()、BufferHistogram2Color()、BufferZigZagColor()、 BufferFillingColor()、BufferBarsColor()、BufferCandlesColor()、 BufferArrowColorIndex()、BufferLineColorIndex()、BufferSectionColorIndex()、 BufferHistogramColorIndex()、BufferHistogram2ColorIndex()、BufferZigZagColorIndex()、BufferFillingColorIndex()、BufferBarsColorIndex()、BufferCandlesColorIndex()各メソッドの名前が変更されました。
たとえば、BufferArrowColor()メソッドは以前はBufferColorArrow()と呼ばれていましたが、その目的を特定するときに混乱を招く可能性があります。これらのメソッドはすべて、最初にバッファタイプを記述し、次に、より視覚的に包括的で正しいと思われるものを返します。
BuffersSetColors()メソッドは、上記で検討したバッファオブジェクトコレクションクラスのSetColors()メソッドを使用して、渡されたカラー配列からすべての指標バッファにカラー値を設定します。
void BuffersSetColors(const color &array_colors[]) { this.m_buffers.SetColors(array_colors); }
特定のバッファオブジェクトにカラーインデックスを設定するメソッドが改訂されました。次に、上記で検討した適切なバッファオブジェクトコレクションクラスメソッドを呼び出します。
void BufferArrowSetColorIndex(const int number,const int series_index,const uchar color_index) { this.m_buffers.SetBufferArrowColorIndex(number,series_index,color_index); } void BufferLineSetColorIndex(const int number,const int series_index,const uchar color_index) { this.m_buffers.SetBufferLineColorIndex(number,series_index,color_index); } void BufferSectionSetColorIndex(const int number,const int series_index,const uchar color_index) { this.m_buffers.SetBufferSectionColorIndex(number,series_index,color_index); } void BufferHistogramSetColorIndex(const int number,const int series_index,const uchar color_index) { this.m_buffers.SetBufferHistogramColorIndex(number,series_index,color_index); } void BufferHistogram2SetColorIndex(const int number,const int series_index,const uchar color_index) { this.m_buffers.SetBufferHistogram2ColorIndex(number,series_index,color_index); } void BufferZigZagSetColorIndex(const int number,const int series_index,const uchar color_index) { this.m_buffers.SetBufferZigZagColorIndex(number,series_index,color_index); } void BufferFillingSetColorIndex(const int number,const int series_index,const uchar color_index) { this.m_buffers.SetBufferFillingColorIndex(number,series_index,color_index); } void BufferBarsSetColorIndex(const int number,const int series_index,const uchar color_index) { this.m_buffers.SetBufferBarsColorIndex(number,series_index,color_index); } void BufferCandlesSetColorIndex(const int number,const int series_index,const uchar color_index) { this.m_buffers.SetBufferCandlesColorIndex(number,series_index,color_index); }
BufferClear()メソッドは、バッファオブジェクトコレクションクラスのClear()メソッドを使用して、指定された時系列バーのリスト内のインデックスによってバッファデータを消去します。
void BufferClear(const int buffer_list_index,const int series_index) { this.m_buffers.Clear(buffer_list_index,series_index); }
バッファオブジェクトを消去するメソッドは、上記のバッファオブジェクトコレクションクラスの適切なメソッドを使用して、指定されたバッファのすべての配列のデータを消去します。
void BufferArrowClear(const int number,const int series_index) { this.m_buffers.ClearBufferArrow(number,series_index); } void BufferLineClear(const int number,const int series_index) { this.m_buffers.ClearBufferLine(number,series_index); } void BufferSectionClear(const int number,const int series_index) { this.m_buffers.ClearBufferSection(number,series_index); } void BufferHistogramClear(const int number,const int series_index) { this.m_buffers.ClearBufferHistogram(number,series_index); } void BufferHistogram2Clear(const int number,const int series_index) { this.m_buffers.ClearBufferHistogram2(number,series_index);} void BufferZigZagClear(const int number,const int series_index) { this.m_buffers.ClearBufferZigZag(number,series_index); } void BufferFillingClear(const int number,const int series_index) { this.m_buffers.ClearBufferFilling(number,series_index); } void BufferBarsClear(const int number,const int series_index) { this.m_buffers.ClearBufferBars(number,series_index); } void BufferCandlesClear(const int number,const int series_index) { this.m_buffers.ClearBufferCandles(number,series_index); }
BuffersPrintShort()メソッドは、プログラムで作成され、バッファオブジェクトコレクションに格納されているすべてのインジケータバッファの簡単な説明を表示します。
//+-------------------------------------------------------------------------+ //| Display short description of all indicator buffers within the collection| //+-------------------------------------------------------------------------+ void CEngine::BuffersPrintShort(void) { //--- Get the pointer to the collection list of buffer objects CArrayObj *list=this.GetListBuffers(); if(list==NULL) return; int total=list.Total(); //--- In a loop by the number of buffers in the list, //--- get the next buffer and display its brief description in the journal for(int i=0;i<total;i++) { CBuffer *buff=list.At(i); if(buff==NULL) continue; buff.PrintShort(); } } //+------------------------------------------------------------------+
メソッドロジックはコードコメントで説明されており、非常に単純です。すべてのコレクションバッファオブジェクトのリストによるループで、次のバッファを取得し、バッファオブジェクトクラスのPrintShort()メソッドを使用してその簡単な説明を表示します。
時系列コレクションクラスオブジェクトへのポインタをバッファオブジェクトコレクションクラスに渡す必要があることはすでに説明しました。
すでにCollectionOnInit()メソッドがあります。メソッドを呼び出すことにより、必要なすべてのコレクションをライブラリに渡します。次に、必要なポインタを渡すようにします。これを行うには、以前に検討したバッファオブジェクトコレクションクラスのOnInit()メソッドを呼び出します。
//--- Pass the pointers to all the necessary collections to the trading class and the indicator buffer collection class void CollectionOnInit(void) { this.m_trading.OnInit(this.GetAccountCurrent(),m_symbols.GetObject(),m_market.GetObject(),m_history.GetObject(),m_events.GetObject()); this.m_buffers.OnInit(this.m_time_series.GetObject()); }
このメソッドは、ライブラリベースのプログラムのOnInit()で呼び出されます。
より正確に言うと、OnInitDoEasy()ライブラリを初期化する関数は、OnInit()で呼び出されます。次に、OnInitDoEasy()ライブラリには、他の構成アクションとともにこのメソッドの呼び出しが含まれています。
これでライブラリクラスの改善は完了です。
複数期間インジケーターの作成をテストしてみましょう。
テスト
テストを実行するには、前の記事のテスト指標を、\MQL5\Indicators\TestDoEasy\Part45\TestDoEasyPart45.mq5として保存します。
指標は、その作業で使用する必要がある設定の時間枠を備えています。また、さまざまな描画タイプを選択することもできます。チャートに表示されるバッファは、選択した図面タイプによって異なります。
前の記事の指標文字列
/*sinput*/ ENUM_TIMEFRAMES_MODE InpModeUsedTFs = TIMEFRAMES_MODE_CURRENT; // Mode of used timeframes list
は新しいもので置き換えられます。
/*sinput*/ ENUM_TIMEFRAMES_MODE InpModeUsedTFs = TIMEFRAMES_MODE_LIST; // Mode of used timeframes list
ここでは、現在の時間枠で機能する指標を、時間枠リストで機能する指標に置き換えました。これで、指標は、ストレージ用の配列にある時間枠のリストが機能し始めるのを待ちます。
次に、必要な時間枠が設定されている入力を設定に追加します(デフォルトは現在の入力です)。それから、選択した時間枠がOnInit()ハンドラーの配列に書き込まれます。したがって、インジケータが現在のチャートに表示するためにデータを取得する必要があるチャート期間を設定します。
sinput ENUM_TIMEFRAMES InpPeriod = PERIOD_CURRENT; // Used chart period //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- Write the name of the working timeframe selected in the settings to the InpUsedTFs variable InpUsedTFs=TimeframeDescription(InpPeriod);
配列に時間枠を書き込んだ後、ライブラリはこの値を使用して必要な時系列を作成します。
次に、すべての描画タイプでバッファを作成し、適切な入力設定に応じてデータウィンドウにそれらの表示フラグを設定し、それらの設定で指定された時間枠を設定します。
//--- indicator buffers mapping //--- Create all the necessary buffer objects engine.BufferCreateArrow(); // 2 arrays engine.BufferCreateLine(); // 2 arrays engine.BufferCreateSection(); // 2 arrays engine.BufferCreateHistogram(); // 2 arrays engine.BufferCreateHistogram2(); // 3 arrays engine.BufferCreateZigZag(); // 3 arrays engine.BufferCreateFilling(); // 2 arrays engine.BufferCreateBars(); // 5 arrays engine.BufferCreateCandles(); // 5 arrays engine.BufferCreateCalculate(); // 1 array //--- Check the number of buffers specified in the 'properties' block if(engine.BuffersPropertyPlotsTotal()!=indicator_plots) Alert(TextByLanguage("Внимание! Значение \"indicator_plots\" должно быть ","Attention! Value of \"indicator_plots\" should be "),engine.BuffersPropertyPlotsTotal()); if(engine.BuffersPropertyBuffersTotal()!=indicator_buffers) Alert(TextByLanguage("Внимание! Значение \"indicator_buffers\" должно быть ","Attention! Value of \"indicator_buffers\" should be "),engine.BuffersPropertyBuffersTotal()); //--- Create the color array and set non-default colors to all buffers within the collection color array_colors[]={clrDodgerBlue,clrRed,clrGray}; engine.BuffersSetColors(array_colors); //--- Set the line width for ZigZag (the sixth drawn buffer) //--- It has the index of 5 considering that the starting point is zero CBuffer *buff_zz=engine.GetBufferByPlot(5); if(buff_zz!=NULL) { buff_zz.SetWidth(2); } //--- In a loop by the list of collection buffer objects, for(int i=0;i<engine.GetListBuffers().Total();i++) { //--- get the next buffer CBuffer *buff=engine.GetListBuffers().At(i); if(buff==NULL) continue; //--- and set its display in the data window depending on its specified usage //--- and also the chart period selected in the settings buff.SetShowData(IsUse(buff.Status())); buff.SetTimeframe(InpPeriod); }
指標内の時系列のようにインデックス付けの方向を使用するので(ライブラリでの時系列の格納方法のため)、OnCalculate()からライブラリにすべてのデータ配列を渡すためのさらに別の関数を作成します。以前は、これはCopyData()関数によって実行されていました。この関数では、「時系列のように」フラグが配列に設定され、その以前のステータスが返されました。
//+------------------------------------------------------------------+ //| Copy data from the second OnCalculate() form to the structure | //+------------------------------------------------------------------+ void CopyData(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[]) { //--- Get the array indexing flags as in the timeseries. If failed, //--- set the indexing direction or the arrays as in the timeseries bool as_series_time=ArrayGetAsSeries(time); if(!as_series_time) ArraySetAsSeries(time,true); bool as_series_open=ArrayGetAsSeries(open); if(!as_series_open) ArraySetAsSeries(open,true); bool as_series_high=ArrayGetAsSeries(high); if(!as_series_high) ArraySetAsSeries(high,true); bool as_series_low=ArrayGetAsSeries(low); if(!as_series_low) ArraySetAsSeries(low,true); bool as_series_close=ArrayGetAsSeries(close); if(!as_series_close) ArraySetAsSeries(close,true); bool as_series_tick_volume=ArrayGetAsSeries(tick_volume); if(!as_series_tick_volume) ArraySetAsSeries(tick_volume,true); bool as_series_volume=ArrayGetAsSeries(volume); if(!as_series_volume) ArraySetAsSeries(volume,true); bool as_series_spread=ArrayGetAsSeries(spread); if(!as_series_spread) ArraySetAsSeries(spread,true); //--- Copy the arrays' zero bar to the OnCalculate() SDataCalculate data structure rates_data.rates_total=rates_total; rates_data.prev_calculated=prev_calculated; rates_data.rates.time=time[0]; rates_data.rates.open=open[0]; rates_data.rates.high=high[0]; rates_data.rates.low=low[0]; rates_data.rates.close=close[0]; rates_data.rates.tick_volume=tick_volume[0]; rates_data.rates.real_volume=(#ifdef __MQL5__ volume[0] #else 0 #endif); rates_data.rates.spread=(#ifdef __MQL5__ spread[0] #else 0 #endif); //--- Return the arrays' initial indexing direction if(!as_series_time) ArraySetAsSeries(time,false); if(!as_series_open) ArraySetAsSeries(open,false); if(!as_series_high) ArraySetAsSeries(high,false); if(!as_series_low) ArraySetAsSeries(low,false); if(!as_series_close) ArraySetAsSeries(close,false); if(!as_series_tick_volume) ArraySetAsSeries(tick_volume,false); if(!as_series_volume) ArraySetAsSeries(volume,false); if(!as_series_spread) ArraySetAsSeries(spread,false); } //+------------------------------------------------------------------+
次に、OnCalculate()のライブラリ時系列で正しく動作するように、これらの配列を「めくり」ました。
//+------------------------------------------------------------------+ //| OnCalculate code block for working with the indicator: | //+------------------------------------------------------------------+ //--- Set OnCalculate arrays as timeseries ArraySetAsSeries(open,true); ArraySetAsSeries(high,true); ArraySetAsSeries(low,true); ArraySetAsSeries(close,true); ArraySetAsSeries(time,true); ArraySetAsSeries(tick_volume,true); ArraySetAsSeries(volume,true); ArraySetAsSeries(spread,true);
不要な作業を避けるために、ライブラリサービス関数のファイルにさらに別の関数を作成して、渡された配列が時系列のようにインデックスの方向を受け取るようにします。\MQL5\Include\DoEasy\Services\DELib.mqhを開いて新しい関数を追加します。
//+------------------------------------------------------------------+ //| Copy data from the second OnCalculate() form to the structure | //| and set the "as timeseries" flag to all arrays | //+------------------------------------------------------------------+ void CopyDataAsSeries(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[]) { //--- set the indexing direction or the arrays as in the timeseries ArraySetAsSeries(time,true); ArraySetAsSeries(open,true); ArraySetAsSeries(high,true); ArraySetAsSeries(low,true); ArraySetAsSeries(close,true); ArraySetAsSeries(tick_volume,true); ArraySetAsSeries(volume,true); ArraySetAsSeries(spread,true); //--- Copy the arrays' zero bar to the OnCalculate() SDataCalculate data structure rates_data.rates_total=rates_total; rates_data.prev_calculated=prev_calculated; rates_data.rates.time=time[0]; rates_data.rates.open=open[0]; rates_data.rates.high=high[0]; rates_data.rates.low=low[0]; rates_data.rates.close=close[0]; rates_data.rates.tick_volume=tick_volume[0]; rates_data.rates.real_volume=(#ifdef __MQL5__ volume[0] #else 0 #endif); rates_data.rates.spread=(#ifdef __MQL5__ spread[0] #else 0 #endif); } //+------------------------------------------------------------------+
この関数は前の関数と同じように機能しますが、配列を元のインデックスに戻しません。ここで、指標からCopyData()関数を呼び出す代わりに、新しい関数であるCopyDataAsSeries()を呼び出します。これにより、配列に必要なインデックスを再設定する必要がなくなります。
//+------------------------------------------------------------------+ //| 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[]) { //+------------------------------------------------------------------+ //| OnCalculate code block for working with the library: | //+------------------------------------------------------------------+ //--- Pass the current symbol data from OnCalculate() to the price structure and set the "as timeseries" flag to the arrays CopyDataAsSeries(rates_total,prev_calculated,time,open,high,low,close,tick_volume,volume,spread); //--- Check for the minimum number of bars for calculation if(rates_total<min_bars || Point()==0) return 0; //--- Handle the Calculate event in the library //--- If the OnCalculate() method of the library returns zero, not all timeseries are ready - leave till the next tick if(engine.0) return 0; //--- If working in the tester if(MQLInfoInteger(MQL_TESTER)) { engine.OnTimer(rates_data); // Working in the library timer EventsHandling(); // Working with library events } //+------------------------------------------------------------------+ //| OnCalculate code block for working with the indicator: | //+------------------------------------------------------------------+ //--- Check and calculate the number of calculated bars //--- If limit = 0, there are no new bars - calculate the current one //--- If limit = 1, a new bar has appeared - calculate the first and the current ones //--- limit > 1 means the first launch or changes in history - the full recalculation of all data int limit=rates_total-prev_calculated; //--- Recalculate the entire history if(limit>1) { limit=rates_total-1; engine.BuffersInitPlots(); engine.BuffersInitCalculates(); } //--- Prepare data //--- Calculate the indicator CBar *bar=NULL; // Bar object for defining the candle direction uchar color_index=0; // Color index to be set for the buffer depending on the candle direction //--- Main calculation loop of the indicator for(int i=limit; i>WRONG_VALUE && !IsStopped(); i--) { //--- Clear the current bar of all created buffers engine.BufferArrowClear(0,0); engine.BufferLineClear(0,0); engine.BufferSectionClear(0,0); engine.BufferHistogramClear(0,0); engine.BufferHistogram2Clear(0,0); engine.BufferZigZagClear(0,0); engine.BufferFillingClear(0,0); engine.BufferBarsClear(0,0); engine.BufferCandlesClear(0,0); //--- Get the timeseries bar corresponding to the loop index time on the chart period specified in the settings bar=engine.SeriesGetBar(NULL,InpPeriod,time[i]); if(bar==NULL) continue; //--- Calculate the color index depending on the candle direction on the timeframe specified in the settings color_index=(bar.TypeBody()==BAR_BODY_TYPE_BULLISH ? 0 : bar.TypeBody()==BAR_BODY_TYPE_BEARISH ? 1 : 2); //--- Check the settings and calculate the arrow buffer if(IsUse(BUFFER_STATUS_ARROW)) engine.BufferSetDataArrow(0,i,bar.Close(),color_index); //--- Check the settings and calculate the line buffer if(IsUse(BUFFER_STATUS_LINE)) engine.BufferSetDataLine(0,i,bar.Open(),color_index); //--- Check the settings and calculate the section buffer if(IsUse(BUFFER_STATUS_SECTION)) engine.BufferSetDataSection(0,i,bar.Close(),color_index); //--- Check the settings and calculate the histogram from zero buffer if(IsUse(BUFFER_STATUS_HISTOGRAM)) engine.BufferSetDataHistogram(0,i,open[i],color_index); //--- Check the settings and calculate the 'histogram on two buffers' buffer if(IsUse(BUFFER_STATUS_HISTOGRAM2)) engine.BufferSetDataHistogram2(0,i,bar.Open(),bar.Close(),color_index); //--- Check the settings and calculate the zigzag buffer if(IsUse(BUFFER_STATUS_ZIGZAG)) { //--- Set the bar's Low value value for the zigzag (for bullish candles) double value1=bar.Low(); double value2=value1; //--- If the candle is bearish (color index = 1), set the bar's High value for the zigzag if(color_index==1) { value1=value2=bar.High(); } engine.BufferSetDataZigZag(0,i,value1,value2,color_index); } //--- Check the settings and calculate the filling buffer if(IsUse(BUFFER_STATUS_FILLING)) { //--- Set filling border values for bullish candles double value1=bar.High(); double value2=bar.Low(); //--- In case of the bearish candle (color index = 1), swap the filling borders to change the color if(color_index==1) { value1=bar.Low(); value2=bar.High(); } engine.BufferSetDataFilling(0,i,value1,value2,color_index); } //--- Check the settings and calculate the bar buffer if(IsUse(BUFFER_STATUS_BARS)) engine.BufferSetDataBars(0,i,bar.Open(),bar.High(),bar.Low(),bar.Close(),color_index); //--- Check the settings and calculate the candle buffer if(IsUse(BUFFER_STATUS_CANDLES)) engine.BufferSetDataCandles(0,i,bar.Open(),bar.High(),bar.Low(),bar.Close(),color_index); } //--- return value of prev_calculated for next call return(rates_total); } //+------------------------------------------------------------------+
残りのアクションとOnCalculate()ハンドラロジックは、コードコメントで詳細に説明されています。複数期間指標の操作がはるかに簡単になりました。自分で計算する必要はありません。代わりに、バッファにデータを書き込むだけで、ライブラリにデータの配置場所と表示方法を定義させることができます。
次の段階
次の記事では、指標バッファコレクションクラスの開発を継続し、指標を複数銘柄モードで使用します。
現在のバージョンのライブラリのすべてのファイルは、テスト用EAファイルと一緒に以下に添付されているので、テストするにはダウンロードしてください。
質問、コメント、提案はコメント欄にお願いします。テスト指標はMQL5向けに開発されたのでご注意ください。
添付ファイルはMetaTrader 5のみを対象としています。現在のライブラリバージョンはMetaTrader 4ではテストされていません。
指標バッファコレクションを作成してテストした後で、MetaTrader 4にいくつかのMQL5機能を実装してみます。
シリーズのこれまでの記事:
DoEasyライブラリの時系列(第35部): バーオブジェクトと銘柄の時系列リスト
DoEasyライブラリの時系列(第36部): すべての使用銘柄期間の時系列オブジェクト
DoEasyライブラリの時系列(第37部): すべての使用銘柄期間の時系列オブジェクト
DoEasyライブラリの時系列(第38部): 時系列コレクション-リアルタイムの更新とプログラムからのデータへのアクセス
DoEasyライブラリの時系列(第39部): ライブラリに基づいた指標 - データイベントと時系列イベントの準備
DoEasyライブラリの時系列(第40部): ライブラリに基づいた指標 - 実時間でのデータ更新
DoEasyライブラリの時系列(第41部): 複数銘柄・複数期間指標の例
DoEasyライブラリの時系列(第42部): 抽象指標バッファオブジェクトクラス
DoEasyライブラリの時系列(第43部): 指標バッファオブジェクトクラス
DoEasyライブラリの時系列(第44部): 指標バッファオブジェクトのコレクションクラス
MetaQuotes Ltdによってロシア語から翻訳されました。
元の記事: https://www.mql5.com/ru/articles/8023
- 無料取引アプリ
- 8千を超えるシグナルをコピー
- 金融ニュースで金融マーケットを探索