//+------------------------------------------------------------------+ //| CADXOnRingBuffer.mqh | //| Copyright 2012, Konstantin Gruzdev | //| https://login.mql5.com/ru/users/Lizar | //| Revision 01 Dec 2012 | //+------------------------------------------------------------------+ #property copyright "Copyright 2012, Konstantin Gruzdev" #property link "https://login.mql5.com/ru/users/Lizar" //--- Class to calculate the MA using the ring buffer: #include //+------------------------------------------------------------------+ //| Class CADXOnRingBuffer | //| Appointment: class is designed for the calculation of the | //| ADX indicator (Average Directional Movement Index, | //| ADX) using the class for working with the ring | //| buffer. | //| Link: http://www.mql5.com/ru/code/1343 | //+------------------------------------------------------------------+ class CADXOnRingBuffer { public: CMAOnRingBuffer pdi; // positive directional index CMAOnRingBuffer ndi; // negative directional index private: CMAOnRingBuffer m_adx; // average directional movement index string m_name; // indicator name bool m_as_series; // true, if the indexing as in time series int m_bars_required; // number of elements required to calculate int m_begin; // index of the first significant element int m_start; // index of element to start the calculation int m_index; // current element index double m_high; // maximal value double m_low; // minimal value double m_close; // closing price double m_phigh; // maximum value of the previous bar double m_plow; // minimum value of the previous bar double m_pclose; // closing price of the previous bar double m_PD; double m_ND; public: CADXOnRingBuffer() {} ~CADXOnRingBuffer() {} //--- initialization method: bool Init(int ma_period=14, ENUM_MA_METHOD ma_method=MODE_EMA, int size_buffer=256, bool as_series=false); //--- basic methods: int MainOnArray(const int rates_total, const int prev_calculated, const double &high[], const double &low[], const double &close[]); double MainOnValue(const int rates_total, const int prev_calculated, const int begin, const double high, const double low, const double close, const int index); //--- methods to get access to private data: int BarsRequired() { return(m_bars_required); } string NameADX() { return("ADX"+m_name); } string NameNDI() { return("-DI"+m_name); } string NamePDI() { return("+DI"+m_name); } string MAMethod() { return(m_adx.MAMethod()); } int MAPeriod() { return(m_adx.MAPeriod()); } int Size() { return(m_adx.Size()); } //--- returns the value of element with the specified index: double operator [](const int index) const { return(m_adx.At(index)); } private: //--- indicator calculation method: void ADX(const int rates_total, const int prev_calculated); }; //+------------------------------------------------------------------+ //| Initialization method | //+------------------------------------------------------------------+ bool CADXOnRingBuffer :: Init(int ma_period=14,ENUM_MA_METHOD ma_method=MODE_EMA, int size_buffer=256, bool as_series=false) { //--- initialize the CMAOnRingBuffer class instances: if(!pdi.Init(ma_period,ma_method,size_buffer)) return false; if(!ndi.Init(ma_period,ma_method,size_buffer)) return false; if(!m_adx.Init(ma_period,ma_method,size_buffer)) return false; //--- m_name="("+IntegerToString(ma_period)+","+MAMethod()+")"; //--- m_as_series=as_series; m_bars_required=m_adx.BarsRequired()+1; return true; } //+------------------------------------------------------------------+ //| Indicator on array | //+------------------------------------------------------------------+ int CADXOnRingBuffer :: MainOnArray(const int rates_total, const int prev_calculated, const double &high[], const double &low[], const double &close[]) { //--- save as_series flags: bool as_series_high = ArrayGetAsSeries(high); bool as_series_low = ArrayGetAsSeries(low); bool as_series_close = ArrayGetAsSeries(close); if(as_series_high) ArraySetAsSeries(high, false); if(as_series_low) ArraySetAsSeries(low, false); if(as_series_close) ArraySetAsSeries(close,false); //--- first calculation: if(prev_calculated==0) { for(int i=0;idTmpN) dTmpN=0.0; else { if(dTmpP