Expert Advisors • Indicators • Scripts • Libraries

MQL.RobotFX.org is the biggest collection of MetaTrader expert advisors (MT5 & MT4), indicators, scripts and libraries that can be used to improve trading results, minimize risks or simply automate trading tasks

Metatrader 5 Indicators | The class for drawing the TEMA using the ring buffer | MQL5 Code

RobotFX curates the best open-source MetaTrader code to inspire your trading automation.

//--- the class with the methods of calculation of the TEMA indicator:
#include <IncOnRingBuffer\CTEMAOnRingBuffer.mqh>
CTEMAOnRingBuffer tema;

...

//+------------------------------------------------------------------+
//| 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 calculation
  {
//--- calculation of the indicator based on a time series:
    tema.MainOnArray(rates_total,prev_calculated,price);

...

//--- use the data from the "tema" ring buffer,
//    for example, copy the data in the indicator buffer:
   for(int i=start;i<rates_total && !IsStopped();i++)
      TEMA_Buffer[i] = tema[rates_total-1-i]; // the TEMA indicator line

...

//--- return value of prev_calculated for next call:
   return(rates_total);
  }

When calculation the TEMA also calculation of the Moving Average and DEMA with the same parameters is performed. We can get the data from the MA ring buffer and DEMA using the MA method(int index) and DEMA(int index), respectively:

//--- use the data from the Moving Average and DEMA ring buffers,
//    for example, copy them in the indicator buffers:
   for(int i=start;i<rates_total && !IsStopped();i++)
     {
      MA_Buffer[i]   = dema.MA(rates_total-1-i);   // the Moving Average indicator line
      DEMA_Buffer[i] = dema.DEMA(rates_total-1-i); // the DEMA indicator line
     }

Please note that indexing in the ring buffers is the same as in the time series.

Trade the powerful Traders Dynamic Index strategy automatically with this dedicated TDI Expert Advisor. More details.

Examples

  1. The indicator calculates the Test_TEMA_OnArrayRB.mq5 file on the basis of the price time series. The MainOnArray() method application is demonstrated
  2. The Test_TEMA_OnValueRB.mq5 demonstrates the use of the MainOnValue() method. At first the TEMA indicator is calculated and drawn. Then on the basis of this indicator ring buffer one more TEMA is drawn.


The result of the work of the Test_TEMA_OnArrayRB.mq5 with the size of the ring buffer of 256 elements



The result of the work of the Test_TEMA_OnValueRB.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.

Level up your trading with professional RobotFX expert advisors and indicators. Visit www.robotfx.org for proven MT4/MT5 tools.

1417

Best MetaTrader Indicators + Profitable Expert Advisors