diff --git a/Indicators/MyIndicators/ALMA.mq5 b/Indicators/MyIndicators/ALMA.mq5 index 8e19240..0b7f0a0 100644 --- a/Indicators/MyIndicators/ALMA.mq5 +++ b/Indicators/MyIndicators/ALMA.mq5 @@ -5,12 +5,12 @@ //+------------------------------------------------------------------+ #property copyright "Copyright 2025, xxxxxxxx" #property link "" -#property version "1.01" // Corrected MQL5 syntax for data handling +#property version "1.02" // Corrected calculation logic to match TradingView #property description "Arnaud Legoux Moving Average (ALMA)" //--- Indicator Window and Plot Properties --- #property indicator_chart_window -#property indicator_buffers 1 +#property indicator_buffers 2 // ALMA and a buffer for the source price #property indicator_plots 1 //--- Plot 1: ALMA line @@ -28,7 +28,7 @@ input double InpAlmaSigma = 6.0; // Sigma (smoothness) //--- Indicator Buffers --- double BufferALMA[]; -double price_buffer[]; // A buffer to store the source price data +double BufferPrice[]; // A buffer to store the source price data //--- Global Variables --- int ExtAlmaPeriod; @@ -49,16 +49,13 @@ void OnInit() ExtAlmaOffset = InpAlmaOffset; ExtAlmaSigma = (InpAlmaSigma <= 0) ? 0.01 : InpAlmaSigma; -//--- Map the buffer and set its properties - SetIndexBuffer(0, BufferALMA, INDICATOR_DATA); - ArraySetAsSeries(BufferALMA, false); +//--- Map the buffers and set them as non-timeseries + SetIndexBuffer(0, BufferALMA, INDICATOR_DATA); + SetIndexBuffer(1, BufferPrice, INDICATOR_CALCULATIONS); + ArraySetAsSeries(BufferALMA, false); + ArraySetAsSeries(BufferPrice, false); -// We also need to set our calculation buffer as a non-timeseries - SetIndexBuffer(1, price_buffer, INDICATOR_CALCULATIONS); - ArraySetAsSeries(price_buffer, false); - -//--- Create a handle to get the source price data --- -// Using iMA with period 1 is a standard trick to get any APPLIED_PRICE series +//--- Create a handle to get the source price data price_handle = iMA(_Symbol, _Period, 1, 0, MODE_SMA, InpAppliedPrice); if(price_handle == INVALID_HANDLE) { @@ -89,8 +86,12 @@ int OnCalculate(const int rates_total, if(rates_total < ExtAlmaPeriod) return(0); -//--- Copy the source price data into our buffer --- - if(CopyBuffer(price_handle, 0, 0, rates_total, price_buffer) != rates_total) +//--- Check if the source indicator is ready + if(BarsCalculated(price_handle) < rates_total) + return(0); + +//--- Copy the source price data into our buffer + if(CopyBuffer(price_handle, 0, 0, rates_total, BufferPrice) != rates_total) { Print("Error copying source price data."); return(0); @@ -99,15 +100,14 @@ int OnCalculate(const int rates_total, //--- Main calculation loop (full recalculation for stability) for(int i = ExtAlmaPeriod - 1; i < rates_total; i++) { - // Calculate ALMA for the current bar 'i' using the copied price data - BufferALMA[i] = CalculateALMA(i, price_buffer); + BufferALMA[i] = CalculateALMA(i, BufferPrice); } return(rates_total); } //+------------------------------------------------------------------+ -//| Calculates a single ALMA value for a given position. | +//| Calculates a single ALMA value for a given position. (CORRECTED) | //+------------------------------------------------------------------+ double CalculateALMA(const int position, const double &price_array[]) { @@ -120,7 +120,13 @@ double CalculateALMA(const int position, const double &price_array[]) for(int j = 0; j < ExtAlmaPeriod; j++) { double weight = MathExp(-1 * MathPow(j - m, 2) / (2 * s * s)); - sum += price_array[position - ExtAlmaPeriod + 1 + j] * weight; + + // --- FIX: Correct indexing to match Pine Script's logic --- + // This calculates the index of the bar within the sliding window, + // starting from the oldest to the newest. + int price_index = position - (ExtAlmaPeriod - 1) + j; + + sum += price_array[price_index] * weight; norm += weight; }