From b77c81ba2c076363a67526e3ae5a8d6307a599c0 Mon Sep 17 00:00:00 2001 From: Toh4iem9 Date: Tue, 19 Aug 2025 16:22:10 +0200 Subject: [PATCH] refactor: --- Indicators/MyIndicators/McGinleyDynamic.mq5 | 111 ++++++++++---------- 1 file changed, 58 insertions(+), 53 deletions(-) diff --git a/Indicators/MyIndicators/McGinleyDynamic.mq5 b/Indicators/MyIndicators/McGinleyDynamic.mq5 index 71c2c7d..deacede 100644 --- a/Indicators/MyIndicators/McGinleyDynamic.mq5 +++ b/Indicators/MyIndicators/McGinleyDynamic.mq5 @@ -1,15 +1,13 @@ //+------------------------------------------------------------------+ -//| McGinleyDynamic.mq5 | +//| McGinleyDynamic.mq5 | //| Copyright 2025, xxxxxxxx | //| | //+------------------------------------------------------------------+ #property copyright "Copyright 2025, xxxxxxxx" #property link "" -#property version "1.00" +#property version "2.01" // Corrected array handling for MQL5 syntax #property description "McGinley Dynamic Indicator" -#include - //--- Indicator Window and Plot Properties --- #property indicator_chart_window #property indicator_buffers 1 @@ -23,42 +21,33 @@ #property indicator_width1 2 //--- Input Parameters --- -input int InpLength = 14; // Period +input int InpLength = 14; // Period input ENUM_APPLIED_PRICE InpAppliedPrice = PRICE_CLOSE; // Applied Price //--- Indicator Buffers --- double BufferMcGinley[]; -double BufferPrice[]; //--- Global Variables --- -int ExtLength; -int price_handle; +int g_ExtLength; //+------------------------------------------------------------------+ //| Custom indicator initialization function. | //+------------------------------------------------------------------+ -void OnInit() +int OnInit() { //--- Validate and store input - ExtLength = (InpLength < 1) ? 1 : InpLength; + g_ExtLength = (InpLength < 1) ? 1 : InpLength; -//--- Map the buffers and set as non-timeseries +//--- Map the buffer and set as non-timeseries SetIndexBuffer(0, BufferMcGinley, INDICATOR_DATA); - SetIndexBuffer(1, BufferPrice, INDICATOR_CALCULATIONS); ArraySetAsSeries(BufferMcGinley, false); - ArraySetAsSeries(BufferPrice, false); - -//--- Create a handle to get the source price data - price_handle = iMA(_Symbol, _Period, 1, 0, MODE_SMA, InpAppliedPrice); - if(price_handle == INVALID_HANDLE) - { - Print("Error creating price source handle (iMA)."); - } //--- Set indicator display properties IndicatorSetInteger(INDICATOR_DIGITS, _Digits); - PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, ExtLength); - IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("McGinley(%d)", ExtLength)); + PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, 1); + IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("McGinley(%d)", g_ExtLength)); + + return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ @@ -75,60 +64,76 @@ int OnCalculate(const int rates_total, const long &volume[], const int &spread[]) { -//--- Check for enough data - if(rates_total < ExtLength) + if(rates_total < 2) return(0); -//--- Check if the source indicator is ready - if(BarsCalculated(price_handle) < rates_total) - return(0); +//--- STEP 1: Prepare the source price array + double price_source[]; + ArrayResize(price_source, rates_total); -//--- Copy the source price data into our buffer - if(CopyBuffer(price_handle, 0, 0, rates_total, BufferPrice) != rates_total) - return(0); - -//--- Main calculation loop (full recalculation for stability) - for(int i = 1; i < rates_total; i++) // Start from 1 to access i-1 + switch(InpAppliedPrice) { - // Skip until we have enough data - if(i < ExtLength) - { - BufferMcGinley[i] = EMPTY_VALUE; - continue; - } + case PRICE_OPEN: + ArrayCopy(price_source, open, 0, 0, rates_total); + break; + case PRICE_HIGH: + ArrayCopy(price_source, high, 0, 0, rates_total); + break; + case PRICE_LOW: + ArrayCopy(price_source, low, 0, 0, rates_total); + break; + case PRICE_MEDIAN: + case PRICE_TYPICAL: + case PRICE_WEIGHTED: + for(int i=0; i