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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
"""Execution Algorithms: TWAP, VWAP, Smart Order Routing

What separates retail execution from institutional execution:
- Retail: Market orders, immediate execution, pay spread
- Institutional: TWAP/VWAP, slice orders across time, minimize market impact

Market impact model: Price moves against you proportional to order size / daily volume
"""
import numpy as np
import pandas as pd
from typing import Dict, List, Optional, Tuple
from dataclasses import dataclass
import warnings
warnings.filterwarnings('ignore')


@dataclass
class Order:
    """Single order specification"""
    symbol: str
    side: str  # 'buy' or 'sell'
    quantity: int
    order_type: str  # 'market', 'limit', 'twap', 'vwap'
    limit_price: Optional[float] = None
    
    def __post_init__(self):
        self.side = self.side.lower()
        self.order_type = self.order_type.lower()


class MarketImpactModel:
    """
    Square-root market impact model (Almgren-Chriss, 1999).
    
    Market impact = σ * sqrt(Q / V)
    Where:
    - σ = daily volatility
    - Q = order quantity
    - V = daily volume
    
    Temporary impact: decays within minutes
    Permanent impact: persists
    """
    
    def __init__(self, 
                 temp_impact_coef: float = 0.5,
                 perm_impact_coef: float = 0.1,
                 decay_halflife: int = 10):
        self.temp_impact_coef = temp_impact_coef
        self.perm_impact_coef = perm_impact_coef
        self.decay_halflife = decay_halflife
    
    def temporary_impact(self, order_size: int, daily_volume: int,
                         volatility: float) -> float:
        """Temporary price impact (bps)"""
        participation = order_size / max(daily_volume, 1)
        return self.temp_impact_coef * volatility * np.sqrt(participation)
    
    def permanent_impact(self, order_size: int, daily_volume: int,
                         volatility: float) -> float:
        """Permanent price impact (bps)"""
        participation = order_size / max(daily_volume, 1)
        return self.perm_impact_coef * volatility * participation


class TWAPScheduler:
    """
    Time-Weighted Average Price execution.
    
    Slices parent order into N child orders, equally distributed in time.
    
    When to use: When you want to minimize timing risk and have no view
    on intraday price direction. Simple, predictable, low market impact.
    
    Formula: Child qty = Total qty / N buckets
    """
    
    def __init__(self, 
                 n_buckets: int = 20,
                 bucket_duration_minutes: int = 15):
        self.n_buckets = n_buckets
        self.bucket_duration = bucket_duration_minutes
    
    def schedule(self, order: Order, 
                 start_time: pd.Timestamp,
                 end_time: Optional[pd.Timestamp] = None) -> pd.DataFrame:
        """
        Create TWAP execution schedule.
        
        Returns DataFrame with bucket_start, bucket_end, target_qty
        """
        if end_time is None:
            end_time = start_time + pd.Timedelta(
                minutes=self.n_buckets * self.bucket_duration
            )
        
        # Time buckets
        buckets = pd.date_range(
            start=start_time, 
            end=end_time,
            periods=self.n_buckets + 1
        )
        
        # Equal quantity per bucket
        qty_per_bucket = order.quantity // self.n_buckets
        remainder = order.quantity % self.n_buckets
        
        quantities = [qty_per_bucket] * self.n_buckets
        # Add remainder to first buckets
        for i in range(remainder):
            quantities[i] += 1
        
        schedule = pd.DataFrame({
            'bucket_start': buckets[:-1],
            'bucket_end': buckets[1:],
            'target_qty': quantities,
            'fraction': 1.0 / self.n_buckets,
            'algorithm': 'TWAP',
            'symbol': order.symbol,
            'side': order.side
        })
        
        return schedule
    
    def execute(self, schedule: pd.DataFrame, 
                market_prices: pd.Series,
                impact_model: Optional[MarketImpactModel] = None,
                daily_volume: int = 1000000,
                volatility: float = 0.02) -> Dict:
        """
        Simulate TWAP execution with market impact.
        
        Returns execution statistics.
        """
        if impact_model is None:
            impact_model = MarketImpactModel()
        
        executed_qty = 0
        total_cost = 0
        prices = []
        impacts = []
        
        for _, row in schedule.iterrows():
            qty = row['target_qty']
            
            # Get price at bucket start (approximation)
            mask = market_prices.index >= row['bucket_start']
            if mask.any():
                price = market_prices[mask].iloc[0]
            else:
                price = market_prices.iloc[-1]
            
            # Market impact
            impact_bps = impact_model.temporary_impact(
                qty, daily_volume, volatility
            )
            impact_price = price * (1 + impact_bps / 10000)
            
            # Cost
            cost = qty * impact_price
            total_cost += cost
            executed_qty += qty
            
            prices.append(price)
            impacts.append(impact_bps)
        
        # VWAP benchmark
        vwap = total_cost / executed_qty if executed_qty > 0 else 0
        
        # Metrics
        avg_impact = np.mean(impacts)
        max_impact = np.max(impacts)
        
        return {
            'algorithm': 'TWAP',
            'total_qty': executed_qty,
            'total_cost': total_cost,
            'avg_price': vwap,
            'avg_impact_bps': avg_impact,
            'max_impact_bps': max_impact,
            'slippage_bps': avg_impact,
            'n_child_orders': len(schedule)
        }


