# Phase 5: Analytics & Reporting - Implementation Guide

**Estimated Time:** 3-4 hours  
**Complexity:** Medium  
**Dependencies:** Phase 4 Complete ✅

---

## Implementation Overview

Phase 5 adds comprehensive analytics and reporting capabilities. This is the "observe, measure, and optimize" layer that helps understand performance, identify what's working, and continuously improve the trading system.

**Core Concept:** What gets measured gets improved. Track everything, attribute performance, find patterns.

---

## Phase A: Trade Analytics Foundation (45 minutes)

### Task A1: Create AnalyticsModels.cs
**Location:** `src/NT8.Core/Analytics/AnalyticsModels.cs`

**Deliverables:**
- `TradeRecord` record - Complete trade lifecycle
- `TradeMetrics` record - Per-trade performance metrics
- `PerformanceSnapshot` record - Point-in-time performance
- `AttributionBreakdown` record - P&L attribution
- `AnalyticsPeriod` enum - Daily/Weekly/Monthly/AllTime

**TradeRecord:**
```csharp
public record TradeRecord(
    string TradeId,
    string Symbol,
    string StrategyName,
    DateTime EntryTime,
    DateTime? ExitTime,
    OrderSide Side,
    int Quantity,
    double EntryPrice,
    double? ExitPrice,
    double RealizedPnL,
    double UnrealizedPnL,
    TradeGrade Grade,
    double ConfluenceScore,
    RiskMode RiskMode,
    VolatilityRegime VolatilityRegime,
    TrendRegime TrendRegime,
    int StopTicks,
    int TargetTicks,
    double RMultiple,
    TimeSpan Duration,
    Dictionary<string, object> Metadata
);
```

**TradeMetrics:**
```csharp
public record TradeMetrics(
    string TradeId,
    double PnL,
    double RMultiple,
    double MAE,              // Maximum Adverse Excursion
    double MFE,              // Maximum Favorable Excursion
    double Slippage,
    double Commission,
    double NetPnL,
    bool IsWinner,
    TimeSpan HoldTime,
    double ROI,              // Return on Investment
    Dictionary<string, object> CustomMetrics
);
```

---

### Task A2: Create TradeRecorder.cs
**Location:** `src/NT8.Core/Analytics/TradeRecorder.cs`

**Deliverables:**
- Record complete trade lifecycle
- Track entry, exit, fills, modifications
- Calculate trade metrics (MAE, MFE, R-multiple)
- Thread-safe trade storage
- Query interface for historical trades

**Key Features:**
- Real-time trade tracking
- Automatic metric calculation
- Historical trade database (in-memory)
- Export to CSV/JSON

**Methods:**
```csharp
public void RecordEntry(string tradeId, StrategyIntent intent, OrderFill fill, ConfluenceScore score, RiskMode mode);
public void RecordExit(string tradeId, OrderFill fill);
public void RecordPartialFill(string tradeId, OrderFill fill);
public TradeRecord GetTrade(string tradeId);
public List<TradeRecord> GetTrades(DateTime start, DateTime end);
public List<TradeRecord> GetTradesByGrade(TradeGrade grade);
public List<TradeRecord> GetTradesByStrategy(string strategyName);
```

---

### Task A3: Create PerformanceCalculator.cs
**Location:** `src/NT8.Core/Analytics/PerformanceCalculator.cs`

**Deliverables:**
- Calculate performance metrics
- Win rate, profit factor, expectancy
- Sharpe ratio, Sortino ratio
- Maximum drawdown, recovery factor
- Risk-adjusted returns

**Performance Metrics:**
```csharp
Total Trades
Win Rate = Wins / Total
Loss Rate = Losses / Total
Average Win = Sum(Winning Trades) / Wins
Average Loss = Sum(Losing Trades) / Losses
Profit Factor = Gross Profit / Gross Loss
Expectancy = (Win% × AvgWin) - (Loss% × AvgLoss)

Sharpe Ratio = (Mean Return - Risk Free Rate) / Std Dev Returns
Sortino Ratio = (Mean Return - Risk Free Rate) / Downside Dev
Max Drawdown = Max(Peak - Trough) / Peak
Recovery Factor = Net Profit / Max Drawdown
```

