File size: 38,857 Bytes
b54319d
 
5a7ab6f
b54319d
 
 
5a7ab6f
b54319d
 
5a7ab6f
b54319d
 
5a7ab6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b54319d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5a7ab6f
 
 
b54319d
 
 
 
 
 
 
 
 
 
5a7ab6f
b54319d
 
5a7ab6f
 
 
b54319d
 
 
 
 
 
 
5a7ab6f
b54319d
 
5a7ab6f
b54319d
 
5a7ab6f
 
 
b54319d
 
 
 
 
 
 
 
 
 
5a7ab6f
b54319d
 
5a7ab6f
 
 
b54319d
 
 
 
 
 
 
 
 
 
 
 
 
5a7ab6f
 
 
b54319d
 
 
 
 
 
 
 
 
 
6a4889b
b54319d
 
 
 
 
 
 
 
 
5a7ab6f
 
 
 
 
 
 
 
612e654
5a7ab6f
 
b54319d
 
 
 
 
 
 
 
612e654
b54319d
 
 
 
 
 
 
 
 
 
612e654
b54319d
 
 
 
 
 
 
 
 
 
612e654
b54319d
 
 
 
 
 
 
 
 
 
612e654
b54319d
 
 
 
 
 
 
 
 
 
612e654
b54319d
 
 
 
 
 
5a7ab6f
b54319d
 
 
612e654
b54319d
 
 
 
 
 
5a7ab6f
b54319d
 
 
612e654
b54319d
 
 
5a7ab6f
 
 
 
 
 
 
 
 
 
 
 
 
6a4889b
5a7ab6f
 
 
 
 
 
 
 
 
 
 
 
b54319d
5a7ab6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b54319d
5a7ab6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b54319d
5a7ab6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b54319d
5a7ab6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b54319d
5a7ab6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b54319d
5a7ab6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b54319d
5a7ab6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b54319d
5a7ab6f
 
 
 
 
 
 
 
 
6a4889b
5a7ab6f
 
 
 
 
 
 
 
 
 
 
b54319d
5a7ab6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b54319d
5a7ab6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b54319d
5a7ab6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b54319d
5a7ab6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b54319d
 
5a7ab6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b54319d
 
 
5a7ab6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b54319d
5a7ab6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b54319d
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
// frontend/src/components/InstitutionalPanel.tsx

import React, { useState, useEffect } from 'react';
import { API_BASE } from '../config';

