 
#ifndef LOSSFILTER_MQH
#define LOSSFILTER_MQH

// =====================================================
// LOSS FILTER LAYER (US30 OPTIMIZED)
// =====================================================

// =====================================================
// INPUTS (USER CONFIGURABLE)
// =====================================================
input bool UseLossFilter              = true;

// Sweep quality controls
input bool RequireDisplacement        = true;
input double MinDisplacementBodyATR   = 1.5;

// Regime protection
input bool BlockChopTrades            = true;

// Volatility spike filter
input bool UseVolatilityFilter        = true;
input double MaxSpreadATRRatio        = 0.18;

// Confirmation delay logic
input bool RequireConfirmationCandle  = true;

// =====================================================
// HELPER: ATR (expects Helpers.mqh)
// =====================================================
double LF_ATR()
{
   return ATR();
}

// =====================================================
// 1. DISPLACEMENT QUALITY CHECK
// =====================================================
bool HasDisplacement()
{
   double body = MathAbs(C(0) - O(0));
   double atr  = LF_ATR();

   if(atr <= 0)
      return false;

   return body >= atr * MinDisplacementBodyATR;
}

// =====================================================
// 2. CHOP DETECTION (micro noise filter)
// =====================================================
bool IsChoppy()
{
   double range = H(1) - L(1);
   double atr   = LF_ATR();

   if(atr <= 0)
      return true;

   // price expanding and contracting too fast = chop
   return range < atr * 0.7;
}

// =====================================================
// 3. VOLATILITY SPIKE FILTER
// =====================================================
bool VolatilitySafe()
{
   if(!UseVolatilityFilter)
      return true;

   double atr = LF_ATR();
   double spread = SymbolInfoInteger(_Symbol, SYMBOL_SPREAD) * _Point;

   if(atr <= 0)
      return false;

   return spread <= atr * MaxSpreadATRRatio;
}

// =====================================================
// 4. CONFIRMATION CANDLE CHECK
// =====================================================
bool HasConfirmation(bool bullish)
{
   if(!RequireConfirmationCandle)
      return true;

   if(bullish)
      return C(0) > C(1) && L(0) > L(1);

   return C(0) < C(1) && H(0) < H(1);
}

// =====================================================
// 5. SWEEP QUALITY VALIDATION
// =====================================================
bool ValidBullSweep()
{
   if(!BullishLiquiditySweep())
      return false;

   if(RequireDisplacement && !HasDisplacement())
      return false;

   if(RequireConfirmationCandle && !HasConfirmation(true))
      return false;

   return true;
}

bool ValidBearSweep()
{
   if(!BearishLiquiditySweep())
      return false;

   if(RequireDisplacement && !HasDisplacement())
      return false;

   if(RequireConfirmationCandle && !HasConfirmation(false))
      return false;

   return true;
}

// =====================================================
// 6. GLOBAL TRADE PERMISSION CHECK
// =====================================================
bool AllowTrade()
{
   if(!UseLossFilter)
      return true;

   if(BlockChopTrades && IsChoppy())
      return false;

   if(!VolatilitySafe())
      return false;

   return true;
}

#endif