**Methods:**
```csharp
public PerformanceMetrics Calculate(List<TradeRecord> trades);
public double CalculateWinRate(List<TradeRecord> trades);
public double CalculateProfitFactor(List<TradeRecord> trades);
public double CalculateExpectancy(List<TradeRecord> trades);
public double CalculateSharpeRatio(List<TradeRecord> trades, double riskFreeRate);
public double CalculateMaxDrawdown(List<TradeRecord> trades);
```

---

## Phase B: P&L Attribution (60 minutes)

### Task B1: Create AttributionModels.cs
**Location:** `src/NT8.Core/Analytics/AttributionModels.cs`

**Deliverables:**
- `AttributionDimension` enum - Strategy/Grade/Regime/Time
- `AttributionSlice` record - P&L by dimension
- `AttributionReport` record - Complete attribution
- `ContributionAnalysis` record - Factor contributions

**AttributionSlice:**
```csharp
public record AttributionSlice(
    string DimensionName,
    string DimensionValue,
    double TotalPnL,
    double AvgPnL,
    int TradeCount,
    double WinRate,
    double ProfitFactor,
    double Contribution  // % of total P&L
);
```

---

### Task B2: Create PnLAttributor.cs
**Location:** `src/NT8.Core/Analytics/PnLAttributor.cs`

**Deliverables:**
- Attribute P&L by strategy
- Attribute P&L by trade grade
- Attribute P&L by regime (volatility/trend)
- Attribute P&L by time of day
- Multi-dimensional attribution

**Attribution Examples:**

**By Grade:**
```
A+ Trades: $2,500 (50% of total, 10 trades, 80% win rate)
A  Trades: $1,200 (24% of total, 15 trades, 70% win rate)
B  Trades:   $800 (16% of total, 20 trades, 60% win rate)
C  Trades:   $500 (10% of total, 25 trades, 52% win rate)
D  Trades: -$1,000 (rejected most, 5 taken, 20% win rate)
```

**By Regime:**
```
Low Vol Trending:   $3,000 (60%)
Normal Vol Trend:   $1,500 (30%)
High Vol Range:     -$500 (-10%)
Extreme Vol:        $0 (no trades taken)
```

**By Time:**
```
First Hour (9:30-10:30): $2,000 (40%)
Mid-Day (10:30-14:00):     $500 (10%)
Last Hour (15:00-16:00): $2,500 (50%)
```

**Methods:**
```csharp
public AttributionReport AttributeByGrade(List<TradeRecord> trades);
public AttributionReport AttributeByRegime(List<TradeRecord> trades);
public AttributionReport AttributeByStrategy(List<TradeRecord> trades);
public AttributionReport AttributeByTimeOfDay(List<TradeRecord> trades);
public AttributionReport AttributeMultiDimensional(List<TradeRecord> trades, List<AttributionDimension> dimensions);
```

---

### Task B3: Create DrawdownAnalyzer.cs
**Location:** `src/NT8.Core/Analytics/DrawdownAnalyzer.cs`

**Deliverables:**
- Track equity curve
- Identify drawdown periods
- Calculate drawdown metrics
- Attribute drawdowns to causes
- Recovery time analysis

**Drawdown Metrics:**
```csharp
Max Drawdown Amount
Max Drawdown %
Current Drawdown
Average Drawdown
Number of Drawdowns
Longest Drawdown Duration
Average Recovery Time
Drawdown Frequency
Underwater Periods
```

**Methods:**
```csharp
public DrawdownReport Analyze(List<TradeRecord> trades);
public List<DrawdownPeriod> IdentifyDrawdowns(List<TradeRecord> trades);
public DrawdownAttribution AttributeDrawdown(DrawdownPeriod period);
public double CalculateRecoveryTime(DrawdownPeriod period);
```

---

## Phase C: Grade & Regime Analysis (60 minutes)

