//+------------------------------------------------------------------+
//|                                         ScatterPlotDashboard.mq5 |
//+------------------------------------------------------------------+
#property script_show_inputs

#include <ScatterDashboard/ScatterTypes.mqh>
#include <ScatterDashboard/ClosedTradeExtractor.mqh>
#include <ScatterDashboard/RegressionCalculator.mqh>
#include <ScatterDashboard/ScatterPlotChart.mqh>
#include <ScatterDashboard/TradeTablePrinter.mqh>

input int InpLookbackDays = 90;    // Number of days to look back from now
input int InpPanelX       = 20;    // Canvas panel X coordinate
input int InpPanelY       = 20;    // Canvas panel Y coordinate
input int InpPanelWidth   = 640;   // Canvas panel width in pixels
input int InpPanelHeight  = 300;   // Canvas panel height in pixels

//+------------------------------------------------------------------+
//| OnStart                                                          |
//| Resolves the date range, extracts closed trades, fits a          |
//| regression line, finds the optimal duration bucket, and renders  |
//| both the scatter plot and the per-symbol Experts tab table.      |
//+------------------------------------------------------------------+
void OnStart(void)
  {
//--- resolve the date range from the lookback input
   datetime to_time   = ::TimeCurrent();
   datetime from_time = to_time - (InpLookbackDays * 86400);
//--- extract closed trades with their duration and profit
   CClosedTradeExtractor extractor;
   CTradePoint           trades[];
   int                   trade_count = extractor.Read(from_time, to_time, trades);
   ::Print("ScatterDashboard: extracted ", trade_count, " closed trades");
   if(trade_count < 1)
     {
      ::Print("ScatterDashboard: no closed trades found in the selected range");
      return;
     }
//--- fit the regression line and find the best-performing bucket
   CRegressionCalculator calculator;
   CRegressionResult     regression;
   bool has_regression = calculator.Compute(trades, trade_count, regression);
   if(!has_regression)
     {
      regression.slope     = 0.0;
      regression.intercept = 0.0;
      regression.r_squared = 0.0;
      ::Print("ScatterDashboard: regression could not be computed (durations may be identical); showing scatter plot and table without a fitted line");
     }
   double best_bucket_avg_profit = 0.0;
   string best_bucket            = calculator.ComputeBestHistoricalBucket(trades, trade_count, best_bucket_avg_profit);
   ::PrintFormat("ScatterDashboard: slope %.4f/min, R^2 %.4f, best bucket %s (avg profit %.2f)",
                 regression.slope, regression.r_squared, best_bucket, best_bucket_avg_profit);
//--- render the scatter plot
   CScatterPlotChart chart;
   chart.Draw(trades, trade_count, regression, has_regression, best_bucket, best_bucket_avg_profit,
              InpPanelX, InpPanelY, InpPanelWidth, InpPanelHeight);
//--- print the per-symbol summary table
   CTradeTablePrinter printer;
   printer.Print(trades, trade_count);
  }
//+------------------------------------------------------------------+