class VWAPScheduler:
    """
    Volume-Weighted Average Price execution.
    
    Slices parent order proportionally to historical volume profile.
    Executes more in high-volume periods (typically open, close, mid-day lull).
    
    When to use: When you want to match the market VWAP.
    Institutional benchmark: Did my execution VWAP match the market VWAP?
    
    Formula: Child qty_i = Total qty * (Volume_i / Total_Volume)
    """
    
    def __init__(self,
                 n_buckets: int = 20,
                 default_profile: Optional[Dict[int, float]] = None):
        self.n_buckets = n_buckets
        
        # Default intraday volume profile (U-shape: high at open/close)
        if default_profile is None:
            # Hour of day -> volume fraction (simplified)
            self.default_profile = {
                9: 0.08,   # 9-10 AM: High
                10: 0.06,
                11: 0.05,
                12: 0.04,  # Mid-day lull
                13: 0.04,
                14: 0.05,
                15: 0.07,
                16: 0.10,  # 3-4 PM: High (close)
            }
        else:
            self.default_profile = default_profile
    
    def estimate_volume_profile(self, 
                                trade_data: pd.DataFrame,
                                bucket_size: str = '30min') -> pd.Series:
        """
        Estimate intraday volume profile from historical trade data.
        
        trade_data columns: timestamp, volume
        """
        trade_data = trade_data.copy()
        trade_data['time'] = pd.to_datetime(trade_data.index).time
        
        # Resample
        profile = trade_data.resample(bucket_size)['volume'].mean()
        
        # Normalize to fractions
        profile = profile / profile.sum()
        
        return profile
    
    def schedule(self, order: Order,
                 start_time: pd.Timestamp,
                 end_time: Optional[pd.Timestamp] = None,
                 volume_profile: Optional[pd.Series] = None) -> pd.DataFrame:
        """Create VWAP execution schedule"""
        if end_time is None:
            end_time = start_time + pd.Timedelta(hours=6)
        
        # Generate time buckets
        n_buckets = self.n_buckets
        buckets = pd.date_range(start=start_time, end=end_time, periods=n_buckets + 1)
        
        # Get volume fractions for each bucket
        if volume_profile is not None:
            # Map buckets to volume profile
            fractions = []
            for i in range(n_buckets):
                bucket_start = buckets[i]
                hour = bucket_start.hour
                frac = volume_profile.get(hour, 1.0 / n_buckets)
                fractions.append(frac)
            
            # Normalize
            fractions = np.array(fractions)
            fractions = fractions / fractions.sum()
        else:
            fractions = np.ones(n_buckets) / n_buckets
        
        # Allocate quantities
        quantities = (fractions * order.quantity).astype(int)
        
        # Handle rounding
        remainder = order.quantity - quantities.sum()
        quantities[0] += remainder
        
        schedule = pd.DataFrame({
            'bucket_start': buckets[:-1],
            'bucket_end': buckets[1:],
            'target_qty': quantities,
            'fraction': fractions,
            'algorithm': 'VWAP',
            'symbol': order.symbol,
            'side': order.side
        })
        
        return schedule
    
    def execute(self, schedule: pd.DataFrame,
                market_prices: pd.Series,
                market_volumes: pd.Series,
                impact_model: Optional[MarketImpactModel] = None) -> Dict:
        """Simulate VWAP execution"""
        if impact_model is None:
            impact_model = MarketImpactModel()
        
        executed_qty = 0
        total_cost = 0
        prices = []
        impacts = []
        
        for _, row in schedule.iterrows():
            qty = row['target_qty']
            if qty <= 0:
                continue
            
            mask = market_prices.index >= row['bucket_start']
            if mask.any():
                price = market_prices[mask].iloc[0]
                vol = market_volumes[mask].iloc[0] if len(market_volumes[mask]) > 0 else 1000000
            else:
                price = market_prices.iloc[-1]
                vol = 1000000
            
            # Impact proportional to participation
            impact_bps = impact_model.temporary_impact(qty, vol, 0.02)
            impact_price = price * (1 + impact_bps / 10000)
            
            cost = qty * impact_price
            total_cost += cost
            executed_qty += qty
            
            prices.append(price)
            impacts.append(impact_bps)
        
        vwap = total_cost / executed_qty if executed_qty > 0 else 0
        
        # Market VWAP (what we tried to match)
        market_vwap = (market_prices * market_volumes).sum() / market_volumes.sum()
        
        return {
            'algorithm': 'VWAP',
            'total_qty': executed_qty,
            'total_cost': total_cost,
            'avg_price': vwap,
            'market_vwap': market_vwap,
            'vwap_deviation_bps': abs(vwap - market_vwap) / market_vwap * 10000 if market_vwap > 0 else 0,
            'avg_impact_bps': np.mean(impacts) if impacts else 0,
            'n_child_orders': len(schedule)
        }