### Task C1: Create GradePerformanceAnalyzer.cs
**Location:** `src/NT8.Core/Analytics/GradePerformanceAnalyzer.cs`

**Deliverables:**
- Performance metrics by grade
- Grade accuracy analysis
- Optimal grade thresholds
- Grade distribution analysis

**Grade Performance Report:**
```csharp
A+ Trades:
  Count: 25
  Win Rate: 84%
  Avg P&L: $250
  Profit Factor: 4.2
  Expectancy: $210
  Total P&L: $5,250
  % of Total: 52%
  
Grade Accuracy:
  A+ predictions: 84% actually profitable
  A  predictions: 72% actually profitable
  B  predictions: 61% actually profitable
  C  predictions: 48% actually profitable
  
Optimal Threshold:
  Current: Accept B+ and above
  Suggested: Accept A- and above (based on expectancy)
```

**Methods:**
```csharp
public GradePerformanceReport AnalyzeByGrade(List<TradeRecord> trades);
public double CalculateGradeAccuracy(TradeGrade grade, List<TradeRecord> trades);
public TradeGrade FindOptimalThreshold(List<TradeRecord> trades);
public Dictionary<TradeGrade, PerformanceMetrics> GetMetricsByGrade(List<TradeRecord> trades);
```

---

### Task C2: Create RegimePerformanceAnalyzer.cs
**Location:** `src/NT8.Core/Analytics/RegimePerformanceAnalyzer.cs`

**Deliverables:**
- Performance by volatility regime
- Performance by trend regime
- Combined regime analysis
- Regime transition impact

**Regime Performance:**
```csharp
Low Volatility:
  Uptrend: $3,000 (15 trades, 73% win rate)
  Range:   $500 (8 trades, 50% win rate)
  Downtrend: -$200 (5 trades, 40% win rate)

Normal Volatility:
  Uptrend: $2,500 (20 trades, 65% win rate)
  Range:   $0 (12 trades, 50% win rate)
  Downtrend: -$500 (7 trades, 29% win rate)

High Volatility:
  All: -$300 (avoid trading in high vol)
```

**Methods:**
```csharp
public RegimePerformanceReport AnalyzeByRegime(List<TradeRecord> trades);
public PerformanceMetrics GetPerformance(VolatilityRegime volRegime, TrendRegime trendRegime, List<TradeRecord> trades);
public List<RegimeTransitionImpact> AnalyzeTransitions(List<TradeRecord> trades);
```

---

### Task C3: Create ConfluenceValidator.cs
**Location:** `src/NT8.Core/Analytics/ConfluenceValidator.cs`

**Deliverables:**
- Validate confluence score accuracy
- Factor importance analysis
- Factor correlation to outcomes
- Recommended factor weights

**Confluence Validation:**
```csharp
Factor Performance Analysis:

ORB Validity Factor:
  High (>0.8): 75% win rate, $180 avg
  Medium (0.5-0.8): 58% win rate, $80 avg
  Low (<0.5): 42% win rate, -$30 avg
  Importance: HIGH (0.35 weight recommended)

Trend Alignment:
  High: 68% win rate, $150 avg
  Medium: 55% win rate, $60 avg
  Low: 48% win rate, $20 avg
  Importance: MEDIUM (0.25 weight recommended)

Current Weights vs Recommended:
  ORB Validity: 0.25 → 0.35 (increase)
  Trend: 0.20 → 0.25 (increase)
  Volatility: 0.20 → 0.15 (decrease)
  Timing: 0.20 → 0.15 (decrease)
  Quality: 0.15 → 0.10 (decrease)
```

**Methods:**
```csharp
public FactorAnalysisReport AnalyzeFactor(FactorType factor, List<TradeRecord> trades);
public Dictionary<FactorType, double> CalculateFactorImportance(List<TradeRecord> trades);
public Dictionary<FactorType, double> RecommendWeights(List<TradeRecord> trades);
public bool ValidateScore(ConfluenceScore score, TradeOutcome outcome);
```

---

## Phase D: Reporting & Visualization (45 minutes)

