mirror of
https://github.com/softwaredevelop/mql5.git
synced 2026-07-31 22:37:42 +00:00
refactor:
This commit is contained in:
@@ -1,59 +1,81 @@
|
||||
//+------------------------------------------------------------------+
|
||||
//| Chart_HeikinAshi.mq5 |
|
||||
//| Copyright 2025, xxxxxxxx |
|
||||
//| |
|
||||
//+------------------------------------------------------------------+
|
||||
#property copyright "Copyright 2025, xxxxxxxx"
|
||||
#property link ""
|
||||
#property version "3.00" // Refactored for stability and new calculator
|
||||
#property version "3.01" // Added DRAW_NONE plot to hide main series and prevent flickering
|
||||
#property description "Draws Heikin Ashi candles on the main chart."
|
||||
|
||||
//--- Custom Toolkit Include ---
|
||||
#include <MyIncludes\HeikinAshi_Tools.mqh>
|
||||
|
||||
//--- Indicator Window and Plot Properties ---
|
||||
#property indicator_chart_window // Draw on the main chart window
|
||||
#property indicator_buffers 5 // 4 for OHLC, 1 for color
|
||||
#property indicator_plots 1
|
||||
#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
|
||||
|
||||
//--- Plot 1: Heikin Ashi Candles
|
||||
//--- Plot 1: Heikin Ashi Candles (Visible)
|
||||
#property indicator_type1 DRAW_COLOR_CANDLES
|
||||
#property indicator_color1 clrCornflowerBlue, clrChocolate // Up and Down colors
|
||||
#property indicator_label1 "HA Open;HA High;HA Low;HA Close" // Labels for Data Window
|
||||
#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
|
||||
|
||||
//--- Indicator Buffers ---
|
||||
// Buffers for Heikin Ashi
|
||||
double BufferHA_Open[];
|
||||
double BufferHA_High[];
|
||||
double BufferHA_Low[];
|
||||
double BufferHA_Close[];
|
||||
double BufferColor[]; // Buffer for candle colors
|
||||
double BufferColor[];
|
||||
|
||||
// Buffers for the hidden main series plot
|
||||
double BufferMain_Open[];
|
||||
double BufferMain_High[];
|
||||
double BufferMain_Low[];
|
||||
double BufferMain_Close[];
|
||||
|
||||
|
||||
//--- Global Objects ---
|
||||
CHeikinAshi_Calculator *g_ha_calculator; // Pointer to our Heikin Ashi calculator
|
||||
CHeikinAshi_Calculator *g_ha_calculator;
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
//| Custom indicator initialization function. |
|
||||
//+------------------------------------------------------------------+
|
||||
int OnInit()
|
||||
{
|
||||
//--- Map the buffers to the indicator's internal memory
|
||||
//--- Map the Heikin Ashi buffers
|
||||
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);
|
||||
PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, 1); // Start drawing HA from the second bar
|
||||
|
||||
//--- Set buffers to non-timeseries for stable calculation
|
||||
//--- 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
|
||||
ArraySetAsSeries(BufferHA_Open, false);
|
||||
ArraySetAsSeries(BufferHA_High, false);
|
||||
ArraySetAsSeries(BufferHA_Low, false);
|
||||
ArraySetAsSeries(BufferHA_Close, false);
|
||||
ArraySetAsSeries(BufferColor, false);
|
||||
ArraySetAsSeries(BufferMain_Open, false);
|
||||
ArraySetAsSeries(BufferMain_High, false);
|
||||
ArraySetAsSeries(BufferMain_Low, false);
|
||||
ArraySetAsSeries(BufferMain_Close, false);
|
||||
|
||||
//--- Set indicator properties
|
||||
IndicatorSetInteger(INDICATOR_DIGITS, _Digits); // Use the same precision as the symbol
|
||||
IndicatorSetInteger(INDICATOR_DIGITS, _Digits);
|
||||
IndicatorSetString(INDICATOR_SHORTNAME, "Heikin Ashi");
|
||||
PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, 0.0); // Define the empty value for the plot
|
||||
|
||||
//--- Create the calculator instance
|
||||
g_ha_calculator = new CHeikinAshi_Calculator();
|
||||
@@ -71,7 +93,6 @@ int OnInit()
|
||||
//+------------------------------------------------------------------+
|
||||
void OnDeinit(const int reason)
|
||||
{
|
||||
//--- Free the calculator object to prevent memory leaks
|
||||
if(CheckPointer(g_ha_calculator) != POINTER_INVALID)
|
||||
{
|
||||
delete g_ha_calculator;
|
||||
@@ -93,27 +114,28 @@ int OnCalculate(const int rates_total,
|
||||
const long &volume[],
|
||||
const int &spread[])
|
||||
{
|
||||
//--- Check if there is enough historical data
|
||||
if(rates_total < 2)
|
||||
return(0);
|
||||
|
||||
//--- STEP 1: Calculate Heikin Ashi bars directly into the indicator buffers
|
||||
//--- 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
|
||||
g_ha_calculator.Calculate(rates_total, open, high, low, close,
|
||||
BufferHA_Open, BufferHA_High, BufferHA_Low, BufferHA_Close);
|
||||
|
||||
//--- STEP 2: Set the colors for the candles
|
||||
//--- STEP 3: Set the colors for the Heikin Ashi candles
|
||||
for(int i = 0; i < rates_total; i++)
|
||||
{
|
||||
//--- Set the color for the current candle
|
||||
// Color index 0 (clrDodgerBlue) for bullish candles
|
||||
// Color index 1 (clrMaroon) for bearish candles
|
||||
if(BufferHA_Open[i] < BufferHA_Close[i])
|
||||
BufferColor[i] = 0.0; // Bullish
|
||||
BufferColor[i] = 0; // Index for the first color (clrCornflowerBlue)
|
||||
else
|
||||
BufferColor[i] = 1.0; // Bearish
|
||||
BufferColor[i] = 1; // Index for the second color (clrChocolate)
|
||||
}
|
||||
|
||||
//--- Return value of rates_total to signal a full recalculation
|
||||
return(rates_total);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
|
||||
Reference in New Issue
Block a user