//+------------------------------------------------------------------+ //| ALMA_HeikenAshi.mq5 | //| Copyright 2025, xxxxxxxx | //| | //+------------------------------------------------------------------+ #property copyright "Copyright 2025, xxxxxxxx" #property link "" #property version "1.04" // Final version with correct indexing and stable structure #property description "Arnaud Legoux Moving Average (ALMA) on Heiken Ashi data" #include //--- Indicator Window and Plot Properties --- #property indicator_chart_window #property indicator_buffers 1 #property indicator_plots 1 //--- Plot 1: ALMA line #property indicator_label1 "HA_ALMA" #property indicator_type1 DRAW_LINE #property indicator_color1 clrMediumVioletRed #property indicator_style1 STYLE_SOLID #property indicator_width1 2 //--- Enum for selecting Heiken Ashi price source --- enum ENUM_HA_APPLIED_PRICE { HA_PRICE_CLOSE, // Heiken Ashi Close HA_PRICE_OPEN, // Heiken Ashi Open HA_PRICE_HIGH, // Heiken Ashi High HA_PRICE_LOW, // Heiken Ashi Low }; //--- Input Parameters --- input int InpAlmaPeriod = 9; input ENUM_HA_APPLIED_PRICE InpAppliedPrice = HA_PRICE_CLOSE; input double InpAlmaOffset = 0.85; input double InpAlmaSigma = 6.0; //--- Indicator Buffers --- double BufferHA_ALMA[]; //--- Global Objects and Variables --- int ExtAlmaPeriod; double ExtAlmaOffset; double ExtAlmaSigma; CHA_Calculator g_ha_calculator; //+------------------------------------------------------------------+ //| Custom indicator initialization function. | //+------------------------------------------------------------------+ void OnInit() { ExtAlmaPeriod = (InpAlmaPeriod < 1) ? 1 : InpAlmaPeriod; ExtAlmaOffset = InpAlmaOffset; ExtAlmaSigma = (InpAlmaSigma <= 0) ? 0.01 : InpAlmaSigma; SetIndexBuffer(0, BufferHA_ALMA, INDICATOR_DATA); ArraySetAsSeries(BufferHA_ALMA, false); IndicatorSetInteger(INDICATOR_DIGITS, _Digits); PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, ExtAlmaPeriod - 1); IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("HA_ALMA(%d, %.2f, %.1f)", ExtAlmaPeriod, ExtAlmaOffset, ExtAlmaSigma)); } //+------------------------------------------------------------------+ //| Arnaud Legoux Moving Average calculation function. | //+------------------------------------------------------------------+ int OnCalculate(const int rates_total, const int prev_calculated, const datetime &time[], const double &open[], const double &high[], const double &low[], const double &close[], const long &tick_volume[], const long &volume[], const int &spread[]) { if(rates_total < ExtAlmaPeriod) return(0); //--- STEP 1: Calculate Heiken Ashi bars using our toolkit if(!g_ha_calculator.Calculate(rates_total, 0, open, high, low, close)) return(0); //--- STEP 2: Calculate ALMA based on the selected HA price double m = ExtAlmaOffset * (ExtAlmaPeriod - 1.0); double s = (double)ExtAlmaPeriod / ExtAlmaSigma; // The main loop iterates through all bars that can be calculated for(int i = ExtAlmaPeriod - 1; i < rates_total; i++) { double sum = 0.0; double norm = 0.0; // The inner loop calculates the weighted sum for the current bar 'i' for(int j = 0; j < ExtAlmaPeriod; j++) { double weight = MathExp(-1 * MathPow(j - m, 2) / (2 * s * s)); // --- 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 = i - (ExtAlmaPeriod - 1) + j; // Select the correct price from the HA buffers using the calculated index double price = 0; switch(InpAppliedPrice) { case HA_PRICE_OPEN: price = g_ha_calculator.ha_open[price_index]; break; case HA_PRICE_HIGH: price = g_ha_calculator.ha_high[price_index]; break; case HA_PRICE_LOW: price = g_ha_calculator.ha_low[price_index]; break; default: // HA_PRICE_CLOSE price = g_ha_calculator.ha_close[price_index]; break; } sum += price * weight; norm += weight; } if(norm > 0) BufferHA_ALMA[i] = sum / norm; else BufferHA_ALMA[i] = 0.0; } return(rates_total); } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+