### Task D1: Create ReportModels.cs
**Location:** `src/NT8.Core/Analytics/ReportModels.cs`

**Deliverables:**
- `DailyReport` record - Daily performance summary
- `WeeklyReport` record - Weekly performance
- `MonthlyReport` record - Monthly performance
- `TradeBlotter` record - Trade log format
- `EquityCurve` record - Equity progression

---

### Task D2: Create ReportGenerator.cs
**Location:** `src/NT8.Core/Analytics/ReportGenerator.cs`

**Deliverables:**
- Generate daily/weekly/monthly reports
- Trade blotter with filtering
- Equity curve data
- Performance summary
- Export to multiple formats (text, CSV, JSON)

**Report Example:**
```
=== Daily Performance Report ===
Date: 2026-02-16

Summary:
  Total Trades: 8
  Winning Trades: 6 (75%)
  Losing Trades: 2 (25%)
  Total P&L: $1,250
  Average P&L: $156
  Largest Win: $450
  Largest Loss: -$120

Grade Distribution:
  A+: 2 trades, $900 P&L
  A:  3 trades, $550 P&L
  B:  2 trades, $100 P&L
  C:  1 trade, -$300 P&L (rejected most C grades)

Risk Mode:
  Started: PCP
  Ended: ECP (elevated after +$1,250)
  Transitions: 1 (PCP→ECP at +$500)

Top Contributing Factor:
  ORB Validity (avg 0.87 for winners)
```

**Methods:**
```csharp
public DailyReport GenerateDailyReport(DateTime date, List<TradeRecord> trades);
public WeeklyReport GenerateWeeklyReport(DateTime weekStart, List<TradeRecord> trades);
public string ExportToText(Report report);
public string ExportToCsv(List<TradeRecord> trades);
public string ExportToJson(Report report);
```

---

### Task D3: Create TradeBlotter.cs
**Location:** `src/NT8.Core/Analytics/TradeBlotter.cs`

**Deliverables:**
- Filterable trade log
- Sort by any column
- Search functionality
- Export capability
- Real-time updates

**Blotter Columns:**
```
| Time   | Symbol | Side | Qty | Entry | Exit  | P&L    | R-Mult | Grade | Regime | Duration |
|--------|--------|------|-----|-------|-------|--------|--------|-------|--------|----------|
| 10:05  | ES     | Long | 3   | 4205  | 4221  | +$600  | 2.0R   | A+    | LowVol | 45m      |
| 10:35  | ES     | Long | 2   | 4210  | 4218  | +$200  | 1.0R   | A     | Normal | 28m      |
| 11:20  | ES     | Short| 2   | 4215  | 4209  | +$150  | 0.75R  | B+    | Normal | 15m      |
```

**Methods:**
```csharp
public List<TradeRecord> FilterByDate(DateTime start, DateTime end);
public List<TradeRecord> FilterBySymbol(string symbol);
public List<TradeRecord> FilterByGrade(TradeGrade grade);
public List<TradeRecord> FilterByPnL(double minPnL, double maxPnL);
public List<TradeRecord> SortBy(string column, SortDirection direction);
```

---

## Phase E: Optimization Tools (60 minutes)

### Task E1: Create ParameterOptimizer.cs
**Location:** `src/NT8.Core/Analytics/ParameterOptimizer.cs`

**Deliverables:**
- Parameter sensitivity analysis
- Walk-forward optimization
- Grid search optimization
- Optimal parameter discovery

**Optimization Example:**
```csharp
Parameter: ORB Minutes (15, 30, 45, 60)

Results:
15 min: $2,500 (but high variance)
30 min: $5,200 (current - OPTIMAL)
45 min: $3,800
60 min: $1,200 (too conservative)

Recommendation: Keep at 30 minutes

Parameter: Stop Ticks (6, 8, 10, 12)

Results:
6 ticks:  $3,000 (61% win rate, tight stops)
8 ticks:  $5,200 (current - OPTIMAL, 68% win rate)
10 ticks: $4,800 (65% win rate, too wide)
12 ticks: $4,000 (63% win rate, too wide)

Recommendation: Keep at 8 ticks
```

