//+------------------------------------------------------------------+
//| SessionRiskCore.mqh - shared session calculations                |
//| Identical arithmetic for the MT5 exporter/replay and the SPSS    |
//| equations.                                                       |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Include guard                                                    |
//+------------------------------------------------------------------+
#ifndef SESSION_RISK_CORE_MQH                  	// Skip definitions if already included.
#define SESSION_RISK_CORE_MQH                  	// Mark the shared include as loaded.
//+------------------------------------------------------------------+
//| ValidPriceBar - validate OHLC prices                             |
//+------------------------------------------------------------------+
bool ValidPriceBar(double o,double h,double l,double c)
  {
   return MathIsValidNumber(o) && MathIsValidNumber(h) &&
          MathIsValidNumber(l) && MathIsValidNumber(c) &&
          o>0 && l>0 && h>=o && h>=c && l<=o && l<=c;
  }
//+------------------------------------------------------------------+
//| SessionFeatures - reconstruct the two morning predictors         |
//+------------------------------------------------------------------+
bool SessionFeatures(double o,double h,double l,double c,double a,double &x1,double &x2)
  {
//--- Validate OHLC prices and the historical scale.
   if(!ValidPriceBar(o,h,l,c) || !MathIsValidNumber(a) || a<=0 || h<=l)
      return false;
//--- Calculate the range and body predictors.
   x1=MathLog(1.0+(h-l)/a);
   x2=MathAbs(c-o)/(h-l);
   return MathIsValidNumber(x1) && MathIsValidNumber(x2);
  }
//+------------------------------------------------------------------+
//| ExceedsBudget - label a target range above its budget            |
//+------------------------------------------------------------------+
int ExceedsBudget(double r,double a)
  {
   return (r>1.25*a)?1:0;
  }
//+------------------------------------------------------------------+
//| RiskSigmoid - convert a finite score to a probability            |
//+------------------------------------------------------------------+
double RiskSigmoid(double z)
  {
//--- Keep the exponential argument non-positive in both branches.
   if(z>=0)
      return 1.0/(1.0+MathExp(-z));
   double e=MathExp(z);
   return e/(1.0+e);
  }
#endif                                         	// SESSION_RISK_CORE_MQH
//+------------------------------------------------------------------+
