//+------------------------------------------------------------------+
//|                                                    ValueArea.mqh |
//+------------------------------------------------------------------+
#ifndef VALUEAREA_MQH
#define VALUEAREA_MQH
#include "VolumeProfile.mqh"

//+------------------------------------------------------------------+
//| CValueArea                                                       |
//| Computes the point of control, value area high, and value area   |
//| low from a finished CVolumeProfile using the standard expansion  |
//| algorithm.                                                       |
//+------------------------------------------------------------------+
class CValueArea
  {
private:
   int               m_poc_index;
   int               m_vah_index;
   int               m_val_index;
   double            m_poc_volume;

public:
                     CValueArea(void);
                    ~CValueArea(void);
   bool              Compute(CVolumeProfile &profile, const double coverage);
   double            PocPrice(CVolumeProfile &profile);
   double            PocVolume(void);
   double            ValueAreaHigh(CVolumeProfile &profile);
   double            ValueAreaLow(CVolumeProfile &profile);
   int               PocIndex(void);
   int               ValueAreaHighIndex(void);
   int               ValueAreaLowIndex(void);
  };

//+------------------------------------------------------------------+
//| Constructor                                                      |
//+------------------------------------------------------------------+
CValueArea::CValueArea(void)
  {
   m_poc_index  = 0;
   m_vah_index  = 0;
   m_val_index  = 0;
   m_poc_volume = 0.0;
  }

//+------------------------------------------------------------------+
//| Destructor                                                       |
//+------------------------------------------------------------------+
CValueArea::~CValueArea(void)
  {
  }

//+------------------------------------------------------------------+
//| Compute                                                          |
//| Implements the standard expansion algorithm: the value area      |
//| starts as a single bin range at the point of control, then grows |
//| one bin at a time toward whichever neighbor (above the current   |
//| range or below it) carries more volume, until the covered volume |
//| reaches the requested coverage fraction of total volume. The     |
//| 0.70 input is the standard convention for a 70 percent value     |
//| area. Starting at the POC and always taking the richer neighbor  |
//| keeps the value area centered on the busiest price level rather  |
//| than drifting toward one side of the distribution. This is a     |
//| greedy, nearest-larger-neighbor expansion: it does not evaluate  |
//| alternative windows, so it does not guarantee the narrowest      |
//| possible contiguous range that reaches the target coverage, only |
//| a locally greedy one.                                            |
//+------------------------------------------------------------------+
bool CValueArea::Compute(CVolumeProfile &profile, const double coverage)
  {
   int    bin_count = profile.BinCount();
   double total_vol = profile.TotalVolume();

   if(bin_count <= 0 || total_vol <= 0.0)
      return(false);

//--- locate the point of control as the richest bin
   m_poc_index  = profile.MaxBinIndex();
   m_poc_volume = profile.BinVolume(m_poc_index);

   int    lo      = m_poc_index;
   int    hi      = m_poc_index;
   double covered = m_poc_volume;
   double target  = coverage * total_vol;

//--- expand outward from the POC toward the richer neighbor each step
   while(covered < target)
     {
      int    up_index   = hi + 1;
      int    down_index = lo - 1;
      double up_vol     = (up_index   < bin_count) ? profile.BinVolume(up_index)   : -1.0;
      double down_vol   = (down_index >= 0)        ? profile.BinVolume(down_index) : -1.0;

      //--- stop once both edges of the histogram are exhausted
      if(up_vol < 0 && down_vol < 0)
         break;

      if(up_vol >= down_vol)
        {
         hi       = up_index;
         covered += up_vol;
        }
      else
        {
         lo       = down_index;
         covered += down_vol;
        }
     }

   m_val_index = lo;
   m_vah_index = hi;

   return(true);
  }

//+------------------------------------------------------------------+
//| PocPrice                                                         |
//+------------------------------------------------------------------+
double CValueArea::PocPrice(CVolumeProfile &profile)
  {
   return(profile.BinPrice(m_poc_index));
  }

//+------------------------------------------------------------------+
//| PocVolume                                                        |
//+------------------------------------------------------------------+
double CValueArea::PocVolume(void)
  {
   return(m_poc_volume);
  }

//+------------------------------------------------------------------+
//| ValueAreaHigh                                                    |
//+------------------------------------------------------------------+
double CValueArea::ValueAreaHigh(CVolumeProfile &profile)
  {
   return(profile.BinPrice(m_vah_index));
  }

//+------------------------------------------------------------------+
//| ValueAreaLow                                                     |
//+------------------------------------------------------------------+
double CValueArea::ValueAreaLow(CVolumeProfile &profile)
  {
   return(profile.BinPrice(m_val_index));
  }

//+------------------------------------------------------------------+
//| PocIndex                                                         |
//+------------------------------------------------------------------+
int CValueArea::PocIndex(void)
  {
   return(m_poc_index);
  }

//+------------------------------------------------------------------+
//| ValueAreaHighIndex                                               |
//+------------------------------------------------------------------+
int CValueArea::ValueAreaHighIndex(void)
  {
   return(m_vah_index);
  }

//+------------------------------------------------------------------+
//| ValueAreaLowIndex                                                |
//+------------------------------------------------------------------+
int CValueArea::ValueAreaLowIndex(void)
  {
   return(m_val_index);
  }

#endif // VALUEAREA_MQH
//+------------------------------------------------------------------+