**Methods:**
```csharp
public OptimizationResult OptimizeParameter(string paramName, List<double> values, List<TradeRecord> trades);
public GridSearchResult GridSearch(Dictionary<string, List<double>> parameters, List<TradeRecord> trades);
public WalkForwardResult WalkForwardTest(StrategyConfig config, List<BarData> historicalData);
```

---

### Task E2: Create MonteCarloSimulator.cs
**Location:** `src/NT8.Core/Analytics/MonteCarloSimulator.cs`

**Deliverables:**
- Monte Carlo scenario generation
- Risk of ruin calculation
- Confidence intervals
- Worst-case scenario analysis

**Monte Carlo Analysis:**
```csharp
Based on 10,000 simulations of 100 trades:

Expected Return: $12,500
95% Confidence Interval: $8,000 - $18,000
Worst Case (5th percentile): $3,500
Best Case (95th percentile): $22,000

Risk of Ruin (25% drawdown): 2.3%
Risk of Ruin (50% drawdown): 0.1%

Max Drawdown Distribution:
  10th percentile: 8%
  25th percentile: 12%
  50th percentile (median): 18%
  75th percentile: 25%
  90th percentile: 32%
```

**Methods:**
```csharp
public MonteCarloResult Simulate(List<TradeRecord> historicalTrades, int numSimulations, int numTrades);
public double CalculateRiskOfRuin(List<TradeRecord> trades, double drawdownThreshold);
public ConfidenceInterval CalculateConfidenceInterval(MonteCarloResult result, double confidenceLevel);
```

---

### Task E3: Create PortfolioOptimizer.cs
**Location:** `src/NT8.Core/Analytics/PortfolioOptimizer.cs`

**Deliverables:**
- Optimal strategy allocation
- Correlation-based diversification
- Risk-parity allocation
- Sharpe-optimal portfolio

**Portfolio Optimization:**
```csharp
Current Allocation:
  ORB Strategy: 100%

Optimal Allocation (if you had multiple strategies):
  ORB Strategy: 60%
  VWAP Bounce: 25%
  Mean Reversion: 15%

Expected Results:
  Current Sharpe: 1.8
  Optimized Sharpe: 2.3
  Correlation Benefit: 0.5 Sharpe increase
```

**Methods:**
```csharp
public AllocationResult OptimizeAllocation(List<StrategyPerformance> strategies);
public double CalculatePortfolioSharpe(Dictionary<string, double> allocation, List<StrategyPerformance> strategies);
public Dictionary<string, double> RiskParityAllocation(List<StrategyPerformance> strategies);
```

---

## Phase F: Comprehensive Testing (60 minutes)

### Task F1: TradeRecorderTests.cs
**Location:** `tests/NT8.Core.Tests/Analytics/TradeRecorderTests.cs`

**Minimum:** 15 tests

---

### Task F2: PerformanceCalculatorTests.cs
**Location:** `tests/NT8.Core.Tests/Analytics/PerformanceCalculatorTests.cs`

**Minimum:** 20 tests

---

### Task F3: PnLAttributorTests.cs
**Location:** `tests/NT8.Core.Tests/Analytics/PnLAttributorTests.cs`

**Minimum:** 18 tests

---

### Task F4: GradePerformanceAnalyzerTests.cs
**Location:** `tests/NT8.Core.Tests/Analytics/GradePerformanceAnalyzerTests.cs`

**Minimum:** 15 tests

---

### Task F5: OptimizationTests.cs
**Location:** `tests/NT8.Core.Tests/Analytics/OptimizationTests.cs`

**Minimum:** 12 tests

---

### Task F6: Phase5IntegrationTests.cs
**Location:** `tests/NT8.Integration.Tests/Phase5IntegrationTests.cs`

**Minimum:** 10 tests

---

## Phase G: Verification (30 minutes)

### Task G1: Build Verification
**Command:** `.\verify-build.bat`

---

### Task G2: Documentation
- Create Phase5_Completion_Report.md
- Update API_REFERENCE.md
- Add analytics examples

---

## Success Criteria

