Timeseries in DoEasy library (part 48): Multi-period multi-symbol indicators on one buffer in a subwindow
Contents
Concept
In the previous article, I used the Accelerator Oscillator standard indicator to highlight the principles and methods of displaying data of standard indicators calculated on any symbol/timeframe on the current chart. Now we need to collect methods allowing us to develop other standard indicators. We are almost all set to achieve this. In the current article, I will perform a test to determine similar code constructions in the methods to subsequently move repeating code blocks to separate methods. This is the simplest thing we need to do to work with single-buffer standard indicators - the ones using only one buffer to display data.
In the following articles, I am going to define what can be improved in the code to reduce and optimize it based on the methods enhanced in the current articles and the methods of working with multi-buffer standard indicators to be developed in the next article.
Today, I will develop a sample custom indicator displaying a standard indicator, selected in the settings, in a subwindow on the current chart. This is one of the standard indicators featuring a single drawn buffer and displaying its data in the main chart subwindow. To do this, I will have to slightly improve library classes. This will be an important preparatory step for creating the methods of working with the rest of the standard indicators.
Improving library classes
First, let's add a new library message to \MQL5\Include\DoEasy\Datas.mqh.
Add the new message index:
MSG_LIB_TEXT_BUFFER_TEXT_INVALID_PROPERTY_BUFF, // Invalid number of indicator buffers (#property indicator_buffers) MSG_LIB_TEXT_BUFFER_TEXT_MAX_BUFFERS_REACHED, // Reached maximum possible number of indicator buffers MSG_LIB_TEXT_BUFFER_TEXT_NO_BUFFER_OBJ, // No buffer object for standard indicator MSG_LIB_TEXT_BUFFER_TEXT_STATUS_NONE, // No drawing
and the message text corresponding to the newly added index:
{"Неправильно указано количество буферов индикатора (#property indicator_buffers)","Number of indicator buffers incorrect (#property indicator_buffers)"}, {"Достигнуто максимально возможное количество индикаторных буферов","Maximum number of indicator buffers reached"}, {"Нет ни одного объекта-буфера для стандартного индикатора","There is no buffer object for the standard indicator"}, {"Нет отрисовки","No drawing"},
All current improvements relate to the indicator buffer collection class in \MQL5\Include\DoEasy\Collections\BuffersCollection.mqh and, naturally, the CEngine class.
In BuffersCollection.mqh, we need to add a single method for preparing the data of the calculated buffer of all created standard indicators so that the library is able to do that on its own after receiving a command from the program. This will simplify the final program code — there will be no need to search and obtain required objects.
Declare the method in the public section of the class:
//--- Prepare calculated buffer data of (1) the specified standard indicator and (2) all created standard indicators int PreparingDataBufferStdInd(const ENUM_INDICATOR std_ind,const int id,const int total_copy); bool PreparingDataAllBuffersStdInd(void); //--- Clear buffer data of the specified standard indicator by the timeseries index
and write its implementation beyond the class body:
//+------------------------------------------------------------------+ //| Prepare the calculated buffer data | //| of all created standard indicators | //+------------------------------------------------------------------+ bool CBuffersCollection::PreparingDataAllBuffersStdInd(void) { CArrayObj *list=this.GetListBuffersWithID(); if(list==NULL || list.Total()==0) { ::Print(DFUN_ERR_LINE,CMessage::Text(MSG_LIB_TEXT_BUFFER_TEXT_NO_BUFFER_OBJ)); return false; } bool res=true; int total=list.Total(); for(int i=0;i<total;i++) { CBuffer *buff=list.At(i); if(buff==NULL || buff.TypeBuffer()==BUFFER_TYPE_DATA || buff.IndicatorType()==WRONG_VALUE) continue; CSeriesDE *series=this.m_timeseries.GetSeries(buff.Symbol(),buff.Timeframe()); if(series==NULL) continue; int used_data=(int)series.AvailableUsedData(); int copied=this.PreparingDataBufferStdInd(buff.IndicatorType(),buff.ID(),used_data); if(copied<used_data) res &=false; } return res; } //+------------------------------------------------------------------+
Each buffer object used to calculate the standard indicator has an ID assigned either automatically according to the standard indicator type (ENUM_INDICATOR), or manually from the program when creating the necessary buffer objects.
The list of buffer objects whose ID is not equal to -1 is the first thing we obtain here.
If the list is empty, display the message that there are no created buffer objects for standard indicators and return false.
Next, in a loop by the number of all buffer objects having a standard indicator ID, get the next buffer object. Each standard indicator features at least two buffer objects of this kind — calculated and drawn one. We need calculated buffers only but we do not select them in the current loop since only a calculated buffer is selected for further handling in the PreparingDataBufferStdInd() method I have considered in the previous article (I am going to improve that method here later). Therefore, I am not going to repeat this selection here.
Now we need to define how many bars we have on a symbol/period the obtained buffer object has been created for. We need this value to define how much standard indicator data should be copied to the buffer object meant for it. To do this, get the necessary timeseries by symbol and timeframe values of the buffer object, and take the amount of available data from it.
After that, call the PreparingDataBufferStdInd() method to copy the necessary amount of data from the indicator handle to the calculated buffer object.
If the amount of copied data is less than required, the res variable receives false. If at least one of the existing buffer objects is not copied in the necessary amount, the res variable contains false. And it is from this method that the variable value is returned. The method returns true if all data of all calculated buffer objects have been successfully copied.
So far, the method copies all existing data from the indicator handle to the calculated buffer. Copying large amounts of data for more than one indicator at each tick is, of course, rather impractical. But since I am currently creating the functionality for working with standard indicators, I will leave this behavior intact for now. The logic is more important here than speed. Later, I will make sure that the data is copied only in the necessary conditions (first launch, changes in history data). In other cases, only the required amount of data (one or two bars) is to be copied.
I have already declared all methods for creating standard indicator handles and accompanying buffers in the previous article. But I have not implemented them yet (except for two methods for creating AC and AD indicators). Today I will implement the methods for creating standard indicator handles and their buffer objects for those having a single indicator buffer, and the indicator draws data in the main chart subwindow on its own.
Besides, I want the color of created indicators correspond the color of the appropriate standard indicators by default. We will still be able to set our own colors for these buffer objects. To do this, we will simply need to pass the index of the necessary color when calculating buffers in the main loop.
Let's consider the changes made to the buffer creation method for Accelerator Oscillator standard indicator:
//+------------------------------------------------------------------+ //| Create multi-symbol multi-period AC | //+------------------------------------------------------------------+ int CBuffersCollection::CreateAC(const string symbol,const ENUM_TIMEFRAMES timeframe,const int id=WRONG_VALUE) { //--- Create the indicator handle and set the default ID int handle=::iAC(symbol,timeframe); int identifier=(id==WRONG_VALUE ? IND_AC : id); color array_colors[3]={clrGreen,clrRed,clrGreen}; CBuffer *buff=NULL; if(handle!=INVALID_HANDLE) { //--- Create the histogram buffer from the zero line this.CreateHistogram(); //--- Get the last created (drawn) buffer object and set all the necessary parameters to it buff=this.GetLastCreateBuffer(); if(buff==NULL) return INVALID_HANDLE; buff.SetSymbol(symbol); buff.SetTimeframe(timeframe); buff.SetID(identifier); buff.SetIndicatorHandle(handle); buff.SetIndicatorType(IND_AC); buff.SetShowData(true); buff.SetLabel("AC("+symbol+","+TimeframeDescription(timeframe)+")"); buff.SetIndicatorName("Accelerator Oscillator"); buff.SetColors(array_colors); //--- Create a calculated buffer storing standard indicator data this.CreateCalculate(); //--- Get the last created (calculated) buffer object and set all the necessary parameters to it buff=this.GetLastCreateBuffer(); if(buff==NULL) return INVALID_HANDLE; buff.SetSymbol(symbol); buff.SetTimeframe(timeframe); buff.SetID(identifier); buff.SetIndicatorHandle(handle); buff.SetIndicatorType(IND_AC); buff.SetEmptyValue(EMPTY_VALUE); buff.SetLabel("AC("+symbol+","+TimeframeDescription(timeframe)+")"); buff.SetIndicatorName("Accelerator Oscillator"); } return handle; } //+------------------------------------------------------------------+
Since Accelerator Oscillator standard indicator features two colors for displaying its values, declare the color array of size 3, which is immediately initialized with three colors. Why three? Because the color with the index of 0 displays ascending values of the indicator line, while the color with the index of 1 displays descending values. But we need one more color that displays equal values of two adjacent bars of the indicator line. In the standard Accelerator Oscillator they are displayed by the color of ascending values. Therefore, we need three colors.
After creating the drawn buffer object, set its drawing colors from this array.
When obtaining the pointer to the last created buffer object, the pointer may be obtained erroneously. Previously, this situation was not taken into account, which posed a potential danger - accessing by an invalid pointer leads to a program crash.
Therefore, in this case, we need to exit the method and return INVALID_HANDLE.
For standard indicators having only one drawing color, I am going to set the color array of only one element. This is all that differs the methods of creating handles of color standard indicators and their buffer objects from the methods of creating monochrome standard indicators.
For example, below is the method of creating Average True Range standard indicator:
//+------------------------------------------------------------------+ //| Create multi-symbol multi-period ATR | //+------------------------------------------------------------------+ int CBuffersCollection::CreateATR(const string symbol,const ENUM_TIMEFRAMES timeframe,const int ma_period,const int id=WRONG_VALUE) { //--- Create the indicator handle and set the default ID int handle=::iATR(symbol,timeframe,ma_period); int identifier=(id==WRONG_VALUE ? IND_ATR : id); color array_colors[1]={clrLightSeaGreen}; CBuffer *buff=NULL; if(handle!=INVALID_HANDLE) { //--- Create the line buffer this.CreateLine(); //--- Get the last created (drawn) buffer object and set all the necessary parameters to it buff=this.GetLastCreateBuffer(); if(buff==NULL) return INVALID_HANDLE; buff.SetSymbol(symbol); buff.SetTimeframe(timeframe); buff.SetID(identifier); buff.SetIndicatorHandle(handle); buff.SetIndicatorType(IND_ATR); buff.SetShowData(true); buff.SetLabel("ATR("+symbol+","+TimeframeDescription(timeframe)+": "+(string)ma_period+")"); buff.SetIndicatorName("Average True Range"); buff.SetColors(array_colors); //--- Create a calculated buffer storing standard indicator data this.CreateCalculate(); //--- Get the last created (calculated) buffer object and set all the necessary parameters to it buff=this.GetLastCreateBuffer(); if(buff==NULL) return INVALID_HANDLE; buff.SetSymbol(symbol); buff.SetTimeframe(timeframe); buff.SetID(identifier); buff.SetIndicatorHandle(handle); buff.SetIndicatorType(IND_ATR); buff.SetEmptyValue(EMPTY_VALUE); buff.SetLabel("ATR("+symbol+","+TimeframeDescription(timeframe)+": "+(string)ma_period+")"); buff.SetIndicatorName("Average True Range"); } return handle; } //+------------------------------------------------------------------+
Today I will implement the methods for single-buffer standard indicators drawn in a subwindow, namely:
- Accelerator Oscillator
- Accumulation/Distribution
- Awesome Oscillator
- Average True Range
- Bears Power
- Bulls Power
- Chaikin Oscillator
- Commodity Channel Index
- DeMarker
- Force Index
- Momentum
- Money Flow Index
- Moving Average of Oscillator
- On Balance Volume
- Relative Strength Index
- Standard Deviation
- Triple Exponential Average
- William's Percent Range
- Volumes
The methods of creating multi-symbol multi-period indicators listed above have already been created. There is no point in considering them here. They are identical to the considered methods and provided in the attachments below.
You have probably noticed that the above list does not contain Market Facilitation Index indicator drawn in a subwindow and featuring a single drawn buffer. At first glance, both conditions are met. However, in order to calculate the histogram column color, it also requires yet another calculated buffer — the volume indicator buffer. Therefore, this indicator is considered a multi-buffer one in a subwindow.
The PreparingDataBufferStdInd() method fills in the calculated buffer object array with data from the indicator handle. I have already considered this method in the previous article, although it only implemented filling the AC indicator buffer at the time:
//+------------------------------------------------------------------+ //| Prepare the calculated buffer data | //| of the specified standard indicator | //+------------------------------------------------------------------+ int CBuffersCollection::PreparingDataBufferStdInd(const ENUM_INDICATOR std_ind,const int id,const int total_copy) { CArrayObj *list=this.GetListBufferByTypeID(std_ind,id); list=CSelect::ByBufferProperty(list,BUFFER_PROP_TYPE,BUFFER_TYPE_CALCULATE,EQUAL); if(list==NULL || list.Total()==0) return 0; CBufferCalculate *buffer=NULL; int copies=WRONG_VALUE; switch((int)std_ind) { case IND_AC : buffer=list.At(0); if(buffer==NULL) return 0; copies=buffer.FillAsSeries(buffer.IndicatorHandle(),0,0,total_copy); return copies; case IND_AD : break; case IND_ADX : break; case IND_ADXW : break; case IND_ALLIGATOR : break; case IND_AMA : break; case IND_AO : break; case IND_ATR : break; case IND_BANDS : break; case IND_BEARS : break; case IND_BULLS : break; case IND_BWMFI : break; case IND_CCI : break; case IND_CHAIKIN : break; case IND_DEMA : break; case IND_DEMARKER : break; case IND_ENVELOPES : break; case IND_FORCE : break; case IND_FRACTALS : break; case IND_FRAMA : break; case IND_GATOR : break; case IND_ICHIMOKU : break; case IND_MA : break; case IND_MACD : break; case IND_MFI : break; case IND_MOMENTUM : break; case IND_OBV : break; case IND_OSMA : break; case IND_RSI : break; case IND_RVI : break; case IND_SAR : break; case IND_STDDEV : break; case IND_STOCHASTIC : break; case IND_TEMA : break; case IND_TRIX : break; case IND_VIDYA : break; case IND_VOLUMES : break; case IND_WPR : break; default: break; } return 0; } //+------------------------------------------------------------------+
Most interestingly, exactly the same actions should be performed for other single-buffer indicators. The switch operator comes to our aid here as it is used to compare the values of the expression with constants in all case variants and pass control to the operator that matches the expression value. Each case ends either by the break or return operator. If none of the operators is set within the case, control is passed to the next case after the expression value is handled in the necessary case.
Therefore, instead of copying similar necessary operations to all cases performing identical operations, it is more reasonable to simply combine all such cases into one with the return or break operator:
//+------------------------------------------------------------------+ //| Prepare the calculated buffer data | //| of the specified standard indicator | //+------------------------------------------------------------------+ int CBuffersCollection::PreparingDataBufferStdInd(const ENUM_INDICATOR std_ind,const int id,const int total_copy) { CArrayObj *list=this.GetListBufferByTypeID(std_ind,id); list=CSelect::ByBufferProperty(list,BUFFER_PROP_TYPE,BUFFER_TYPE_CALCULATE,EQUAL); if(list==NULL || list.Total()==0) { ::Print(DFUN_ERR_LINE,CMessage::Text(MSG_LIB_TEXT_BUFFER_TEXT_NO_BUFFER_OBJ)); return 0; } CBufferCalculate *buffer=NULL; int copies=WRONG_VALUE; switch((int)std_ind) { //--- Single-buffer standard indicators in a subwindow case IND_AC : case IND_AD : case IND_AO : case IND_ATR : case IND_BEARS : case IND_BULLS : case IND_CHAIKIN : case IND_CCI : case IND_DEMARKER : case IND_FORCE : case IND_MOMENTUM : case IND_MFI : case IND_OSMA : case IND_OBV : case IND_RSI : case IND_STDDEV : case IND_TRIX : case IND_VOLUMES : case IND_WPR : buffer=list.At(0); if(buffer==NULL) return 0; copies=buffer.FillAsSeries(buffer.IndicatorHandle(),0,0,total_copy); return copies; case IND_ADX : break; case IND_ADXW : break; case IND_ALLIGATOR : break; case IND_AMA : break; case IND_BANDS : break; case IND_BWMFI : break; case IND_DEMA : break; case IND_ENVELOPES : break; case IND_FRACTALS : break; case IND_FRAMA : break; case IND_GATOR : break; case IND_ICHIMOKU : break; case IND_MA : break; case IND_MACD : break; case IND_RVI : break; case IND_SAR : break; case IND_STOCHASTIC : break; case IND_TEMA : break; case IND_VIDYA : break; default: break; } return 0; } //+------------------------------------------------------------------+
Do the same in the method of clearing buffer data of the specified standard indicator:
//+------------------------------------------------------------------+ //| Clear buffer data of the specified standard indicator | //| by the timeseries index | //+------------------------------------------------------------------+ void CBuffersCollection::ClearDataBufferStdInd(const ENUM_INDICATOR std_ind,const int id,const int series_index) { //--- Get the list of buffer objects by type and ID CArrayObj *list=this.GetListBufferByTypeID(std_ind,id); if(list==NULL || list.Total()==0) return; list=CSelect::ByBufferProperty(list,BUFFER_PROP_TYPE,BUFFER_TYPE_DATA,EQUAL); if(list.Total()==0) return; CBuffer *buffer=NULL; switch((int)std_ind) { //--- Single-buffer standard indicators in a subwindow case IND_AC : case IND_AD : case IND_AO : case IND_ATR : case IND_BEARS : case IND_BULLS : case IND_CHAIKIN : case IND_CCI : case IND_DEMARKER : case IND_FORCE : case IND_MOMENTUM : case IND_MFI : case IND_OSMA : case IND_OBV : case IND_RSI : case IND_STDDEV : case IND_TRIX : case IND_VOLUMES : case IND_WPR : buffer=list.At(0); if(buffer==NULL) return; buffer.SetBufferValue(0,series_index,buffer.EmptyValue()); break; case IND_ADX : break; case IND_ADXW : break; case IND_ALLIGATOR : break; case IND_AMA : break; case IND_BANDS : break; case IND_BWMFI : break; case IND_DEMA : break; case IND_ENVELOPES : break; case IND_FRACTALS : break; case IND_FRAMA : break; case IND_GATOR : break; case IND_ICHIMOKU : break; case IND_MA : break; case IND_MACD : break; case IND_RVI : break; case IND_SAR : break; case IND_STOCHASTIC : break; case IND_TEMA : break; case IND_VIDYA : break; default: break; } } //+------------------------------------------------------------------+
and in the method of setting the value for the buffer data of the specified standard indicator:
//+------------------------------------------------------------------+ //| Set values for the current chart to the specified buffer | //| of the standard indicator by the timeseries index according to | //| the buffer object symbol/period | //+------------------------------------------------------------------+ bool CBuffersCollection::SetDataBufferStdInd(const ENUM_INDICATOR ind_type,const int id,const int series_index,const datetime series_time,const char color_index=WRONG_VALUE) { //--- Get the list of buffer objects by type and ID CArrayObj *list=this.GetListBufferByTypeID(ind_type,id); if(list==NULL || list.Total()==0) { ::Print(DFUN,CMessage::Text(MSG_LIB_TEXT_BUFFER_TEXT_NO_BUFFER_OBJ)); return false; } //--- Get the list of drawn objects with ID CArrayObj *list_data=CSelect::ByBufferProperty(list,BUFFER_PROP_TYPE,BUFFER_TYPE_DATA,EQUAL); list_data=CSelect::ByBufferProperty(list_data,BUFFER_PROP_IND_TYPE,ind_type,EQUAL); //--- Get the list of calculated buffers with ID CArrayObj *list_calc=CSelect::ByBufferProperty(list,BUFFER_PROP_TYPE,BUFFER_TYPE_CALCULATE,EQUAL); list_calc=CSelect::ByBufferProperty(list_calc,BUFFER_PROP_IND_TYPE,ind_type,EQUAL); //--- Exit if any of the lists is empty if(list_data.Total()==0 || list_calc.Total()==0) return false; //--- Declare the necessary objects and variables CBuffer *buffer_data=NULL; CBuffer *buffer_calc=NULL; int index_period=0; int series_index_start=0; int num_bars=1,index=0; datetime time_period=0; double value0=EMPTY_VALUE, value1=EMPTY_VALUE; //--- Depending on the standard indicator type switch((int)ind_type) { //--- Single-buffer standard indicators case IND_AC : case IND_AD : case IND_AO : case IND_ATR : case IND_BEARS : case IND_BULLS : case IND_CHAIKIN : case IND_CCI : case IND_DEMARKER : case IND_FORCE : case IND_MOMENTUM : case IND_MFI : case IND_OSMA : case IND_OBV : case IND_RSI : case IND_STDDEV : case IND_TRIX : case IND_VOLUMES : case IND_WPR : //--- Get drawn and calculated buffer objects buffer_data=list_data.At(0); buffer_calc=list_calc.At(0); if(buffer_calc==NULL || buffer_data==NULL || buffer_calc.GetDataTotal(0)==0) return false; //--- Find the bar index corresponding to the current bar start time index_period=::iBarShift(buffer_calc.Symbol(),buffer_calc.Timeframe(),series_time,true); if(index_period==WRONG_VALUE || index_period>buffer_calc.GetDataTotal()-1) return false; //--- Get the value by the index from the indicator buffer value0=buffer_calc.GetDataBufferValue(0,index_period); if(buffer_calc.Symbol()==::Symbol() && buffer_calc.Timeframe()==::Period()) { series_index_start=series_index; num_bars=1; } else { //--- Get the bar time the bar with the index_period index falls into on the calculated buffer period and symbol time_period=::iTime(buffer_calc.Symbol(),buffer_calc.Timeframe(),index_period); if(time_period==0) return false; //--- Get the appropriate current chart bar series_index_start=::iBarShift(::Symbol(),::Period(),time_period,true); if(series_index_start==WRONG_VALUE) return false; //--- Calculate the number of bars on the current chart which should be filled with calculated buffer data num_bars=::PeriodSeconds(buffer_calc.Timeframe())/::PeriodSeconds(PERIOD_CURRENT); if(num_bars==0) num_bars=1; } //--- Take values to calculate colors value1=(series_index_start+num_bars>buffer_data.GetDataTotal()-1 ? value0 : buffer_data.GetDataBufferValue(0,series_index_start+num_bars)); //--- In the loop by the number of bars in num_bars, fill in the drawn buffer with the calculated buffer value taken by the index_period index //--- and set the color of the drawn buffer depending on the value0 and value1 values ratio for(int i=0;i<num_bars;i++) { index=series_index_start-i; buffer_data.SetBufferValue(0,index,value0); buffer_data.SetBufferColorIndex(index,color_index==WRONG_VALUE ? uchar(value0>value1 ? 0 : value0<value1 ? 1 : 2) : color_index); } return true; case IND_ADX : break; case IND_ADXW : break; case IND_ALLIGATOR : break; case IND_AMA : break; case IND_BANDS : break; case IND_BWMFI : break; case IND_DEMA : break; case IND_ENVELOPES : break; case IND_FRACTALS : break; case IND_FRAMA : break; case IND_GATOR : break; case IND_ICHIMOKU : break; case IND_MA : break; case IND_MACD : break; case IND_RVI : break; case IND_SAR : break; case IND_STOCHASTIC : break; case IND_TEMA : break; case IND_VIDYA : break; default: break; } return false; } //+------------------------------------------------------------------+
I have considered all three methods in the previous article. In the current one, I have simply combined the cases, in which the same handler should be made.
The very end of the handler requires the return or break operator, so that handling the switch value does not pass to other cases having another handler.
Now we need to improve the CEngine library main object class in \MQL5\Include\DoEasy\Engine.mqh.
The methods for creating standard indicators and buffer objects for them are set in the buffer collection class (some methods that have not yet been considered now simply return INVALID_HANDLE), and we need to set access to all these methods for the program in the CEngine class.
In the public class section, write the call of all these methods://--- The methods of creating standard indicators and buffer objects for them //--- Create the standard Accelerator Oscillator indicator bool BufferCreateAC(const string symbol,const ENUM_TIMEFRAMES timeframe,const int id) { return(this.m_buffers.CreateAC(symbol,timeframe,id)!=INVALID_HANDLE); } //--- Create the standard Accumulation/Distribution indicator bool BufferCreateAD(const string symbol,const ENUM_TIMEFRAMES timeframe,const ENUM_APPLIED_VOLUME applied_volume,const int id) { return(this.m_buffers.CreateAD(symbol,timeframe,applied_volume,id)!=INVALID_HANDLE); } //--- Create the standard ADX indicator bool BufferCreateADX(const string symbol,const ENUM_TIMEFRAMES timeframe,const int adx_period,const int id) { return(this.m_buffers.CreateADX(symbol,timeframe,adx_period,id)!=INVALID_HANDLE); } //--- Create the standard ADX Wilder indicator bool BufferCreateADXWilder(const string symbol,const ENUM_TIMEFRAMES timeframe,const int adx_period,const int id) { return(this.m_buffers.CreateADXWilder(symbol,timeframe,adx_period,id)!=INVALID_HANDLE); } //--- Create the standard Alligator indicator bool BufferCreateAlligator(const string symbol,const ENUM_TIMEFRAMES timeframe, const int jaw_period,const int jaw_shift, const int teeth_period,const int teeth_shift, const int lips_period,const int lips_shift, const ENUM_MA_METHOD ma_method,const ENUM_APPLIED_PRICE applied_price,const int id) { return(this.m_buffers.CreateAlligator(symbol,timeframe, jaw_period,jaw_shift, teeth_period,teeth_shift, lips_period,lips_shift, ma_method,applied_price,id)!=INVALID_HANDLE); } //--- Create the standard Adaprive Moving Average indicator bool BufferCreateAMA(const string symbol,const ENUM_TIMEFRAMES timeframe, const int ama_period, const int fast_ema_period, const int slow_ema_period, const int ama_shift, const ENUM_APPLIED_PRICE applied_price, const int id) { return(this.m_buffers.CreateAMA(symbol,timeframe, ama_period, fast_ema_period,slow_ema_period, ama_shift,applied_price,id)!=INVALID_HANDLE); } //--- Create the standard Awesome Oscillator indicator bool BufferCreateAO(const string symbol,const ENUM_TIMEFRAMES timeframe,const int id) { return(this.m_buffers.CreateAO(symbol,timeframe,id)!=INVALID_HANDLE); } //--- Create the standard Average True Range indicator bool BufferCreateATR(const string symbol,const ENUM_TIMEFRAMES timeframe,const int ma_period,const int id) { return(this.m_buffers.CreateATR(symbol,timeframe,ma_period,id)!=INVALID_HANDLE); } //--- Create the standard Bollinger Bands indicator bool BufferCreateBands(const string symbol,const ENUM_TIMEFRAMES timeframe, const int bands_period, const int bands_shift, const double deviation, const ENUM_APPLIED_PRICE applied_price, const int id) { return(this.m_buffers.CreateBands(symbol,timeframe, bands_period,bands_shift, deviation,applied_price,id)!=INVALID_HANDLE); } //--- Create the standard Bears Power indicator bool BufferCreateBearsPower(const string symbol,const ENUM_TIMEFRAMES timeframe,const int ma_period,const int id) { return(this.m_buffers.CreateBearsPower(symbol,timeframe,ma_period,id)!=INVALID_HANDLE); } //--- Create the standard Bulls Power indicator bool BufferCreateBullsPower(const string symbol,const ENUM_TIMEFRAMES timeframe,const int ma_period,const int id) { return(this.m_buffers.CreateBullsPower(symbol,timeframe,ma_period,id)!=INVALID_HANDLE); } //--- Create the standard Chaikin Oscillator indicator bool BufferCreateChaikin(const string symbol,const ENUM_TIMEFRAMES timeframe, const int fast_ma_period, const int slow_ma_period, const ENUM_MA_METHOD ma_method, const ENUM_APPLIED_VOLUME applied_volume, const int id) { return(this.m_buffers.CreateChaikin(symbol,timeframe, fast_ma_period,slow_ma_period, ma_method,applied_volume,id)!=INVALID_HANDLE); } //--- Create the standard Commodity Channel Index indicator bool BufferCreateCCI(const string symbol,const ENUM_TIMEFRAMES timeframe, const int ma_period, const ENUM_APPLIED_PRICE applied_price, const int id) { return(this.m_buffers.CreateCCI(symbol,timeframe,ma_period,applied_price,id)!=INVALID_HANDLE); } //--- Create the standard DEMA indicator bool BufferCreateDEMA(const string symbol,const ENUM_TIMEFRAMES timeframe, const int ma_period, const int ma_shift, const ENUM_APPLIED_PRICE applied_price, const int id) { return(this.m_buffers.CreateDEMA(symbol,timeframe,ma_period,ma_shift,applied_price,id)!=INVALID_HANDLE); } //--- Create the standard DeMarker indicator bool BufferCreateDeMarker(const string symbol,const ENUM_TIMEFRAMES timeframe,const int ma_period,const int id) { return(this.m_buffers.CreateDeMarker(symbol,timeframe,ma_period,id)!=INVALID_HANDLE); } //--- Create the standard Envelopes indicator bool BufferCreateEnvelopes(const string symbol,const ENUM_TIMEFRAMES timeframe, const int ma_period, const int ma_shift, const ENUM_MA_METHOD ma_method, const ENUM_APPLIED_PRICE applied_price, const double deviation, const int id) { return(this.m_buffers.CreateEnvelopes(symbol,timeframe, ma_period,ma_shift,ma_method, applied_price,deviation,id)!=INVALID_HANDLE); } //--- Create the standard Force Index indicator bool BufferCreateForce(const string symbol,const ENUM_TIMEFRAMES timeframe, const int ma_period, const ENUM_MA_METHOD ma_method, const ENUM_APPLIED_VOLUME applied_volume, const int id) { return(this.m_buffers.CreateForce(symbol,timeframe,ma_period,ma_method,applied_volume,id)!=INVALID_HANDLE); } //--- Create the standard Fractals indicator bool BufferCreateFractals(const string symbol,const ENUM_TIMEFRAMES timeframe,const int id) { return(this.m_buffers.CreateFractals(symbol,timeframe,id)!=INVALID_HANDLE); } //--- Create the standard FrAMA indicator bool BufferCreateFrAMA(const string symbol,const ENUM_TIMEFRAMES timeframe, const int ma_period, const int ma_shift, const ENUM_APPLIED_PRICE applied_price, const int id) { return(this.m_buffers.CreateFrAMA(symbol,timeframe,ma_period,ma_shift,applied_price,id)!=INVALID_HANDLE); } //--- Create the standard Gator indicator bool BufferCreateGator(const string symbol,const ENUM_TIMEFRAMES timeframe, const int jaw_period, const int jaw_shift, const int teeth_period, const int teeth_shift, const int lips_period, const int lips_shift, const ENUM_MA_METHOD ma_method, const ENUM_APPLIED_PRICE applied_price, const int id) { return(this.m_buffers.CreateGator(symbol,timeframe, jaw_period,jaw_shift, teeth_period,teeth_shift, lips_period,lips_shift, ma_method,applied_price,id)!=INVALID_HANDLE); } //--- Create the standard Ichimoku indicator bool BufferCreateIchimoku(const string symbol,const ENUM_TIMEFRAMES timeframe, const int tenkan_sen, const int kijun_sen, const int senkou_span_b, const int id) { return(this.m_buffers.CreateIchimoku(symbol,timeframe,tenkan_sen,kijun_sen,senkou_span_b,id)!=INVALID_HANDLE); } //--- Create the standard BW MFI indicator bool BufferCreateBWMFI(const string symbol,const ENUM_TIMEFRAMES timeframe, const ENUM_APPLIED_VOLUME applied_volume, const int id) { return(this.m_buffers.CreateBWMFI(symbol,timeframe,applied_volume,id)!=INVALID_HANDLE); } //--- Create the standard Momentum indicator bool BufferCreateMomentum(const string symbol,const ENUM_TIMEFRAMES timeframe, const int mom_period, const ENUM_APPLIED_PRICE applied_price, const int id) { return(this.m_buffers.CreateMomentum(symbol,timeframe,mom_period,applied_price,id)!=INVALID_HANDLE); } //--- Create the standard Money Flow Index indicator bool BufferCreateMFI(const string symbol,const ENUM_TIMEFRAMES timeframe, const int ma_period, const ENUM_APPLIED_VOLUME applied_volume, const int id) { return(this.m_buffers.CreateMFI(symbol,timeframe,ma_period,applied_volume,id)!=INVALID_HANDLE); } //--- Create the standard Moving Average indicator bool BufferCreateMA(const string symbol,const ENUM_TIMEFRAMES timeframe, const int ma_period, const int ma_shift, const ENUM_MA_METHOD ma_method, const ENUM_APPLIED_PRICE applied_price, const int id) { return(this.m_buffers.CreateMA(symbol,timeframe,ma_period,ma_shift,ma_method,applied_price,id)!=INVALID_HANDLE); } //--- Create the standard Moving Average of Oscillator indicator bool BufferCreateOsMA(const string symbol,const ENUM_TIMEFRAMES timeframe, const int fast_ema_period, const int slow_ema_period, const int signal_period, const ENUM_APPLIED_PRICE applied_price, const int id) { return(this.m_buffers.CreateOsMA(symbol,timeframe,fast_ema_period,slow_ema_period,signal_period,applied_price,id)!=INVALID_HANDLE); } //--- Create the standard MACD indicator bool BufferCreateMACD(const string symbol,const ENUM_TIMEFRAMES timeframe, const int fast_ema_period, const int slow_ema_period, const int signal_period, const ENUM_APPLIED_PRICE applied_price, const int id) { return(this.m_buffers.CreateMACD(symbol,timeframe,fast_ema_period,slow_ema_period,signal_period,applied_price,id)!=INVALID_HANDLE); } //--- Create the standard On Balance Volume indicator bool BufferCreateOBV(const string symbol,const ENUM_TIMEFRAMES timeframe, const ENUM_APPLIED_VOLUME applied_volume, const int id) { return(this.m_buffers.CreateOBV(symbol,timeframe,applied_volume,id)!=INVALID_HANDLE); } //--- Create the standard Parabolic SAR indicator bool BufferCreateSAR(const string symbol,const ENUM_TIMEFRAMES timeframe, const double step, const double maximum, const int id) { return(this.m_buffers.CreateSAR(symbol,timeframe,step,maximum,id)!=INVALID_HANDLE); } //--- Create the standard Relative Strength Index indicator bool BufferCreateRSI(const string symbol,const ENUM_TIMEFRAMES timeframe, const int ma_period, const ENUM_APPLIED_PRICE applied_price, const int id) { return(this.m_buffers.CreateRSI(symbol,timeframe,ma_period,applied_price,id)!=INVALID_HANDLE); } //--- Create the standard RVI indicator bool BufferCreateRVI(const string symbol,const ENUM_TIMEFRAMES timeframe,const int ma_period,const int id) { return(this.m_buffers.CreateRVI(symbol,timeframe,ma_period,id)!=INVALID_HANDLE); } //--- Create the standard Standard Deviation indicator bool BufferCreateStdDev(const string symbol,const ENUM_TIMEFRAMES timeframe, const int ma_period, const int ma_shift, const ENUM_MA_METHOD ma_method, const ENUM_APPLIED_PRICE applied_price, const int id) { return(this.m_buffers.CreateStdDev(symbol,timeframe,ma_period,ma_shift,ma_method,applied_price,id)!=INVALID_HANDLE); } //--- Create the standard Stochastic indicator bool BufferCreateStochastic(const string symbol,const ENUM_TIMEFRAMES timeframe, const int Kperiod, const int Dperiod, const int slowing, const ENUM_MA_METHOD ma_method, const ENUM_STO_PRICE price_field, const int id) { return(this.m_buffers.CreateStochastic(symbol,timeframe,Kperiod,Dperiod,slowing,ma_method,price_field,id)!=INVALID_HANDLE); } //--- Create the standard TEMA indicator bool BufferCreateTEMA(const string symbol,const ENUM_TIMEFRAMES timeframe, const int ma_period, const int ma_shift, const ENUM_APPLIED_PRICE applied_price, const int id) { return(this.m_buffers.CreateTEMA(symbol,timeframe,ma_period,ma_shift,applied_price,id)!=INVALID_HANDLE); } //--- Create the standard Triple Exponential Average indicator bool BufferCreateTriX(const string symbol,const ENUM_TIMEFRAMES timeframe, const int ma_period, const ENUM_APPLIED_PRICE applied_price, const int id) { return(this.m_buffers.CreateTriX(symbol,timeframe,ma_period,applied_price,id)!=INVALID_HANDLE); } //--- Create the standard William's Percent Range indicator bool BufferCreateWPR(const string symbol,const ENUM_TIMEFRAMES timeframe,const int calc_period,const int id) { return(this.m_buffers.CreateWPR(symbol,timeframe,calc_period,id)!=INVALID_HANDLE); } //--- Create the standard VIDYA indicator bool BufferCreateVIDYA(const string symbol,const ENUM_TIMEFRAMES timeframe, const int cmo_period, const int ema_period, const int ma_shift, const ENUM_APPLIED_PRICE applied_price, const int id) { return(this.m_buffers.CreateVIDYA(symbol,timeframe,cmo_period,ema_period,ma_shift,applied_price,id)!=INVALID_HANDLE); } //--- Create the standard Volumes indicator bool BufferCreateVolumes(const string symbol,const ENUM_TIMEFRAMES timeframe,const ENUM_APPLIED_VOLUME applied_volume,const int id) { return(this.m_buffers.CreateVolumes(symbol,timeframe,applied_volume,id)!=INVALID_HANDLE); } //--- Initialize all drawn buffers by a (1) specified value, (2) empty value set for the buffer object
The methods in the collection class return int type, or, to be more precise, the handle of the created standard indicator. Here I will implement the return of the bool type value. This simplifies working with the methods from the final program. Therefore, these methods return the result of comparing the value returned by the appropriate method of the buffer collection class with the INVALID_HANDLE value.
In the public class section, write two methods — the method of preparing the calculated buffer data for all created standard indicators returning the result of the same-name buffer object collection class method I have considered above and declare the method returning the description of the buffer object by standard indicator type and its ID:
//--- Prepare data of the calculated buffer of all created standard indicators bool BufferPreparingDataAllBuffersStdInd(void) { return this.m_buffers.PreparingDataAllBuffersStdInd(); } //--- Return the standard indicator buffer description by type and ID string BufferGetLabelByTypeID(const ENUM_INDICATOR ind_type,const int id); //--- Display short description of all indicator buffers of the buffer collection void BuffersPrintShort(void);
Implement the method beyond the class body:
//+------------------------------------------------------------------+ //| Return the standard indicator buffer description | //| by type and ID | //+------------------------------------------------------------------+ string CEngine::BufferGetLabelByTypeID(const ENUM_INDICATOR ind_type,const int id) { CArrayObj *list=m_buffers.GetListBufferByTypeID(ind_type,id); if(list==NULL || list.Total()==0) return ""; CBuffer *buff=list.At(0); if(buff==NULL) return ""; return buff.Label(); } //+------------------------------------------------------------------+
Here all is simple: obtain the list of buffer objects with the appropriate standard indicator type (passed to the method) and its ID.
Thus, there will be two such buffer objects in the list — drawn and calculated one. There is no need to care about the exact object to receive data from, since both objects contain similar data. Therefore, obtain the pointer to the first buffer object in the list and return its description.
While implementing the OnDoEasyEvent() method in the "New bar" event handling block, let's slightly correct obtaining the amount of copied data:
//--- Handling timeseries events else if(idx>SERIES_EVENTS_NO_EVENT && idx<SERIES_EVENTS_NEXT_CODE) { //--- "New bar" event if(idx==SERIES_EVENTS_NEW_BAR) { ::Print(DFUN,TextByLanguage("Новый бар на ","New Bar on "),sparam," ",TimeframeDescription((ENUM_TIMEFRAMES)dparam),": ",TimeToString(lparam)); CArrayObj *list=this.m_buffers.GetListBuffersWithID(); if(list!=NULL) { int total=list.Total(); for(int i=0;i<total;i++) { CBuffer *buff=list.At(i); if(buff==NULL) continue; string symbol=sparam; ENUM_TIMEFRAMES timeframe=(ENUM_TIMEFRAMES)dparam; if(buff.TypeBuffer()==BUFFER_TYPE_DATA || buff.IndicatorType()==WRONG_VALUE) continue; if(buff.Symbol()==symbol && buff.Timeframe()==timeframe ) { CSeriesDE *series=this.SeriesGetSeries(symbol,timeframe); if(series==NULL) continue; int count=::fmin((int)series.AvailableUsedData(),::fmin(buff.GetDataTotal(),buff.IndicatorBarsCalculated())); this.m_buffers.PreparingDataBufferStdInd(buff.IndicatorType(),buff.ID(),count); } } } } //--- "Bars skipped" event if(idx==SERIES_EVENTS_MISSING_BARS) { ::Print(DFUN,TextByLanguage("Пропущены бары на ","Missed bars on "),sparam," ",TimeframeDescription((ENUM_TIMEFRAMES)dparam),": ",(string)lparam); } } //--- Handling trading events
Previously, in the highlighted string, we obtained the minimum value from the total amount of buffer object data and calculated standard indicator data:
int count=::fmin(buff.GetDataTotal(),buff.IndicatorBarsCalculated());
However, since the number of used timeseries data is set to 1000 bars in the library (and thus, in the program), now we will copy the minimum value from three data sources — total number of buffer object data, calculated standard indicator data and the amount of default data (1000). Usually, the value of 1000 is always less than the other two, and this is more beneficial when bulk copying data.
This concludes the current improvements of the library classes.
Test
In order to test the creation of single-buffer multi-symbol multi-period standard indicators drawn in a subwindow, I will do as follows:
create a custom indicator having the following selection in the settings:
- Used symbol the standard indicator is created on
- Used chart period (timeframe) the standard indicator is created on
- Type of the created single-buffer multi-symbol multi-period standard indicator
Data of the selected standard indicator is displayed in the subwindow of the current symbol/period main chart.
Let's use the indicator from the previous article and save it in \MQL5\Indicators\TestDoEasy\Part48\ as TestDoEasyPart48.mq5.
In the indicator inputs, add the parameter allowing us to select a standard indicator to be displayed:
//| TestDoEasyPart48.mq5 | //| 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" //--- includes #include <DoEasy\Engine.mqh> //--- properties #property indicator_separate_window #property indicator_buffers 3 #property indicator_plots 1 //--- classes //--- enums //--- defines //--- structures //--- input variables sinput string InpUsedSymbols = "GBPUSD"; // Used symbol (one only) sinput ENUM_TIMEFRAMES InpPeriod = PERIOD_M30; // Used chart period sinput ENUM_INDICATOR InpIndType = IND_AC; // Type standard indicator //--- sinput bool InpUseSounds = true; // Use sounds //--- indicator buffers //--- global variables ENUM_SYMBOLS_MODE InpModeUsedSymbols= SYMBOLS_MODE_DEFINES; // Mode of used symbols list ENUM_TIMEFRAMES_MODE InpModeUsedTFs = TIMEFRAMES_MODE_LIST; // Mode of used timeframes list string InpUsedTFs; // List of used timeframes CEngine engine; // CEngine library main object string prefix; // Prefix of graphical object names int min_bars; // The minimum number of bars for the indicator calculation int used_symbols_mode; // Mode of working with symbols string array_used_symbols[]; // The array for passing used symbols to the library string array_used_periods[]; // The array for passing used timeframes to the library //+------------------------------------------------------------------+
Only standard indicators having one buffer for construction and displayed in a subwindow will work with the custom indicator. In the OnInit() handler, standard indicators of a suitable type are created in the library. Let's implement the error message for the remaining indicators:
//+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- Write the name of the working timeframe selected in the settings to the InpUsedTFs variable InpUsedTFs=TimeframeDescription(InpPeriod); //--- Initialize DoEasy library OnInitDoEasy(); //--- Set indicator global variables prefix=engine.Name()+"_"; //--- calculate the number of bars of the current period fitting in the maximum used period //--- Use the obtained value if it exceeds 2, otherwise use 2 int num_bars=NumberBarsInTimeframe(InpPeriod); min_bars=(num_bars>2 ? num_bars : 2); //--- Check and remove remaining indicator graphical objects if(IsPresentObectByPrefix(prefix)) ObjectsDeleteAll(0,prefix); //--- Create the button panel //--- Check playing a standard sound using macro substitutions engine.PlaySoundByDescription(SND_OK); //--- Wait for 600 milliseconds engine.Pause(600); engine.PlaySoundByDescription(SND_NEWS); //--- indicator buffers mapping //--- Create all the necessary buffer objects for constructing a selected standard indicator bool success=false; switch(InpIndType) { case IND_AC : success=engine.BufferCreateAC(InpUsedSymbols,InpPeriod,1); break; case IND_AD : success=engine.BufferCreateAD(InpUsedSymbols,InpPeriod,VOLUME_TICK,1); break; case IND_AO : success=engine.BufferCreateAO(InpUsedSymbols,InpPeriod,1); break; case IND_ATR : success=engine.BufferCreateATR(InpUsedSymbols,InpPeriod,14,1); break; case IND_BEARS : success=engine.BufferCreateBearsPower(InpUsedSymbols,InpPeriod,13,1); break; case IND_BULLS : success=engine.BufferCreateBullsPower(InpUsedSymbols,InpPeriod,13,1); break; case IND_CHAIKIN : success=engine.BufferCreateChaikin(InpUsedSymbols,InpPeriod,3,10,MODE_EMA,VOLUME_TICK,1); break; case IND_CCI : success=engine.BufferCreateCCI(InpUsedSymbols,InpPeriod,14,PRICE_TYPICAL,1); break; case IND_DEMARKER : success=engine.BufferCreateDeMarker(InpUsedSymbols,InpPeriod,14,1); break; case IND_FORCE : success=engine.BufferCreateForce(InpUsedSymbols,InpPeriod,13,MODE_SMA,VOLUME_TICK,1); break; case IND_MOMENTUM : success=engine.BufferCreateMomentum(InpUsedSymbols,InpPeriod,14,PRICE_CLOSE,1); break; case IND_MFI : success=engine.BufferCreateMFI(InpUsedSymbols,InpPeriod,14,VOLUME_TICK,1); break; case IND_OSMA : success=engine.BufferCreateOsMA(InpUsedSymbols,InpPeriod,12,26,9,PRICE_CLOSE,1); break; case IND_OBV : success=engine.BufferCreateOBV(InpUsedSymbols,InpPeriod,VOLUME_TICK,1); break; case IND_RSI : success=engine.BufferCreateRSI(InpUsedSymbols,InpPeriod,14,PRICE_CLOSE,1); break; case IND_STDDEV : success=engine.BufferCreateStdDev(InpUsedSymbols,InpPeriod,20,0,MODE_SMA,PRICE_CLOSE,1); break; case IND_TRIX : success=engine.BufferCreateTriX(InpUsedSymbols,InpPeriod,14,PRICE_CLOSE,1); break; case IND_WPR : success=engine.BufferCreateWPR(InpUsedSymbols,InpPeriod,14,1); break; case IND_VOLUMES : success=engine.BufferCreateVolumes(InpUsedSymbols,InpPeriod,VOLUME_TICK,1); break; default: break; } if(!success) { Print(TextByLanguage("Ошибка. Индикатор не создан","Error. Indicator not created")); return INIT_FAILED; } //--- Check the number of buffers specified in the 'properties' block
Each of standard indicators drawn in a subwindow has its own number of displayed decimal places. Besides, some of them also draw level lines. Let's create an individual set for each of the used standard indicators, in which we are to specify the decimal capacity of displayed data and define levels provided that the standard indicator has them:
//--- Display short descriptions of created indicator buffers engine.BuffersPrintShort(); //--- Set levels where they are required and define the data decimal capacity int digits=(int)SymbolInfoInteger(InpUsedSymbols,SYMBOL_DIGITS); switch(InpIndType) { case IND_AC : digits+=2; break; case IND_AD : digits=0; break; case IND_AO : digits+=1; break; case IND_ATR : break; case IND_BEARS : digits+=1; break; case IND_BULLS : digits+=1; break; case IND_CHAIKIN : digits=0; break; case IND_CCI : IndicatorSetInteger(INDICATOR_LEVELS,2); IndicatorSetDouble(INDICATOR_LEVELVALUE,0,100); IndicatorSetDouble(INDICATOR_LEVELVALUE,1,-100); digits=2; break; case IND_DEMARKER : IndicatorSetInteger(INDICATOR_LEVELS,2); IndicatorSetDouble(INDICATOR_LEVELVALUE,0,0.7); IndicatorSetDouble(INDICATOR_LEVELVALUE,1,0.3); digits=3; break; case IND_FORCE : digits+=1; break; case IND_MOMENTUM : digits=2; break; case IND_MFI : IndicatorSetInteger(INDICATOR_LEVELS,2); IndicatorSetDouble(INDICATOR_LEVELVALUE,0,80); IndicatorSetDouble(INDICATOR_LEVELVALUE,1,20); break; case IND_OSMA : digits+=2; break; case IND_OBV : digits=0; break; case IND_RSI : IndicatorSetInteger(INDICATOR_LEVELS,3); IndicatorSetDouble(INDICATOR_LEVELVALUE,0,70); IndicatorSetDouble(INDICATOR_LEVELVALUE,1,50); IndicatorSetDouble(INDICATOR_LEVELVALUE,2,30); digits=2; break; case IND_STDDEV : digits+=1; break; case IND_TRIX : break; case IND_WPR : IndicatorSetInteger(INDICATOR_LEVELS,2); IndicatorSetDouble(INDICATOR_LEVELVALUE,0,-80); IndicatorSetDouble(INDICATOR_LEVELVALUE,1,-20); digits=2; break; case IND_VOLUMES : digits=0; break; default: IndicatorSetInteger(INDICATOR_LEVELS,0); break; } //--- Set the short name for the indicator and bit depth string label=engine.BufferGetLabelByTypeID(InpIndType,1); IndicatorSetString(INDICATOR_SHORTNAME,label); IndicatorSetInteger(INDICATOR_DIGITS,digits); //--- Successful return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+
The OnCalculate() handler has been greatly simplified as compared to the previous test indicator. This is because I have created the necessary methods for working with standard indicators, and all we need is prepare standard indicator data and display the calculated data of the standard indicator on the current chart in the main loop:
//+------------------------------------------------------------------+ //| 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 engine.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(); engine.BufferPreparingDataAllBuffersStdInd(); } //--- Prepare data //--- Fill in calculated buffers of all created standard indicators with data int bars_total=engine.SeriesGetBarsTotal(InpUsedSymbols,InpPeriod); int total_copy=(limit<min_bars ? min_bars : fmin(limit,bars_total)); if(!engine.BufferPreparingDataAllBuffersStdInd()) return 0; //--- Calculate the indicator //--- Main calculation loop of the indicator for(int i=limit; i>WRONG_VALUE && !IsStopped(); i--) { engine.GetBuffersCollection().SetDataBufferStdInd(InpIndType,1,i,time[i]); } //--- return value of prev_calculated for next call return(rates_total); } //+------------------------------------------------------------------+
This is all that needs to be done for creating the test indicator.
The full indicator code is provided in the files attached below.
Compile the created indicator, set GBPUSD M5 in the settings and launch the indicator on EURUSD M1:
The figure does not show all possible indicators but the main things are displayed: the indicator draws a selected standard indicator based on data different from the current chart data. The level lines are drawn where needed.
What's next?
In the next article, I will start implementing the methods for creating standard indicators, displaying data in the main chart window, and indicators having several drawn buffers.
All files of the current version of the library are attached below together with the test indicator files for you to test and download.
Leave your questions, comments and suggestions in the comments.
Please keep in mind that here I have developed the MQL5 test indicator for MetaTrader 5.
The attached files are intended only for MetaTrader 5. The current library version has not been tested in MetaTrader 4.
After developing and testing the functionality for working with indicator buffers, I will try to implement some MQL5 features in MetaTrader 4.
Previous articles within the series:
Timeseries in DoEasy library (part 35): Bar object and symbol timeseries listTimeseries in DoEasy library (part 36): Object of timeseries for all used symbol periods
Timeseries in DoEasy library (part 37): Timeseries collection - database of timeseries by symbols and periods
Timeseries in DoEasy library (part 38): Timeseries collection - real-time updates and accessing data from the program
Timeseries in DoEasy library (part 39): Library-based indicators - preparing data and timeseries events
Timeseries in DoEasy library (part 40): Library-based indicators - updating data in real time
Timeseries in DoEasy library (part 41): Sample multi-symbol multi-period indicator
Timeseries in DoEasy library (part 42): Abstract indicator buffer object class
Timeseries in DoEasy library (part 43): Classes of indicator buffer objects
Timeseries in DoEasy library (part 44): Collection class of indicator buffer objects
Timeseries in DoEasy library (part 45): Multi-period indicator buffers
Timeseries in DoEasy library (part 46): Multi-period multi-symbol indicator buffers
Timeseries in DoEasy library (part 47): Multi-period multi-symbol standard indicators
Translated from Russian by MetaQuotes Ltd.
Original article: https://www.mql5.com/ru/articles/8257
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use