//+------------------------------------------------------------------+
//|                                                         SMIF.mq5 |
//|                                  Copyright 2025, MetaQuotes Ltd. |
//|                     https://www.mql5.com/en/users/johnhlomohang/ |
//+------------------------------------------------------------------+
#property copyright "Copyright 2025, MetaQuotes Ltd."
#property link      "https://www.mql5.com/en/users/johnhlomohang/"
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 0
#property indicator_plots   0



//--- every object created by this indicator starts with SMIF_
#define SMIF_PREFIX       "SMIF_"
#define SMIF_PREV_PREFIX  "SMIF_PREV_"
#define SMIF_DASH_PREFIX  "SMIF_DASHBOARD_"
#define SMIF_BTN_PREV     "SMIF_BTN_PREV"
//--- number of similarity features
#define SMIF_FEAT         12

//+------------------------------------------------------------------+
//| Enumerations                                                     |
//+------------------------------------------------------------------+
enum ENUM_SMIF_TIMEFRAME
  {
   SMIF_TF_CURRENT=0,   // Current Chart
   SMIF_TF_M1,          // M1
   SMIF_TF_M5,          // M5
   SMIF_TF_M15,         // M15
   SMIF_TF_M30,         // M30
   SMIF_TF_H1,          // H1
   SMIF_TF_H4,          // H4
   SMIF_TF_D1           // D1
  };

enum ENUM_REFERENCE_TYPE
  {
   REFERENCE_HIGH=0,    // Reference high (movement develops downward)
   REFERENCE_LOW=1      // Reference low (movement develops upward)
  };

enum ENUM_FORECAST_DIRECTION
  {
   FORECAST_SELL=-1,
   FORECAST_NONE=0,
   FORECAST_BUY=1
  };

enum ENUM_FORECAST_STATE
  {
   STATE_NO_FORECAST=0,
   STATE_REFERENCE_DETECTED,
   STATE_MOVEMENT_DEVELOPING,
   STATE_FORECAST_READY,
   STATE_ENTRY_REACHED,
   STATE_FORECAST_ACTIVE,
   STATE_TP_REACHED,
   STATE_SL_REACHED,
   STATE_FORECAST_INVALIDATED
  };

enum ENUM_FORECAST_OUTCOME
  {
   OUTCOME_NONE=0,
   OUTCOME_TP,
   OUTCOME_SL,
   OUTCOME_INVALIDATED,
   OUTCOME_EXPIRED
  };

enum ENUM_INVALID_REASON
  {
   INVALID_NONE=0,
   INVALID_REFERENCE_BROKEN,
   INVALID_TARGET_BEFORE_ENTRY,
   INVALID_ENTRY_EXPIRED
  };

enum ENUM_SMIF_SESSION
  {
   SMIF_SESSION_ASIA=0,
   SMIF_SESSION_LONDON,
   SMIF_SESSION_NEWYORK,
   SMIF_SESSION_LATE
  };

enum ENUM_SMIF_REGIME
  {
   SMIF_REGIME_LOW=0,
   SMIF_REGIME_NORMAL,
   SMIF_REGIME_HIGH
  };

enum ENUM_OBS_OUTCOME
  {
   OBS_REVERSED=1,      // opposite move completed normally
   OBS_RESUMED=2        // original movement resumed beyond its extreme
  };

//+------------------------------------------------------------------+
//| Inputs                                                           |
//+------------------------------------------------------------------+
input group "=== Analysis ==="
input ENUM_SMIF_TIMEFRAME InpAnalysisTimeframe = SMIF_TF_CURRENT; // Analysis timeframe
input int      InpHistoryBars              = 10000;  // Closed bars loaded for the statistical database
input int      InpATRPeriod                = 14;     // ATR period (Wilder)

input group "=== Reference Detection ==="
input int      InpReferenceLeftBars        = 5;      // Left confirmation bars
input int      InpReferenceRightBars       = 3;      // Right confirmation bars
input double   InpMinimumMoveATR           = 1.0;    // Minimum move from reference (ATR) to qualify
input int      InpMinimumPriceDistance     = 0;      // Minimum move from reference (points)
input int      InpMaximumReferenceAge      = 60;     // Max bars for a reference to qualify

input group "=== Movement Termination ==="
input double   InpTerminationATR           = 1.0;    // Retracement needed to confirm termination (ATR)
input double   InpTerminationRetracement   = 0.382;  // Retracement needed (fraction of the leg)
input double   InpMinimumOppositeMove      = 0.5;    // Minimum opposite move (ATR)
input bool     InpRequireConfirmation      = true;   // Require a CLOSE beyond the threshold
input int      InpMaxObservationBars       = 300;    // Max bars a historical observation may span

input group "=== Statistical Engine ==="
input int      InpMinimumHistoricalSamples = 40;     // Minimum comparable observations in the pool
input int      InpSimilaritySamples        = 80;     // Nearest observations used (K)
input double   InpTerminationPercentile    = 40.0;   // Termination percentile (entry depth)
input double   InpStopPercentile           = 80.0;   // Adverse-excursion percentile (stop)
input double   InpTargetPercentile         = 45.0;   // Opposite-move percentile (target)
input double   InpStopBufferATR            = 0.10;   // Buffer beyond the statistical stop (ATR)
input bool     InpUseFallbackSL            = false;  // Use fixed fallback SL when samples are thin
input double   InpFallbackSLATR            = 1.5;    // Fallback SL (ATR)
input bool     InpUseSessionFeature        = true;   // Use time-of-day in similarity
input bool     InpUseContextFeature        = true;   // Use higher-timeframe context in similarity
input ENUM_TIMEFRAMES InpContextTimeframe  = PERIOD_H4; // Higher timeframe for context
input int      InpContextEMAPeriod         = 50;     // Higher-timeframe EMA period

input group "=== Forecast Filters ==="
input int      InpMinimumSamples           = 20;     // Minimum filled samples
input double   InpMinimumTPHitRate         = 40.0;   // Minimum TP hit rate (%)
input double   InpMaximumSLHitRate         = 55.0;   // Maximum SL hit rate (%)
input double   InpMinimumRiskReward        = 0.8;    // Minimum reward:risk
input double   InpMinimumExpectedMovePercent = 0.05; // Minimum expected move (%)
input bool     InpSingleOpenForecast       = true;   // One open forecast per direction

input group "=== Forecast Lifecycle ==="
input int      InpForecastExpiryBars       = 30;     // Bars to reach entry before invalidation
input int      InpMaxHoldBars              = 120;    // Bars after entry before expiry
input bool     InpValidationMode           = true;   // Generate & resolve forecasts on history

input group "=== Visualization ==="
input color    InpRiskColor                = clrCrimson;    // Risk rectangle colour
input color    InpRewardColor              = clrDodgerBlue; // Reward rectangle colour
input color    InpEntryColor               = clrGold;       // Entry line colour
input int      InpRectangleTransparency    = 65;     // Rectangle transparency (0-100)
input int      InpRectangleExtensionBars   = 20;     // Rectangle extension into future bars
input bool     InpShowLabels               = true;   // Show forecast labels
input int      InpLabelFontSize            = 8;      // Label font size

input group "=== Previous Forecasts ==="
input bool     InpShowPreviousForecasts    = false;  // Show previous forecasts at start (toggle: button or P)
input bool     InpPrevShowTP               = true;   // Previous: show TP reached
input bool     InpPrevShowSL               = true;   // Previous: show SL reached
input bool     InpPrevShowInvalidated      = true;   // Previous: show invalidated / expired
input int      InpMaxPreviousDrawn         = 150;    // Previous: maximum drawn
input int      InpPreviousTransparency     = 82;     // Previous: rectangle transparency (0-100)

input group "=== Dashboard ==="
input bool     InpShowDashboard            = true;   // Show dashboard
input ENUM_BASE_CORNER InpDashboardCorner  = CORNER_LEFT_UPPER;  // Dashboard corner
input int      InpDashboardX               = 10;     // Dashboard X offset
input int      InpDashboardY               = 25;     // Dashboard Y offset
input int      InpDashboardFontSize        = 9;      // Dashboard font size
input color    InpDashboardBg              = C'18,22,30';   // Dashboard background
input color    InpDashboardText            = clrGainsboro;  // Dashboard text
input color    InpDashboardHeader          = clrGold;       // Dashboard section headers

input group "=== Sessions & Regimes (server time) ==="
input int      InpAsiaStartHour            = 0;      // Asia start hour
input int      InpLondonStartHour          = 8;      // London start hour
input int      InpNewYorkStartHour         = 13;     // New York start hour
input int      InpNewYorkEndHour           = 21;     // New York end hour
input double   InpLowVolRatio              = 0.85;   // ATR ratio below = LOW volatility
input double   InpHighVolRatio             = 1.15;   // ATR ratio above = HIGH volatility

input group "=== Export & Debug ==="
input bool     InpExportCSV                = true;   // Export forecasts to CSV
input bool     InpDebugMode                = false;  // Print debug information
input bool     InpDebugHistory             = false;  // Also print during historical replay

//+------------------------------------------------------------------+
//| Structures                                                       |
//+------------------------------------------------------------------+
//--- confirmed reference and the movement developing from it
struct SReference
  {
   long              id;
   int               type;            // ENUM_REFERENCE_TYPE
   int               dirMove;         // -1 movement down (from a high), +1 movement up (from a low)
   double            price;
   datetime          time;
   int               bar;             // chronological bar index of the reference
   int               confirmBar;      // bar at which it became confirmed
   int               phase;           // 0 developing, 1 opposite leg, 2 finished, 3 dropped
   double            extreme;
   int               extremeBar;
   double            oppPeak;
   int               oppPeakBar;
   int               termConfirmBar;
   bool              snapshotTaken;
   int               snapBar;
   double            snapClose;
   double            snapATR;
   double            feat[SMIF_FEAT];
   bool              evaluated;
  };

//--- measurements of a developing movement
struct SMovementInfo
  {
   bool              valid;
   int               dir;
   int               refType;
   double            refPrice;
   datetime          refTime;
   double            distance;
   double            pct;
   int               durBars;
   long              durSec;
   double            atr;
   double            atrN;
   double            avgBody;
   double            maxBody;
   double            avgRange;
   double            maxRange;
   double            volRatio;
   double            momentum;
   double            accel;
   double            retrace;
   int               bull;
   int               bear;
   int               consec;
  };

//--- completed historical movement = one statistical observation
struct SHistoricalObservation
  {
   long              refId;
   int               refType;
   double            refPrice;
   datetime          refTime;
   int               dirMove;
   double            initATR;
   double            initVol;
   double            initMom;
   int               snapBar;
   double            snapClose;
   double            snapATR;
   double            feat[SMIF_FEAT];
   double            maxExtension;
   double            termPrice;
   double            termDist;
   double            termPct;
   int               termDuration;
   double            oppDist;
   double            oppPct;
   int               oppDuration;
   double            mae;             // price extension beyond the snapshot (continuation)
   double            mfe;             // opposite excursion beyond the snapshot
   double            remN;            // remaining extension after the snapshot (ATR)
   double            favN;            // opposite peak relative to the snapshot close (ATR)
   int               completeBar;     // first bar at which this observation is known
   int               outcome;         // ENUM_OBS_OUTCOME
  };

//--- a forecast (developing, confirmed or resolved)
struct SForecast
  {
   long              id;
   long              refId;
   int               refType;
   double            refPrice;
   datetime          refTime;
   int               direction;       // ENUM_FORECAST_DIRECTION
   int               createBar;
   datetime          createTime;
   double            predTerm;
   double            termZoneHigh;
   double            termZoneLow;
   double            termProb;
   double            entry;
   double            entryZoneHigh;
   double            entryZoneLow;
   double            sl;
   double            tp;
   double            riskPrice;
   double            rewardPrice;
   double            riskPts;
   double            rewardPts;
   double            riskPctPrice;
   double            rr;
   double            expMovePct;
   double            atr;
   double            volRatio;
   double            momentum;
   double            movDist;
   int               movDur;
   int               samples;         // resolved comparable observations
   int               filled;          // of which reached the entry
   int               tpHits;
   int               slHits;
   int               invalidCount;
   int               expiredCount;
   double            tpRate;
   double            slRate;
   double            invRate;
   double            avgOutcome;
   double            medOutcome;
   double            avgFavN;
   double            avgAdvN;
   double            similarity;
   int               state;           // ENUM_FORECAST_STATE
   int               outcome;         // ENUM_FORECAST_OUTCOME
   int               invalidReason;   // ENUM_INVALID_REASON
   int               entryBar;
   datetime          entryTime;
   int               resolveBar;
   datetime          resolveTime;
   double            exitPrice;
   double            outcomePts;
   double            outcomePct;
   int               session;
   int               regime;
   bool              logged;
   bool              dirty;
   bool              activeDrawn;
  };

