//+------------------------------------------------------------------+
//|                                                  StopOutMath.mqh |
//|                                       Copyright 2026, Ali Rajput |
//+------------------------------------------------------------------+
#property copyright "Copyright 2026, Ali Rajput"
#include "StopOutTypes.mqh"
//+------------------------------------------------------------------+
//| Defines                                                          |
//+------------------------------------------------------------------+
#define SOG_SCAN_STEPS 32 // equal steps of the scan before the bisection
//+------------------------------------------------------------------+
//| Equity and margin of the account as a function of one bid        |
//+------------------------------------------------------------------+
class IAccountModel
  {
public:
   virtual bool      Evaluate(const double bid,double &equity,double &margin)=0;
  };
//+------------------------------------------------------------------+
//| Level arithmetic and the price search, no terminal calls         |
//+------------------------------------------------------------------+
class CStopOutMath
  {
public:
   static double     Threshold(const bool percent,const double level,const double margin);
   static double     Gap(const bool percent,const double level,const double equity,const double margin);
   static double     MarginLevel(const double equity,const double margin);
   static double     SnapToTick(const double price,const double tick,const bool down);
   static ENUM_SOG_STATUS Solve(IAccountModel &model,const bool percent,const double level,
                                 const double bid_now,const double bid_limit,const double tick,
                                 double &bid_at,double &equity_at,double &margin_at);
   static double     LinearDistance(const bool percent,const double level,const double equity,
                                    const double margin,const double volume,const double tick_value,
                                    const double tick_size);
  };
//+------------------------------------------------------------------+
//| Equity at which the level is reached for a given margin          |
//+------------------------------------------------------------------+
double CStopOutMath::Threshold(const bool percent,const double level,const double margin)
  {
//--- percent mode compares equity/margin*100 with the level, money mode compares equity itself
   return(percent ? level*margin/100.0 : level);
  }
//+------------------------------------------------------------------+
//| Equity left above the level, negative once the level is crossed  |
//+------------------------------------------------------------------+
double CStopOutMath::Gap(const bool percent,const double level,const double equity,const double margin)
  {
   return(equity-Threshold(percent,level,margin));
  }
//+------------------------------------------------------------------+
//| Margin level in percent, 0 when no margin is used                |
//+------------------------------------------------------------------+
double CStopOutMath::MarginLevel(const double equity,const double margin)
  {
   return(margin>0.0 ? 100.0*equity/margin : 0.0);
  }
//+------------------------------------------------------------------+
//| Round a price to the tick grid in the direction of the search    |
//+------------------------------------------------------------------+
double CStopOutMath::SnapToTick(const double price,const double tick,const bool down)
  {
   if(tick<=0.0)
      return(price);
   double steps=price/tick;
//--- the small offset keeps a price that already sits on the grid where it is
   double whole=down ? MathFloor(steps+1e-7) : MathCeil(steps-1e-7);
   return(whole*tick);
  }
//+------------------------------------------------------------------+
//| First grid price from bid_now toward bid_limit at the level      |
//+------------------------------------------------------------------+
ENUM_SOG_STATUS CStopOutMath::Solve(IAccountModel &model,const bool percent,const double level,
                                    const double bid_now,const double bid_limit,const double tick,
                                    double &bid_at,double &equity_at,double &margin_at)
  {
   bid_at=0.0;
   double e=0.0,m=0.0;
   if(!model.Evaluate(bid_now,e,m))
      return(SOG_CALC_FAILED);
   equity_at=e;
   margin_at=m;
   if(m<=0.0 && percent)
      return(SOG_NO_MARGIN);
   if(Gap(percent,level,e,m)<=0.0)
     {
      bid_at=bid_now;
      return(SOG_ALREADY_BELOW);
     }
//--- a scan in equal steps finds the first step past the level, so a gap that crosses
//--- the level more than once still gives the nearest crossing, unless it dips below
//--- the level and comes back within one step
   double lo=bid_now,hi=bid_now;
   bool   found=false;
   for(int s=1; s<=SOG_SCAN_STEPS && !found; s++)
     {
      double p=bid_now+(bid_limit-bid_now)*s/SOG_SCAN_STEPS;
      if(!model.Evaluate(p,e,m))
         return(SOG_CALC_FAILED);
      if(Gap(percent,level,e,m)<=0.0)
        {
         hi=p;
         found=true;
        }
      else
         lo=p;
     }
   if(!found)
      return(SOG_OUT_OF_RANGE);
//--- lo keeps a price above the level, hi a price at or below it
   for(int i=0; i<200 && MathAbs(hi-lo)>tick*0.5; i++)
     {
      double mid=0.5*(lo+hi);
      if(!model.Evaluate(mid,e,m))
         return(SOG_CALC_FAILED);
      if(Gap(percent,level,e,m)>0.0)
         lo=mid;
      else
         hi=mid;
     }
//--- the answer is the first price on the tick grid at or past the crossing:
//--- step away from the bid until the level is reached, then back while it still is
   bool   down=(bid_limit<bid_now);
   double step=down ? -tick : tick;
   double t=SnapToTick(0.5*(lo+hi),tick,down);
   for(int i=0; i<4; i++)
     {
      if(!model.Evaluate(t,e,m))
         return(SOG_CALC_FAILED);
      if(Gap(percent,level,e,m)<=0.0)
         break;
      t+=step;
     }
   for(int i=0; i<4; i++)
     {
      double back=t-step;
      if(!model.Evaluate(back,e,m))
         return(SOG_CALC_FAILED);
      if(Gap(percent,level,e,m)>0.0)
         break;
      t=back;
     }
   if(!model.Evaluate(t,e,m))
      return(SOG_CALC_FAILED);
   bid_at=t;
   equity_at=e;
   margin_at=m;
   return(SOG_OK);
  }
//+------------------------------------------------------------------+
//| The usual shortcut: fixed margin and a fixed tick value          |
//+------------------------------------------------------------------+
double CStopOutMath::LinearDistance(const bool percent,const double level,const double equity,
                                    const double margin,const double volume,const double tick_value,
                                    const double tick_size)
  {
   double money_per_unit=MathAbs(volume)*tick_value/tick_size;
   if(money_per_unit<=0.0)
      return(DBL_MAX);
//--- price distance that eats the equity above the level, in price units
   return(Gap(percent,level,equity,margin)/money_per_unit);
  }
//+------------------------------------------------------------------+