### Code Quality
- ✅ C# 5.0 syntax only
- ✅ Thread-safe
- ✅ XML docs
- ✅ No breaking changes

### Testing
- ✅ >180 total tests passing
- ✅ >80% coverage
- ✅ All analytics scenarios tested

### Functionality
- ✅ Trade recording works
- ✅ Performance metrics accurate
- ✅ Attribution functional
- ✅ Reports generate correctly
- ✅ Optimization tools operational

---

## File Creation Checklist

### New Files (17):
**Analytics (13):**
- [ ] `src/NT8.Core/Analytics/AnalyticsModels.cs`
- [ ] `src/NT8.Core/Analytics/TradeRecorder.cs`
- [ ] `src/NT8.Core/Analytics/PerformanceCalculator.cs`
- [ ] `src/NT8.Core/Analytics/AttributionModels.cs`
- [ ] `src/NT8.Core/Analytics/PnLAttributor.cs`
- [ ] `src/NT8.Core/Analytics/DrawdownAnalyzer.cs`
- [ ] `src/NT8.Core/Analytics/GradePerformanceAnalyzer.cs`
- [ ] `src/NT8.Core/Analytics/RegimePerformanceAnalyzer.cs`
- [ ] `src/NT8.Core/Analytics/ConfluenceValidator.cs`
- [ ] `src/NT8.Core/Analytics/ReportModels.cs`
- [ ] `src/NT8.Core/Analytics/ReportGenerator.cs`
- [ ] `src/NT8.Core/Analytics/TradeBlotter.cs`
- [ ] `src/NT8.Core/Analytics/ParameterOptimizer.cs`
- [ ] `src/NT8.Core/Analytics/MonteCarloSimulator.cs`
- [ ] `src/NT8.Core/Analytics/PortfolioOptimizer.cs`

**Tests (6):**
- [ ] `tests/NT8.Core.Tests/Analytics/TradeRecorderTests.cs`
- [ ] `tests/NT8.Core.Tests/Analytics/PerformanceCalculatorTests.cs`
- [ ] `tests/NT8.Core.Tests/Analytics/PnLAttributorTests.cs`
- [ ] `tests/NT8.Core.Tests/Analytics/GradePerformanceAnalyzerTests.cs`
- [ ] `tests/NT8.Core.Tests/Analytics/OptimizationTests.cs`
- [ ] `tests/NT8.Integration.Tests/Phase5IntegrationTests.cs`

**Total:** 19 new files

---

## Estimated Timeline

| Phase | Tasks | Time | Cumulative |
|-------|-------|------|------------|
| **A** | Trade Analytics | 45 min | 0:45 |
| **B** | P&L Attribution | 60 min | 1:45 |
| **C** | Grade/Regime Analysis | 60 min | 2:45 |
| **D** | Reporting | 45 min | 3:30 |
| **E** | Optimization | 60 min | 4:30 |
| **F** | Testing | 60 min | 5:30 |
| **G** | Verification | 30 min | 6:00 |

**Total:** 6 hours (budget 3-4 hours for Kilocode efficiency)

---

## Ready to Start?

**Paste into Kilocode Code Mode:**

```
I'm ready to implement Phase 5: Analytics & Reporting.

Follow Phase5_Implementation_Guide.md starting with Phase A, Task A1.

CRITICAL REQUIREMENTS:
- C# 5.0 syntax ONLY
- Thread-safe with locks on shared state
- XML docs on all public members
- NO interface modifications
- NO breaking changes to Phase 1-4

File Creation Permissions:
✅ CREATE in: src/NT8.Core/Analytics/
✅ CREATE in: tests/NT8.Core.Tests/Analytics/
❌ FORBIDDEN: Any interface files, Phase 1-4 core implementations

Start with Task A1: Create AnalyticsModels.cs in src/NT8.Core/Analytics/

After each file:
1. Build (Ctrl+Shift+B)
2. Verify zero errors
3. Continue to next task

Let's begin with AnalyticsModels.cs!
```

---

**Phase 5 will complete your analytics layer!** 📊
