//+------------------------------------------------------------------+
//|                             Polynomial_Regression_Object_Pro.mq5 |
//|                                          Copyright 2025, xxxxxxxx|
//+------------------------------------------------------------------+
#property copyright "Copyright 2025, xxxxxxxx"
#property version   "1.10" // Optimized for incremental calculation
#property description "Draws a single, moving Curvilinear Regression Channel using objects."

#property indicator_chart_window
#property indicator_buffers 0
#property indicator_plots   0

#include <MyIncludes\Polynomial_Regression_Object_Calculator.mqh>

//--- Input Parameters ---
input int                       InpPeriod      = 50;    // Regression Period
input double                    InpDeviation   = 2.0;   // Deviation for bands
input color                     InpMidlineColor = clrDeepSkyBlue;     // Midline Color
input color                     InpUpperBandColor = clrDeepSkyBlue;   // Upper Band Color
input color                     InpLowerBandColor = clrDeepSkyBlue;   // Lower Band Color
input ENUM_APPLIED_PRICE_HA_ALL InpSourcePrice = PRICE_CLOSE_STD;

//--- Global variables ---
CPolynomialRegressionObjectCalculator *g_calculator;
string                               g_unique_prefix;

//+------------------------------------------------------------------+
int OnInit()
  {
//--- CORRECTED: Create a STABLE and unique prefix for our objects ---
   long chart_id = ChartID();
// Find the subwindow where this indicator instance is running
   int window_index = ChartWindowFind();
   if(window_index < 0) // Safety check
     {
      Print("Error: Could not find indicator window.");
      return(INIT_FAILED);
     }
   g_unique_prefix = StringFormat("PolyRegObj_%d_%d_", chart_id, window_index);

//--- This will now correctly find and delete old objects on re-init ---
   ObjectsDeleteAll(0, g_unique_prefix);

   if(InpSourcePrice <= PRICE_HA_CLOSE)
      g_calculator = new CPolynomialRegressionObjectCalculator_HA();
   else
      g_calculator = new CPolynomialRegressionObjectCalculator();

   if(CheckPointer(g_calculator) == POINTER_INVALID ||
      !g_calculator.Init(InpPeriod, InpDeviation, g_unique_prefix, InpMidlineColor, InpUpperBandColor, InpLowerBandColor))
     {
      Print("Failed to initialize Polynomial Regression Object Calculator.");
      return(INIT_FAILED);
     }

   IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("PolyReg Obj%s(%d,%.1f)", (InpSourcePrice <= PRICE_HA_CLOSE ? " HA" : ""), InpPeriod, InpDeviation));
   IndicatorSetInteger(INDICATOR_DIGITS, _Digits);

   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   ObjectsDeleteAll(0, g_unique_prefix);
   if(CheckPointer(g_calculator) != POINTER_INVALID)
      delete g_calculator;
  }

//+------------------------------------------------------------------+
//| Custom indicator 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&[], const long&[], const int&[])
  {
   if(CheckPointer(g_calculator) == POINTER_INVALID)
      return 0;

   ENUM_APPLIED_PRICE price_type;
   if(InpSourcePrice <= PRICE_HA_CLOSE)
      price_type = (ENUM_APPLIED_PRICE)(-(int)InpSourcePrice);
   else
      price_type = (ENUM_APPLIED_PRICE)InpSourcePrice;

//--- Full Recalculation (No prev_calculated passed)
   g_calculator.Calculate(rates_total, time, price_type, open, high, low, close);

   return(rates_total);
  }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
