Files
mql5/Indicators/MyIndicators/ADXW_HeikenAshi.mq5
T
2025-08-12 18:49:51 +02:00

193 lines
16 KiB
Plaintext

//+------------------------------------------------------------------+
//| ADX_HeikenAshi.mq5 |
//| Copyright 2025, xxxxxxxx (Based on MetaQuotes ADXW) |
//| |
//+------------------------------------------------------------------+
#property copyright "Copyright 2025, xxxxxxxx"
#property link ""
#property version "3.00" // Clean version, focused on calculation
#property description "ADX by Welles Wilder on Heiken Ashi data."
// --- Standard and Custom Includes ---
#include <MovingAverages.mqh>
#include <MyIncludes\HA_Tools.mqh>
//--- Indicator Window and Level Properties ---
#property indicator_separate_window
#property indicator_buffers 7 // 3 for plotting, 4 for calculations
#property indicator_plots 3
//--- Plot 1: ADX line (Main trend strength)
#property indicator_label1 "HA_ADX"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrLightSeaGreen
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
//--- Plot 2: +DI line (Positive Directional Indicator)
#property indicator_label2 "HA_+DI"
#property indicator_type2 DRAW_LINE
#property indicator_color2 clrLimeGreen
#property indicator_style2 STYLE_DOT
#property indicator_width2 1
//--- Plot 3: -DI line (Negative Directional Indicator)
#property indicator_label3 "HA_-DI"
#property indicator_type3 DRAW_LINE
#property indicator_color3 clrTomato
#property indicator_style3 STYLE_DOT
#property indicator_width3 1
//--- Input Parameters ---
input int InpPeriodADX = 14; // Period for ADX calculations
//--- Indicator Buffers ---
double BufferHA_ADX[];
double BufferHA_PDI[];
double BufferHA_NDI[];
double BufferSmoothed_PDM[];
double BufferSmoothed_NDM[];
double BufferSmoothed_TR[];
double BufferDX[];
//--- Global Objects and Variables ---
int ExtADXPeriod;
CHA_Calculator g_ha_calculator; // Global instance of our Heiken Ashi calculator
//+------------------------------------------------------------------+
//| Custom indicator initialization function. |
//+------------------------------------------------------------------+
void OnInit()
{
//--- Validate and store the ADX period
ExtADXPeriod = (InpPeriodADX < 1) ? 1 : InpPeriodADX;
//--- Map the buffers to the indicator's internal memory
SetIndexBuffer(0, BufferHA_ADX, INDICATOR_DATA);
SetIndexBuffer(1, BufferHA_PDI, INDICATOR_DATA);
SetIndexBuffer(2, BufferHA_NDI, INDICATOR_DATA);
SetIndexBuffer(3, BufferSmoothed_PDM, INDICATOR_CALCULATIONS);
SetIndexBuffer(4, BufferSmoothed_NDM, INDICATOR_CALCULATIONS);
SetIndexBuffer(5, BufferSmoothed_TR, INDICATOR_CALCULATIONS);
SetIndexBuffer(6, BufferDX, INDICATOR_CALCULATIONS);
//--- Set indicator properties
IndicatorSetInteger(INDICATOR_DIGITS, 2);
PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, ExtADXPeriod * 2 - 1);
PlotIndexSetInteger(1, PLOT_DRAW_BEGIN, ExtADXPeriod - 1);
PlotIndexSetInteger(2, PLOT_DRAW_BEGIN, ExtADXPeriod - 1);
IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("HA_ADXW(%d)", ExtADXPeriod));
}
//+------------------------------------------------------------------+
//| Custom indicator 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[])
{
//--- Check if there is enough historical data for the calculation
if(rates_total < ExtADXPeriod + 1)
return(0);
//--- STEP 1: Calculate Heiken Ashi bars using our toolkit
if(!g_ha_calculator.Calculate(rates_total, prev_calculated, open, high, low, close))
{
Print("Heiken Ashi calculation failed.");
return(0);
}
//--- STEP 2: Calculate ADX using the results from the HA calculator
int start_adx;
if(prev_calculated > ExtADXPeriod)
start_adx = prev_calculated - 1;
else
start_adx = ExtADXPeriod;
//--- Main calculation loop
for(int i = start_adx; i < rates_total; i++)
{
// Get Heiken Ashi values from the calculator's public buffers
double ha_high = g_ha_calculator.ha_high[i];
double prev_ha_high = g_ha_calculator.ha_high[i-1];
double ha_low = g_ha_calculator.ha_low[i];
double prev_ha_low = g_ha_calculator.ha_low[i-1];
double prev_ha_close = g_ha_calculator.ha_close[i-1];
// Calculate raw +DM, -DM, and TR for the current bar
double pdm = ha_high - prev_ha_high;
double ndm = prev_ha_low - ha_low;
if(pdm < 0 || pdm < ndm)
pdm = 0;
if(ndm < 0 || ndm < pdm)
ndm = 0;
double tr = MathMax(ha_high, prev_ha_close) - MathMin(ha_low, prev_ha_close);
// Smooth PDM, NDM, and TR using Wilder's Smoothing method
if(i == ExtADXPeriod) // First calculation is a simple sum of the first period
{
double sum_pdm=0, sum_ndm=0, sum_tr=0;
for(int j=1; j<=ExtADXPeriod; j++)
{
double p_pdm = g_ha_calculator.ha_high[j] - g_ha_calculator.ha_high[j-1];
double p_ndm = g_ha_calculator.ha_low[j-1] - g_ha_calculator.ha_low[j];
if(p_pdm < 0 || p_pdm < p_ndm)
p_pdm = 0;
if(p_ndm < 0 || p_ndm < p_pdm)
p_ndm = 0;
sum_pdm += p_pdm;
sum_ndm += p_ndm;
sum_tr += MathMax(g_ha_calculator.ha_high[j], g_ha_calculator.ha_close[j-1]) - MathMin(g_ha_calculator.ha_low[j], g_ha_calculator.ha_close[j-1]);
}
BufferSmoothed_PDM[i] = sum_pdm;
BufferSmoothed_NDM[i] = sum_ndm;
BufferSmoothed_TR[i] = sum_tr;
}
else // Subsequent calculations use the recursive smoothing formula
{
BufferSmoothed_PDM[i] = BufferSmoothed_PDM[i-1] - (BufferSmoothed_PDM[i-1] / ExtADXPeriod) + pdm;
BufferSmoothed_NDM[i] = BufferSmoothed_NDM[i-1] - (BufferSmoothed_NDM[i-1] / ExtADXPeriod) + ndm;
BufferSmoothed_TR[i] = BufferSmoothed_TR[i-1] - (BufferSmoothed_TR[i-1] / ExtADXPeriod) + tr;
}
// Calculate +DI and -DI from the smoothed values
if(BufferSmoothed_TR[i] != 0.0)
{
BufferHA_PDI[i] = (BufferSmoothed_PDM[i] / BufferSmoothed_TR[i]) * 100.0;
BufferHA_NDI[i] = (BufferSmoothed_NDM[i] / BufferSmoothed_TR[i]) * 100.0;
}
// Calculate the Directional Index (DX)
double di_sum = BufferHA_PDI[i] + BufferHA_NDI[i];
if(di_sum != 0.0)
BufferDX[i] = MathAbs(BufferHA_PDI[i] - BufferHA_NDI[i]) / di_sum * 100.0;
else
BufferDX[i] = 0.0;
// Smooth the DX to get the final ADX value
if(i == ExtADXPeriod * 2 - 1) // First ADX value is a simple average
{
double sum_dx = 0;
for(int j=i-ExtADXPeriod+1; j<=i; j++)
sum_dx += BufferDX[j];
BufferHA_ADX[i] = sum_dx / ExtADXPeriod;
}
else
if(i > ExtADXPeriod * 2 - 1) // Subsequent ADX values are smoothed
{
BufferHA_ADX[i] = (BufferHA_ADX[i-1] * (ExtADXPeriod - 1) + BufferDX[i]) / ExtADXPeriod;
}
}
//--- return value of prev_calculated for next call
return(rates_total);
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+