//--- result of replaying Entry/SL/TP rules over closed bars
struct SSimResult
  {
   bool              resolved;
   bool              entered;
   int               outcome;
   int               reason;
   int               entryBar;
   int               resolveBar;
   double            exitPrice;
   double            mfe;
   double            mae;
  };

//--- descriptive statistics of a distribution
struct SDistribution
  {
   int               n;
   double            min;
   double            max;
   double            mean;
   double            median;
   double            std;
   double            p25;
   double            p50;
   double            p75;
   double            p90;
  };

//--- validation summary
struct SValidation
  {
   int               total;
   int               buy;
   int               sell;
   int               tp;
   int               sl;
   int               inv;
   int               exp;
   int               entered;
   double            tpRate;
   double            slRate;
   double            invRate;
   double            avgRet;
   double            medRet;
   double            avgRisk;
   double            avgReward;
   double            avgRR;
   double            avgBarsTP;
   double            avgBarsSL;
  };

//+------------------------------------------------------------------+
//| Small helpers                                                    |
//+------------------------------------------------------------------+
int IMax(const int a,const int b) { return (a>b) ? a : b; }
int IMin(const int a,const int b) { return (a<b) ? a : b; }

string TFToString(const ENUM_TIMEFRAMES tf)
  {
   return StringSubstr(EnumToString(tf),7);
  }

ENUM_TIMEFRAMES ResolveTimeframe(const ENUM_SMIF_TIMEFRAME t)
  {
   switch(t)
     {
      case SMIF_TF_M1:  return PERIOD_M1;
      case SMIF_TF_M5:  return PERIOD_M5;
      case SMIF_TF_M15: return PERIOD_M15;
      case SMIF_TF_M30: return PERIOD_M30;
      case SMIF_TF_H1:  return PERIOD_H1;
      case SMIF_TF_H4:  return PERIOD_H4;
      case SMIF_TF_D1:  return PERIOD_D1;
      default:          break;
     }
   return (ENUM_TIMEFRAMES)_Period;
  }

string StateToString(const int s)
  {
   switch(s)
     {
      case STATE_NO_FORECAST:          return "NO FORECAST";
      case STATE_REFERENCE_DETECTED:   return "REFERENCE DETECTED";
      case STATE_MOVEMENT_DEVELOPING:  return "MOVEMENT DEVELOPING";
      case STATE_FORECAST_READY:       return "READY (PENDING ENTRY)";
      case STATE_ENTRY_REACHED:        return "ENTRY REACHED";
      case STATE_FORECAST_ACTIVE:      return "ACTIVE";
      case STATE_TP_REACHED:           return "TP REACHED";
      case STATE_SL_REACHED:           return "SL REACHED";
      case STATE_FORECAST_INVALIDATED: return "INVALIDATED";
     }
   return "UNKNOWN";
  }

string OutcomeToString(const int o)
  {
   switch(o)
     {
      case OUTCOME_TP:          return "TP";
      case OUTCOME_SL:          return "SL";
      case OUTCOME_INVALIDATED: return "INVALIDATED";
      case OUTCOME_EXPIRED:     return "EXPIRED";
     }
   return "OPEN";
  }

string InvalidReasonToString(const int r)
  {
   switch(r)
     {
      case INVALID_REFERENCE_BROKEN:    return "reference broken";
      case INVALID_TARGET_BEFORE_ENTRY: return "target reached before entry";
      case INVALID_ENTRY_EXPIRED:       return "entry not reached in time";
     }
   return "";
  }

string SessionToString(const int s)
  {
   switch(s)
     {
      case SMIF_SESSION_ASIA:    return "ASIA";
      case SMIF_SESSION_LONDON:  return "LONDON";
      case SMIF_SESSION_NEWYORK: return "NEWYORK";
     }
   return "LATE";
  }

string RegimeToString(const int r)
  {
   switch(r)
     {
      case SMIF_REGIME_LOW:  return "LOW";
      case SMIF_REGIME_HIGH: return "HIGH";
     }
   return "NORMAL";
  }

int SessionOf(const datetime t)
  {
   MqlDateTime dt;
   TimeToStruct(t,dt);
   int h=dt.hour;
   if(h>=InpNewYorkEndHour)   return SMIF_SESSION_LATE;
   if(h>=InpNewYorkStartHour) return SMIF_SESSION_NEWYORK;
   if(h>=InpLondonStartHour)  return SMIF_SESSION_LONDON;
   if(h>=InpAsiaStartHour)    return SMIF_SESSION_ASIA;
   return SMIF_SESSION_LATE;
  }

int RegimeOf(const double volRatio)
  {
   if(volRatio<InpLowVolRatio)  return SMIF_REGIME_LOW;
   if(volRatio>InpHighVolRatio) return SMIF_REGIME_HIGH;
   return SMIF_REGIME_NORMAL;
  }

//--- blends a colour toward the chart background (standard objects have no alpha)
color BlendColor(const color c,const int transparency)
  {
   int    t =IMax(0,IMin(100,transparency));
   double a =t/100.0;
   int    bg=(int)ChartGetInteger(0,CHART_COLOR_BACKGROUND);
   int    ci=(int)c;
   int cr=ci&0xFF,  cg=(ci>>8)&0xFF,  cb=(ci>>16)&0xFF;
   int br=bg&0xFF,  bgg=(bg>>8)&0xFF, bb=(bg>>16)&0xFF;
   int r=(int)MathRound(cr*(1.0-a)+br*a);
   int g=(int)MathRound(cg*(1.0-a)+bgg*a);
   int b=(int)MathRound(cb*(1.0-a)+bb*a);
   return (color)((b<<16)|(g<<8)|r);
  }

string SanitizeFileName(string s)
  {
   StringReplace(s,"/","_");
   StringReplace(s,"\\","_");
   StringReplace(s,":","_");
   StringReplace(s,"*","_");
   StringReplace(s,"?","_");
   StringReplace(s,"\"","_");
   StringReplace(s,"<","_");
   StringReplace(s,">","_");
   StringReplace(s,"|","_");
   StringReplace(s,"#","_");
   return s;
  }

string SignedPct(const double v,const int decimals)
  {
   return ((v>=0.0) ? "+" : "")+DoubleToString(v,decimals)+"%";
  }

//+------------------------------------------------------------------+
//| CStats - descriptive statistics                                  |
//+------------------------------------------------------------------+
class CStats
  {
public:
   //--- linear-interpolated percentile (0..100)
   static double     Percentile(const double &src[],const double pct)
     {
      int n=ArraySize(src);
      if(n<=0)
         return 0.0;
      double tmp[];
      ArrayCopy(tmp,src);
      ArraySort(tmp);
      if(n==1)
         return tmp[0];
      double p  =MathMax(0.0,MathMin(100.0,pct));
      double pos=p/100.0*(n-1);
      int    lo =(int)MathFloor(pos);
      int    hi =(int)MathCeil(pos);
      return tmp[lo]+(tmp[hi]-tmp[lo])*(pos-lo);
     }

   static double     Mean(const double &src[])
     {
      int n=ArraySize(src);
      if(n<=0)
         return 0.0;
      double s=0.0;
      for(int i=0;i<n;i++)
         s+=src[i];
      return s/n;
     }

   static double     StdDev(const double &src[])
     {
      int n=ArraySize(src);
      if(n<2)
         return 0.0;
      double m=Mean(src);
      double s=0.0;
      for(int i=0;i<n;i++)
         s+=(src[i]-m)*(src[i]-m);
      return MathSqrt(s/(n-1));
     }

   static void       Describe(const double &src[],SDistribution &d)
     {
      ZeroMemory(d);
      d.n=ArraySize(src);
      if(d.n<=0)
         return;
      d.min=src[ArrayMinimum(src)];
      d.max=src[ArrayMaximum(src)];
      d.mean=Mean(src);
      d.std=StdDev(src);
      d.p25=Percentile(src,25.0);
      d.p50=Percentile(src,50.0);
      d.p75=Percentile(src,75.0);
      d.p90=Percentile(src,90.0);
      d.median=d.p50;
     }
  };

