 
#property strict

#include <Trade/Trade.mqh>
CTrade trade;

// =====================================================
// INCLUDE MODULES
// =====================================================
#include "Helpers.mqh"
#include "Risk.mqh"
#include "Filters.mqh"
#include "Institutional.mqh"
#include "Scoring.mqh"
#include "Execution.mqh"
#include "SessionFilter.mqh"

// =====================================================
// INPUTS (GLOBAL CONTROL PANEL)
// =====================================================
input double BaseRisk       = 1.0;
input double DailyLossLimit = 5.0;
input double ScoreThreshold = 7.0;

// =====================================================
// STATE (shared across modules)
// =====================================================
double dayStartEquity = 0;
int currentDay = -1;

// =====================================================
// INIT
// =====================================================
int OnInit()
{
   dayStartEquity = AccountInfoDouble(ACCOUNT_EQUITY);

   Print("Institutional EA initialized successfully");
   return INIT_SUCCEEDED;
}

// =====================================================
// MAIN ENGINE
// =====================================================
void OnTick()
{
   // =================================================
   // 1. DAILY RESET + RISK CHECK
   // =================================================
   ResetDaily();

   if(DailyLossHit(DailyLossLimit))
      return;

   // =================================================
   // 2. POSITION MANAGEMENT (ALWAYS ACTIVE)
   // =================================================
   ApplyBreakEven();
   ApplyTrailingStop();

   // =================================================
   // 3. FILTERS (NO TRADE CONDITIONS)
   // =================================================
   if(HasPosition())
      return;

   if(SpreadTooHigh(ATR(), 0.15))
      return;

   if(IsNewsTime())
      return;

   if(!StrongVolume())
      return;

   if(DetectRegime() != 1)
      return;
      
   if(!IsNYSession())
   return;

   // =================================================
   // 4. SCORING ENGINE
   // =================================================
   double buy  = BuyScore();
   double sell = SellScore();

   double threshold = ScoreThreshold;

   if(ATR() > AvgBody() * 2.0)
      threshold += 1.0;

   // =================================================
   // 5. LOT CALCULATION
   // =================================================
   double lot = CalcLot(BaseRisk);

   // =================================================
   // 6. EXECUTION LOGIC
   // =================================================
   if(buy >= threshold && buy > sell)
   {
      Execute(true, buy, lot);
   }

   if(sell >= threshold && sell > buy)
   {
      Execute(false, sell, lot);
   }
}