class SmartOrderRouter:
    """
    Smart Order Routing: Select optimal venue/algorithm based on order characteristics.
    
    Decision tree:
    - Small orders (< 1% ADV): Market/limit, single venue
    - Medium orders (1-10% ADV): TWAP over 1-2 hours
    - Large orders (> 10% ADV): VWAP over full day, possibly dark pools
    - Urgent: Market order, accept impact
    - Patient: TWAP/VWAP, minimize impact
    """
    
    def __init__(self, impact_model: Optional[MarketImpactModel] = None):
        self.impact_model = impact_model or MarketImpactModel()
        self.twap = TWAPScheduler()
        self.vwap = VWAPScheduler()
    
    def route_order(self, order: Order,
                    avg_daily_volume: int,
                    urgency: str = 'normal',
                    volatility: float = 0.02) -> Dict:
        """
        Route order to optimal execution strategy.
        
        Args:
            order: Order specification
            avg_daily_volume: Average daily volume of the symbol
            urgency: 'urgent', 'normal', 'patient'
            volatility: Daily volatility
        
        Returns:
            Dict with routing decision and execution schedule
        """
        participation = order.quantity / max(avg_daily_volume, 1)
        
        # Decision logic
        if urgency == 'urgent' or participation < 0.01:
            # Small or urgent: Single market/limit order
            strategy = 'market'
            expected_impact = self.impact_model.temporary_impact(
                order.quantity, avg_daily_volume, volatility
            )
            schedule = pd.DataFrame({
                'bucket_start': [pd.Timestamp.now()],
                'bucket_end': [pd.Timestamp.now()],
                'target_qty': [order.quantity],
                'fraction': [1.0],
                'algorithm': 'MARKET',
                'symbol': [order.symbol],
                'side': [order.side]
            })
        
        elif participation < 0.05:
            # Medium: TWAP over 2 hours
            strategy = 'twap'
            schedule = self.twap.schedule(
                order, pd.Timestamp.now(), 
                end_time=pd.Timestamp.now() + pd.Timedelta(hours=2)
            )
            expected_impact = self.impact_model.temporary_impact(
                order.quantity // len(schedule), avg_daily_volume, volatility
            )
        
        else:
            # Large: VWAP over full day
            strategy = 'vwap'
            schedule = self.vwap.schedule(
                order, pd.Timestamp.now(),
                end_time=pd.Timestamp.now() + pd.Timedelta(hours=6)
            )
            expected_impact = self.impact_model.temporary_impact(
                order.quantity // len(schedule), avg_daily_volume, volatility
            )
        
        return {
            'order': order,
            'strategy': strategy,
            'participation_rate': participation,
            'expected_impact_bps': expected_impact,
            'schedule': schedule,
            'urgency': urgency
        }