//+------------------------------------------------------------------+
//| CSmifEngine - historical analysis + live forecasting             |
//| All decisions at bar i use closed bars 0..i only.                |
//+------------------------------------------------------------------+
class CSmifEngine
  {
private:
   string            m_symbol;
   ENUM_TIMEFRAMES   m_tf;
   int               m_periodSec;
   double            m_point;
   int               m_digits;
   bool              m_ready;
   bool              m_inHistory;
   int               m_liveStart;
   long              m_nextRefId;
   long              m_nextFcId;
   //--- higher-timeframe context
   bool              m_ctxEnabled;
   datetime          m_ctxStart;
   datetime          m_ctxLastOpen;
   datetime          m_ctxTime[];
   double            m_ctxClose[];
   double            m_ctxEma[];

public:
   MqlRates          m_rates[];       // closed bars, index 0 = oldest
   double            m_atr[];
   int               m_lastBar;
   SReference        m_refs[];
   SHistoricalObservation m_obs[];
   SForecast         m_fc[];
   SMovementInfo     m_move;
   int               m_engineState;
   string            m_lastReject;
   datetime          m_lastRejectTime;

                     CSmifEngine(void)
     {
      m_ready=false;
      m_lastBar=-1;
      m_ctxEnabled=false;
     }

   //--- accessors
   bool              IsReady(void)        { return m_ready; }
   string            GetSymbol(void)      { return m_symbol; }
   ENUM_TIMEFRAMES   GetTimeframe(void)   { return m_tf; }
   int               GetDigits(void)      { return m_digits; }
   int               GetPeriodSec(void)   { return m_periodSec; }
   double            GetPoint(void)       { return m_point; }
   int               ObservationCount(void) { return ArraySize(m_obs); }

   //---
   void              Init(const string symbol,const ENUM_TIMEFRAMES tf)
     {
      m_symbol   =symbol;
      m_tf       =tf;
      m_periodSec=PeriodSeconds(tf);
      m_point    =SymbolInfoDouble(symbol,SYMBOL_POINT);
      m_digits   =(int)SymbolInfoInteger(symbol,SYMBOL_DIGITS);
      if(m_point<=0.0)
         m_point=_Point;
      m_ready=false;
     }

   //--- debug printing (state changes and new bars only)
   void              Log(const string msg)
     {
      if(!InpDebugMode)
         return;
      if(m_inHistory && !InpDebugHistory)
         return;
      PrintFormat("[SMIF %s %s] %s",m_symbol,TFToString(m_tf),msg);
     }

   double            Atr(const int i)
     {
      double a=m_atr[i];
      return (a>m_point) ? a : m_point;
     }

   int               WarmupBars(void)
     {
      return IMax(InpReferenceLeftBars+InpReferenceRightBars+1,InpATRPeriod+50);
     }

   //+-------------------------------------------------------------+
   //|initial load and historical replay                           |
   //+-------------------------------------------------------------+
   bool              Load(void)
     {
      m_ready=false;
      ArrayFree(m_rates);
      ArrayFree(m_atr);
      ArrayFree(m_refs);
      ArrayFree(m_obs);
      ArrayFree(m_fc);
      m_nextRefId=0;
      m_nextFcId=0;
      m_lastReject="";
      m_lastRejectTime=0;
      m_engineState=STATE_NO_FORECAST;
      ZeroMemory(m_move);

      int warm =WarmupBars();
      int avail=Bars(m_symbol,m_tf);
      if(avail<warm+100)
         return false;
      int count=(InpHistoryBars<avail-1) ? InpHistoryBars : avail-1;
      ResetLastError();
      int got=CopyRates(m_symbol,m_tf,1,count,m_rates);
      if(got<warm+100)
        {
         ArrayFree(m_rates);
         return false;
        }
      m_lastBar=got-1;
      ArrayResize(m_atr,got,1000);
      for(int i=0;i<got;i++)
         ComputeATR(i);
      if(!LoadContext())
        {
         ArrayFree(m_rates);
         ArrayFree(m_atr);
         return false;
        }
      //--- chronological replay: the same code path used live
      m_inHistory=true;
      m_liveStart=InpValidationMode ? warm : IMax(warm,m_lastBar-InpForecastExpiryBars);
      for(int i=warm;i<=m_lastBar;i++)
         ProcessBar(i);
      m_inHistory=false;
      m_ready=true;
      PrintFormat("[SMIF] %s %s ready: %d bars, %d observations, %d forecasts",
                  m_symbol,TFToString(m_tf),got,ArraySize(m_obs),ArraySize(m_fc));
      return true;
     }

   //--- appends newly closed bars and processes them one by one
   int               SyncNewBars(void)
     {
      if(!m_ready || m_lastBar<0)
         return 0;
      datetime lastClosed=iTime(m_symbol,m_tf,1);
      if(lastClosed==0 || lastClosed<=m_rates[m_lastBar].time)
         return 0;
      MqlRates tmp[];
      int got=CopyRates(m_symbol,m_tf,m_rates[m_lastBar].time,lastClosed,tmp);
      if(got<=0)
         return 0;
      if(m_ctxEnabled && iTime(m_symbol,InpContextTimeframe,0)!=m_ctxLastOpen)
         ReloadContext();
      int added=0;
      for(int k=0;k<got;k++)
        {
         if(tmp[k].time<=m_rates[m_lastBar].time)
            continue;
         int n=m_lastBar+1;
         ArrayResize(m_rates,n+1,1000);
         ArrayResize(m_atr,n+1,1000);
         m_rates[n]=tmp[k];
         m_lastBar=n;
         ComputeATR(n);
         ProcessBar(n);
         added++;
        }
      if(added>0)
         Log(StringFormat("New bar %s processed. Observations=%d",TimeToString(m_rates[m_lastBar].time),ArraySize(m_obs)));
      return added;
     }

   //+-------------------------------------------------------------+
   //| per-bar pipeline                                            |
   //+-------------------------------------------------------------+
   void              ProcessBar(const int i)
     {
      DetectReferences(i);
      //--- pass 1: movement tracking and completion of observations
      int n=ArraySize(m_refs);
      for(int k=0;k<n;k++)
         TrackReference(m_refs[k],i);
      //--- pass 2: snapshots and forecast evaluation (database now contains everything known at i)
      for(int k=0;k<n;k++)
         if(m_refs[k].phase==0 && !m_refs[k].snapshotTaken && i>=m_refs[k].confirmBar)
            TrySnapshot(m_refs[k],i);
      UpdateForecasts(i);
      CompactReferences();
      UpdateMarketInfo(i);
     }

   //+-------------------------------------------------------------+
   //| ATR (Wilder) computed on the analysis timeframe             |
   //+-------------------------------------------------------------+
   void              ComputeATR(const int i)
     {
      double h=m_rates[i].high,l=m_rates[i].low;
      if(i==0)
        {
         m_atr[0]=h-l;
         return;
        }
      double pc=m_rates[i-1].close;
      double tr=MathMax(h-l,MathMax(MathAbs(h-pc),MathAbs(l-pc)));
      if(i<InpATRPeriod)
         m_atr[i]=(m_atr[i-1]*i+tr)/(i+1);
      else
         m_atr[i]=(m_atr[i-1]*(InpATRPeriod-1)+tr)/InpATRPeriod;
     }

   //---------------------------------------------------------------
   //--- higher-timeframe context
   //---------------------------------------------------------------
   bool              LoadContext(void)
     {
      m_ctxEnabled=false;
      ArrayFree(m_ctxTime);
      ArrayFree(m_ctxClose);
      ArrayFree(m_ctxEma);
      if(!InpUseContextFeature)
         return true;
      if(PeriodSeconds(InpContextTimeframe)<=m_periodSec)
         return true;   // context must be a higher timeframe
      m_ctxStart=m_rates[0].time-(datetime)(InpContextEMAPeriod*3*PeriodSeconds(InpContextTimeframe));
      if(!ReloadContext())
         return false;
      m_ctxEnabled=true;
      return true;
     }

   bool              ReloadContext(void)
     {
      MqlRates tmp[];
      int got=CopyRates(m_symbol,InpContextTimeframe,m_ctxStart,TimeCurrent(),tmp);
      if(got<InpContextEMAPeriod+2)
         return false;
      ArrayResize(m_ctxTime,got);
      ArrayResize(m_ctxClose,got);
      ArrayResize(m_ctxEma,got);
      double alpha=2.0/(InpContextEMAPeriod+1.0);
      for(int k=0;k<got;k++)
        {
         m_ctxTime[k] =tmp[k].time;
         m_ctxClose[k]=tmp[k].close;
         m_ctxEma[k]  =(k==0) ? tmp[k].close : m_ctxEma[k-1]+alpha*(tmp[k].close-m_ctxEma[k-1]);
        }
      m_ctxLastOpen=m_ctxTime[got-1];
      return true;
     }

   //--- distance of the last COMPLETED higher-timeframe close from its EMA, in analysis ATR
   double            ContextAt(const int s)
     {
      if(!m_ctxEnabled)
         return 0.0;
      int n=ArraySize(m_ctxTime);
      if(n==0)
         return 0.0;
      int      htfSec=PeriodSeconds(InpContextTimeframe);
      datetime closeT=m_rates[s].time+(datetime)m_periodSec;
      int lo=0,hi=n-1,k=-1;
      while(lo<=hi)
        {
         int mid=(lo+hi)/2;
         if(m_ctxTime[mid]+(datetime)htfSec<=closeT)
           {
            k=mid;
            lo=mid+1;
           }
         else
            hi=mid-1;
        }
      if(k<InpContextEMAPeriod)
         return 0.0;
      return (m_ctxClose[k]-m_ctxEma[k])/Atr(s);
     }

   //---------------------------------------------------------------
   //--- reference detection (confirmed pivots only, never repaints)
   //---------------------------------------------------------------
   void              DetectReferences(const int i)
     {
      int lb=InpReferenceLeftBars;
      int rb=InpReferenceRightBars;
      int p =i-rb;
      if(p-lb<0)
         return;
      bool isHigh=true,isLow=true;
      for(int k=1;k<=lb;k++)
        {
         if(m_rates[p].high<=m_rates[p-k].high)
            isHigh=false;
         if(m_rates[p].low>=m_rates[p-k].low)
            isLow=false;
        }
      for(int k=1;k<=rb;k++)
        {
         if(m_rates[p].high<m_rates[p+k].high)
            isHigh=false;
         if(m_rates[p].low>m_rates[p+k].low)
            isLow=false;
        }
      if(isHigh)
         AddReference(REFERENCE_HIGH,p,i);
      if(isLow)
         AddReference(REFERENCE_LOW,p,i);
     }

   void              AddReference(const int type,const int p,const int i)
     {
      SReference r;
      ZeroMemory(r);
      r.id        =++m_nextRefId;
      r.type      =type;
      r.dirMove   =(type==REFERENCE_HIGH) ? -1 : 1;
      r.price     =(type==REFERENCE_HIGH) ? m_rates[p].high : m_rates[p].low;
      r.time      =m_rates[p].time;
      r.bar       =p;
      r.confirmBar=i;
      r.phase     =0;
      r.extreme   =r.price;
      r.extremeBar=p;
      r.snapBar   =-1;
      //--- catch up on the confirmation bars (all closed and known at bar i)
      for(int b=p+1;b<i && r.phase==0;b++)
         TrackReference(r,b);
      if(r.phase!=0)
         return;
      int n=ArraySize(m_refs);
      ArrayResize(m_refs,n+1,64);
      m_refs[n]=r;
      Log(StringFormat("Reference #%s detected: %s at %s (%s)",IntegerToString(r.id),
                       (type==REFERENCE_HIGH) ? "HIGH" : "LOW",
                       DoubleToString(r.price,m_digits),TimeToString(r.time)));
     }

   //---------------------------------------------------------------
   //--- movement / termination / opposite-move tracking
   //--- identical rules for history and live
   //---------------------------------------------------------------
   void              TrackReference(SReference &r,const int i)
     {
      if(r.phase>=2 || i<=r.bar)
         return;
      double hi=m_rates[i].high,lo=m_rates[i].low,cl=m_rates[i].close;
      int    d =r.dirMove;

      if(r.phase==0)
        {
         //--- reference violated before the movement qualified
         bool broken=(d<0) ? (hi>r.price) : (lo<r.price);
         if(broken && !r.snapshotTaken)
           {
            r.phase=3;
            return;
           }
         //--- extend the movement
         double adv=(d<0) ? lo : hi;
         if((adv-r.extreme)*d>0)
           {
            r.extreme=adv;
            r.extremeBar=i;
           }
         //--- termination test
         if(i>r.extremeBar)
           {
            double probe=InpRequireConfirmation ? cl : ((d<0) ? hi : lo);
            double retr =(probe-r.extreme)*(-d);
            double leg  =(r.price-r.extreme)*(-d);
            double atrE =Atr(r.extremeBar);
            double need =MathMax(InpTerminationATR*atrE,InpTerminationRetracement*leg);
            need=MathMax(need,InpMinimumOppositeMove*atrE);
            if(leg>0 && retr>=need)
              {
               if(!r.snapshotTaken)
                 {
                  r.phase=3;   // never qualified: not an observation
                  return;
                 }
               r.phase=1;
               r.termConfirmBar=i;
               r.oppPeak=r.extreme;
               r.oppPeakBar=r.extremeBar;
               for(int b=r.extremeBar+1;b<=i;b++)
                 {
                  double v=(d<0) ? m_rates[b].high : m_rates[b].low;
                  if((v-r.oppPeak)*(-d)>0)
                    {
                     r.oppPeak=v;
                     r.oppPeakBar=b;
                    }
                 }
               return;
              }
           }
         //--- ageing
         if(!r.snapshotTaken && i-r.bar>InpMaximumReferenceAge)
            r.phase=3;
         else
            if(r.snapshotTaken && i-r.bar>InpMaxObservationBars)
               r.phase=3;
         return;
        }

      //--- phase 1: opposite move
      bool resumed=(d<0) ? (lo<r.extreme) : (hi>r.extreme);
      if(resumed)
        {
         CompleteObservation(r,i,OBS_RESUMED);
         r.phase=2;
         return;
        }
      double pk=(d<0) ? hi : lo;
      if((pk-r.oppPeak)*(-d)>0)
        {
         r.oppPeak=pk;
         r.oppPeakBar=i;
        }
      if(i>r.oppPeakBar)
        {
         double probe=InpRequireConfirmation ? cl : ((d<0) ? lo : hi);
         double retr =(r.oppPeak-probe)*(-d);
         double leg  =(r.oppPeak-r.extreme)*(-d);
         double atrP =Atr(r.oppPeakBar);
         double need =MathMax(InpTerminationATR*atrP,InpTerminationRetracement*leg);
         need=MathMax(need,InpMinimumOppositeMove*atrP);
         if(leg>0 && retr>=need)
           {
            CompleteObservation(r,i,OBS_REVERSED);
            r.phase=2;
            return;
           }
        }
      if(i-r.bar>InpMaxObservationBars)
         r.phase=3;
     }

   void              CompleteObservation(const SReference &r,const int i,const int outcome)
     {
      int    d  =r.dirMove;
      double sc =r.snapClose;
      double atr=r.snapATR;
      //--- deepest extension after the snapshot, until termination was confirmed
      double best=sc;
      int    bestBar=r.snapBar;
      for(int b=r.snapBar+1;b<=r.termConfirmBar;b++)
        {
         double v=(d<0) ? m_rates[b].low : m_rates[b].high;
         if((v-best)*d>0)
           {
            best=v;
            bestBar=b;
           }
        }
      //--- best opposite excursion after that extension
      double peak=best;
      int    lastB=(outcome==OBS_RESUMED) ? i-1 : i;
      for(int b=bestBar+1;b<=lastB;b++)
        {
         double v=(d<0) ? m_rates[b].high : m_rates[b].low;
         if((v-peak)*(-d)>0)
            peak=v;
        }

      SHistoricalObservation o;
      ZeroMemory(o);
      o.refId       =r.id;
      o.refType     =r.type;
      o.refPrice    =r.price;
      o.refTime     =r.time;
      o.dirMove     =d;
      o.initATR     =Atr(r.bar);
      o.initVol     =r.feat[2];
      o.initMom     =r.feat[3];
      o.snapBar     =r.snapBar;
      o.snapClose   =sc;
      o.snapATR     =atr;
      for(int f=0;f<SMIF_FEAT;f++)
         o.feat[f]=r.feat[f];
      o.maxExtension=r.extreme;
      o.termPrice   =r.extreme;
      o.termDist    =MathAbs(r.price-r.extreme);
      o.termPct     =(r.price>0) ? o.termDist/r.price*100.0 : 0.0;
      o.termDuration=r.extremeBar-r.bar;
      o.oppDist     =MathAbs(r.oppPeak-r.extreme);
      o.oppPct      =(r.extreme>0) ? o.oppDist/r.extreme*100.0 : 0.0;
      o.oppDuration =r.oppPeakBar-r.extremeBar;
      o.remN        =MathMax(0.0,(best-sc)*d)/atr;
      o.favN        =(peak-sc)*(-d)/atr;
      o.mae         =o.remN*atr;
      o.mfe         =MathMax(0.0,(peak-sc)*(-d));
      o.completeBar =i;
      o.outcome     =outcome;
      int n=ArraySize(m_obs);
      ArrayResize(m_obs,n+1,256);
      m_obs[n]=o;
     }

   //---------------------------------------------------------------
   //--- movement measurements
   //---------------------------------------------------------------
   void              MeasureMovement(const SReference &r,const int s,SMovementInfo &mi)
     {
      ZeroMemory(mi);
      int p=r.bar;
      int n=s-p;
      if(n<1 || s>m_lastBar)
         return;
      int    d  =r.dirMove;
      double atr=Atr(s);
      double cl =m_rates[s].close;
      mi.valid   =true;
      mi.dir     =d;
      mi.refType =r.type;
      mi.refPrice=r.price;
      mi.refTime =r.time;
      mi.distance=(cl-r.price)*d;
      mi.pct     =(r.price>0) ? mi.distance/r.price*100.0 : 0.0;
      mi.durBars =n;
      mi.durSec  =(long)(m_rates[s].time-r.time);
      mi.atr     =atr;
      mi.atrN    =mi.distance/atr;

      double sumBody=0.0,sumRange=0.0,runExt=r.price,maxRetr=0.0;
      for(int b=p+1;b<=s;b++)
        {
         double body =MathAbs(m_rates[b].close-m_rates[b].open);
         double range=m_rates[b].high-m_rates[b].low;
         sumBody +=body;
         sumRange+=range;
         if(body>mi.maxBody)
            mi.maxBody=body;
         if(range>mi.maxRange)
            mi.maxRange=range;
         if(m_rates[b].close>m_rates[b].open)
            mi.bull++;
         else
            if(m_rates[b].close<m_rates[b].open)
               mi.bear++;
         //--- retracement against the movement versus the extreme reached so far
         double leg=(runExt-r.price)*d;
         if(leg>0)
           {
            double against=(d<0) ? m_rates[b].high-runExt : runExt-m_rates[b].low;
            double ratio=against/leg;
            if(ratio>maxRetr)
               maxRetr=ratio;
           }
         double adv=(d<0) ? m_rates[b].low : m_rates[b].high;
         if((adv-runExt)*d>0)
            runExt=adv;
        }
      mi.retrace =MathMin(maxRetr,2.0);
      mi.avgBody =sumBody/n;
      mi.avgRange=sumRange/n;

      for(int b=s;b>p;b--)
        {
         if((m_rates[b].close-m_rates[b].open)*d>0)
            mi.consec++;
         else
            break;
        }

      int k=IMin(5,n);
      mi.momentum=(s-k>=0) ? (cl-m_rates[s-k].close)*d/atr : 0.0;
      mi.accel   =(s-2*k>=0) ? mi.momentum-(m_rates[s-k].close-m_rates[s-2*k].close)*d/atr : 0.0;

      int    from=IMax(0,s-49);
      double sa=0.0;
      int    cnt=0;
      for(int b=from;b<=s;b++)
        {
         sa+=m_atr[b];
         cnt++;
        }
      mi.volRatio=(cnt>0 && sa>0.0) ? atr/(sa/cnt) : 1.0;
     }

   void              BuildFeatures(SReference &r,const int s,const SMovementInfo &mi)
     {
      double atr=mi.atr;
      r.feat[0]=mi.atrN;
      r.feat[1]=MathLog(1.0+mi.durBars);
      r.feat[2]=mi.volRatio;
      r.feat[3]=mi.momentum;
      r.feat[4]=mi.accel;
      r.feat[5]=mi.avgBody/atr;
      r.feat[6]=mi.maxRange/atr;
      r.feat[7]=mi.retrace;
      int dirBars=(r.dirMove<0) ? mi.bear : mi.bull;
      r.feat[8]=(mi.durBars>0) ? (double)dirBars/mi.durBars : 0.0;
      MqlDateTime dt;
      TimeToStruct(m_rates[s].time,dt);
      double h=dt.hour+dt.min/60.0;
      r.feat[9] =MathSin(2.0*M_PI*h/24.0);
      r.feat[10]=MathCos(2.0*M_PI*h/24.0);
      r.feat[11]=ContextAt(s)*r.dirMove;
     }

   void              FeatureWeights(double &w[])
     {
      for(int f=0;f<SMIF_FEAT;f++)
         w[f]=1.0;
      w[9] =InpUseSessionFeature ? 0.5 : 0.0;
      w[10]=InpUseSessionFeature ? 0.5 : 0.0;
      w[11]=m_ctxEnabled ? 1.0 : 0.0;
     }

   //+-------------------------------------------------------------+
   //| snapshot = the single decision moment for a reference       |
   //+-------------------------------------------------------------+
   void              TrySnapshot(SReference &r,const int i)
     {
      double dist=(m_rates[i].close-r.price)*r.dirMove;
      double need=MathMax(InpMinimumMoveATR*Atr(i),InpMinimumPriceDistance*m_point);
      if(dist<need)
         return;
      SMovementInfo mi;
      MeasureMovement(r,i,mi);
      BuildFeatures(r,i,mi);
      r.snapshotTaken=true;
      r.snapBar  =i;
      r.snapClose=m_rates[i].close;
      r.snapATR  =Atr(i);
      Log(StringFormat("Reference #%s qualified: distance %s (%.2f ATR) after %d bars",
                       IntegerToString(r.id),DoubleToString(dist,m_digits),mi.atrN,mi.durBars));
      if(!r.evaluated && i>=m_liveStart)
        {
         if(InpSingleOpenForecast && HasOpenForecast(-r.dirMove))
            Reject(r,i,"An open forecast in this direction already exists");
         else
            EvaluateForecast(r,i,mi);
        }
      r.evaluated=true;   // one evaluation per reference: no duplicates
     }

   bool              HasOpenForecast(const int dir)
     {
      for(int k=ArraySize(m_fc)-1;k>=0;k--)
         if(m_fc[k].outcome==OUTCOME_NONE && m_fc[k].direction==dir)
            return true;
      return false;
     }

   void              Reject(const SReference &r,const int s,const string reason)
     {
      m_lastReject    =reason;
      m_lastRejectTime=m_rates[s].time;
      Log(StringFormat("NO VALID FORECAST for reference #%s: %s",IntegerToString(r.id),reason));
     }

   //+-------------------------------------------------------------+
   //| statistical forecast                                        |
   //+-------------------------------------------------------------+
   void              EvaluateForecast(SReference &r,const int s,const SMovementInfo &mi)
     {
      int d=r.dirMove;
      //--- candidate pool: same direction, known no later than the decision bar
      int nobs=ArraySize(m_obs);
      int pool[];
      ArrayResize(pool,nobs);
      int np=0;
      for(int j=0;j<nobs;j++)
         if(m_obs[j].dirMove==d && m_obs[j].completeBar<=s)
            pool[np++]=j;
      if(np<InpMinimumHistoricalSamples)
        {
         Reject(r,s,StringFormat("Insufficient history: %d/%d observations",np,InpMinimumHistoricalSamples));
         return;
        }
      //--- standardise features over the pool
      double mean[SMIF_FEAT],sd[SMIF_FEAT],w[SMIF_FEAT];
      FeatureWeights(w);
      for(int f=0;f<SMIF_FEAT;f++)
        {
         double sum=0.0,sq=0.0;
         for(int x=0;x<np;x++)
           {
            double v=m_obs[pool[x]].feat[f];
            sum+=v;
            sq +=v*v;
           }
         mean[f]=sum/np;
         double var=sq/np-mean[f]*mean[f];
         sd[f]=(var>1e-12) ? MathSqrt(var) : 1.0;
        }
      //--- rank by weighted standardised Euclidean distance
      double pairs[][2];
      ArrayResize(pairs,np);
      for(int x=0;x<np;x++)
        {
         double dist=0.0;
         for(int f=0;f<SMIF_FEAT;f++)
           {
            if(w[f]<=0.0)
               continue;
            double z=(m_obs[pool[x]].feat[f]-r.feat[f])/sd[f];
            dist+=w[f]*z*z;
           }
         pairs[x][0]=MathSqrt(dist);
         pairs[x][1]=pool[x];
        }
      ArraySort(pairs);
      int K=(InpSimilaritySamples<np) ? InpSimilaritySamples : np;
      int    nb[];
      double rem[];
      ArrayResize(nb,K);
      ArrayResize(rem,K);
      double sumDist=0.0;
      for(int x=0;x<K;x++)
        {
         nb[x] =(int)pairs[x][1];
         rem[x]=m_obs[nb[x]].remN;
         sumDist+=pairs[x][0];
        }
      double similarity=1.0/(1.0+sumDist/K);

      //--- termination distribution (remaining extension after the decision point)
      SDistribution dRem;
      CStats::Describe(rem,dRem);
      double eN=CStats::Percentile(rem,InpTerminationPercentile);
      double zA=CStats::Percentile(rem,MathMax(0.0,InpTerminationPercentile-25.0));
      double zB=CStats::Percentile(rem,MathMin(100.0,InpTerminationPercentile+25.0));

      //--- adverse / opposite excursion of neighbours that reached the entry depth
      double adv[],fav[];
      ArrayResize(adv,K);
      ArrayResize(fav,K);
      int ne=0;
      for(int x=0;x<K;x++)
         if(rem[x]>=eN)
           {
            adv[ne]=rem[x]-eN;
            fav[ne]=m_obs[nb[x]].favN+eN;
            ne++;
           }
      ArrayResize(adv,ne);
      ArrayResize(fav,ne);
      double reachProb=100.0*ne/K;
      if(ne<3)
        {
         Reject(r,s,"Too few neighbours reached the termination level");
         return;
        }
      SDistribution dAdv,dFav;
      CStats::Describe(adv,dAdv);
      CStats::Describe(fav,dFav);
      double slN=CStats::Percentile(adv,InpStopPercentile)+InpStopBufferATR;
      if(InpUseFallbackSL && ne<InpMinimumSamples)
         slN=InpFallbackSLATR;
      double tpN=CStats::Percentile(fav,InpTargetPercentile);
      if(slN<=1e-6)
        {
         Reject(r,s,"Degenerate statistical stop (zero adverse excursion)");
         return;
        }
      if(tpN<=0.0)
        {
         Reject(r,s,"Opposite-move distribution is not positive");
         return;
        }
      if(tpN<=eN)
        {
         Reject(r,s,"Statistical target does not exceed the current price");
         return;
        }

      //--- price levels
      double atr=r.snapATR,sc=r.snapClose;
      int    tdir=-d;
      double entry=NormalizeDouble(sc+d*eN*atr,m_digits);
      double sl   =NormalizeDouble(entry+d*slN*atr,m_digits);
      double tp   =NormalizeDouble(entry-d*tpN*atr,m_digits);
      double zp1  =sc+d*zA*atr;
      double zp2  =sc+d*zB*atr;

      //--- empirical outcomes: same rules replayed on every neighbour (bars <= s only)
      int    resolved=0,filled=0,tpH=0,slH=0,invC=0,expC=0,no=0;
      double outs[];
      ArrayResize(outs,K);
      double sumMfe=0.0,sumMae=0.0;
      for(int x=0;x<K;x++)
        {
         int    j =nb[x];
         double oa=m_obs[j].snapATR;
         double oc=m_obs[j].snapClose;
         double e =oc+d*eN*oa;
         double sL=e+d*slN*oa;
         double tP=e-d*tpN*oa;
         SSimResult sr;
         Simulate(m_obs[j].snapBar,s,tdir,e,sL,tP,m_obs[j].refPrice,sr);
         if(!sr.resolved)
            continue;
         resolved++;
         if(sr.outcome==OUTCOME_INVALIDATED)
           {
            invC++;
            continue;
           }
         filled++;
         if(sr.outcome==OUTCOME_TP)
            tpH++;
         else
            if(sr.outcome==OUTCOME_SL)
               slH++;
            else
               if(sr.outcome==OUTCOME_EXPIRED)
                  expC++;
         outs[no++]=(sr.exitPrice-e)*tdir/e*100.0;
         sumMfe+=sr.mfe/oa;
         sumMae+=sr.mae/oa;
        }
      ArrayResize(outs,no);

      double tpRate =(filled>0) ? 100.0*tpH/filled : 0.0;
      double slRate =(filled>0) ? 100.0*slH/filled : 0.0;
      double invRate=(resolved>0) ? 100.0*invC/resolved : 0.0;
      double rr     =tpN/slN;
      double expMove=MathAbs(tp-entry)/entry*100.0;

      //--- filters
      string reason="";
      if(filled<InpMinimumSamples)
         reason=StringFormat("Samples %d < minimum %d",filled,InpMinimumSamples);
      else
         if(tpRate<InpMinimumTPHitRate)
            reason=StringFormat("TP hit rate %.1f%% < %.1f%% (n=%d)",tpRate,InpMinimumTPHitRate,filled);
         else
            if(slRate>InpMaximumSLHitRate)
               reason=StringFormat("SL hit rate %.1f%% > %.1f%% (n=%d)",slRate,InpMaximumSLHitRate,filled);
            else
               if(rr<InpMinimumRiskReward)
                  reason=StringFormat("R:R %.2f < %.2f",rr,InpMinimumRiskReward);
               else
                  if(expMove<InpMinimumExpectedMovePercent)
                     reason=StringFormat("Expected move %.3f%% < %.3f%%",expMove,InpMinimumExpectedMovePercent);
      if(reason!="")
        {
         Reject(r,s,reason);
         return;
        }

      //--- build the forecast
      SForecast f;
      ZeroMemory(f);
      f.id           =++m_nextFcId;
      f.refId        =r.id;
      f.refType      =r.type;
      f.refPrice     =r.price;
      f.refTime      =r.time;
      f.direction    =tdir;
      f.createBar    =s;
      f.createTime   =m_rates[s].time;
      f.predTerm     =entry;
      f.termZoneHigh =MathMax(zp1,zp2);
      f.termZoneLow  =MathMin(zp1,zp2);
      f.termProb     =reachProb;
      f.entry        =entry;
      f.entryZoneHigh=f.termZoneHigh;
      f.entryZoneLow =f.termZoneLow;
      f.sl           =sl;
      f.tp           =tp;
      f.riskPrice    =MathAbs(entry-sl);
      f.rewardPrice  =MathAbs(tp-entry);
      f.riskPts      =f.riskPrice/m_point;
      f.rewardPts    =f.rewardPrice/m_point;
      f.riskPctPrice =f.riskPrice/entry*100.0;
      f.rr           =rr;
      f.expMovePct   =expMove;
      f.atr          =atr;
      f.volRatio     =mi.volRatio;
      f.momentum     =mi.momentum;
      f.movDist      =mi.distance;
      f.movDur       =mi.durBars;
      f.samples      =resolved;
      f.filled       =filled;
      f.tpHits       =tpH;
      f.slHits       =slH;
      f.invalidCount =invC;
      f.expiredCount =expC;
      f.tpRate       =tpRate;
      f.slRate       =slRate;
      f.invRate      =invRate;
      f.avgOutcome   =CStats::Mean(outs);
      f.medOutcome   =CStats::Percentile(outs,50.0);
      f.avgFavN      =(filled>0) ? sumMfe/filled : 0.0;
      f.avgAdvN      =(filled>0) ? sumMae/filled : 0.0;
      f.similarity   =similarity;
      f.state        =STATE_FORECAST_READY;
      f.outcome      =OUTCOME_NONE;
      f.entryBar     =-1;
      f.resolveBar   =-1;
      f.session      =SessionOf(f.createTime);
      f.regime       =RegimeOf(mi.volRatio);
      f.dirty        =true;
      int n=ArraySize(m_fc);
      ArrayResize(m_fc,n+1,128);
      m_fc[n]=f;

      Log(StringFormat("Forecast #%s %s | ref #%s | pool=%d K=%d similarity=%.3f | termination p25/p50/p75/p90 = %.2f/%.2f/%.2f/%.2f ATR",
                       IntegerToString(f.id),(tdir>0) ? "BUY" : "SELL",IntegerToString(r.id),np,K,similarity,
                       dRem.p25,dRem.p50,dRem.p75,dRem.p90));
      Log(StringFormat("Forecast #%s predicted termination %s | Entry %s SL %s TP %s | R:R %.2f | move %.2f%% | samples %d filled %d | TP %.1f%% SL %.1f%% INV %.1f%%",
                       IntegerToString(f.id),DoubleToString(entry,m_digits),DoubleToString(entry,m_digits),
                       DoubleToString(sl,m_digits),DoubleToString(tp,m_digits),rr,expMove,resolved,filled,
                       tpRate,slRate,invRate));
     }

   //+--------------------------------------------------------------+
   //| replays Entry/SL/TP rules over closed bars (start+1 .. limit)|
   //+--------------------------------------------------------------+
   void              Resolve(SSimResult &sr,const int outcome,const int bar,const double price,const int reason)
     {
      sr.resolved  =true;
      sr.outcome   =outcome;
      sr.resolveBar=bar;
      sr.exitPrice =price;
      sr.reason    =reason;
     }

   void              Simulate(const int start,const int limit,const int tdir,const double entry,
                              const double sl,const double tp,const double refPrice,SSimResult &sr)
     {
      ZeroMemory(sr);
      sr.entryBar  =-1;
      sr.resolveBar=-1;
      sr.outcome   =OUTCOME_NONE;
      sr.reason    =INVALID_NONE;
      int last=IMin(limit,m_lastBar);
      for(int b=start+1;b<=last;b++)
        {
         double hi=m_rates[b].high,lo=m_rates[b].low,cl=m_rates[b].close;
         if(!sr.entered)
           {
            bool touch=(tdir>0) ? (lo<=entry) : (hi>=entry);
            if(touch)
              {
               sr.entered =true;
               sr.entryBar=b;
               double a=(tdir>0) ? entry-lo : hi-entry;
               if(a>sr.mae)
                  sr.mae=a;
               bool slHit=(tdir>0) ? (lo<=sl) : (hi>=sl);
               if(slHit)
                 {
                  Resolve(sr,OUTCOME_SL,b,sl,INVALID_NONE);  // conservative on the fill bar
                  return;
                 }
               continue;
              }
            bool broken=(tdir>0) ? (hi>refPrice) : (lo<refPrice);
            if(broken)
              {
               Resolve(sr,OUTCOME_INVALIDATED,b,cl,INVALID_REFERENCE_BROKEN);
               return;
              }
            bool ran=(tdir>0) ? (hi>=tp) : (lo<=tp);
            if(ran)
              {
               Resolve(sr,OUTCOME_INVALIDATED,b,cl,INVALID_TARGET_BEFORE_ENTRY);
               return;
              }
            if(b-start>=InpForecastExpiryBars)
              {
               Resolve(sr,OUTCOME_INVALIDATED,b,cl,INVALID_ENTRY_EXPIRED);
               return;
              }
            continue;
           }
         double fv=(tdir>0) ? hi-entry : entry-lo;
         double av=(tdir>0) ? entry-lo : hi-entry;
         if(av>sr.mae)
            sr.mae=av;
         bool slHit=(tdir>0) ? (lo<=sl) : (hi>=sl);
         if(slHit)
           {
            Resolve(sr,OUTCOME_SL,b,sl,INVALID_NONE);   // SL first when both touch
            return;
           }
         if(fv>sr.mfe)
            sr.mfe=fv;
         bool tpHit=(tdir>0) ? (hi>=tp) : (lo<=tp);
         if(tpHit)
           {
            Resolve(sr,OUTCOME_TP,b,tp,INVALID_NONE);
            return;
           }
         if(b-sr.entryBar>=InpMaxHoldBars)
           {
            Resolve(sr,OUTCOME_EXPIRED,b,cl,INVALID_NONE);
            return;
           }
        }
      sr.resolved=false;
     }

   //+-------------------------------------------------------------+
   //| forecast state machine (resolved forecasts are frozen)      |
   //+-------------------------------------------------------------+
   void              UpdateForecasts(const int i)
     {
      int n=ArraySize(m_fc);
      for(int k=0;k<n;k++)
        {
         if(m_fc[k].outcome!=OUTCOME_NONE || i<=m_fc[k].createBar)
            continue;
         SSimResult sr;
         Simulate(m_fc[k].createBar,i,m_fc[k].direction,m_fc[k].entry,m_fc[k].sl,m_fc[k].tp,m_fc[k].refPrice,sr);
         int prev=m_fc[k].state;
         if(sr.entryBar>=0)
           {
            m_fc[k].entryBar =sr.entryBar;
            m_fc[k].entryTime=m_rates[sr.entryBar].time;
           }
         if(sr.resolved)
           {
            m_fc[k].outcome      =sr.outcome;
            m_fc[k].invalidReason=sr.reason;
            m_fc[k].resolveBar   =sr.resolveBar;
            m_fc[k].resolveTime  =m_rates[sr.resolveBar].time;
            m_fc[k].exitPrice    =sr.exitPrice;
            if(sr.outcome==OUTCOME_INVALIDATED)
              {
               m_fc[k].outcomePts=0.0;
               m_fc[k].outcomePct=0.0;
              }
            else
              {
               m_fc[k].outcomePts=(sr.exitPrice-m_fc[k].entry)*m_fc[k].direction/m_point;
               m_fc[k].outcomePct=(sr.exitPrice-m_fc[k].entry)*m_fc[k].direction/m_fc[k].entry*100.0;
              }
            if(sr.outcome==OUTCOME_TP)
               m_fc[k].state=STATE_TP_REACHED;
            else
               if(sr.outcome==OUTCOME_SL)
                  m_fc[k].state=STATE_SL_REACHED;
               else
                  m_fc[k].state=STATE_FORECAST_INVALIDATED;
           }
         else
            m_fc[k].state=sr.entered ? ((sr.entryBar==i) ? STATE_ENTRY_REACHED : STATE_FORECAST_ACTIVE)
                                     : STATE_FORECAST_READY;
         if(m_fc[k].state!=prev)
           {
            m_fc[k].dirty=true;
            string extra=(m_fc[k].outcome==OUTCOME_INVALIDATED) ? " ("+InvalidReasonToString(m_fc[k].invalidReason)+")" : "";
            Log(StringFormat("Forecast #%s: %s -> %s%s",IntegerToString(m_fc[k].id),
                             StateToString(prev),StateToString(m_fc[k].state),extra));
           }
        }
     }

   void              CompactReferences(void)
     {
      int n=ArraySize(m_refs),w=0;
      for(int k=0;k<n;k++)
        {
         if(m_refs[k].phase>=2)
            continue;
         if(w!=k)
            m_refs[w]=m_refs[k];
         w++;
        }
      if(w!=n)
         ArrayResize(m_refs,w,64);
     }

   int               LatestOpenForecast(void)
     {
      for(int k=ArraySize(m_fc)-1;k>=0;k--)
         if(m_fc[k].outcome==OUTCOME_NONE)
            return k;
      return -1;
     }

   void              UpdateMarketInfo(const int i)
     {
      int best=-1;
      int n=ArraySize(m_refs);
      for(int k=0;k<n;k++)
         if(m_refs[k].phase==0 && m_refs[k].confirmBar<=i)
            if(best<0 || m_refs[k].confirmBar>m_refs[best].confirmBar)
               best=k;
      if(best>=0)
         MeasureMovement(m_refs[best],i,m_move);
      else
         ZeroMemory(m_move);
      int latest=LatestOpenForecast();
      if(latest>=0)
         m_engineState=m_fc[latest].state;
      else
         if(best>=0)
            m_engineState=(m_refs[best].confirmBar==i) ? STATE_REFERENCE_DETECTED : STATE_MOVEMENT_DEVELOPING;
         else
            m_engineState=STATE_NO_FORECAST;
     }

   //+-------------------------------------------------------------+
   //| validation statistics over resolved forecasts               |
   //+-------------------------------------------------------------+
   void              ComputeValidation(SValidation &v,const int dirF,const int sesF,const int regF)
     {
      ZeroMemory(v);
      int n=ArraySize(m_fc);
      double rets[];
      ArrayResize(rets,n);
      int    nr=0;
      double sumRisk=0.0,sumRew=0.0,sumRR=0.0,sumTTP=0.0,sumTSL=0.0;
      for(int k=0;k<n;k++)
        {
         if(m_fc[k].outcome==OUTCOME_NONE)
            continue;
         if(dirF!=0 && m_fc[k].direction!=dirF)
            continue;
         if(sesF>=0 && m_fc[k].session!=sesF)
            continue;
         if(regF>=0 && m_fc[k].regime!=regF)
            continue;
         v.total++;
         if(m_fc[k].direction>0)
            v.buy++;
         else
            v.sell++;
         sumRisk+=m_fc[k].riskPrice;
         sumRew +=m_fc[k].rewardPrice;
         sumRR  +=m_fc[k].rr;
         switch(m_fc[k].outcome)
           {
            case OUTCOME_TP:
               v.tp++;
               sumTTP+=m_fc[k].resolveBar-m_fc[k].entryBar;
               break;
            case OUTCOME_SL:
               v.sl++;
               sumTSL+=m_fc[k].resolveBar-m_fc[k].entryBar;
               break;
            case OUTCOME_INVALIDATED:
               v.inv++;
               break;
            case OUTCOME_EXPIRED:
               v.exp++;
               break;
           }
         if(m_fc[k].outcome!=OUTCOME_INVALIDATED)
           {
            v.entered++;
            rets[nr++]=m_fc[k].outcomePct;
           }
        }
      ArrayResize(rets,nr);
      v.tpRate   =(v.entered>0) ? 100.0*v.tp/v.entered : 0.0;
      v.slRate   =(v.entered>0) ? 100.0*v.sl/v.entered : 0.0;
      v.invRate  =(v.total>0) ? 100.0*v.inv/v.total : 0.0;
      v.avgRet   =CStats::Mean(rets);
      v.medRet   =CStats::Percentile(rets,50.0);
      v.avgRisk  =(v.total>0) ? sumRisk/v.total : 0.0;
      v.avgReward=(v.total>0) ? sumRew/v.total : 0.0;
      v.avgRR    =(v.total>0) ? sumRR/v.total : 0.0;
      v.avgBarsTP=(v.tp>0) ? sumTTP/v.tp : 0.0;
      v.avgBarsSL=(v.sl>0) ? sumTSL/v.sl : 0.0;
     }
  };

