refactor: Heikin Ashi logic

This commit is contained in:
Toh4iem9
2025-10-20 14:58:56 +02:00
parent 62fe2762b9
commit 449ab7536b
@@ -3,7 +3,7 @@
//| Copyright 2025, xxxxxxxx|
//+------------------------------------------------------------------+
#property copyright "Copyright 2025, xxxxxxxx"
#property version "6.10" // REFACTORED: Improved object and buffer cleanup on re-init
#property version "6.30" // FIXED: Unified Heikin Ashi logic for all components
#property description "Draws boxes, analytics, and session-based VWAP via high-performance buffers."
#property indicator_chart_window
// Buffers: M1(Pre A/B, Core A/B, Post A/B, Full A/B) = 8. Total for 3 markets = 24
@@ -77,7 +77,7 @@ enum ENUM_CANDLE_SOURCE
input group "Global Settings"
input bool InpFillBoxes = false;
input ENUM_APPLIED_VOLUME InpVolumeType = VOLUME_TICK;
input ENUM_CANDLE_SOURCE InpCandleSource = CANDLE_STANDARD; // For VWAP
input ENUM_CANDLE_SOURCE InpCandleSource = CANDLE_STANDARD; // For VWAP and other analytics
input ENUM_APPLIED_PRICE InpSourcePrice = PRICE_TYPICAL; // For Mean/LinReg
//--- Market 1 Settings ---
@@ -267,7 +267,7 @@ int OnInit()
PlotIndexSetInteger(22, PLOT_LINE_COLOR, InpM3_FullDay_Color);
PlotIndexSetInteger(23, PLOT_LINE_COLOR, InpM3_FullDay_Color);
// --- CORRECTED: Centralized Cleanup Logic ---
// --- Centralized Cleanup Logic ---
MathSrand((int)TimeCurrent() + (int)ChartID());
string temp_short_name = StringFormat("SessPro_TempID_%d_%d", TimeCurrent(), MathRand());
IndicatorSetString(INDICATOR_SHORTNAME, temp_short_name);
@@ -278,60 +278,51 @@ int OnInit()
string unique_prefix = StringFormat("SessPro_%d_%d_", ChartID(), window_index);
ObjectsDeleteAll(0, unique_prefix);
// --- Unified Initialization for both calculator types ---
bool is_ha_mode = (InpCandleSource == CANDLE_HEIKIN_ASHI);
for(int i=0; i<TOTAL_SESSIONS; i++)
{
if(is_ha_mode)
{
g_box_analyzers[i] = new CSessionAnalyzer_HA();
g_vwap_calculators[i] = new CVWAPCalculator_HA();
}
else
{
g_box_analyzers[i] = new CSessionAnalyzer();
g_vwap_calculators[i] = new CVWAPCalculator();
}
}
// --- Init Logic for Box/Mean/LinReg Analyzers (Object-based) ---
g_box_analyzers[0] = new CSessionAnalyzer();
g_box_analyzers[0].Init(InpM1_Enable && InpM1_PreMarket_Enable, InpM1_PreMarket_Start, InpM1_PreMarket_End, InpM1_PreMarket_Color, InpFillBoxes, InpM1_PreMarket_Mean, InpM1_PreMarket_LinReg, unique_prefix + "M1_Pre_");
g_box_analyzers[1] = new CSessionAnalyzer();
g_box_analyzers[1].Init(InpM1_Enable && InpM1_Core_Enable, InpM1_Core_Start, InpM1_Core_End, InpM1_Core_Color, InpFillBoxes, InpM1_Core_Mean, InpM1_Core_LinReg, unique_prefix + "M1_Core_");
g_box_analyzers[2] = new CSessionAnalyzer();
g_box_analyzers[2].Init(InpM1_Enable && InpM1_PostMarket_Enable, InpM1_PostMarket_Start, InpM1_PostMarket_End, InpM1_PostMarket_Color, InpFillBoxes, InpM1_PostMarket_Mean, InpM1_PostMarket_LinReg, unique_prefix + "M1_Post_");
g_box_analyzers[3] = new CSessionAnalyzer();
g_box_analyzers[3].Init(InpM1_Enable && InpM1_FullDay_Enable, InpM1_PreMarket_Start, InpM1_PostMarket_End, InpM1_FullDay_Color, InpFillBoxes, InpM1_FullDay_Mean, InpM1_FullDay_LinReg, unique_prefix + "M1_Full_");
g_box_analyzers[4] = new CSessionAnalyzer();
g_box_analyzers[4].Init(InpM2_Enable && InpM2_PreMarket_Enable, InpM2_PreMarket_Start, InpM2_PreMarket_End, InpM2_PreMarket_Color, InpFillBoxes, InpM2_PreMarket_Mean, InpM2_PreMarket_LinReg, unique_prefix + "M2_Pre_");
g_box_analyzers[5] = new CSessionAnalyzer();
g_box_analyzers[5].Init(InpM2_Enable && InpM2_Core_Enable, InpM2_Core_Start, InpM2_Core_End, InpM2_Core_Color, InpFillBoxes, InpM2_Core_Mean, InpM2_Core_LinReg, unique_prefix + "M2_Core_");
g_box_analyzers[6] = new CSessionAnalyzer();
g_box_analyzers[6].Init(InpM2_Enable && InpM2_PostMarket_Enable, InpM2_PostMarket_Start, InpM2_PostMarket_End, InpM2_PostMarket_Color, InpFillBoxes, InpM2_PostMarket_Mean, InpM2_PostMarket_LinReg, unique_prefix + "M2_Post_");
g_box_analyzers[7] = new CSessionAnalyzer();
g_box_analyzers[7].Init(InpM2_Enable && InpM2_FullDay_Enable, InpM2_PreMarket_Start, InpM2_PostMarket_End, InpM2_FullDay_Color, InpFillBoxes, InpM2_FullDay_Mean, InpM2_FullDay_LinReg, unique_prefix + "M2_Full_");
g_box_analyzers[8] = new CSessionAnalyzer();
g_box_analyzers[8].Init(InpM3_Enable && InpM3_PreMarket_Enable, InpM3_PreMarket_Start, InpM3_PreMarket_End, InpM3_PreMarket_Color, InpFillBoxes, InpM3_PreMarket_Mean, InpM3_PreMarket_LinReg, unique_prefix + "M3_Pre_");
g_box_analyzers[9] = new CSessionAnalyzer();
g_box_analyzers[9].Init(InpM3_Enable && InpM3_Core_Enable, InpM3_Core_Start, InpM3_Core_End, InpM3_Core_Color, InpFillBoxes, InpM3_Core_Mean, InpM3_Core_LinReg, unique_prefix + "M3_Core_");
g_box_analyzers[10] = new CSessionAnalyzer();
g_box_analyzers[10].Init(InpM3_Enable && InpM3_PostMarket_Enable, InpM3_PostMarket_Start, InpM3_PostMarket_End, InpM3_PostMarket_Color, InpFillBoxes, InpM3_PostMarket_Mean, InpM3_PostMarket_LinReg, unique_prefix + "M3_Post_");
g_box_analyzers[11] = new CSessionAnalyzer();
g_box_analyzers[11].Init(InpM3_Enable && InpM3_FullDay_Enable, InpM3_PreMarket_Start, InpM3_PostMarket_End, InpM3_FullDay_Color, InpFillBoxes, InpM3_FullDay_Mean, InpM3_FullDay_LinReg, unique_prefix + "M3_Full_");
// --- Init Logic for VWAP Calculators (Buffer-based) ---
bool is_ha_candle = (InpCandleSource == CANDLE_HEIKIN_ASHI);
g_vwap_calculators[0] = is_ha_candle ? new CVWAPCalculator_HA() : new CVWAPCalculator();
g_vwap_calculators[0].Init(InpM1_PreMarket_Start, InpM1_PreMarket_End, InpVolumeType, InpM1_Enable && InpM1_PreMarket_Enable && InpM1_PreMarket_VWAP);
g_vwap_calculators[1] = is_ha_candle ? new CVWAPCalculator_HA() : new CVWAPCalculator();
g_vwap_calculators[1].Init(InpM1_Core_Start, InpM1_Core_End, InpVolumeType, InpM1_Enable && InpM1_Core_Enable && InpM1_Core_VWAP);
g_vwap_calculators[2] = is_ha_candle ? new CVWAPCalculator_HA() : new CVWAPCalculator();
g_vwap_calculators[2].Init(InpM1_PostMarket_Start, InpM1_PostMarket_End, InpVolumeType, InpM1_Enable && InpM1_PostMarket_Enable && InpM1_PostMarket_VWAP);
g_vwap_calculators[3] = is_ha_candle ? new CVWAPCalculator_HA() : new CVWAPCalculator();
g_vwap_calculators[3].Init(InpM1_PreMarket_Start, InpM1_PostMarket_End, InpVolumeType, InpM1_Enable && InpM1_FullDay_Enable && InpM1_FullDay_VWAP);
g_vwap_calculators[4] = is_ha_candle ? new CVWAPCalculator_HA() : new CVWAPCalculator();
g_vwap_calculators[4].Init(InpM2_PreMarket_Start, InpM2_PreMarket_End, InpVolumeType, InpM2_Enable && InpM2_PreMarket_Enable && InpM2_PreMarket_VWAP);
g_vwap_calculators[5] = is_ha_candle ? new CVWAPCalculator_HA() : new CVWAPCalculator();
g_vwap_calculators[5].Init(InpM2_Core_Start, InpM2_Core_End, InpVolumeType, InpM2_Enable && InpM2_Core_Enable && InpM2_Core_VWAP);
g_vwap_calculators[6] = is_ha_candle ? new CVWAPCalculator_HA() : new CVWAPCalculator();
g_vwap_calculators[6].Init(InpM2_PostMarket_Start, InpM2_PostMarket_End, InpVolumeType, InpM2_Enable && InpM2_PostMarket_Enable && InpM2_PostMarket_VWAP);
g_vwap_calculators[7] = is_ha_candle ? new CVWAPCalculator_HA() : new CVWAPCalculator();
g_vwap_calculators[7].Init(InpM2_PreMarket_Start, InpM2_PostMarket_End, InpVolumeType, InpM2_Enable && InpM2_FullDay_Enable && InpM2_FullDay_VWAP);
g_vwap_calculators[8] = is_ha_candle ? new CVWAPCalculator_HA() : new CVWAPCalculator();
g_vwap_calculators[8].Init(InpM3_PreMarket_Start, InpM3_PreMarket_End, InpVolumeType, InpM3_Enable && InpM3_PreMarket_Enable && InpM3_PreMarket_VWAP);
g_vwap_calculators[9] = is_ha_candle ? new CVWAPCalculator_HA() : new CVWAPCalculator();
g_vwap_calculators[9].Init(InpM3_Core_Start, InpM3_Core_End, InpVolumeType, InpM3_Enable && InpM3_Core_Enable && InpM3_Core_VWAP);
g_vwap_calculators[10] = is_ha_candle ? new CVWAPCalculator_HA() : new CVWAPCalculator();
g_vwap_calculators[10].Init(InpM3_PostMarket_Start, InpM3_PostMarket_End, InpVolumeType, InpM3_Enable && InpM3_PostMarket_Enable && InpM3_PostMarket_VWAP);
g_vwap_calculators[11] = is_ha_candle ? new CVWAPCalculator_HA() : new CVWAPCalculator();
g_vwap_calculators[11].Init(InpM3_PreMarket_Start, InpM3_PostMarket_End, InpVolumeType, InpM3_Enable && InpM3_FullDay_Enable && InpM3_FullDay_VWAP);
IndicatorSetString(INDICATOR_SHORTNAME, "Session Analysis");
IndicatorSetString(INDICATOR_SHORTNAME, "Session Analysis" + (is_ha_mode ? " HA" : ""));
return(INIT_SUCCEEDED);
}
@@ -355,7 +346,12 @@ void OnDeinit(const int reason)
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total, const int, 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[])
{
// --- CORRECTED: Explicitly clear all VWAP buffers at the start of each calculation ---
if(rates_total > 0 && time[rates_total - 1] == g_last_bar_time && Bars(_Symbol, _Period) == rates_total)
return(rates_total);
if(rates_total > 0)
g_last_bar_time = time[rates_total - 1];
// --- Explicitly clear all VWAP buffers at the start of each new bar calculation ---
ArrayInitialize(BufferM1_Pre_A, EMPTY_VALUE);
ArrayInitialize(BufferM1_Pre_B, EMPTY_VALUE);
ArrayInitialize(BufferM1_Core_A, EMPTY_VALUE);
@@ -381,11 +377,6 @@ int OnCalculate(const int rates_total, const int, const datetime& time[], const
ArrayInitialize(BufferM3_Full_A, EMPTY_VALUE);
ArrayInitialize(BufferM3_Full_B, EMPTY_VALUE);
if(rates_total > 0 && time[rates_total - 1] == g_last_bar_time && Bars(_Symbol, _Period) == rates_total)
return(rates_total);
if(rates_total > 0)
g_last_bar_time = time[rates_total - 1];
// --- Object Drawing Logic (Boxes, etc.) ---
for(int i=0; i<TOTAL_SESSIONS; i++)
{