Fast Sliding SMA algorithm
- Bibliotecas
- Andrei Khloptsau
- Versión: 1.0
- Activaciones: 20
A Simple Moving Average (SMA) is a statistical indicator used in time series analysis. This indicator represents the arithmetic mean of a sequence of values over a specific period of time. SMA is used to smooth short-term fluctuations in data, helping to highlight the overall trend or direction of changes. This aids analysts and traders in better understanding the general dynamics of the time series and identifying potential trends or changes in direction. More information you can find in Wiki https://en.wikipedia.org/wiki/Moving_average.
In simple terms, the SMA is the average value of a sequence of data over a specified time period. This period can be in days, weeks, hours, etc., depending on the context and analysis objectives.
For the basic calculation of the Simple Moving Average (SMA) with a fixed window size n, the standard asymptotic time complexity is O(n). This means that the algorithm's execution time is linearly proportional to the size of the window or the number of data points.
However, the improvved version of the algorithm use a queue and has an execution asymptotic of O(1) for each new element, making the algorithm efficient compared to the linear asymptotic of O(n).
The improved version of the moving average algorithm using a queue offers several advantages over the basic implementation:
-
Constant Time for Each New Element: The algorithm ensures constant time (O(1)) for adding new elements and removing old elements from the queue, making it efficient regardless of the window size.
-
Efficient Update Operations: Leveraging a queue enables efficient addition of new elements at the end and removal of old elements from the beginning, reducing the number of operations required for updating the average.
-
Optimized Window Management: The queue serves as an effective data structure for window management in the moving average, eliminating the need to recalculate the entire average when adding a new element.
-
Increased Efficiency with Large Data Sets: Constant time for each new element ensures the algorithm remains efficient even when processing large volumes of data.
-
Easy Implementation and Maintenance: The use of a queue makes the code more understandable and easy to maintain, avoiding the necessity of iterating through the entire window for updating the average.
In summary, the enhanced algorithm provides more efficient data processing while maintaining a fixed window for the moving average.
Import section:
#import "FastSlidingSMA.ex5" bool InitNewInstance(string key, const long windowSize); // Initialize a new instance of FastMovingSMA bool PushValue(string key, const double &value); // Push a single value into the FastMovingSMA instance bool PushArray(string key, double &values[]); // Push an array of values into the FastMovingSMA instance bool PushVector(string key, vector &values); // Push a vector of values into the FastMovingSMA instance bool GetSMA(string key, double &sma); // Get the value of the moving average from the FastMovingSMA instance bool ClearInstance(string key); // Clear the FastMovingSMA instance bool GetTopValue(string key, double &topValue); // Get the top value from the FastMovingSMA instance bool GetPoppedValue(string key, double &poppedValue); // Get the popped value from the FastMovingSMA instance #import
How to use code example:
#property copyright "Copyright 2023, Andrei Khloptsau Ltd." #property link "https://www.mql5.com" #property version "1.00" #import "FastSlidingSMA.ex5" bool InitNewInstance(string key, const long windowSize); bool PushValue(string key, const double &value); bool PushArray(string key, double &values[]); bool PushVector(string key, vector &values); bool GetSMA(string key, double &sma); bool ClearInstance(string key); bool GetTopValue(string key, double &topValue); bool GetPoppedValue(string key, double &poppedValue); #import const string INSTANCE_KEY = "MyInstance"; input int NumberOfBars = 5; int OnInit() { if (!InitNewInstance(INSTANCE_KEY, NumberOfBars)) return INIT_FAILED; double closePrices[]; ArraySetAsSeries(closePrices, true); if (CopyClose(_Symbol, _Period, 0, NumberOfBars, closePrices) > 0) PushArray(INSTANCE_KEY, closePrices); else return INIT_FAILED; return INIT_SUCCEEDED; } void OnDeinit(const int reason) { ClearInstance(INSTANCE_KEY); } void OnTick() { double currentPrice = iClose(_Symbol, _Period, 0); PushValue(INSTANCE_KEY, currentPrice); double sma; if (GetSMA(INSTANCE_KEY, sma)) { Print("Current value SMA: ", sma); } }