//+------------------------------------------------------------------+
//| CSmifRenderer - chart objects for active and previous forecasts |
//+------------------------------------------------------------------+
class CSmifRenderer
  {
private:
   int               m_digits;
   int               m_periodSec;

   string            Px(const double v) { return DoubleToString(v,m_digits); }

   void              Rect(const string name,const datetime t1,const double p1,const datetime t2,const double p2,const color clr)
     {
      if(ObjectFind(0,name)<0)
        {
         ObjectCreate(0,name,OBJ_RECTANGLE,0,t1,p1,t2,p2);
         ObjectSetInteger(0,name,OBJPROP_FILL,true);
         ObjectSetInteger(0,name,OBJPROP_BACK,true);
         ObjectSetInteger(0,name,OBJPROP_SELECTABLE,false);
         ObjectSetInteger(0,name,OBJPROP_HIDDEN,true);
        }
      else
        {
         ObjectMove(0,name,0,t1,p1);
         ObjectMove(0,name,1,t2,p2);
        }
      ObjectSetInteger(0,name,OBJPROP_COLOR,clr);
     }

   void              Segment(const string name,const datetime t1,const datetime t2,const double price,
                             const color clr,const ENUM_LINE_STYLE style,const int width)
     {
      if(ObjectFind(0,name)<0)
        {
         ObjectCreate(0,name,OBJ_TREND,0,t1,price,t2,price);
         ObjectSetInteger(0,name,OBJPROP_RAY_RIGHT,false);
         ObjectSetInteger(0,name,OBJPROP_RAY_LEFT,false);
         ObjectSetInteger(0,name,OBJPROP_SELECTABLE,false);
         ObjectSetInteger(0,name,OBJPROP_HIDDEN,true);
        }
      else
        {
         ObjectMove(0,name,0,t1,price);
         ObjectMove(0,name,1,t2,price);
        }
      ObjectSetInteger(0,name,OBJPROP_COLOR,clr);
      ObjectSetInteger(0,name,OBJPROP_STYLE,style);
      ObjectSetInteger(0,name,OBJPROP_WIDTH,width);
     }

   void              Text(const string name,const datetime t,const double price,const string text,
                          const color clr,const ENUM_ANCHOR_POINT anchor)
     {
      if(ObjectFind(0,name)<0)
        {
         ObjectCreate(0,name,OBJ_TEXT,0,t,price);
         ObjectSetInteger(0,name,OBJPROP_SELECTABLE,false);
         ObjectSetInteger(0,name,OBJPROP_HIDDEN,true);
        }
      else
         ObjectMove(0,name,0,t,price);
      ObjectSetString(0,name,OBJPROP_TEXT,text);
      ObjectSetString(0,name,OBJPROP_FONT,"Arial");
      ObjectSetInteger(0,name,OBJPROP_FONTSIZE,InpLabelFontSize);
      ObjectSetInteger(0,name,OBJPROP_COLOR,clr);
      ObjectSetInteger(0,name,OBJPROP_ANCHOR,anchor);
     }

   bool              PreviousAllowed(const int outcome)
     {
      if(outcome==OUTCOME_TP)
         return InpPrevShowTP;
      if(outcome==OUTCOME_SL)
         return InpPrevShowSL;
      return InpPrevShowInvalidated;   // invalidated and expired
     }

public:
   void              Init(const int digits,const ENUM_TIMEFRAMES tf)
     {
      m_digits   =digits;
      m_periodSec=PeriodSeconds(tf);
     }

   //--- removes the live objects of a forecast (explicit names only)
   void              DeleteActive(const long id)
     {
      string s=IntegerToString(id);
      ObjectDelete(0,SMIF_PREFIX+"RISK_"+s);
      ObjectDelete(0,SMIF_PREFIX+"REWARD_"+s);
      ObjectDelete(0,SMIF_PREFIX+"ENTRY_"+s);
      ObjectDelete(0,SMIF_PREFIX+"SL_"+s);
      ObjectDelete(0,SMIF_PREFIX+"TP_"+s);
      ObjectDelete(0,SMIF_PREFIX+"LABEL_"+s);
      ObjectDelete(0,SMIF_PREFIX+"LABEL_"+s+"_S");
      ObjectDelete(0,SMIF_PREFIX+"LABEL_"+s+"_TP");
      ObjectDelete(0,SMIF_PREFIX+"LABEL_"+s+"_SL");
     }

   void              DrawActive(const SForecast &f,const datetime tEnd)
     {
      string   s =IntegerToString(f.id);
      datetime t1=f.createTime;
      bool pending=(f.state==STATE_FORECAST_READY);
      Rect(SMIF_PREFIX+"RISK_"+s,t1,f.entry,tEnd,f.sl,BlendColor(InpRiskColor,InpRectangleTransparency));
      Rect(SMIF_PREFIX+"REWARD_"+s,t1,f.entry,tEnd,f.tp,BlendColor(InpRewardColor,InpRectangleTransparency));
      Segment(SMIF_PREFIX+"ENTRY_"+s,t1,tEnd,f.entry,InpEntryColor,pending ? STYLE_DASH : STYLE_SOLID,2);
      Segment(SMIF_PREFIX+"SL_"+s,t1,tEnd,f.sl,InpRiskColor,STYLE_SOLID,1);
      Segment(SMIF_PREFIX+"TP_"+s,t1,tEnd,f.tp,InpRewardColor,STYLE_SOLID,1);
      if(!InpShowLabels)
         return;
      color  tc =(color)ChartGetInteger(0,CHART_COLOR_FOREGROUND);
      string dir=(f.direction>0) ? "BUY" : "SELL";
      double mv =(f.direction>0) ? f.expMovePct : -f.expMovePct;
      Text(SMIF_PREFIX+"LABEL_"+s,tEnd,f.entry,
           StringFormat("FORECAST: %s   Entry %s   Status: %s",dir,Px(f.entry),StateToString(f.state)),
           tc,ANCHOR_LEFT_LOWER);
      Text(SMIF_PREFIX+"LABEL_"+s+"_S",tEnd,f.entry,
           StringFormat("Samples %d (filled %d)   TP Hit %.1f%%   SL Hit %.1f%%   Invalidated %.1f%%",
                        f.samples,f.filled,f.tpRate,f.slRate,f.invRate),
           tc,ANCHOR_LEFT_UPPER);
      Text(SMIF_PREFIX+"LABEL_"+s+"_TP",tEnd,f.tp,
           StringFormat("TP %s   Expected Move %s   Reward %s   R:R %.2f",Px(f.tp),SignedPct(mv,2),Px(f.rewardPrice),f.rr),
           InpRewardColor,ANCHOR_LEFT);
      Text(SMIF_PREFIX+"LABEL_"+s+"_SL",tEnd,f.sl,
           StringFormat("SL %s   Risk %s (%.2f%%)",Px(f.sl),Px(f.riskPrice),f.riskPctPrice),
           InpRiskColor,ANCHOR_LEFT);
     }

   //--- a resolved forecast drawn only between its creation and resolution
   void              DrawPrevious(const SForecast &f)
     {
      string   s =IntegerToString(f.id);
      datetime t1=f.createTime;
      datetime t2=(f.resolveTime>t1) ? f.resolveTime : t1+(datetime)m_periodSec;
      bool filled=(f.outcome!=OUTCOME_INVALIDATED);
      Rect(SMIF_PREV_PREFIX+"RISK_"+s,t1,f.entry,t2,f.sl,BlendColor(InpRiskColor,InpPreviousTransparency));
      Rect(SMIF_PREV_PREFIX+"REWARD_"+s,t1,f.entry,t2,f.tp,BlendColor(InpRewardColor,InpPreviousTransparency));
      Segment(SMIF_PREV_PREFIX+"ENTRY_"+s,t1,t2,f.entry,InpEntryColor,filled ? STYLE_SOLID : STYLE_DOT,1);
      Segment(SMIF_PREV_PREFIX+"SL_"+s,t1,t2,f.sl,InpRiskColor,STYLE_DOT,1);
      Segment(SMIF_PREV_PREFIX+"TP_"+s,t1,t2,f.tp,InpRewardColor,STYLE_DOT,1);
      if(!InpShowLabels)
         return;
      string dir=(f.direction>0) ? "BUY" : "SELL";
      string txt;
      color  c;
      double px;
      switch(f.outcome)
        {
         case OUTCOME_TP:
            txt=StringFormat("#%s %s  TP HIT  %s",s,dir,SignedPct(f.outcomePct,2));
            c=InpRewardColor;
            px=f.tp;
            break;
         case OUTCOME_SL:
            txt=StringFormat("#%s %s  SL HIT  %s",s,dir,SignedPct(f.outcomePct,2));
            c=InpRiskColor;
            px=f.sl;
            break;
         case OUTCOME_EXPIRED:
            txt=StringFormat("#%s %s  EXPIRED  %s",s,dir,SignedPct(f.outcomePct,2));
            c=clrGray;
            px=f.exitPrice;
            break;
         default:
            txt=StringFormat("#%s %s  INVALIDATED (%s)",s,dir,InvalidReasonToString(f.invalidReason));
            c=clrGray;
            px=f.entry;
            break;
        }
      Text(SMIF_PREV_PREFIX+"LABEL_"+s,t2,px,txt,c,ANCHOR_LEFT);
     }

   void              ClearPrevious(void)
     {
      ObjectsDeleteAll(0,SMIF_PREV_PREFIX);
     }

   void              RebuildPrevious(CSmifEngine &eng)
     {
      ClearPrevious();
      int drawn=0;
      for(int k=ArraySize(eng.m_fc)-1;k>=0 && drawn<InpMaxPreviousDrawn;k--)
        {
         if(eng.m_fc[k].outcome==OUTCOME_NONE)
            continue;
         if(!PreviousAllowed(eng.m_fc[k].outcome))
            continue;
         DrawPrevious(eng.m_fc[k]);
         drawn++;
        }
     }

   //--- draws open forecasts; removes live objects of resolved ones
   void              Sync(CSmifEngine &eng,const bool showPrev,const bool rebuildPrev)
     {
      if(eng.m_lastBar<0)
         return;
      datetime tEnd=eng.m_rates[eng.m_lastBar].time+(datetime)(InpRectangleExtensionBars*m_periodSec);
      bool newResolved=false;
      int  n=ArraySize(eng.m_fc);
      for(int k=0;k<n;k++)
        {
         if(eng.m_fc[k].outcome==OUTCOME_NONE)
           {
            DrawActive(eng.m_fc[k],tEnd);
            eng.m_fc[k].activeDrawn=true;
            eng.m_fc[k].dirty=false;
            continue;
           }
         //--- TP reached, SL violated, invalidated or expired: remove the live objects
         if(eng.m_fc[k].activeDrawn)
           {
            DeleteActive(eng.m_fc[k].id);
            eng.m_fc[k].activeDrawn=false;
            newResolved=true;
           }
         else
            if(eng.m_fc[k].dirty)
               newResolved=true;
         eng.m_fc[k].dirty=false;
        }
      if(showPrev && (newResolved || rebuildPrev))
         RebuildPrevious(eng);
     }
  };

