//+------------------------------------------------------------------+ //| Gann_HiLo.mq5 | //| Copyright 2025, xxxxxxxx | //| | //+------------------------------------------------------------------+ #property copyright "Copyright 2025, xxxxxxxx" #property link "" #property version "2.00" // Refactored for stability with fully manual MA calculations #property description "Gann HiLo Activator with selectable MA for trend following" //--- Indicator Window and Plot Properties --- #property indicator_chart_window #property indicator_buffers 5 #property indicator_plots 1 //--- Plot 1: Gann HiLo line #property indicator_label1 "Gann_HiLo" #property indicator_type1 DRAW_COLOR_LINE #property indicator_color1 clrDodgerBlue, clrTomato #property indicator_style1 STYLE_SOLID #property indicator_width1 2 //--- Input Parameters --- input int InpPeriod = 10; // Period for High/Low averages input ENUM_MA_METHOD InpMAMethod = MODE_SMA; // Method for High/Low averages //--- Indicator Buffers --- double BufferGannHiLo[]; double BufferColor[]; double BufferHiAvg[]; double BufferLoAvg[]; double BufferTrend[]; //--- Global Variables --- int g_ExtPeriod; //+------------------------------------------------------------------+ //| Custom indicator initialization function. | //+------------------------------------------------------------------+ int OnInit() { g_ExtPeriod = (InpPeriod < 1) ? 1 : InpPeriod; SetIndexBuffer(0, BufferGannHiLo, INDICATOR_DATA); SetIndexBuffer(1, BufferColor, INDICATOR_COLOR_INDEX); SetIndexBuffer(2, BufferHiAvg, INDICATOR_CALCULATIONS); SetIndexBuffer(3, BufferLoAvg, INDICATOR_CALCULATIONS); SetIndexBuffer(4, BufferTrend, INDICATOR_CALCULATIONS); ArraySetAsSeries(BufferGannHiLo, false); ArraySetAsSeries(BufferColor, false); ArraySetAsSeries(BufferHiAvg, false); ArraySetAsSeries(BufferLoAvg, false); ArraySetAsSeries(BufferTrend, false); IndicatorSetInteger(INDICATOR_DIGITS, _Digits); PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, g_ExtPeriod); IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("Gann_HiLo(%d)", g_ExtPeriod)); return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Gann HiLo Activator 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 <= g_ExtPeriod) return(0); //--- Variables for manual SMA calculation double sma_sum_high = 0; double sma_sum_low = 0; //--- Main calculation loop for(int i = 1; i < rates_total; i++) { if(i < g_ExtPeriod - 1) continue; // --- STEP 1: Calculate the two moving averages (High and Low) --- switch(InpMAMethod) { case MODE_EMA: case MODE_SMMA: if(i == g_ExtPeriod - 1) // Initialization with manual SMA { double sum_h=0, sum_l=0; for(int j=0; j 0) { BufferHiAvg[i] = lwma_sum_h / weight_sum; BufferLoAvg[i] = lwma_sum_l / weight_sum; } } break; default: // MODE_SMA if(i == g_ExtPeriod - 1) { for(int j=0; j BufferHiAvg[i-1]) BufferTrend[i] = 1; else if(close[i] < BufferLoAvg[i-1]) BufferTrend[i] = -1; else BufferTrend[i] = BufferTrend[i-1]; if(BufferTrend[i] == 1) { BufferGannHiLo[i] = BufferLoAvg[i]; BufferColor[i] = 0; if(BufferTrend[i-1] == -1) BufferGannHiLo[i-1] = BufferLoAvg[i]; } else { BufferGannHiLo[i] = BufferHiAvg[i]; BufferColor[i] = 1; if(BufferTrend[i-1] == 1) BufferGannHiLo[i-1] = BufferHiAvg[i]; } } return(rates_total); } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+