refactor: CalculateALMA

This commit is contained in:
Toh4iem9
2025-08-13 10:57:42 +02:00
parent 3739b22d87
commit 06e86b020c
+24 -18
View File
@@ -5,12 +5,12 @@
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
#property copyright "Copyright 2025, xxxxxxxx" #property copyright "Copyright 2025, xxxxxxxx"
#property link "" #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)" #property description "Arnaud Legoux Moving Average (ALMA)"
//--- Indicator Window and Plot Properties --- //--- Indicator Window and Plot Properties ---
#property indicator_chart_window #property indicator_chart_window
#property indicator_buffers 1 #property indicator_buffers 2 // ALMA and a buffer for the source price
#property indicator_plots 1 #property indicator_plots 1
//--- Plot 1: ALMA line //--- Plot 1: ALMA line
@@ -28,7 +28,7 @@ input double InpAlmaSigma = 6.0; // Sigma (smoothness)
//--- Indicator Buffers --- //--- Indicator Buffers ---
double BufferALMA[]; 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 --- //--- Global Variables ---
int ExtAlmaPeriod; int ExtAlmaPeriod;
@@ -49,16 +49,13 @@ void OnInit()
ExtAlmaOffset = InpAlmaOffset; ExtAlmaOffset = InpAlmaOffset;
ExtAlmaSigma = (InpAlmaSigma <= 0) ? 0.01 : InpAlmaSigma; ExtAlmaSigma = (InpAlmaSigma <= 0) ? 0.01 : InpAlmaSigma;
//--- Map the buffer and set its properties //--- Map the buffers and set them as non-timeseries
SetIndexBuffer(0, BufferALMA, INDICATOR_DATA); SetIndexBuffer(0, BufferALMA, INDICATOR_DATA);
ArraySetAsSeries(BufferALMA, false); SetIndexBuffer(1, BufferPrice, INDICATOR_CALCULATIONS);
ArraySetAsSeries(BufferALMA, false);
ArraySetAsSeries(BufferPrice, false);
// We also need to set our calculation buffer as a non-timeseries //--- Create a handle to get the source price data
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
price_handle = iMA(_Symbol, _Period, 1, 0, MODE_SMA, InpAppliedPrice); price_handle = iMA(_Symbol, _Period, 1, 0, MODE_SMA, InpAppliedPrice);
if(price_handle == INVALID_HANDLE) if(price_handle == INVALID_HANDLE)
{ {
@@ -89,8 +86,12 @@ int OnCalculate(const int rates_total,
if(rates_total < ExtAlmaPeriod) if(rates_total < ExtAlmaPeriod)
return(0); return(0);
//--- Copy the source price data into our buffer --- //--- Check if the source indicator is ready
if(CopyBuffer(price_handle, 0, 0, rates_total, price_buffer) != rates_total) 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."); Print("Error copying source price data.");
return(0); return(0);
@@ -99,15 +100,14 @@ int OnCalculate(const int rates_total,
//--- Main calculation loop (full recalculation for stability) //--- Main calculation loop (full recalculation for stability)
for(int i = ExtAlmaPeriod - 1; i < rates_total; i++) 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, BufferPrice);
BufferALMA[i] = CalculateALMA(i, price_buffer);
} }
return(rates_total); 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[]) 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++) for(int j = 0; j < ExtAlmaPeriod; j++)
{ {
double weight = MathExp(-1 * MathPow(j - m, 2) / (2 * s * s)); 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; norm += weight;
} }