//+------------------------------------------------------------------+
//| CSmifDashboard - upper-right statistical dashboard + toggle      |
//+------------------------------------------------------------------+
class CSmifDashboard
  {
private:
   string            m_keys[];
   string            m_vals[];
   int               m_kinds[];       // 0 title, 1 section, 2 key/value, 3 note
   color             m_colors[];
   int               m_lastRows;
   int               m_x;
   int               m_y;

   void              Add(const string k,const string v,const int kind,const color c)
     {
      int n=ArraySize(m_keys);
      ArrayResize(m_keys,n+1);
      ArrayResize(m_vals,n+1);
      ArrayResize(m_kinds,n+1);
      ArrayResize(m_colors,n+1);
      m_keys[n]=k;
      m_vals[n]=v;
      m_kinds[n]=kind;
      m_colors[n]=c;
     }
   void              Section(const string title) { Add(title,"",1,InpDashboardHeader); }
   void              Row(const string k,const string v) { Add(k,v,2,InpDashboardText); }
   void              RowC(const string k,const string v,const color c) { Add(k,v,2,c); }
   void              Note(const string text)
     {
      string t=text;
      while(StringLen(t)>44)
        {
         Add(StringSubstr(t,0,44),"",3,clrOrange);
         t=StringSubstr(t,44);
        }
      if(StringLen(t)>0)
         Add(t,"",3,clrOrange);
     }

   void              Label(const string name,const int x,const int y,const string text,const color clr,
                           const ENUM_ANCHOR_POINT anchor,const bool bold)
     {
      if(ObjectFind(0,name)<0)
        {
         ObjectCreate(0,name,OBJ_LABEL,0,0,0);
         ObjectSetInteger(0,name,OBJPROP_CORNER,CORNER_LEFT_UPPER);
         ObjectSetInteger(0,name,OBJPROP_SELECTABLE,false);
         ObjectSetInteger(0,name,OBJPROP_HIDDEN,true);
         ObjectSetInteger(0,name,OBJPROP_BACK,false);
        }
      ObjectSetInteger(0,name,OBJPROP_XDISTANCE,x);
      ObjectSetInteger(0,name,OBJPROP_YDISTANCE,y);
      ObjectSetInteger(0,name,OBJPROP_ANCHOR,anchor);
      ObjectSetInteger(0,name,OBJPROP_COLOR,clr);
      ObjectSetInteger(0,name,OBJPROP_FONTSIZE,InpDashboardFontSize);
      ObjectSetString(0,name,OBJPROP_FONT,bold ? "Arial Bold" : "Arial");
      ObjectSetString(0,name,OBJPROP_TEXT,text);
     }

   void              ComputeOrigin(const int w,const int totalH)
     {
      int cw=(int)ChartGetInteger(0,CHART_WIDTH_IN_PIXELS);
      int ch=(int)ChartGetInteger(0,CHART_HEIGHT_IN_PIXELS);
      switch(InpDashboardCorner)
        {
         case CORNER_LEFT_UPPER:
            m_x=InpDashboardX;
            m_y=InpDashboardY;
            break;
         case CORNER_RIGHT_UPPER:
            m_x=cw-w-InpDashboardX;
            m_y=InpDashboardY;
            break;
         case CORNER_LEFT_LOWER:
            m_x=InpDashboardX;
            m_y=ch-totalH-InpDashboardY;
            break;
         case CORNER_RIGHT_LOWER:
            m_x=cw-w-InpDashboardX;
            m_y=ch-totalH-InpDashboardY;
            break;
         default:
            m_x=InpDashboardX;
            m_y=InpDashboardY;
            break;
        }
      m_x=IMax(0,m_x);
      m_y=IMax(0,m_y);
     }

   void              Render(const bool showPrev)
     {
      int fs  =InpDashboardFontSize;
      int rowH=(int)MathRound(fs*1.9)+2;
      int w   =fs*34;
      int pad =8;
      int rows=ArraySize(m_keys);
      int h   =rows*rowH+2*pad;
      int btnH=rowH+8;
      int panelH=InpShowDashboard ? h+4 : 0;
      ComputeOrigin(w,panelH+btnH);

      if(InpShowDashboard)
        {
         string bg=SMIF_DASH_PREFIX+"BG";
         if(ObjectFind(0,bg)<0)
           {
            ObjectCreate(0,bg,OBJ_RECTANGLE_LABEL,0,0,0);
            ObjectSetInteger(0,bg,OBJPROP_CORNER,CORNER_LEFT_UPPER);
            ObjectSetInteger(0,bg,OBJPROP_BORDER_TYPE,BORDER_FLAT);
            ObjectSetInteger(0,bg,OBJPROP_SELECTABLE,false);
            ObjectSetInteger(0,bg,OBJPROP_HIDDEN,true);
            ObjectSetInteger(0,bg,OBJPROP_BACK,false);
           }
         ObjectSetInteger(0,bg,OBJPROP_XDISTANCE,m_x);
         ObjectSetInteger(0,bg,OBJPROP_YDISTANCE,m_y);
         ObjectSetInteger(0,bg,OBJPROP_XSIZE,w);
         ObjectSetInteger(0,bg,OBJPROP_YSIZE,h);
         ObjectSetInteger(0,bg,OBJPROP_BGCOLOR,InpDashboardBg);
         ObjectSetInteger(0,bg,OBJPROP_COLOR,InpDashboardHeader);

         for(int r=0;r<rows;r++)
           {
            int    y =m_y+pad+r*rowH;
            string kn=SMIF_DASH_PREFIX+"K_"+IntegerToString(r);
            string vn=SMIF_DASH_PREFIX+"V_"+IntegerToString(r);
            bool   bold=(m_kinds[r]<=1);
            color  kc=(m_kinds[r]==2) ? InpDashboardText : m_colors[r];
            Label(kn,m_x+pad,y,m_keys[r],kc,ANCHOR_LEFT_UPPER,bold);
            Label(vn,m_x+w-pad,y,m_vals[r],m_colors[r],ANCHOR_RIGHT_UPPER,false);
           }
         for(int r=rows;r<m_lastRows;r++)
           {
            ObjectDelete(0,SMIF_DASH_PREFIX+"K_"+IntegerToString(r));
            ObjectDelete(0,SMIF_DASH_PREFIX+"V_"+IntegerToString(r));
           }
         m_lastRows=rows;
        }
      DrawButton(showPrev,m_x,m_y+panelH,w,btnH);
     }

public:
                     CSmifDashboard(void) { m_lastRows=0; m_x=0; m_y=0; }

   void              DrawButton(const bool showPrev,const int x,const int y,const int w,const int h)
     {
      string b=SMIF_BTN_PREV;
      if(ObjectFind(0,b)<0)
        {
         ObjectCreate(0,b,OBJ_BUTTON,0,0,0);
         ObjectSetInteger(0,b,OBJPROP_CORNER,CORNER_LEFT_UPPER);
         ObjectSetInteger(0,b,OBJPROP_SELECTABLE,false);
         ObjectSetInteger(0,b,OBJPROP_HIDDEN,true);
         ObjectSetInteger(0,b,OBJPROP_ZORDER,10);
        }
      ObjectSetInteger(0,b,OBJPROP_XDISTANCE,x);
      ObjectSetInteger(0,b,OBJPROP_YDISTANCE,y);
      ObjectSetInteger(0,b,OBJPROP_XSIZE,w);
      ObjectSetInteger(0,b,OBJPROP_YSIZE,h);
      ObjectSetInteger(0,b,OBJPROP_FONTSIZE,InpDashboardFontSize);
      ObjectSetString(0,b,OBJPROP_FONT,"Arial Bold");
      ObjectSetString(0,b,OBJPROP_TEXT,showPrev ? "Previous Forecasts: ON  [P]" : "Previous Forecasts: OFF  [P]");
      ObjectSetInteger(0,b,OBJPROP_BGCOLOR,showPrev ? C'20,90,60' : C'60,60,70');
      ObjectSetInteger(0,b,OBJPROP_COLOR,clrWhite);
      ObjectSetInteger(0,b,OBJPROP_BORDER_COLOR,InpDashboardHeader);
      ObjectSetInteger(0,b,OBJPROP_STATE,false);
     }

   void              Update(CSmifEngine &eng,const bool showPrev)
     {
      ArrayResize(m_keys,0);
      ArrayResize(m_vals,0);
      ArrayResize(m_kinds,0);
      ArrayResize(m_colors,0);
      Add(StringFormat("SMIF  |  %s  %s",eng.GetSymbol(),TFToString(eng.GetTimeframe())),"",0,InpDashboardHeader);

      if(!eng.IsReady())
        {
         Section("SYSTEM STATUS");
         RowC("Statistical Engine","LOADING",clrOrange);
         RowC("Historical Data","SYNCHRONISING",clrOrange);
         RowC("Forecast","NONE",clrOrange);
         Render(showPrev);
         return;
        }

      int dg=eng.GetDigits();
      int fi=eng.LatestOpenForecast();

      //--- MARKET FORECAST
      Section("MARKET FORECAST");
      if(fi>=0)
        {
         SForecast f=eng.m_fc[fi];
         double mv=(f.direction>0) ? f.expMovePct : -f.expMovePct;
         RowC("Direction",(f.direction>0) ? "BUY" : "SELL",(f.direction>0) ? InpRewardColor : InpRiskColor);
         Row("Reference",StringFormat("%s %s",(f.refType==REFERENCE_HIGH) ? "High" : "Low",DoubleToString(f.refPrice,dg)));
         RowC("Entry",DoubleToString(f.entry,dg),InpEntryColor);
         RowC("SL",DoubleToString(f.sl,dg),InpRiskColor);
         RowC("TP",DoubleToString(f.tp,dg),InpRewardColor);
         Row("Expected Move %",SignedPct(mv,2));
         Row("Risk",DoubleToString(f.riskPrice,dg));
         Row("Reward",DoubleToString(f.rewardPrice,dg));
         Row("R:R",DoubleToString(f.rr,2));
        }
      else
        {
         RowC("Direction","NO VALID FORECAST",clrOrange);
         if(eng.m_lastReject!="")
            Note("Reason: "+eng.m_lastReject);
        }

      //--- STATISTICS
      Section("STATISTICS");
      if(fi>=0)
        {
         SForecast f=eng.m_fc[fi];
         Row("Historical Samples",StringFormat("%d (filled %d)",f.samples,f.filled));
         Row("TP Hit Rate",StringFormat("%.1f%% (%d/%d)",f.tpRate,f.tpHits,f.filled));
         Row("SL Hit Rate",StringFormat("%.1f%% (%d/%d)",f.slRate,f.slHits,f.filled));
         Row("Invalidation Rate",StringFormat("%.1f%% (%d/%d)",f.invRate,f.invalidCount,f.samples));
         Row("Median Outcome",SignedPct(f.medOutcome,3));
         Row("Average Outcome",SignedPct(f.avgOutcome,3));
        }
      else
        {
         Row("Historical Samples","-");
         Row("TP Hit Rate","-");
         Row("SL Hit Rate","-");
        }

      //--- MARKET CONDITIONS
      Section("MARKET CONDITIONS");
      SMovementInfo mv2=eng.m_move;
      if(mv2.valid)
        {
         Row("ATR",DoubleToString(mv2.atr,dg));
         Row("Volatility",StringFormat("%.2fx (%s)",mv2.volRatio,RegimeToString(RegimeOf(mv2.volRatio))));
         Row("Momentum",StringFormat("%.2f ATR",mv2.momentum));
         Row("Movement Duration",StringFormat("%d bars",mv2.durBars));
         Row("Movement Distance",StringFormat("%s (%.2f ATR)",DoubleToString(mv2.distance,dg),mv2.atrN));
        }
      else
         Row("Movement","no developing reference");
      Row("Forecast Status",StateToString(eng.m_engineState));

      //--- VALIDATION
      Section("VALIDATION (RESOLVED)");
      SValidation v;
      eng.ComputeValidation(v,0,-1,-1);
      Row("Forecasts",StringFormat("%d (B %d / S %d)",v.total,v.buy,v.sell));
      Row("TP / SL / Inv / Exp",StringFormat("%d / %d / %d / %d",v.tp,v.sl,v.inv,v.exp));
      Row("TP Hit Rate",(v.entered>0) ? StringFormat("%.1f%% of %d filled",v.tpRate,v.entered) : "-");
      Row("Avg / Median Return",(v.entered>0) ? SignedPct(v.avgRet,3)+" / "+SignedPct(v.medRet,3) : "-");

      //--- SYSTEM STATUS
      Section("SYSTEM STATUS");
      RowC("Statistical Engine","READY",clrLimeGreen);
      int nobs=eng.ObservationCount();
      if(nobs>=InpMinimumHistoricalSamples)
         RowC("Historical Data",StringFormat("AVAILABLE (%d obs)",nobs),clrLimeGreen);
      else
         RowC("Historical Data",StringFormat("INSUFFICIENT (%d obs)",nobs),clrOrange);
      string fs="NONE";
      if(fi>=0)
         fs=(eng.m_fc[fi].state==STATE_FORECAST_READY) ? "DEVELOPING (PENDING ENTRY)" : "ACTIVE";
      RowC("Forecast",fs,(fi>=0) ? clrLimeGreen : InpDashboardText);

      Render(showPrev);
     }
  };

