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 momentum using a ring buffer - MetaTrader 5 script | Forex indicator download - MT4/MT5 resources

author EAcpu | 4 reads | 0 comments |

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

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

The CMomentumOnRingBuffer class is designed for calculating the technical indicator Momentum ( Momentum ) using an algorithmic ring buffer .

 Class CMomentumOnRingBuffer: publicCArrayRing

title

 #include

Files of the CMomentumOnRingBuffer.mqh class must be placed in the IncOnRingBuffer folder and do not need 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 ( // returns false if an error occurs, success - true integer period = 14 , // momentum period integer buffer size = 256 , // size of ring buffer boolean as_series = false // true if time series, otherwise - false );
 //--- Calculation method based on time series or indicator buffer:
Integer MainOnArray( // Returns the number of elements processed constant integer rate_total, // the size of the array array[] const integer previously calculated, // the number of elements processed on the last call constant double & bulk[], // the array of input data );
 //--- Array-based calculation method for individual series elements
double the main value ( // return value momentum for the set element constant integer rate_total, // the size of the array constant integer previously calculated, // the processed array element constant integer start, // the place where the important data of the array starts constant long value, // the element value of the array constant integer exponent // element index );
 //--- Methods to access data:
Integer BarRequired(); // Returns the number of bars required to draw the indicator
string name(); // returns the name of the indicator
integer period (); // returns period
integer size(); // Returns the size of the ring buffer

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

//--- Class with methods for calculating the momentum indicator:
#include CMomentumOnRingBuffer momentum;

... //+----------------------------------------------------------------------------------+
//|Custom indicator iteration function |
//+------------------------------------------------------------------+
When integer calculation ( const integer rate_total, // size of array prices[] constant integer previously calculated, // constant integer start of bar processed at last call , // const double & price[] where important data starts ) // array for calculation { //--- calculation of indicators based on time series: Momentum.MainOnArray(rates_total,prev_calculated,price);

... //--- use data from the "momentum" ring buffer,
// For example, copy the data in the indicator buffer: for ( integer i = start;i  Stopped();i++)
      Momentum_Buffer[i] = Momentum[rates_total- 1 -i]; // Indicator line ... //--- Return the prev_calculated value for the next call: return (rates_total);
  }

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

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

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

The class for drawing the Momentum using the ring buffer - indicator for MetaTrader 5
Working result of Test_Momentum_OnValueRB.mq5 with ring buffer size of 256 elements

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


Attachment download

📎 cmomentumonringbuffer.mqh (6.76 KB)

📎 carrayring.mqh (6.64 KB)

📎 test_momentum_onarrayrb.mq5 (3.02 KB)

📎 test_momentum_onvaluerb.mq5 (3.45 KB)

Source: MQL5 #1396

Verification code Refresh