refactor: Added History Limit to reduce template size

This commit is contained in:
Toh4iem9
2025-12-04 19:47:56 +01:00
parent add0baec93
commit 4e5fc90973
+178 -103
View File
@@ -1,7 +1,6 @@
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| Session_Analysis_Calculator.mqh | //| Session_Analysis_Calculator.mqh |
//| Calculation engine for drawing session boxes and analytics. | //| VERSION 2.10: Added history limit for objects. |
//| Restored original logic with updated HA call. |
//| Copyright 2025, xxxxxxxx | //| Copyright 2025, xxxxxxxx |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
#property copyright "Copyright 2025, xxxxxxxx" #property copyright "Copyright 2025, xxxxxxxx"
@@ -22,23 +21,44 @@ protected:
bool m_fill_box; bool m_fill_box;
bool m_show_mean; bool m_show_mean;
bool m_show_linreg; bool m_show_linreg;
int m_max_history_days; // Limit object history
//--- Persistent Data Buffers
double m_src_high[], m_src_low[], m_src_price[]; double m_src_high[], m_src_low[], m_src_price[];
//--- Persistent State for Incremental Logic
bool m_in_session;
int m_session_start_bar;
datetime m_session_start_time;
bool IsTimeInSession(const MqlDateTime &dt); bool IsTimeInSession(const MqlDateTime &dt);
virtual bool PrepareSourceData(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[], ENUM_APPLIED_PRICE price_type);
virtual bool PrepareSourceData(int rates_total, int start_index, const double &open[], const double &high[], const double &low[], const double &close[], ENUM_APPLIED_PRICE price_type);
void DrawSession(int start_bar, int end_bar, long session_id, const datetime &time[]); void DrawSession(int start_bar, int end_bar, long session_id, const datetime &time[]);
public: public:
CSessionAnalyzer(void) {}; CSessionAnalyzer(void);
virtual ~CSessionAnalyzer(void) {}; virtual ~CSessionAnalyzer(void) {};
void Init(bool enabled, string start_time, string end_time, color box_color, bool fill_box, bool show_mean, bool show_linreg, string prefix);
void Update(const int rates_total, const datetime &time[], const double &open[], const double &high[], const double &low[], const double &close[], ENUM_APPLIED_PRICE price_type); void Init(bool enabled, string start_time, string end_time, color box_color, bool fill_box, bool show_mean, bool show_linreg, string prefix, int max_history_days);
void Update(int rates_total, int prev_calculated, const datetime &time[], const double &open[], const double &high[], const double &low[], const double &close[], ENUM_APPLIED_PRICE price_type);
void Cleanup(void); void Cleanup(void);
}; };
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
void CSessionAnalyzer::Init(bool enabled, string start_time, string end_time, color box_color, bool fill_box, bool show_mean, bool show_linreg, string prefix) CSessionAnalyzer::CSessionAnalyzer(void)
{
m_in_session = false;
m_session_start_bar = -1;
m_session_start_time = 0;
m_max_history_days = 0;
}
//+------------------------------------------------------------------+
void CSessionAnalyzer::Init(bool enabled, string start_time, string end_time, color box_color, bool fill_box, bool show_mean, bool show_linreg, string prefix, int max_history_days)
{ {
m_enabled = enabled; m_enabled = enabled;
m_prefix = prefix; m_prefix = prefix;
@@ -46,6 +66,7 @@ void CSessionAnalyzer::Init(bool enabled, string start_time, string end_time, co
m_fill_box = fill_box; m_fill_box = fill_box;
m_show_mean = show_mean; m_show_mean = show_mean;
m_show_linreg = show_linreg; m_show_linreg = show_linreg;
m_max_history_days = max_history_days;
string parts[]; string parts[];
if(StringSplit(start_time, ':', parts) == 2) if(StringSplit(start_time, ':', parts) == 2)
@@ -80,48 +101,88 @@ void CSessionAnalyzer::Cleanup(void)
} }
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
void CSessionAnalyzer::Update(const int rates_total, const datetime &time[], const double &open[], const double &high[], const double &low[], const double &close[], ENUM_APPLIED_PRICE price_type) // Main Update Method
//+------------------------------------------------------------------+
void CSessionAnalyzer::Update(int rates_total, int prev_calculated, const datetime &time[], const double &open[], const double &high[], const double &low[], const double &close[], ENUM_APPLIED_PRICE price_type)
{ {
if(!m_enabled || rates_total < 2) if(!m_enabled || rates_total < 2)
return; return;
if(!PrepareSourceData(rates_total, open, high, low, close, price_type)) // Force full recalculation logic for stability (as requested)
// But we use the structure that supports incremental if needed later.
// Here we reset state every time because OnCalculate passes prev_calculated but we might want to redraw.
// Actually, to fix the "bloat" issue, we must redraw only visible/recent history.
// Reset state for full recalc
int start_index = 0;
m_in_session = false;
m_session_start_bar = -1;
m_session_start_time = 0;
// Note: We don't call Cleanup() here every tick because it causes flickering.
// We rely on ObjectFind/ObjectMove inside DrawSession.
// However, if we change history limit, old objects might remain.
// Ideally, Cleanup() should be called if parameters change (OnInit).
if(ArraySize(m_src_high) != rates_total)
{
ArrayResize(m_src_high, rates_total);
ArrayResize(m_src_low, rates_total);
ArrayResize(m_src_price, rates_total);
}
if(!PrepareSourceData(rates_total, start_index, open, high, low, close, price_type))
return; return;
bool in_session = false; // Calculate cutoff time for history limit
int session_start_bar = -1; datetime cutoff_time = 0;
if(m_max_history_days > 0)
cutoff_time = TimeCurrent() - m_max_history_days * 86400;
// Optimization: Only check visible bars + buffer? int i = start_index;
// For now, full loop as requested to match original behavior. if(i == 0)
for(int i = 1; i < rates_total; i++) i = 1;
for(; i < rates_total; i++)
{ {
MqlDateTime dt; MqlDateTime dt;
TimeToStruct(time[i], dt); TimeToStruct(time[i], dt);
bool is_in_current_session = IsTimeInSession(dt); bool is_in_current_session = IsTimeInSession(dt);
if(is_in_current_session && !in_session) if(is_in_current_session && !m_in_session)
{ {
in_session = true; m_in_session = true;
session_start_bar = i; m_session_start_bar = i;
m_session_start_time = time[i];
} }
else else
if(!is_in_current_session && in_session) if(!is_in_current_session && m_in_session)
{ {
in_session = false; m_in_session = false;
MqlDateTime start_dt;
TimeToStruct(time[session_start_bar], start_dt);
long session_id = (long)time[session_start_bar] - (start_dt.hour * 3600 + start_dt.min * 60 + start_dt.sec);
DrawSession(session_start_bar, i - 1, session_id, time);
session_start_bar = -1;
}
}
if(in_session && session_start_bar != -1) // Only draw if session end time is newer than cutoff
{ if(time[i] >= cutoff_time)
MqlDateTime start_dt; {
TimeToStruct(time[session_start_bar], start_dt); MqlDateTime start_dt;
long session_id = (long)time[session_start_bar] - (start_dt.hour * 3600 + start_dt.min * 60 + start_dt.sec); TimeToStruct(m_session_start_time, start_dt);
DrawSession(session_start_bar, rates_total - 1, session_id, time); long session_id = (long)m_session_start_time - (start_dt.hour * 3600 + start_dt.min * 60 + start_dt.sec);
DrawSession(m_session_start_bar, i - 1, session_id, time);
}
m_session_start_bar = -1;
}
if(m_in_session)
{
if(time[i] >= cutoff_time)
{
MqlDateTime start_dt;
TimeToStruct(m_session_start_time, start_dt);
long session_id = (long)m_session_start_time - (start_dt.hour * 3600 + start_dt.min * 60 + start_dt.sec);
DrawSession(m_session_start_bar, i, session_id, time);
}
}
} }
} }
@@ -131,8 +192,12 @@ void CSessionAnalyzer::DrawSession(int start_bar, int end_bar, long session_id,
if(start_bar < 0 || end_bar < start_bar) if(start_bar < 0 || end_bar < start_bar)
return; return;
double session_high = m_src_high[ArrayMaximum(m_src_high, start_bar, end_bar - start_bar + 1)]; int count = end_bar - start_bar + 1;
double session_low = m_src_low[ArrayMinimum(m_src_low, start_bar, end_bar - start_bar + 1)]; int high_idx = ArrayMaximum(m_src_high, start_bar, count);
int low_idx = ArrayMinimum(m_src_low, start_bar, count);
double session_high = m_src_high[high_idx];
double session_low = m_src_low[low_idx];
string box_name = m_prefix + "Box_" + (string)session_id; string box_name = m_prefix + "Box_" + (string)session_id;
if(ObjectFind(0, box_name) < 0) if(ObjectFind(0, box_name) < 0)
@@ -150,6 +215,7 @@ void CSessionAnalyzer::DrawSession(int start_bar, int end_bar, long session_id,
ObjectMove(0, box_name, 1, time[end_bar], session_low); ObjectMove(0, box_name, 1, time[end_bar], session_low);
} }
// --- Mean and LinReg ---
if(m_show_mean || m_show_linreg) if(m_show_mean || m_show_linreg)
{ {
double cumulative_price = 0; double cumulative_price = 0;
@@ -209,96 +275,105 @@ void CSessionAnalyzer::DrawSession(int start_bar, int end_bar, long session_id,
} }
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
bool CSessionAnalyzer::PrepareSourceData(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[], ENUM_APPLIED_PRICE price_type) bool CSessionAnalyzer::PrepareSourceData(int rates_total, int start_index, const double &open[], const double &high[], const double &low[], const double &close[], ENUM_APPLIED_PRICE price_type)
{ {
ArrayResize(m_src_high, rates_total); // Optimized copy loop
ArrayCopy(m_src_high, high, 0, 0, rates_total); for(int i = start_index; i < rates_total; i++)
ArrayResize(m_src_low, rates_total);
ArrayCopy(m_src_low, low, 0, 0, rates_total);
ArrayResize(m_src_price, rates_total);
switch(price_type)
{ {
case PRICE_OPEN: m_src_high[i] = high[i];
ArrayCopy(m_src_price, open, 0, 0, rates_total); m_src_low[i] = low[i];
break;
case PRICE_HIGH: switch(price_type)
ArrayCopy(m_src_price, high, 0, 0, rates_total); {
break; case PRICE_OPEN:
case PRICE_LOW: m_src_price[i] = open[i];
ArrayCopy(m_src_price, low, 0, 0, rates_total); break;
break; case PRICE_HIGH:
case PRICE_MEDIAN: m_src_price[i] = high[i];
for(int i=0; i<rates_total; i++) break;
case PRICE_LOW:
m_src_price[i] = low[i];
break;
case PRICE_MEDIAN:
m_src_price[i] = (high[i]+low[i])/2.0; m_src_price[i] = (high[i]+low[i])/2.0;
break; break;
case PRICE_TYPICAL: case PRICE_TYPICAL:
for(int i=0; i<rates_total; i++)
m_src_price[i] = (high[i]+low[i]+close[i])/3.0; m_src_price[i] = (high[i]+low[i]+close[i])/3.0;
break; break;
case PRICE_WEIGHTED: case PRICE_WEIGHTED:
for(int i=0; i<rates_total; i++)
m_src_price[i] = (high[i]+low[i]+2*close[i])/4.0; m_src_price[i] = (high[i]+low[i]+2*close[i])/4.0;
break; break;
default: default:
ArrayCopy(m_src_price, close, 0, 0, rates_total); m_src_price[i] = close[i];
break; break;
}
} }
return true; return true;
} }
//+==================================================================+
//| CLASS 2: CSessionAnalyzer_HA (Heikin Ashi) |
//+==================================================================+ //+==================================================================+
class CSessionAnalyzer_HA : public CSessionAnalyzer class CSessionAnalyzer_HA : public CSessionAnalyzer
{ {
private: private:
CHeikinAshi_Calculator m_ha_calculator; CHeikinAshi_Calculator m_ha_calculator;
// Internal HA buffers
double m_ha_open[], m_ha_high[], m_ha_low[], m_ha_close[];
protected: protected:
virtual bool PrepareSourceData(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[], ENUM_APPLIED_PRICE price_type) override; virtual bool PrepareSourceData(int rates_total, int start_index, const double &open[], const double &high[], const double &low[], const double &close[], ENUM_APPLIED_PRICE price_type) override;
}; };
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| CSessionAnalyzer_HA: Prepares the HA source data. | //| Prepare Source Data (Heikin Ashi - Optimized) |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
bool CSessionAnalyzer_HA::PrepareSourceData(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[], ENUM_APPLIED_PRICE price_type) bool CSessionAnalyzer_HA::PrepareSourceData(int rates_total, int start_index, const double &open[], const double &high[], const double &low[], const double &close[], ENUM_APPLIED_PRICE price_type)
{ {
double ha_open[], ha_high[], ha_low[], ha_close[]; // Resize internal HA buffers
ArrayResize(ha_open, rates_total); if(ArraySize(m_ha_open) != rates_total)
ArrayResize(ha_high, rates_total);
ArrayResize(ha_low, rates_total);
ArrayResize(ha_close, rates_total);
//--- UPDATED: Pass '0' as start_index for full recalculation
m_ha_calculator.Calculate(rates_total, 0, open, high, low, close, ha_open, ha_high, ha_low, ha_close);
ArrayCopy(m_src_high, ha_high, 0, 0, rates_total);
ArrayCopy(m_src_low, ha_low, 0, 0, rates_total);
ArrayResize(m_src_price, rates_total);
switch(price_type)
{ {
case PRICE_OPEN: ArrayResize(m_ha_open, rates_total);
ArrayCopy(m_src_price, ha_open, 0, 0, rates_total); ArrayResize(m_ha_high, rates_total);
break; ArrayResize(m_ha_low, rates_total);
case PRICE_HIGH: ArrayResize(m_ha_close, rates_total);
ArrayCopy(m_src_price, ha_high, 0, 0, rates_total); }
break;
case PRICE_LOW: //--- STRICT CALL: Use the optimized 10-param HA calculation
ArrayCopy(m_src_price, ha_low, 0, 0, rates_total); //--- Note: Since we force start_index=0 in Update for full recalc, this will recalc HA too.
break; m_ha_calculator.Calculate(rates_total, start_index, open, high, low, close,
case PRICE_MEDIAN: m_ha_open, m_ha_high, m_ha_low, m_ha_close);
for(int i=0; i<rates_total; i++)
m_src_price[i] = (ha_high[i]+ha_low[i])/2.0; //--- Copy to source buffers (Optimized loop)
break; for(int i = start_index; i < rates_total; i++)
case PRICE_TYPICAL: {
for(int i=0; i<rates_total; i++) m_src_high[i] = m_ha_high[i];
m_src_price[i] = (ha_high[i]+ha_low[i]+ha_close[i])/3.0; m_src_low[i] = m_ha_low[i];
break;
case PRICE_WEIGHTED: switch(price_type)
for(int i=0; i<rates_total; i++) {
m_src_price[i] = (ha_high[i]+ha_low[i]+2*ha_close[i])/4.0; case PRICE_OPEN:
break; m_src_price[i] = m_ha_open[i];
default: break;
ArrayCopy(m_src_price, ha_close, 0, 0, rates_total); case PRICE_HIGH:
break; m_src_price[i] = m_ha_high[i];
break;
case PRICE_LOW:
m_src_price[i] = m_ha_low[i];
break;
case PRICE_MEDIAN:
m_src_price[i] = (m_ha_high[i]+m_ha_low[i])/2.0;
break;
case PRICE_TYPICAL:
m_src_price[i] = (m_ha_high[i]+m_ha_low[i]+m_ha_close[i])/3.0;
break;
case PRICE_WEIGHTED:
m_src_price[i] = (m_ha_high[i]+m_ha_low[i]+2*m_ha_close[i])/4.0;
break;
default:
m_src_price[i] = m_ha_close[i];
break;
}
} }
return true; return true;
} }