//+------------------------------------------------------------------+
//| CSmifCsvLogger - forecast export and validation summary          |
//+------------------------------------------------------------------+
class CSmifCsvLogger
  {
private:
   int               m_handle;
   string            m_file;
   string            m_summary;
   string            m_tfName;

   void              SummaryRow(const int h,const string scope,const SValidation &v)
     {
      FileWrite(h,scope,m_tfName,v.total,v.buy,v.sell,v.tp,v.sl,v.inv,v.exp,v.entered,
                DoubleToString(v.tpRate,2),DoubleToString(v.slRate,2),DoubleToString(v.invRate,2),
                DoubleToString(v.avgRet,4),DoubleToString(v.medRet,4),
                DoubleToString(v.avgRisk,6),DoubleToString(v.avgReward,6),DoubleToString(v.avgRR,3),
                DoubleToString(v.avgBarsTP,1),DoubleToString(v.avgBarsSL,1));
     }

public:
                     CSmifCsvLogger(void) { m_handle=INVALID_HANDLE; }

   bool              Open(const string sym,const string tfName)
     {
      m_tfName =tfName;
      m_file   =SanitizeFileName("SMIF_"+sym+"_"+tfName+"_forecasts.csv");
      m_summary=SanitizeFileName("SMIF_"+sym+"_"+tfName+"_validation.csv");
      if(!InpExportCSV)
         return true;
      m_handle=FileOpen(m_file,FILE_WRITE|FILE_CSV|FILE_ANSI|FILE_SHARE_READ,',');
      if(m_handle==INVALID_HANDLE)
        {
         PrintFormat("[SMIF] Cannot open %s (error %d)",m_file,GetLastError());
         return false;
        }
      FileWrite(m_handle,"ForecastID","Date","Time","Symbol","Timeframe",
                "ReferenceType","ReferencePrice","ReferenceTime","Direction",
                "Entry","SL","TP","RiskPoints","RewardPoints","RiskReward","ExpectedMovePercent",
                "ATR","Volatility","Momentum","MovementDistance","MovementDuration",
                "HistoricalSamples","FilledSamples","TPHitRate","SLHitRate","InvalidationRate",
                "Outcome","OutcomePoints","OutcomePercent","Resolution","ResolutionTime","Duration",
                "Session","VolatilityRegime","Similarity","InvalidReason");
      FileFlush(m_handle);
      return true;
     }

   void              Close(void)
     {
      if(m_handle!=INVALID_HANDLE)
         FileClose(m_handle);
      m_handle=INVALID_HANDLE;
     }

   void              WriteRow(CSmifEngine &eng,const SForecast &f)
     {
      int dg=eng.GetDigits();
      FileWrite(m_handle,IntegerToString(f.id),
                TimeToString(f.createTime,TIME_DATE),TimeToString(f.createTime,TIME_MINUTES),
                eng.GetSymbol(),m_tfName,
                (f.refType==REFERENCE_HIGH) ? "HIGH" : "LOW",DoubleToString(f.refPrice,dg),
                TimeToString(f.refTime,TIME_DATE|TIME_MINUTES),
                (f.direction>0) ? "BUY" : "SELL",
                DoubleToString(f.entry,dg),DoubleToString(f.sl,dg),DoubleToString(f.tp,dg),
                DoubleToString(f.riskPts,1),DoubleToString(f.rewardPts,1),DoubleToString(f.rr,3),
                DoubleToString(f.expMovePct,4),
                DoubleToString(f.atr,dg),DoubleToString(f.volRatio,3),DoubleToString(f.momentum,3),
                DoubleToString(f.movDist,dg),IntegerToString(f.movDur),
                IntegerToString(f.samples),IntegerToString(f.filled),
                DoubleToString(f.tpRate,2),DoubleToString(f.slRate,2),DoubleToString(f.invRate,2),
                OutcomeToString(f.outcome),DoubleToString(f.outcomePts,1),DoubleToString(f.outcomePct,4),
                StateToString(f.state),TimeToString(f.resolveTime,TIME_DATE|TIME_MINUTES),
                IntegerToString(f.resolveBar-f.createBar),
                SessionToString(f.session),RegimeToString(f.regime),DoubleToString(f.similarity,4),
                InvalidReasonToString(f.invalidReason));
     }

   //--- writes every newly resolved forecast exactly once
   void              Flush(CSmifEngine &eng)
     {
      int  n=ArraySize(eng.m_fc);
      bool wrote=false;
      for(int k=0;k<n;k++)
        {
         if(eng.m_fc[k].outcome==OUTCOME_NONE || eng.m_fc[k].logged)
            continue;
         eng.m_fc[k].logged=true;
         if(m_handle==INVALID_HANDLE)
            continue;
         WriteRow(eng,eng.m_fc[k]);
         wrote=true;
        }
      if(wrote)
         FileFlush(m_handle);
     }

   void              WriteSummary(CSmifEngine &eng)
     {
      if(!InpExportCSV || !eng.IsReady())
         return;
      int h=FileOpen(m_summary,FILE_WRITE|FILE_CSV|FILE_ANSI|FILE_SHARE_READ,',');
      if(h==INVALID_HANDLE)
        {
         PrintFormat("[SMIF] Cannot open %s (error %d)",m_summary,GetLastError());
         return;
        }
      FileWrite(h,"Scope","Timeframe","TotalForecasts","BuyForecasts","SellForecasts",
                "TPHits","SLHits","Invalidated","Expired","Entered",
                "TPHitRate","SLHitRate","InvalidationRate","AverageReturn%","MedianReturn%",
                "AverageRisk","AverageReward","AverageRR","AvgBarsToTP","AvgBarsToSL");
      SValidation v;
      eng.ComputeValidation(v,0,-1,-1);
      SummaryRow(h,"ALL",v);
      eng.ComputeValidation(v,1,-1,-1);
      SummaryRow(h,"BUY",v);
      eng.ComputeValidation(v,-1,-1,-1);
      SummaryRow(h,"SELL",v);
      for(int s=SMIF_SESSION_ASIA;s<=SMIF_SESSION_LATE;s++)
        {
         eng.ComputeValidation(v,0,s,-1);
         SummaryRow(h,"SESSION_"+SessionToString(s),v);
        }
      for(int r=SMIF_REGIME_LOW;r<=SMIF_REGIME_HIGH;r++)
        {
         eng.ComputeValidation(v,0,-1,r);
         SummaryRow(h,"VOLATILITY_"+RegimeToString(r),v);
        }
      FileClose(h);
     }
  };

