//+------------------------------------------------------------------+ //| ADX_HeikenAshi.mq5 | //| Copyright 2025, xxxxxxxx (Based on MetaQuotes ADXW) | //| | //+------------------------------------------------------------------+ #property copyright "Copyright 2025, xxxxxxxx" #property link "" #property version "2.12" // Refactored with comments and conventions #property description "ADX by Welles Wilder on Heiken Ashi data. Can export results." // --- Standard Includes --- #include // --- Custom Toolkit Includes --- #include #include // --- 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 (using 'Inp' prefix convention) --- input int InpPeriodADX = 14; // Period for ADX calculations input group "Export Settings" // Group for export parameters in the input dialog input bool InpExportData = false; // If true, exports data when the indicator is removed input int InpExportCandles = 1000; // Number of last candles to export input string InpExportFileName = "mql5_adx_results.csv"; // File name for the export //--- Indicator Buffers --- // Buffers for plotting double BufferHA_ADX[]; double BufferHA_PDI[]; double BufferHA_NDI[]; // Buffers for intermediate calculations double BufferSmoothed_PDM[]; double BufferSmoothed_NDM[]; double BufferSmoothed_TR[]; double BufferDX[]; //--- Global Objects and Variables --- int ExtADXPeriod; // Stores the validated ADX period CHA_Calculator g_ha_calculator; // Global instance of our Heiken Ashi calculator CIndicatorExporter g_exporter; // Global instance of our data exporter //+------------------------------------------------------------------+ //| Custom indicator initialization function. | //| Called once when the indicator is first loaded. | //+------------------------------------------------------------------+ 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); // Set display precision to 2 decimal places // Set the first bar from which the indicator will be drawn PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, ExtADXPeriod * 2 - 1); // ADX needs more data PlotIndexSetInteger(1, PLOT_DRAW_BEGIN, ExtADXPeriod - 1); // +DI PlotIndexSetInteger(2, PLOT_DRAW_BEGIN, ExtADXPeriod - 1); // -DI // Set the short name displayed in the indicator window IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("HA_ADXW(%d)", ExtADXPeriod)); } //+------------------------------------------------------------------+ //| Custom indicator deinitialization function. | //| Called when the indicator is removed or the terminal is closed. | //| Handles the data export logic. | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- Check if the user requested to export data if(InpExportData) { Print("--- Starting Data Export on Deinit ---"); int bars_available = Bars(_Symbol, _Period); int bars_to_export = MathMin(InpExportCandles, bars_available); // We need a buffer for the timestamps datetime time_buffer[]; ArraySetAsSeries(time_buffer, false); // Ensure chronological order if(CopyTime(_Symbol, _Period, 0, bars_to_export, time_buffer) <= 0) { Print("Error copying time data for export."); return; } // Access the indicator's own buffers directly ArraySetAsSeries(BufferHA_ADX, false); ArraySetAsSeries(BufferHA_PDI, false); ArraySetAsSeries(BufferHA_NDI, false); // Use the global exporter object to perform the export if(g_exporter.OpenFile(InpExportFileName)) { string headers[] = {"time", "HA_ADX", "HA_PDI", "HA_NDI"}; g_exporter.WriteHeader(headers); // Loop through the available data and write it row by row for(int i = 0; i < bars_to_export; i++) { double values[3]; values[0] = BufferHA_ADX[i]; values[1] = BufferHA_PDI[i]; values[2] = BufferHA_NDI[i]; g_exporter.WriteRow(time_buffer[i], values); } g_exporter.CloseFile(); PrintFormat("Export successful to MQL5\\Files\\%s", InpExportFileName); } } } //+------------------------------------------------------------------+ //| Custom indicator calculation function. | //| Called on every new tick or new bar. | //+------------------------------------------------------------------+ 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; // Optimization for subsequent calls else start_adx = ExtADXPeriod; // Start from the first bar with enough data //--- 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++) { // Recalculate values for the summation period 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 of the first period of DX values { 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 recursively { BufferHA_ADX[i] = (BufferHA_ADX[i-1] * (ExtADXPeriod - 1) + BufferDX[i]) / ExtADXPeriod; } } //--- return value of prev_calculated for next call return(rates_total); } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+