Files
mql5/Indicators/MyIndicators/Gann_HiLo.mq5
T
2025-08-15 12:15:08 +02:00

155 lines
11 KiB
Plaintext

//+------------------------------------------------------------------+
//| Gann_HiLo.mq5|
//| Copyright 2025, xxxxxxxx |
//| |
//+------------------------------------------------------------------+
#property copyright "Copyright 2025, xxxxxxxx"
#property link ""
#property version "1.10" // Added selectable MA Method
#property description "Gann HiLo Activator with selectable MA for trend following"
#include <MovingAverages.mqh>
//--- 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 ExtPeriod;
//+------------------------------------------------------------------+
//| Custom indicator initialization function. |
//+------------------------------------------------------------------+
void OnInit()
{
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, ExtPeriod - 1);
IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("Gann_HiLo(%d)", ExtPeriod));
}
//+------------------------------------------------------------------+
//| 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 < ExtPeriod)
return(0);
//--- STEP 1: Calculate the two moving averages (High and Low) ---
// We use a single loop and a switch for efficiency and clarity
for(int i = 1; i < rates_total; i++)
{
if(i < ExtPeriod - 1)
continue; // Skip until enough data is available
switch(InpMAMethod)
{
case MODE_EMA:
if(i == ExtPeriod - 1) // First value is an SMA
{
BufferHiAvg[i] = SimpleMA(i, ExtPeriod, high);
BufferLoAvg[i] = SimpleMA(i, ExtPeriod, low);
}
else // Subsequent values are calculated recursively
{
double pr = 2.0 / (ExtPeriod + 1.0);
BufferHiAvg[i] = high[i] * pr + BufferHiAvg[i-1] * (1.0 - pr);
BufferLoAvg[i] = low[i] * pr + BufferLoAvg[i-1] * (1.0 - pr);
}
break;
case MODE_SMMA:
if(i == ExtPeriod - 1) // First value is an SMA
{
BufferHiAvg[i] = SimpleMA(i, ExtPeriod, high);
BufferLoAvg[i] = SimpleMA(i, ExtPeriod, low);
}
else // Subsequent values are calculated recursively
{
BufferHiAvg[i] = (BufferHiAvg[i-1] * (ExtPeriod - 1) + high[i]) / ExtPeriod;
BufferLoAvg[i] = (BufferLoAvg[i-1] * (ExtPeriod - 1) + low[i]) / ExtPeriod;
}
break;
case MODE_LWMA:
BufferHiAvg[i] = LinearWeightedMA(i, ExtPeriod, high);
BufferLoAvg[i] = LinearWeightedMA(i, ExtPeriod, low);
break;
default: // MODE_SMA
BufferHiAvg[i] = SimpleMA(i, ExtPeriod, high);
BufferLoAvg[i] = SimpleMA(i, ExtPeriod, low);
break;
}
}
//--- STEP 2 & 3: Determine trend and set the final Gann HiLo value
for(int i = 1; i < rates_total; i++)
{
if(i < ExtPeriod -1)
continue;
if(close[i] > BufferHiAvg[i])
BufferTrend[i] = 1; // Up trend
else
if(close[i] < BufferLoAvg[i])
BufferTrend[i] = -1; // Down trend
else
BufferTrend[i] = BufferTrend[i-1];
if(BufferTrend[i] == 1)
{
BufferGannHiLo[i] = BufferLoAvg[i];
BufferColor[i] = 0;
}
else
{
BufferGannHiLo[i] = BufferHiAvg[i];
BufferColor[i] = 1;
}
}
return(rates_total);
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+