export const InstitutionalPanel: React.FC = () => {
  const [activeSubTab, setActiveSubTab] = useState<'alphaLab' | 'optimal' | 'statArb' | 'riskParity' | 'dsr' | 'lowLatency' | 'ofi' | 'multiAsset'>('alphaLab');
  const [loading, setLoading] = useState<boolean>(false);
  const [resultData, setResultData] = useState<any>(null);
  const [alphaResearchData, setAlphaResearchData] = useState<any>(null);
  const [error, setError] = useState<string | null>(null);

  // Fetch precomputed Alpha Research Lab results on mount
  useEffect(() => {
    fetchLatestAlphaResearch();
  }, []);

  const fetchLatestAlphaResearch = async () => {
    setLoading(true);
    setError(null);
    try {
      const res = await fetch(`${API_BASE}/api/research/latest_results`);
      const data = await res.json();
      if (data.success) {
        setAlphaResearchData(data);
      } else {
        setError(data.error || 'Failed to fetch research results');
      }
    } catch (e: any) {
      setError(e.message);
    } finally {
      setLoading(false);
    }
  };

  const triggerRunAlphaExperiment = async () => {
    setLoading(true);
    setError(null);
    try {
      const res = await fetch(`${API_BASE}/api/research/run`, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ lookback_days: 20, holding_days: 5, cost_bps: 5.0, use_synthetic: true })
      });
      const data = await res.json();
      if (data.success) {
        fetchLatestAlphaResearch();
      } else {
        setError(data.error || 'Failed to trigger alpha experiment');
      }
    } catch (e: any) {
      setError(e.message);
    } finally {
      setLoading(false);
    }
  };

  // OFI Trigger
  const runOFI = async () => {
    setLoading(true);
    setError(null);
    try {
      const res = await fetch(`${API_BASE}/api/orderbook/ofi`, { method: 'POST' });
      const data = await res.json();
      if (data.success) setResultData(data.result);
      else setError(data.error || 'Failed to compute OFI');
    } catch (e: any) { setError(e.message); } finally { setLoading(false); }
  };

  // Multi-Asset Backtest Trigger
  const runMultiAsset = async () => {
    setLoading(true);
    setError(null);
    try {
      const res = await fetch(`${API_BASE}/api/portfolio/backtest-multi`, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ tickers: ['AAPL', 'MSFT', 'TSLA', 'NVDA', 'AMZN', 'GOOGL', 'META'], period: '1y', top_n: 3 })
      });
      const data = await res.json();
      if (data.success) setResultData(data.result);
      else setError(data.error || 'Failed to run multi-asset backtest');
    } catch (e: any) { setError(e.message); } finally { setLoading(false); }
  };

  // 1. Almgren-Chriss Optimal Execution Demo Trigger
  const runOptimalExecution = async () => {
    setLoading(true);
    setError(null);
    try {
      const res = await fetch(`${API_BASE}/api/optimal-execution/simulate`, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({
          total_shares: 100000,
          num_intervals: 10,
          daily_volatility: 0.02,
          avg_daily_volume: 5000000.0,
          risk_aversion_lambda: 0.00001,
          current_price: 150.0
        })
      });
      const data = await res.json();
      if (data.success) setResultData(data);
      else setError(data.error || 'Failed to simulate optimal execution');
    } catch (e: any) { setError(e.message); } finally { setLoading(false); }
  };

  // 2. StatArb Pairs Trading Trigger
  const runStatArb = async () => {
    setLoading(true);
    setError(null);
    try {
      const res = await fetch(`${API_BASE}/api/stat-arb/run`, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ ticker_y: 'KO', ticker_x: 'PEP', period: '1y', z_entry: 2.0, z_exit: 0.5 })
      });
      const data = await res.json();
      if (data.success) setResultData(data.result);
      else setError(data.error || 'Failed to run stat arb');
    } catch (e: any) { setError(e.message); } finally { setLoading(false); }
  };

  // 3. Risk Parity Allocation Trigger
  const runRiskParity = async () => {
    setLoading(true);
    setError(null);
    try {
      const res = await fetch(`${API_BASE}/api/portfolio/risk-parity`, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ tickers: ['AAPL', 'MSFT', 'GOOGL', 'AMZN', 'TLT', 'GLD'], period: '1y' })
      });
      const data = await res.json();
      if (data.success) setResultData(data.result);
      else setError(data.error || 'Failed to compute Risk Parity');
    } catch (e: any) { setError(e.message); } finally { setLoading(false); }
  };

  // 4. Deflated Sharpe Ratio Trigger
  const runDSR = async () => {
    setLoading(true);
    setError(null);
    try {
      const res = await fetch(`${API_BASE}/api/metrics/dsr`, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ ticker: 'TSLA', period: '1y', num_trials: 50 })
      });
      const data = await res.json();
      if (data.success) setResultData(data.result);
      else setError(data.error || 'Failed to calculate DSR');
    } catch (e: any) { setError(e.message); } finally { setLoading(false); }
  };

  // 5. Zero-Allocation Memory Profiling Trigger
  const runLowLatencyBench = async () => {
    setLoading(true);
    setError(null);
    try {
      const res = await fetch(`${API_BASE}/api/low-latency/benchmark`, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ num_events: 500000 })
      });
      const data = await res.json();
      if (data.success) setResultData(data.result);
      else setError(data.error || 'Failed to run low latency benchmark');
    } catch (e: any) { setError(e.message); } finally { setLoading(false); }
  };

  return (
    <div className="bg-slate-900 border border-slate-800 rounded-xl p-6 shadow-2xl text-slate-100 mt-6">
      <div className="flex items-center justify-between border-b border-slate-800 pb-4 mb-6">
        <div>
          <h2 className="text-xl font-bold tracking-tight text-white flex items-center gap-2">
            <span>🏛️</span> Institutional Quantitative Architecture Terminal
          </h2>
          <p className="text-xs text-slate-400 mt-1">
            专业量化研究与策略验证终端 (Institutional Quantitative Research Terminal)
          </p>
        </div>
        <span className="px-3 py-1 rounded-full text-xs font-mono bg-indigo-500/20 text-indigo-300 border border-indigo-500/30">
          PRO QUANT ENGINE ACTIVE
        </span>
      </div>

      {/* Sub-tab Selectors */}
      <div className="flex gap-2 border-b border-slate-800 pb-3 mb-6 overflow-x-auto">
        <button
          onClick={() => { setActiveSubTab('alphaLab'); setResultData(null); }}
          className={`px-4 py-2 text-xs font-semibold rounded-lg transition-all ${
            activeSubTab === 'alphaLab'
              ? 'bg-amber-600 text-white shadow-lg shadow-amber-600/30'
              : 'bg-slate-800 text-slate-400 hover:text-white'
          }`}
        >
          🔬 样本外 Alpha 验证 (Out-of-Sample Alpha Lab)
        </button>

        <button
          onClick={() => { setActiveSubTab('optimal'); setResultData(null); }}
          className={`px-4 py-2 text-xs font-semibold rounded-lg transition-all ${
            activeSubTab === 'optimal'
              ? 'bg-indigo-600 text-white shadow-lg shadow-indigo-600/30'
              : 'bg-slate-800 text-slate-400 hover:text-white'
          }`}
        >
          📈 最优算法执行 (Almgren-Chriss Model)
        </button>

        <button
          onClick={() => { setActiveSubTab('statArb'); setResultData(null); }}
          className={`px-4 py-2 text-xs font-semibold rounded-lg transition-all ${
            activeSubTab === 'statArb'
              ? 'bg-indigo-600 text-white shadow-lg shadow-indigo-600/30'
              : 'bg-slate-800 text-slate-400 hover:text-white'
          }`}
        >
          ⚖️ 协整与统计套利 (StatArb OU)
        </button>

        <button
          onClick={() => { setActiveSubTab('riskParity'); setResultData(null); }}
          className={`px-4 py-2 text-xs font-semibold rounded-lg transition-all ${
            activeSubTab === 'riskParity'
              ? 'bg-indigo-600 text-white shadow-lg shadow-indigo-600/30'
              : 'bg-slate-800 text-slate-400 hover:text-white'
          }`}
        >
          🛡️ 风险平价组合 (Risk Parity ERC)
        </button>

        <button
          onClick={() => { setActiveSubTab('dsr'); setResultData(null); }}
          className={`px-4 py-2 text-xs font-semibold rounded-lg transition-all ${
            activeSubTab === 'dsr'
              ? 'bg-indigo-600 text-white shadow-lg shadow-indigo-600/30'
              : 'bg-slate-800 text-slate-400 hover:text-white'
          }`}
        >
          🔬 夏普比率过拟合审计 (Deflated Sharpe Ratio)
        </button>

        <button
          onClick={() => { setActiveSubTab('lowLatency'); setResultData(null); }}
          className={`px-4 py-2 text-xs font-semibold rounded-lg transition-all ${
            activeSubTab === 'lowLatency'
              ? 'bg-emerald-600 text-white shadow-lg shadow-emerald-600/30'
              : 'bg-slate-800 text-slate-400 hover:text-white'
          }`}
        >
          ⚡ C++ 内存性能分析 (Memory Profiler)
        </button>

        <button
          onClick={() => { setActiveSubTab('ofi'); setResultData(null); }}
          className={`px-4 py-2 text-xs font-semibold rounded-lg transition-all ${
            activeSubTab === 'ofi'
              ? 'bg-indigo-600 text-white shadow-lg shadow-indigo-600/30'
              : 'bg-slate-800 text-slate-400 hover:text-white'
          }`}
        >
          📊 订单流不平衡度 (Order Flow Imbalance)
        </button>

        <button
          onClick={() => { setActiveSubTab('multiAsset'); setResultData(null); }}
          className={`px-4 py-2 text-xs font-semibold rounded-lg transition-all ${
            activeSubTab === 'multiAsset'
              ? 'bg-indigo-600 text-white shadow-lg shadow-indigo-600/30'
              : 'bg-slate-800 text-slate-400 hover:text-white'
          }`}
        >
          🌐 多资产组合回测 (Multi-Asset Universe)
        </button>
      </div>

      {error && (
        <div className="mb-4 p-3 bg-red-500/10 border border-red-500/30 rounded-lg text-red-400 text-xs font-mono">
          ⚠️ {error}
        </div>
      )}

      {/* SubTab Content */}
      {activeSubTab === 'alphaLab' && (
        <div className="space-y-6">
          <div className="bg-slate-950/60 border border-amber-500/20 rounded-xl p-5">
            <div className="flex items-center justify-between">
              <div>
                <h3 className="text-sm font-semibold text-amber-300">
                  点位时间一致性与 Purged Walk-Forward Alpha 验证
                </h3>
                <p className="text-xs text-slate-400 mt-1">
                  Point-in-time universe across 38 liquid ETFs (94,040 rows OHLCV). Zero future leakage, 5d embargo, 5 bps friction.
                </p>
              </div>
              <button
                onClick={triggerRunAlphaExperiment}
                disabled={loading}
                className="px-4 py-2 bg-amber-600 hover:bg-amber-500 text-white rounded-lg text-xs font-bold transition-all shadow-lg shadow-amber-600/20 disabled:opacity-50"
              >
                {loading ? 'Running Experiment...' : '⚡ Re-Run Out-of-Sample CV'}
              </button>
            </div>

            {alphaResearchData && (
              <div className="mt-5 space-y-5">
                <div className="grid grid-cols-2 md:grid-cols-4 gap-3 font-mono text-xs">
                  <div className="bg-slate-900/80 p-3 rounded-lg border border-slate-800">
                    <span className="text-slate-500 block text-[10px]">HISTORICAL SPAN</span>
                    <span className="text-white font-bold">{alphaResearchData.trading_dates} Trading Days</span>
                  </div>
                  <div className="bg-slate-900/80 p-3 rounded-lg border border-slate-800">
                    <span className="text-slate-500 block text-[10px]">UNIVERSE SIZE</span>
                    <span className="text-white font-bold">{alphaResearchData.universe_size} Liquid ETFs</span>
                  </div>
                  <div className="bg-slate-900/80 p-3 rounded-lg border border-slate-800">
                    <span className="text-slate-500 block text-[10px]">VALIDATION SCHEME</span>
                    <span className="text-amber-400 font-bold">Purged Walk-Forward (5d Embargo)</span>
                  </div>
                  <div className="bg-slate-900/80 p-3 rounded-lg border border-slate-800">
                    <span className="text-slate-500 block text-[10px]">TRANSACTION FRICTION</span>
                    <span className="text-emerald-400 font-bold">{alphaResearchData.cost_bps} bps</span>
                  </div>
                </div>

                {/* Model Suite Comparison Table */}
                <div>
                  <h4 className="text-xs font-bold text-slate-300 uppercase tracking-wider mb-2">
                    Model Hierarchy Out-of-Sample Performance Table
                  </h4>
                  <div className="overflow-x-auto border border-slate-800 rounded-lg">
                    <table className="w-full text-left text-xs font-mono">
                      <thead className="bg-slate-900 text-slate-400 uppercase text-[10px]">
                        <tr>
                          <th className="py-2.5 px-3">Model Name</th>
                          <th className="py-2.5 px-3">Rank IC</th>
                          <th className="py-2.5 px-3">Net Sharpe</th>
                          <th className="py-2.5 px-3">Max Drawdown</th>
                          <th className="py-2.5 px-3">Turnover</th>
                          <th className="py-2.5 px-3">Deflated Sharpe (DSR)</th>
                        </tr>
                      </thead>
                      <tbody className="divide-y divide-slate-800/60 bg-slate-950/40">
                        {alphaResearchData.results.map((m: any, idx: number) => (
                          <tr key={idx} className="hover:bg-slate-900/50 transition-colors">
                            <td className="py-2.5 px-3 font-bold text-slate-200">
                              {m.model_name}
                              <span className="block text-[10px] text-slate-500 font-normal">{m.description}</span>
                            </td>
                            <td className={`py-2.5 px-3 ${m.rank_ic >= 0 ? 'text-emerald-400' : 'text-rose-400'}`}>
                              {m.rank_ic.toFixed(4)}
                            </td>
                            <td className={`py-2.5 px-3 font-bold ${m.net_sharpe >= 0.5 ? 'text-emerald-400' : m.net_sharpe >= 0 ? 'text-amber-400' : 'text-rose-400'}`}>
                              {m.net_sharpe.toFixed(2)}
                            </td>
                            <td className="py-2.5 px-3 text-rose-400">
                              {(m.max_drawdown * 100).toFixed(1)}%
                            </td>
                            <td className="py-2.5 px-3 text-slate-300">
                              {(m.turnover * 100).toFixed(1)}%
                            </td>
                            <td className="py-2.5 px-3 text-indigo-300 font-bold">
                              {m.dsr.toFixed(2)}
                            </td>
                          </tr>
                        ))}
                      </tbody>
                    </table>
                  </div>
                </div>

                {/* Feature Drift PSI Audit */}
                <div>
                  <h4 className="text-xs font-bold text-slate-300 uppercase tracking-wider mb-2">
                    Population Stability Index (PSI) Feature Drift Audit
                  </h4>
                  <div className="grid grid-cols-2 md:grid-cols-5 gap-2 font-mono text-xs">
                    {alphaResearchData.drift_audit.map((d: any, idx: number) => (
                      <div key={idx} className="bg-slate-900 p-2.5 rounded-lg border border-slate-800 flex items-center justify-between">
                        <div>
                          <span className="text-[10px] text-slate-400 block">{d.feature}</span>
                          <span className="font-bold text-slate-200">PSI: {d.psi.toFixed(4)}</span>
                        </div>
                        <span className="px-2 py-0.5 rounded text-[9px] font-bold bg-emerald-500/20 text-emerald-300 border border-emerald-500/30">
                          {d.status}
                        </span>
                      </div>
                    ))}
                  </div>
                </div>
              </div>
            )}
          </div>
        </div>
      )}

      {/* Subtab 1: Almgren-Chriss Execution */}
      {activeSubTab === 'optimal' && (
        <div className="space-y-4">
          <div className="bg-slate-950/60 border border-slate-800 rounded-xl p-5">
            <div className="flex items-center justify-between">
              <div>
                <h3 className="text-sm font-semibold text-indigo-300">Almgren-Chriss Optimal Execution Simulator</h3>
                <p className="text-xs text-slate-400 mt-1">
                  Solves for the efficient execution frontier by balancing Permanent & Temporary Market Impact against Volatility Risk ($\lambda$).
                </p>
              </div>
              <button
                onClick={runOptimalExecution}
                disabled={loading}
                className="px-4 py-2 bg-indigo-600 hover:bg-indigo-500 text-white rounded-lg text-xs font-semibold transition-all shadow-lg shadow-indigo-600/20 disabled:opacity-50"
              >
                {loading ? 'Simulating...' : 'Run Almgren-Chriss Engine'}
              </button>
            </div>

            {resultData && (
              <div className="mt-5 space-y-4 font-mono text-xs">
                <div className="grid grid-cols-3 gap-3">
                  <div className="bg-slate-900 p-3 rounded-lg border border-slate-800">
                    <span className="text-slate-500 block text-[10px]">TOTAL SHARES</span>
                    <span className="text-white font-bold">{resultData.parameters?.total_shares?.toLocaleString()}</span>
                  </div>
                  <div className="bg-slate-900 p-3 rounded-lg border border-slate-800">
                    <span className="text-slate-500 block text-[10px]">TOTAL EXPECTED COST</span>
                    <span className="text-amber-400 font-bold">${resultData.total_expected_cost?.toFixed(2)}</span>
                  </div>
                  <div className="bg-slate-900 p-3 rounded-lg border border-slate-800">
                    <span className="text-slate-500 block text-[10px]">RISK VARIANCE</span>
                    <span className="text-indigo-400 font-bold">{resultData.total_variance?.toFixed(4)}</span>
                  </div>
                </div>

                <div className="overflow-x-auto border border-slate-800 rounded-lg">
                  <table className="w-full text-left text-xs">
                    <thead className="bg-slate-900 text-slate-400 uppercase text-[10px]">
                      <tr>
                        <th className="py-2 px-3">Interval</th>
                        <th className="py-2 px-3">Trade Shares</th>
                        <th className="py-2 px-3">Remaining Inventory</th>
                        <th className="py-2 px-3">Temp Impact ($)</th>
                      </tr>
                    </thead>
                    <tbody className="divide-y divide-slate-800/60">
                      {resultData.trajectory?.map((row: any) => (
                        <tr key={row.interval}>
                          <td className="py-2 px-3 font-semibold text-indigo-300">T+{row.interval}</td>
                          <td className="py-2 px-3 text-emerald-400 font-bold">{row.trade_shares?.toLocaleString()}</td>
                          <td className="py-2 px-3 text-slate-300">{row.remaining_inventory?.toLocaleString()}</td>
                          <td className="py-2 px-3 text-rose-400">${row.temp_impact?.toFixed(4)}</td>
                        </tr>
                      ))}
                    </tbody>
                  </table>
                </div>
              </div>
            )}
          </div>
        </div>
      )}

      {/* Subtab 2: StatArb Pairs */}
      {activeSubTab === 'statArb' && (
        <div className="space-y-4">
          <div className="bg-slate-950/60 border border-slate-800 rounded-xl p-5">
            <div className="flex items-center justify-between">
              <div>
                <h3 className="text-sm font-semibold text-indigo-300">Statistical Arbitrage & Cointegration Engine</h3>
                <p className="text-xs text-slate-400 mt-1">
                  Engle-Granger Cointegration Test + Ornstein-Uhlenbeck (OU) Mean Reversion Half-Life Estimation ($t_{1/2}$).
                </p>
              </div>
              <button
                onClick={runStatArb}
                disabled={loading}
                className="px-4 py-2 bg-indigo-600 hover:bg-indigo-500 text-white rounded-lg text-xs font-semibold transition-all shadow-lg shadow-indigo-600/20 disabled:opacity-50"
              >
                {loading ? 'Fitting OU Process...' : 'Run Pair Test (KO / PEP)'}
              </button>
            </div>

            {resultData && (
              <div className="mt-5 space-y-4 font-mono text-xs">
                <div className="grid grid-cols-2 md:grid-cols-4 gap-3">
                  <div className="bg-slate-900 p-3 rounded-lg border border-slate-800">
                    <span className="text-slate-500 block text-[10px]">HEDGE RATIO (BETA)</span>
                    <span className="text-white font-bold">{resultData.hedge_ratio?.toFixed(4)}</span>
                  </div>
                  <div className="bg-slate-900 p-3 rounded-lg border border-slate-800">
                    <span className="text-slate-500 block text-[10px]">ADF P-VALUE</span>
                    <span className={`font-bold ${resultData.is_cointegrated ? 'text-emerald-400' : 'text-rose-400'}`}>
                      {resultData.p_value?.toFixed(4)} {resultData.is_cointegrated ? '(COINTEGRATED)' : '(NOT COINT)'}
                    </span>
                  </div>
                  <div className="bg-slate-900 p-3 rounded-lg border border-slate-800">
                    <span className="text-slate-500 block text-[10px]">OU HALF-LIFE (DAYS)</span>
                    <span className="text-amber-400 font-bold">{resultData.half_life_days?.toFixed(2)} Days</span>
                  </div>
                  <div className="bg-slate-900 p-3 rounded-lg border border-slate-800">
                    <span className="text-slate-500 block text-[10px]">TOTAL TRADES GENERATED</span>
                    <span className="text-indigo-300 font-bold">{resultData.total_trades}</span>
                  </div>
                </div>

                <div className="p-3 bg-slate-900 rounded-lg border border-slate-800">
                  <div className="flex justify-between text-xs mb-1">
                    <span className="text-slate-400">Current Spread Z-Score:</span>
                    <span className="text-emerald-400 font-bold">{resultData.current_z_score?.toFixed(2)}</span>
                  </div>
                  <div className="w-full bg-slate-800 h-2 rounded-full overflow-hidden flex">
                    <div className="bg-rose-500 h-full" style={{ width: '25%' }} title="-2.0 Entry"></div>
                    <div className="bg-slate-700 h-full" style={{ width: '50%' }}></div>
                    <div className="bg-emerald-500 h-full" style={{ width: '25%' }} title="+2.0 Entry"></div>
                  </div>
                </div>
              </div>
            )}
          </div>
        </div>
      )}

      {/* Subtab 3: Risk Parity */}
      {activeSubTab === 'riskParity' && (
        <div className="space-y-4">
          <div className="bg-slate-950/60 border border-slate-800 rounded-xl p-5">
            <div className="flex items-center justify-between">
              <div>
                <h3 className="text-sm font-semibold text-indigo-300">Risk Parity & ERC Portfolio Optimization</h3>
                <p className="text-xs text-slate-400 mt-1">
                  Equal Risk Contribution (ERC) portfolio weights with Ledoit-Wolf Shrinkage Covariance estimation.
                </p>
              </div>
              <button
                onClick={runRiskParity}
                disabled={loading}
                className="px-4 py-2 bg-indigo-600 hover:bg-indigo-500 text-white rounded-lg text-xs font-semibold transition-all shadow-lg shadow-indigo-600/20 disabled:opacity-50"
              >
                {loading ? 'Optimizing ERC...' : 'Run Risk Parity Optimizer'}
              </button>
            </div>

            {resultData && (
              <div className="mt-5 space-y-4 font-mono text-xs">
                <div className="grid grid-cols-2 gap-3">
                  <div className="bg-slate-900 p-3 rounded-lg border border-slate-800">
                    <span className="text-slate-500 block text-[10px]">PORTFOLIO ANN RETURN</span>
                    <span className="text-emerald-400 font-bold">{(resultData.annualized_return * 100).toFixed(2)}%</span>
                  </div>
                  <div className="bg-slate-900 p-3 rounded-lg border border-slate-800">
                    <span className="text-slate-500 block text-[10px]">PORTFOLIO ANN VOLATILITY</span>
                    <span className="text-indigo-400 font-bold">{(resultData.annualized_volatility * 100).toFixed(2)}%</span>
                  </div>
                </div>

                <div className="overflow-x-auto border border-slate-800 rounded-lg">
                  <table className="w-full text-left text-xs">
                    <thead className="bg-slate-900 text-slate-400 uppercase text-[10px]">
                      <tr>
                        <th className="py-2 px-3">Asset Ticker</th>
                        <th className="py-2 px-3">ERC Weight (%)</th>
                        <th className="py-2 px-3">Risk Contribution (%)</th>
                      </tr>
                    </thead>
                    <tbody className="divide-y divide-slate-800/60">
                      {Object.keys(resultData.weights || {}).map((ticker) => (
                        <tr key={ticker}>
                          <td className="py-2 px-3 font-bold text-white">{ticker}</td>
                          <td className="py-2 px-3 text-emerald-400">{(resultData.weights[ticker] * 100).toFixed(2)}%</td>
                          <td className="py-2 px-3 text-indigo-300">{(resultData.risk_contributions[ticker] * 100).toFixed(2)}%</td>
                        </tr>
                      ))}
                    </tbody>
                  </table>
                </div>
              </div>
            )}
          </div>
        </div>
      )}

      {/* Subtab 4: Deflated Sharpe Ratio */}
      {activeSubTab === 'dsr' && (
        <div className="space-y-4">
          <div className="bg-slate-950/60 border border-slate-800 rounded-xl p-5">
            <div className="flex items-center justify-between">
              <div>
                <h3 className="text-sm font-semibold text-indigo-300">Deflated Sharpe Ratio (DSR) 夏普比率衰减与过拟合审计</h3>
                <p className="text-xs text-slate-400 mt-1">
                  Adjusts observed Sharpe Ratio for non-normal returns, skewness, kurtosis, and multiple testing trial count ($N$).
                </p>
              </div>
              <button
                onClick={runDSR}
                disabled={loading}
                className="px-4 py-2 bg-indigo-600 hover:bg-indigo-500 text-white rounded-lg text-xs font-semibold transition-all shadow-lg shadow-indigo-600/20 disabled:opacity-50"
              >
                {loading ? 'Auditing DSR...' : 'Audit DSR (50 Trials)'}
              </button>
            </div>

            {resultData && (
              <div className="mt-5 space-y-4 font-mono text-xs">
                <div className="grid grid-cols-2 md:grid-cols-4 gap-3">
                  <div className="bg-slate-900 p-3 rounded-lg border border-slate-800">
                    <span className="text-slate-500 block text-[10px]">OBSERVED SHARPE</span>
                    <span className="text-white font-bold">{resultData.observed_sharpe?.toFixed(2)}</span>
                  </div>
                  <div className="bg-slate-900 p-3 rounded-lg border border-slate-800">
                    <span className="text-slate-500 block text-[10px]">MIN BENCHMARK SHARPE</span>
                    <span className="text-amber-400 font-bold">{resultData.benchmark_sharpe?.toFixed(2)}</span>
                  </div>
                  <div className="bg-slate-900 p-3 rounded-lg border border-slate-800">
                    <span className="text-slate-500 block text-[10px]">DEFLATED SHARPE PROBABILITY</span>
                    <span className={`font-bold ${resultData.dsr_probability >= 0.95 ? 'text-emerald-400' : 'text-rose-400'}`}>
                      {(resultData.dsr_probability * 100).toFixed(1)}%
                    </span>
                  </div>
                  <div className="bg-slate-900 p-3 rounded-lg border border-slate-800">
                    <span className="text-slate-500 block text-[10px]">OVERFITTING VERDICT</span>
                    <span className={`font-bold ${resultData.is_statistically_significant ? 'text-emerald-400' : 'text-rose-400'}`}>
                      {resultData.is_statistically_significant ? 'PASSED (GENUINE)' : 'FAILED (OVERFITTED)'}
                    </span>
                  </div>
                </div>
              </div>
            )}
          </div>
        </div>
      )}

      {/* Subtab 5: Low Latency Memory Profiler */}
      {activeSubTab === 'lowLatency' && (
        <div className="space-y-4">
          <div className="bg-slate-950/60 border border-slate-800 rounded-xl p-5">
            <div className="flex items-center justify-between">
              <div>
                <h3 className="text-sm font-semibold text-emerald-300">Zero-Allocation Freelist Memory Profiler</h3>
                <p className="text-xs text-slate-400 mt-1">
                  Benchmarks custom C++/Python zero-allocation freelist memory pool against standard heap allocation across 500,000 order events.
                </p>
              </div>
              <button
                onClick={runLowLatencyBench}
                disabled={loading}
                className="px-4 py-2 bg-emerald-600 hover:bg-emerald-500 text-white rounded-lg text-xs font-semibold transition-all shadow-lg shadow-emerald-600/20 disabled:opacity-50"
              >
                {loading ? 'Profiling 500k Events...' : 'Run Memory Benchmark'}
              </button>
            </div>

            {resultData && (
              <div className="mt-5 space-y-4 font-mono text-xs">
                <div className="grid grid-cols-2 md:grid-cols-4 gap-3">
                  <div className="bg-slate-900 p-3 rounded-lg border border-slate-800">
                    <span className="text-slate-500 block text-[10px]">FREELIST EXEC TIME</span>
                    <span className="text-emerald-400 font-bold">{resultData.freelist_execution_time_sec?.toFixed(4)}s</span>
                  </div>
                  <div className="bg-slate-900 p-3 rounded-lg border border-slate-800">
                    <span className="text-slate-500 block text-[10px]">STANDARD HEAP TIME</span>
                    <span className="text-rose-400 font-bold">{resultData.standard_heap_execution_time_sec?.toFixed(4)}s</span>
                  </div>
                  <div className="bg-slate-900 p-3 rounded-lg border border-slate-800">
                    <span className="text-slate-500 block text-[10px]">SPEEDUP FACTOR</span>
                    <span className="text-amber-300 font-bold">{resultData.speedup_factor?.toFixed(2)}x Faster</span>
                  </div>
                  <div className="bg-slate-900 p-3 rounded-lg border border-slate-800">
                    <span className="text-slate-500 block text-[10px]">FREELIST ALLOCATIONS</span>
                    <span className="text-emerald-300 font-bold">{resultData.freelist_allocations_count} (ZERO GC)</span>
                  </div>
                </div>
              </div>
            )}
          </div>
        </div>
      )}

      {/* Subtab 6: OFI */}
      {activeSubTab === 'ofi' && (
        <div className="space-y-4">
          <div className="bg-slate-950/60 border border-slate-800 rounded-xl p-5">
            <div className="flex items-center justify-between">
              <div>
                <h3 className="text-sm font-semibold text-indigo-300">Level-2 Order Flow Imbalance (OFI) & Micro-Price</h3>
                <p className="text-xs text-slate-400 mt-1">
                  Calculates Order Flow Imbalance (OFI) and Micro-Price (P_micro) for HFT lead-lag signals.
                </p>
              </div>
              <button
                onClick={runOFI}
                disabled={loading}
                className="px-4 py-2 bg-indigo-600 hover:bg-indigo-500 text-white rounded-lg text-xs font-semibold transition-all shadow-lg shadow-indigo-600/20 disabled:opacity-50"
              >
                {loading ? 'Computing OFI...' : 'Run OFI Signal Test'}
              </button>
            </div>

            {resultData && (
              <div className="mt-5 space-y-4 font-mono text-xs">
                <div className="overflow-x-auto border border-slate-800 rounded-lg">
                  <table className="w-full text-left text-xs">
                    <thead className="bg-slate-900 text-slate-400 uppercase text-[10px]">
                      <tr>
                        <th className="py-2 px-3">Bid Price</th>
                        <th className="py-2 px-3">Bid Vol</th>
                        <th className="py-2 px-3">Ask Price</th>
                        <th className="py-2 px-3">Ask Vol</th>
                        <th className="py-2 px-3">OFI Signal</th>
                        <th className="py-2 px-3">Micro-Price</th>
                      </tr>
                    </thead>
                    <tbody className="divide-y divide-slate-800/60">
                      {resultData.map((row: any, idx: number) => (
                        <tr key={idx}>
                          <td className="py-2 px-3 text-emerald-400 font-bold">${row.bid_price}</td>
                          <td className="py-2 px-3 text-slate-300">{row.bid_vol}</td>
                          <td className="py-2 px-3 text-rose-400 font-bold">${row.ask_price}</td>
                          <td className="py-2 px-3 text-slate-300">{row.ask_vol}</td>
                          <td className={`py-2 px-3 font-bold ${row.ofi > 0 ? 'text-emerald-400' : 'text-rose-400'}`}>{row.ofi}</td>
                          <td className="py-2 px-3 text-amber-300">${row.micro_price?.toFixed(2)}</td>
                        </tr>
                      ))}
                    </tbody>
                  </table>
                </div>
              </div>
            )}
          </div>
        </div>
      )}

      {/* Subtab 7: Multi-Asset */}
      {activeSubTab === 'multiAsset' && (
        <div className="space-y-4">
          <div className="bg-slate-950/60 border border-slate-800 rounded-xl p-5">
            <div className="flex items-center justify-between">
              <div>
                <h3 className="text-sm font-semibold text-indigo-300">Multi-Asset Stock Pool Backtest Engine</h3>
                <p className="text-xs text-slate-400 mt-1">
                  Vectorized multi-asset stock pool simulator with daily alpha ranking, sector concentration caps & position limits.
                </p>
              </div>
              <button
                onClick={runMultiAsset}
                disabled={loading}
                className="px-4 py-2 bg-indigo-600 hover:bg-indigo-500 text-white rounded-lg text-xs font-semibold transition-all shadow-lg shadow-indigo-600/20 disabled:opacity-50"
              >
                {loading ? 'Simulating Stock Pool...' : 'Run Pool Simulation (7 Stocks)'}
              </button>
            </div>

            {resultData && (
              <div className="mt-5 space-y-4 font-mono text-xs">
                <div className="grid grid-cols-2 md:grid-cols-4 gap-3">
                  <div className="bg-slate-900 p-3 rounded-lg border border-slate-800">
                    <span className="text-slate-500 block text-[10px]">TOTAL RETURN</span>
                    <span className="text-emerald-400 font-bold">{(resultData.total_return * 100).toFixed(2)}%</span>
                  </div>
                  <div className="bg-slate-900 p-3 rounded-lg border border-slate-800">
                    <span className="text-slate-500 block text-[10px]">SHARPE RATIO</span>
                    <span className="text-indigo-400 font-bold">{resultData.sharpe_ratio?.toFixed(2)}</span>
                  </div>
                  <div className="bg-slate-900 p-3 rounded-lg border border-slate-800">
                    <span className="text-slate-500 block text-[10px]">MAX DRAWDOWN</span>
                    <span className="text-rose-400 font-bold">{(resultData.max_drawdown * 100).toFixed(2)}%</span>
                  </div>
                  <div className="bg-slate-900 p-3 rounded-lg border border-slate-800">
                    <span className="text-slate-500 block text-[10px]">TOTAL REBALANCES</span>
                    <span className="text-amber-300 font-bold">{resultData.total_rebalance_count}</span>
                  </div>
                </div>
              </div>
            )}
          </div>
        </div>
      )}
    </div>
  );
};