Welcome Forex EA downloads & MT4/MT5 auto-trading resources — EAs, Gold EAs, quant tools and real-world automation.
Sign In Sign Up

Class for drawing moving averages using ring buffers - MetaTrader 5 Script | Trading Script Download - MT4/MT5 Resources

author EAcpu | 4 reads | 0 comments |

The class for drawing Moving Average using the ring buffer - indicator for MetaTrader 5

The class for drawing Moving Average using the ring buffer - indicator for MetaTrader 5

The CMAOnRingBuffer class is designed to calculate a moving average (Moving Average) ring buffer using the following algorithm.

 Class CMAOnRingBuffer: publicCArrayRing

title

 #include

Files of the CMAOnRingBuffer.mqh class should be placed in the IncOnRingBuffer folder that needs to be created in MQL5\Include\. Attached to the description are two files containing examples used by the classes in this folder. A file ring buffer of class . must also be in this folder.

 //--- Initialization method:
Boolean initialization ( // Return false if error, true if success integer ma_period = 14 , // Moving average smoothing period ENUM_MA_METHOD ma_method = mode_SMA , // Moving average smoothing method integer buffer size = 256 , // Ring buffer size, number of stored data boolean as_series = false // true if time series, false if usual index of input data );
 //--- Calculation method based on time series or indicator buffer:
Integer MainOnArray( // Returns the number of processed elements in the array constant integer rate_total, // the size of the array array[] constant integer previously calculated, // the array elements processed in the last call constant double & bulk[] // the array used for calculation );
 //--- Array-based calculation method for individual series elements
double major value ( // Returns the MA value of the collection element constant integer rate_total, // the size of this batch of constant integer previously calculated, // handles the elements in the array constant integer start, // where the important data of the array begins constant double value , // array element value constant integer exponent // array element index );
 //--- Methods to access data:
Integer BarRequired(); // Returns the number of bars required to draw the indicator
String name(); // Return indicator name
String MA Method(); // Returns the smooth method as a line of text
Integer MAPeriod(); // Returns the smoothing period

Calculated data of the indicator can be obtained from the ring buffer just like from a usual array. For example:

#include CMAOnRingBuffer ma;
... //+----------------------------------------------------------------------------------+
//|Custom indicator iteration function |
//+------------------------------------------------------------------+
integer calculation ( const integer rate_total,
constant integer previously calculated,
Starting with a constant integer ,
const double &price[])
  { //--- Indicator calculation: ma.MainOnArray(rates_total,prev_calculated,price);

...
//--- copy data from "ma" ring buffer to indicator: for ( integer i = start; i  1 -i];
    }
return (rate_total);
  }

Note that the index in the ring buffer is the same as the time series .

The class for drawing Moving Average using the ring buffer - indicator for MetaTrader 5

Working result of Test_MA_OnArrayRB.mq5, ring buffer size is 256 elements

The class for drawing Moving Average using the ring buffer - indicator for MetaTrader 5
Working result of Test_MA_OnValueRB.mq5, ring buffer size is 256 elements

When writing code for the development of MetaQuotes Software Inc. , Integer and Godzilla were used.


Attachment download

📎 cmaonringbuffer.mqh (14.9 KB)

📎 carrayring.mqh (6.57 KB)

📎 test_ma_onarrayrb.mq5 (2.61 KB)

📎 test_ma_onvaluerb.mq5 (3.27 KB)

Source: MQL5 #1342

Verification code Refresh