Files
mql5/Indicators/MyIndicators/Chart_HeikinAshi.mq5
T

143 lines
5.4 KiB
Plaintext
Raw Normal View History

2025-08-23 15:12:47 +02:00
//+------------------------------------------------------------------+
//| Chart_HeikinAshi.mq5 |
//| Copyright 2025, xxxxxxxx |
//+------------------------------------------------------------------+
#property copyright "Copyright 2025, xxxxxxxx"
#property link ""
2025-11-01 19:03:25 +01:00
#property version "3.01" // Added DRAW_NONE plot to hide main series and prevent flickering
2025-08-23 15:12:47 +02:00
#property description "Draws Heikin Ashi candles on the main chart."
//--- Custom Toolkit Include ---
#include <MyIncludes\HeikinAshi_Tools.mqh>
//--- Indicator Window and Plot Properties ---
2025-11-01 19:03:25 +01:00
#property indicator_chart_window
#property indicator_buffers 8 // 4 for HA candles, 4 for the hidden main series
#property indicator_plots 2 // 1 for HA candles, 1 hidden plot
2025-08-23 15:12:47 +02:00
2025-11-01 19:03:25 +01:00
//--- Plot 1: Heikin Ashi Candles (Visible)
2025-08-23 15:12:47 +02:00
#property indicator_type1 DRAW_COLOR_CANDLES
2025-10-05 11:46:52 +02:00
#property indicator_color1 clrCornflowerBlue, clrChocolate // Up and Down colors
2025-11-01 19:03:25 +01:00
#property indicator_label1 "HA Open;HA High;HA Low;HA Close"
//--- Plot 2: Main Price Series (Hidden)
#property indicator_type2 DRAW_NONE
#property indicator_label2 "OHLC" // Label for Data Window
2025-08-23 15:12:47 +02:00
//--- Indicator Buffers ---
2025-11-01 19:03:25 +01:00
// Buffers for Heikin Ashi
2025-08-23 15:12:47 +02:00
double BufferHA_Open[];
double BufferHA_High[];
double BufferHA_Low[];
double BufferHA_Close[];
2025-11-01 19:03:25 +01:00
double BufferColor[];
// Buffers for the hidden main series plot
double BufferMain_Open[];
double BufferMain_High[];
double BufferMain_Low[];
double BufferMain_Close[];
2025-08-23 15:12:47 +02:00
//--- Global Objects ---
2025-11-01 19:03:25 +01:00
CHeikinAshi_Calculator *g_ha_calculator;
2025-08-23 15:12:47 +02:00
//+------------------------------------------------------------------+
//| Custom indicator initialization function. |
//+------------------------------------------------------------------+
int OnInit()
{
2025-11-01 19:03:25 +01:00
//--- Map the Heikin Ashi buffers
2025-08-23 15:12:47 +02:00
SetIndexBuffer(0, BufferHA_Open, INDICATOR_DATA);
SetIndexBuffer(1, BufferHA_High, INDICATOR_DATA);
SetIndexBuffer(2, BufferHA_Low, INDICATOR_DATA);
SetIndexBuffer(3, BufferHA_Close, INDICATOR_DATA);
SetIndexBuffer(4, BufferColor, INDICATOR_COLOR_INDEX);
2025-11-01 19:03:25 +01:00
PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, 1); // Start drawing HA from the second bar
2025-08-23 15:12:47 +02:00
2025-11-01 19:03:25 +01:00
//--- Map the hidden main series buffers
SetIndexBuffer(5, BufferMain_Open, INDICATOR_DATA);
SetIndexBuffer(6, BufferMain_High, INDICATOR_DATA);
SetIndexBuffer(7, BufferMain_Low, INDICATOR_DATA);
SetIndexBuffer(8, BufferMain_Close, INDICATOR_DATA);
// No PLOT_DRAW_BEGIN needed for DRAW_NONE
//--- Set all buffers to non-timeseries for stable calculation
2025-08-23 15:12:47 +02:00
ArraySetAsSeries(BufferHA_Open, false);
ArraySetAsSeries(BufferHA_High, false);
ArraySetAsSeries(BufferHA_Low, false);
ArraySetAsSeries(BufferHA_Close, false);
ArraySetAsSeries(BufferColor, false);
2025-11-01 19:03:25 +01:00
ArraySetAsSeries(BufferMain_Open, false);
ArraySetAsSeries(BufferMain_High, false);
ArraySetAsSeries(BufferMain_Low, false);
ArraySetAsSeries(BufferMain_Close, false);
2025-08-23 15:12:47 +02:00
//--- Set indicator properties
2025-11-01 19:03:25 +01:00
IndicatorSetInteger(INDICATOR_DIGITS, _Digits);
2025-08-23 15:12:47 +02:00
IndicatorSetString(INDICATOR_SHORTNAME, "Heikin Ashi");
//--- Create the calculator instance
g_ha_calculator = new CHeikinAshi_Calculator();
if(CheckPointer(g_ha_calculator) == POINTER_INVALID)
{
Print("Error creating CHeikinAshi_Calculator object");
return(INIT_FAILED);
}
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function. |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
if(CheckPointer(g_ha_calculator) != POINTER_INVALID)
{
delete g_ha_calculator;
g_ha_calculator = NULL;
}
}
//+------------------------------------------------------------------+
//| Heikin Ashi 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 < 2)
return(0);
2025-11-01 19:03:25 +01:00
//--- STEP 1: Hide the main series by copying data to the DRAW_NONE plot
ArrayCopy(BufferMain_Open, open, 0, 0, rates_total);
ArrayCopy(BufferMain_High, high, 0, 0, rates_total);
ArrayCopy(BufferMain_Low, low, 0, 0, rates_total);
ArrayCopy(BufferMain_Close, close, 0, 0, rates_total);
//--- STEP 2: Calculate Heikin Ashi bars
2025-08-23 15:12:47 +02:00
g_ha_calculator.Calculate(rates_total, open, high, low, close,
BufferHA_Open, BufferHA_High, BufferHA_Low, BufferHA_Close);
2025-11-01 19:03:25 +01:00
//--- STEP 3: Set the colors for the Heikin Ashi candles
2025-08-23 15:12:47 +02:00
for(int i = 0; i < rates_total; i++)
{
if(BufferHA_Open[i] < BufferHA_Close[i])
2025-11-01 19:03:25 +01:00
BufferColor[i] = 0; // Index for the first color (clrCornflowerBlue)
2025-08-23 15:12:47 +02:00
else
2025-11-01 19:03:25 +01:00
BufferColor[i] = 1; // Index for the second color (clrChocolate)
2025-08-23 15:12:47 +02:00
}
return(rates_total);
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+