//+------------------------------------------------------------------+
//|                                     SessionVolumeProfile.mq5     |
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 0
#property indicator_plots   0

#include <Session_Volume_Profile_Builder\SessionManager.mqh>
#include <Session_Volume_Profile_Builder\TickCollector.mqh>
#include <Session_Volume_Profile_Builder\PriceHistogram.mqh>
#include <Session_Volume_Profile_Builder\VolumeProfileCalculator.mqh>
#include <Session_Volume_Profile_Builder\ProfileRenderer.mqh>
#include <Session_Volume_Profile_Builder\ProfileStatistics.mqh>

//--- Inputs
input ENUM_SESSION_TYPE InpSessionType      = SESSION_LONDON; // Session type
input int               InpStartHour        = 8;              // Custom start hour
input int               InpStartMinute      = 0;              // Custom start minute
input int               InpEndHour          = 17;             // Custom end hour
input int               InpEndMinute        = 0;              // Custom end minute
input int               InpBinSizePoints    = 10;             // Bin size (points)
input double            InpValueAreaPercent = 70.0;           // Value Area percentage
input color             InpBarColor         = clrSteelBlue;   // Histogram bar color
input color             InpPOCColor         = clrYellow;      // POC line color
input color             InpVAHColor         = clrLimeGreen;   // VAH line color
input color             InpVALColor         = clrRed;         // VAL line color
input int               InpBarWidth         = 2;              // Bar line width
input string            InpPrefix           = "SVP_";         // Object name prefix
input bool              InpFixedWidth       = true;           // Use fixed bar width mode
input int               InpFixedBarCount    = 20;             // POC bar width (chart bars)

//--- Global instances
CSessionManager          g_session_mgr;
CTickCollector           g_tick_collector;
CPriceHistogram          g_histogram;
CVolumeProfileCalculator g_calculator;
CProfileRenderer         g_renderer;
CProfileStatistics       g_statistics;

//+------------------------------------------------------------------+
//| Indicator Initialization                                         |
//+------------------------------------------------------------------+
int OnInit(void)
  {
   g_session_mgr.Configure(InpSessionType,
                           InpStartHour, InpStartMinute,
                           InpEndHour,   InpEndMinute);

   g_renderer.Configure(InpPrefix,
                        InpBarColor,
                        InpPOCColor,
                        InpVAHColor,
                        InpVALColor,
                        InpBarWidth,
                        InpFixedBarCount,
                        InpFixedWidth);

   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//| Indicator Deinitialization                                       |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   g_renderer.RemoveSessionObjects();
  }

//+------------------------------------------------------------------+
//| Indicator Calculation                                            |
//+------------------------------------------------------------------+
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     &tick_volume[],
                const long     &volume[],
                const int      &spread[])
  {
   if(rates_total < 1)
      return(0);

//--- Suppress tick-driven recalculation; only process on new bar
   if(prev_calculated == rates_total)
      return(rates_total);

   datetime current_time = time[rates_total - 1];

   bool is_new = g_session_mgr.Update(current_time);

   if(!is_new)
      return(rates_total);

//--- Remove previous session objects
   g_renderer.RemoveSessionObjects();

//--- Collect ticks for the new session
   datetime session_start = g_session_mgr.GetSessionStart();
   datetime session_end   = g_session_mgr.GetSessionEnd();

   bool collected = g_tick_collector.Collect(_Symbol,
                    session_start,
                    session_end);
   if(!collected)
      return(rates_total);

//--- Build the price histogram
   g_histogram.Reset();
   bool built = g_histogram.Build(_Symbol,
                                  InpBinSizePoints,
                                  g_tick_collector);
   if(!built)
      return(rates_total);

//--- Calculate POC and Value Area
   bool calculated = g_calculator.Calculate(g_histogram,
                     InpValueAreaPercent);
   if(!calculated)
      return(rates_total);

//--- Render the profile
   g_renderer.Render(g_histogram,
                     g_calculator.GetPOC(),
                     g_calculator.GetVAH(),
                     g_calculator.GetVAL(),
                     session_start,
                     session_end);

//--- Print diagnostics
   int object_count = ObjectsTotal(ChartID(), 0, -1);
   g_statistics.Print(g_tick_collector.GetCount(),
                      g_histogram.GetBinCount(),
                      g_calculator.GetPOC(),
                      g_calculator.GetVAH(),
                      g_calculator.GetVAL(),
                      object_count);

   return(rates_total);
  }
//+------------------------------------------------------------------+