Class for drawing ATR using ring buffer - MetaTrader 5 script | Trading script download - MT4/MT5 resources









The CATROnRingBuffer class is designed for calculating the average true range indicator ( Average True Range , ATR) using an algorithmic ring buffer .
Class CATROn Ring Buffertitle
#includeThe CATROnRingBuffer.mqh file 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. Files for class Ringbuffer and class Moving Average must also be in this folder.
//--- Initialization method: Boolean initialization ( // Returns false if error, true if successful integer ma_period = 14 , // Period for moving average smoothing ENUM_MA_METHOD ma_method = mode_SMA , // Integer for moving average smoothing method 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 elements processed constant integer rate_total, // size of the array constant integer previously calculated, // elements processed on the last call constant double & high [] // maximum value array constant double & low [] // minimum value array constant double & close [] // array of closing prices );
//--- Calculation method based on individual series elements in an array double's primary value ( // returns the ATR value of the collection element constant integer rate_total, // the size of the array constant integer previously calculated, // the processed array element constant integer start, // the position starting from the valid value of the array constant double's high, // the maximum value constant double's low, // the minimum value constant double's close, // the closing price constant integer index // element index );
//--- Methods to access data: Integer BarRequired(); // Returns the number of bars required to draw the indicator StringName (); // Returns the name of the indicator String MA Method(); // Returns the smooth method as a line of text Integer MAPeriod(); // Returns the smoothing 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:
#includeCATROnRingBuffer atr; ... //+----------------------------------------------------------------------------------+ //|Custom indicator iteration function | //+------------------------------------------------------------------+ integer calculation ( const integer rate_total, constant integer previously calculated, const datetime &time[], const double & open[], const double &high[], const double &low[], const double &close[], constant long &scale[], const long &volume[], const int & spread[]) { //--- Indicator calculation: atr.MainOnArray(rates_total, prev_calculated, highest price, lowest price, closing price); ... //--- copy data from "atr" 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 .
Working result of Test_ATR_OnArrayRB.mq5, ring buffer size is 256 elements When writing code for the development of MetaQuotes Software Inc. , Integer and Godzilla were used. 📎 catronringbuffer.mqh (7.36 KB) 📎 cmaonringbuffer.mqh (14.9 KB) 📎 carrayring.mqh (6.64 KB) 📎 test_atr_onarrayrb.mq5 (2.89 KB) 📎 test_atr_onvaluerb.mq5 (3.49 KB) Source: MQL5 #1344

Working result of Test_ATR_OnValueRB.mq5, ring buffer size is 256 elements Attachment download
💡 Featured Recommendations
✍️ Latest by the author
- •
- •
- •
- •
- •
- •
📌 Popular topics
- •
- •
- •
- •
- •
- •
- •
- •
🔗 You May Be Interested In
- •
- •
- •
- •
- •
- •