//+------------------------------------------------------------------+
//| Global module instances                                          |
//+------------------------------------------------------------------+
CSmifEngine    g_engine;
CSmifRenderer  g_renderer;
CSmifDashboard g_dashboard;
CSmifCsvLogger g_logger;
bool           g_showPrevious=false;

//+------------------------------------------------------------------+
//| Orchestration                                                    |
//+------------------------------------------------------------------+
void RefreshOutputs(const bool rebuildPrev)
  {
   g_renderer.Sync(g_engine,g_showPrevious,rebuildPrev);
   g_logger.Flush(g_engine);
   g_dashboard.Update(g_engine,g_showPrevious);
   ChartRedraw();
  }

bool TryLoad(void)
  {
   if(g_engine.IsReady())
      return true;
   if(!g_engine.Load())
      return false;
   RefreshOutputs(true);
   g_logger.WriteSummary(g_engine);
   return true;
  }

//--- toggles the previous-forecast layer (button or key P)
void TogglePrevious(void)
  {
   g_showPrevious=!g_showPrevious;
   if(g_showPrevious && g_engine.IsReady())
      g_renderer.RebuildPrevious(g_engine);
   else
      g_renderer.ClearPrevious();
   g_dashboard.Update(g_engine,g_showPrevious);
   ChartRedraw();
  }