def benchmark_execution_algorithms():
    """Compare TWAP vs VWAP vs Market order on synthetic data"""
    np.random.seed(42)
    
    # Generate synthetic intraday data
    n_minutes = 390  # Trading day minutes (9:30 - 16:00)
    times = pd.date_range('2024-01-01 09:30', periods=n_minutes, freq='1min')
    
    # Price: random walk with slight drift
    price = 100.0
    prices = [price]
    for _ in range(n_minutes - 1):
        price *= (1 + np.random.randn() * 0.001)
        prices.append(price)
    
    # Volume: U-shaped intraday pattern
    base_vol = 1000
    hours = np.arange(n_minutes) / 60
    vol_pattern = 0.5 + 2.0 * np.exp(-((hours - 0.5) ** 2) / 0.1) + \
                  0.5 * np.sin(hours * np.pi)
    volumes = (base_vol * vol_pattern * (1 + np.random.randn(n_minutes) * 0.2)).astype(int)
    volumes = np.maximum(volumes, 100)
    
    price_series = pd.Series(prices, index=times)
    volume_series = pd.Series(volumes, index=times)
    
    # Create order
    order = Order(symbol='AAPL', side='buy', quantity=50000, order_type='twap')
    
    # TWAP
    twap = TWAPScheduler(n_buckets=20)
    twap_schedule = twap.schedule(order, times[0])
    twap_result = twap.execute(twap_schedule, price_series, 
                                daily_volume=volumes.sum(), volatility=0.02)
    
    # VWAP
    vwap = VWAPScheduler(n_buckets=20)
    vwap_schedule = vwap.schedule(order, times[0], 
                                   volume_profile=None)
    vwap_result = vwap.execute(vwap_schedule, price_series, volume_series)
    
    # Market (single order)
    market_impact = MarketImpactModel()
    market_price = price_series.iloc[0]
    impact = market_impact.temporary_impact(50000, volumes.sum(), 0.02)
    market_cost = 50000 * market_price * (1 + impact / 10000)
    
    print("=" * 60)
    print("EXECUTION ALGORITHM BENCHMARK")
    print("=" * 60)
    print(f"\nOrder: Buy 50,000 AAPL shares")
    print(f"ADV: {volumes.sum():,} | Participation: {50000/volumes.sum()*100:.1f}%")
    print()
    print(f"MARKET ORDER:")
    print(f"  Cost: ${market_cost:,.2f} | Impact: {impact:.1f} bps | Slippage: {impact:.1f} bps")
    print()
    print(f"TWAP:")
    print(f"  Cost: ${twap_result['total_cost']:,.2f} | Impact: {twap_result['avg_impact_bps']:.1f} bps")
    print(f"  Avg Price: ${twap_result['avg_price']:.2f} | Child Orders: {twap_result['n_child_orders']}")
    print()
    print(f"VWAP:")
    print(f"  Cost: ${vwap_result['total_cost']:,.2f} | Impact: {vwap_result['avg_impact_bps']:.1f} bps")
    print(f"  Avg Price: ${vwap_result['avg_price']:.2f}")
    print(f"  Market VWAP: ${vwap_result['market_vwap']:.2f} | Deviation: {vwap_result['vwap_deviation_bps']:.1f} bps")
    print()
    
    savings_twap = (market_cost - twap_result['total_cost']) / market_cost * 100
    savings_vwap = (market_cost - vwap_result['total_cost']) / market_cost * 100
    print(f"Savings vs Market Order:")
    print(f"  TWAP: {savings_twap:.2f}% | VWAP: {savings_vwap:.2f}%")


if __name__ == '__main__':
    benchmark_execution_algorithms()