Discover expert developed MetaTrader tools that can complement professional solutions.
//--- the class with the methods of calculation of the DEMA indicator: #include <IncOnRingBuffer\CDEMAOnRingBuffer.mqh> CDEMAOnRingBuffer dema; ... //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ int OnCalculate (const int rates_total, // the size of the array price[] const int prev_calculated, // processed bars on the previous call const int begin, // from where the significant data starts const double& price[]) // the array for the calculation { //--- calculation of the indicator based on a time series: dema.MainOnArray(rates_total,prev_calculated,price); ... //--- use the data from the "dema" ring buffer, // for example, copy the data in the indicator buffer: for(int i=start;i<rates_total && !IsStopped();i++) DEMA_Buffer[i] = dema[rates_total-1-i]; // the DEMA indicator line ... //--- return value of prev_calculated for next call: return(rates_total); }
When calculation the DEMA also calculation of the Moving Average with the same parameters is performed. We can get the data from the MA ring buffer using the MA method(int index):
//--- use the data from the Moving Average ring buffer, // for example, copy the data in the indicator buffer: for(int i=start;i<rates_total && !IsStopped();i++) MA_Buffer[i] = dema.MA(rates_total-1-i); // the Moving Average indicator line
Please note that indexing in the ring buffers is the same as in the time series.
Grid trading done right – try the robust Grid Expert Advisor for controlled risk. Details here.
Examples
- The indicator calculates the Test_DEMA_OnArrayRB.mq5 file on the basis of the price time series. The MainOnArray() method application is demonstrated
- The Test_DEMA_OnValueRB.mq5 file demonstrates the use of the MainOnValue() method. At first the DEMA indicator is calculated and drawn. Then on the basis of this indicator ring buffer one more DEMA is drawn.
The result of the work of the Test_DEMA_OnArrayRB.mq5 with the size of the ring buffer of 256 elements When writing code the developments of MetaQuotes Software Corp., Integer and GODZILLA were used.

The result of the work of the Test_DEMA_OnValueRB.mq5 with the size of the ring buffer of 256 elements
Thanks for reading. Discover premium MetaTrader solutions at RobotFX.
1416