bool ValidateInputs(void)
  {
   string err="";
   if(InpReferenceLeftBars<1 || InpReferenceRightBars<1)
      err="Reference confirmation bars must be >= 1";
   else
      if(InpATRPeriod<1)
         err="ATR period must be >= 1";
      else
         if(InpSimilaritySamples<5 || InpMinimumHistoricalSamples<5)
            err="Similarity / historical samples must be >= 5";
         else
            if(InpTerminationPercentile<0 || InpTerminationPercentile>100 ||
               InpStopPercentile<0 || InpStopPercentile>100 ||
               InpTargetPercentile<0 || InpTargetPercentile>100)
               err="Percentiles must be within 0..100";
            else
               if(InpForecastExpiryBars<1 || InpMaxHoldBars<1)
                  err="Expiry and hold bars must be >= 1";
               else
                  if(InpHistoryBars<500)
                     err="History bars must be >= 500";
   if(err!="")
     {
      Print("[SMIF] Invalid input: ",err);
      return false;
     }
   return true;
  }

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   if(!ValidateInputs())
      return INIT_PARAMETERS_INCORRECT;
   ENUM_TIMEFRAMES tf=ResolveTimeframe(InpAnalysisTimeframe);
   g_showPrevious=InpShowPreviousForecasts;
   g_engine.Init(_Symbol,tf);
   g_renderer.Init((int)SymbolInfoInteger(_Symbol,SYMBOL_DIGITS),tf);
   g_logger.Open(_Symbol,TFToString(tf));
   IndicatorSetString(INDICATOR_SHORTNAME,"SMIF("+TFToString(tf)+")");
   EventSetTimer(1);
   if(!TryLoad())
     {
      g_dashboard.Update(g_engine,g_showPrevious);
      ChartRedraw();
     }
   return INIT_SUCCEEDED;
  }

//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   EventKillTimer();
   g_logger.WriteSummary(g_engine);
   g_logger.Close();
   ObjectsDeleteAll(0,SMIF_PREFIX);   // only objects created by this indicator
   ChartRedraw();
  }

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
   if(!g_engine.IsReady())
     {
      TryLoad();
      return rates_total;
     }
   if(g_engine.SyncNewBars()>0)
      RefreshOutputs(false);
   return rates_total;
  }

//+------------------------------------------------------------------+
//| Timer: retries loading and catches bars without chart ticks      |
//+------------------------------------------------------------------+
void OnTimer()
  {
   if(!g_engine.IsReady())
     {
      TryLoad();
      return;
     }
   if(g_engine.SyncNewBars()>0)
      RefreshOutputs(false);
  }

//+------------------------------------------------------------------+
//| Chart events: previous-forecast toggle and dashboard placement   |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,const long &lparam,const double &dparam,const string &sparam)
  {
   if(id==CHARTEVENT_OBJECT_CLICK && sparam==SMIF_BTN_PREV)
     {
      ObjectSetInteger(0,SMIF_BTN_PREV,OBJPROP_STATE,false);
      TogglePrevious();
      return;
     }
   if(id==CHARTEVENT_KEYDOWN && lparam==80)   // 'P'
     {
      TogglePrevious();
      return;
     }
   if(id==CHARTEVENT_CHART_CHANGE)
     {
      g_dashboard.Update(g_engine,g_showPrevious);
      ChartRedraw();
     }
  }
//+------------------------------------------------------------------+