Premchan369 commited on
Commit
6b8ec76
·
verified ·
1 Parent(s): 7d67e7a

Upload hedging_engine.py

Browse files
Files changed (1) hide show
  1. hedging_engine.py +118 -0
hedging_engine.py ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Dynamic Hedging System using Options Model."""
2
+ import numpy as np
3
+ import pandas as pd
4
+ from typing import Dict, Optional
5
+ import warnings
6
+ warnings.filterwarnings('ignore')
7
+
8
+
9
+ class DynamicHedgingEngine:
10
+ """Dynamic hedging using options model integration."""
11
+
12
+ def __init__(self,
13
+ max_hedge_ratio: float = 0.5,
14
+ hedge_cost: float = 0.002,
15
+ delta_threshold: float = 0.1):
16
+ self.max_hedge_ratio = max_hedge_ratio
17
+ self.hedge_cost = hedge_cost
18
+ self.delta_threshold = delta_threshold
19
+ self.hedge_history = []
20
+
21
+ def compute_hedge_ratio(self,
22
+ portfolio_delta: float,
23
+ portfolio_gamma: float,
24
+ volatility: float,
25
+ risk_aversion: float = 2.0) -> float:
26
+ """
27
+ Compute optimal hedge ratio.
28
+
29
+ Args:
30
+ portfolio_delta: Current delta exposure
31
+ portfolio_gamma: Current gamma exposure
32
+ volatility: Current volatility
33
+ risk_aversion: Risk aversion parameter
34
+
35
+ Returns:
36
+ Hedge ratio (0 to max_hedge_ratio)
37
+ """
38
+ # Base hedge from delta
39
+ delta_hedge = abs(portfolio_delta) if portfolio_delta > self.delta_threshold else 0
40
+
41
+ # Gamma adjustment (more hedging when gamma is negative/high)
42
+ gamma_hedge = abs(portfolio_gamma) * volatility * 0.5 if portfolio_gamma > 0 else 0
43
+
44
+ # Combine with risk aversion
45
+ raw_hedge = (delta_hedge + gamma_hedge) * risk_aversion / 2.0
46
+
47
+ # Cap at max
48
+ hedge_ratio = np.clip(raw_hedge, 0, self.max_hedge_ratio)
49
+
50
+ self.hedge_history.append({
51
+ 'hedge_ratio': hedge_ratio,
52
+ 'delta_hedge': delta_hedge,
53
+ 'gamma_hedge': gamma_hedge
54
+ })
55
+
56
+ return hedge_ratio
57
+
58
+ def compute_portfolio_greeks(self,
59
+ positions: Dict[str, float],
60
+ option_greeks: pd.DataFrame) -> Dict[str, float]:
61
+ """Compute portfolio-level Greeks."""
62
+ total_delta = 0
63
+ total_gamma = 0
64
+ total_theta = 0
65
+ total_vega = 0
66
+
67
+ for ticker, position in positions.items():
68
+ if ticker in option_greeks.index:
69
+ row = option_greeks.loc[ticker]
70
+ total_delta += row.get('delta', 0) * position
71
+ total_gamma += row.get('gamma', 0) * position
72
+ total_theta += row.get('theta', 0) * position
73
+ total_vega += row.get('vega', 0) * position
74
+ else:
75
+ # Equity position: delta = 1 per share
76
+ total_delta += position
77
+
78
+ return {
79
+ 'delta': total_delta,
80
+ 'gamma': total_gamma,
81
+ 'theta': total_theta,
82
+ 'vega': total_vega
83
+ }
84
+
85
+ def compute_delta_neutral_weights(self,
86
+ portfolio_greeks: Dict[str, float],
87
+ hedge_instruments: Dict[str, float]) -> Dict[str, float]:
88
+ """Compute delta-neutral hedge weights."""
89
+ total_delta = portfolio_greeks['delta']
90
+
91
+ if abs(total_delta) < 0.01:
92
+ return {k: 0 for k in hedge_instruments}
93
+
94
+ # Distribute hedge proportionally
95
+ hedge_weights = {}
96
+ total_hedge_delta = sum(abs(v) for v in hedge_instruments.values())
97
+
98
+ for instrument, delta in hedge_instruments.items():
99
+ if total_hedge_delta > 0:
100
+ hedge_weights[instrument] = -total_delta * abs(delta) / total_hedge_delta
101
+ else:
102
+ hedge_weights[instrument] = 0
103
+
104
+ return hedge_weights
105
+
106
+ def get_hedge_performance(self) -> Dict:
107
+ """Get hedging performance statistics."""
108
+ if not self.hedge_history:
109
+ return {}
110
+
111
+ hedge_ratios = [h['hedge_ratio'] for h in self.hedge_history]
112
+
113
+ return {
114
+ 'avg_hedge_ratio': np.mean(hedge_ratios),
115
+ 'max_hedge_ratio': np.max(hedge_ratios),
116
+ 'hedge_utilization': np.mean(hedge_ratios) / self.max_hedge_ratio,
117
+ 'n_rebalances': len(self.hedge_history)
118
+ }