-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathverify_position_vector.py
More file actions
260 lines (203 loc) Β· 8.67 KB
/
Copy pathverify_position_vector.py
File metadata and controls
260 lines (203 loc) Β· 8.67 KB
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
#!/usr/bin/env python3
"""Standalone verification of 24-dimensional position state vector."""
import sys
import numpy as np
import pandas as pd
from datetime import datetime, timedelta
from enum import Enum, IntEnum
from dataclasses import dataclass
from typing import List, Optional, Dict, Any
import math
# Minimal dependencies for verification
class StrategyType(Enum):
LONG_CALL = "long_call"
IRON_CONDOR = "iron_condor"
class OptionType(Enum):
CALL = "CALL"
PUT = "PUT"
class PositionZones(IntEnum):
DEEP_LOSS = -3
LOSS = -2
WARNING = -1
SAFE = 0
PROFIT = 1
HIGH_PROFIT = 2
MAX_PROFIT = 3
@dataclass
class Position:
strategy_type: StrategyType
entry_date: datetime
expiration_date: datetime
strikes: List[int]
option_types: List[OptionType]
quantities: List[int]
entry_prices: List[int]
current_prices: List[int]
underlying_price_at_entry: int
current_underlying_price: int
adjustments_made: int = 0
@property
def days_to_expiration(self) -> int:
return (self.expiration_date - datetime.now()).days
@property
def days_held(self) -> int:
return (datetime.now() - self.entry_date).days
def calculate_price_zone(self) -> PositionZones:
return PositionZones.SAFE # Simplified for verification
def calculate_breakevens(self) -> List[int]:
if self.strategy_type == StrategyType.LONG_CALL:
return [self.strikes[0] + self.entry_prices[0]]
return [min(self.strikes), max(self.strikes)]
# Minimal vector assembler for verification
class PositionStateVector:
def __init__(self):
self.strategy_mapping = {
StrategyType.LONG_CALL: 0,
StrategyType.IRON_CONDOR: 1
}
def calculate(self, position: Position) -> np.ndarray:
"""Calculate 24-dimensional position state vector."""
features = np.zeros(24)
# 1. Strategy Identity (1 feature)
features[0] = self._calculate_strategy_identity(position)
# 2. Board Position (6 features)
board_features = self._calculate_board_position(position)
features[1:7] = board_features
# 3. Time (3 features)
time_features = self._calculate_time_features(position)
features[7:10] = time_features
# 4. Volatility (3 features) - simplified
features[10:13] = [0.3, 0.3, 0.0] # Stubbed IV features
# 5. Greeks (4 features) - simplified
features[13:17] = [0.5, 0.1, -0.05, 0.2] # Stubbed Greeks
# 6. P/L Status (5 features)
pnl_features = self._calculate_pnl_features(position)
features[17:22] = pnl_features
# 7. Meta (2 features)
meta_features = self._calculate_meta_features(position)
features[22:24] = meta_features
return features
def _calculate_strategy_identity(self, position: Position) -> float:
strategy_idx = self.strategy_mapping.get(position.strategy_type, 0)
return (strategy_idx / (len(self.strategy_mapping) - 1)) * 2 - 1
def _calculate_board_position(self, position: Position) -> np.ndarray:
features = np.zeros(6)
current_price = position.current_underlying_price
strikes = position.strikes
breakevens = position.calculate_breakevens()
# Price zone
features[0] = position.calculate_price_zone().value / 3.0
# Zone velocity (simplified)
features[1] = 0.0
# Strike distances
if strikes:
features[2] = np.tanh((max(strikes) - current_price) / current_price)
features[3] = np.tanh((current_price - min(strikes)) / current_price)
# Breakeven distances
if breakevens:
features[4] = np.tanh((max(breakevens) - current_price) / current_price)
features[5] = np.tanh((current_price - min(breakevens)) / current_price)
return features
def _calculate_time_features(self, position: Position) -> np.ndarray:
features = np.zeros(3)
days_to_exp = position.days_to_expiration
features[0] = np.tanh(days_to_exp / 30.0)
total_days = (position.expiration_date - position.entry_date).days
if total_days > 0:
pct_time_remaining = max(days_to_exp / total_days, 0.0)
features[1] = pct_time_remaining * 2 - 1
if days_to_exp > 0:
theta_pressure = min(1.0, max(0.0, (30 - days_to_exp) / 30.0))
features[2] = theta_pressure * 2 - 1
else:
features[2] = 1.0
return features
def _calculate_pnl_features(self, position: Position) -> np.ndarray:
features = np.zeros(5)
# Calculate basic P/L
current_value = sum(q * p for q, p in zip(position.quantities, position.current_prices))
entry_value = sum(q * p for q, p in zip(position.quantities, position.entry_prices))
unrealized_pnl = current_value - entry_value
# Normalize by underlying price
underlying = position.current_underlying_price
features[0] = np.tanh(entry_value / underlying) # Entry credit
features[1] = np.tanh(current_value / underlying) # Current value
features[2] = np.tanh(unrealized_pnl / underlying) # Unrealized P/L
features[3] = np.tanh(unrealized_pnl / 1000) # Percent max profit (simplified)
features[4] = np.tanh(abs(unrealized_pnl) / 1000) # Percent max loss (simplified)
return features
def _calculate_meta_features(self, position: Position) -> np.ndarray:
features = np.zeros(2)
features[0] = np.tanh(position.days_held / 30.0)
features[1] = np.tanh(position.adjustments_made / 3.0)
return features
def validate(self, state_vector: np.ndarray) -> None:
if state_vector.shape != (24,):
raise ValueError(f"State vector must be 24-dimensional, got {state_vector.shape}")
if np.any(np.isnan(state_vector)):
raise ValueError("State vector contains NaN values")
if np.any(np.isinf(state_vector)):
raise ValueError("State vector contains infinite values")
def get_feature_names(self) -> List[str]:
return [
'strategy_identity', 'price_zone', 'zone_velocity',
'distance_to_upper_strike', 'distance_to_lower_strike',
'distance_to_upper_breakeven', 'distance_to_lower_breakeven',
'days_to_expiration', 'percent_time_remaining', 'theta_pressure',
'iv_at_entry', 'current_iv', 'iv_change',
'position_delta', 'position_gamma', 'position_theta', 'position_vega',
'entry_credit', 'current_value', 'unrealized_pnl',
'percent_of_max_profit', 'percent_of_max_loss',
'days_held', 'adjustments_made'
]
if __name__ == '__main__':
print("π Verifying 24-dimensional position state vector assembly...")
# Create test position
test_position = Position(
strategy_type=StrategyType.LONG_CALL,
entry_date=datetime(2024, 1, 1),
expiration_date=datetime(2024, 2, 16),
strikes=[10000],
option_types=[OptionType.CALL],
quantities=[1],
entry_prices=[500],
current_prices=[300],
underlying_price_at_entry=9800,
current_underlying_price=10200,
adjustments_made=0
)
# Create vector assembler
psv = PositionStateVector()
# Calculate state vector
state = psv.calculate(test_position)
print(f"β Position state vector calculation successful")
print(f" Shape: {state.shape}")
print(f" Type: {type(state)}")
print(f" No NaN values: {not np.any(np.isnan(state))}")
print(f" No infinite values: {not np.any(np.isinf(state))}")
# Validate
try:
psv.validate(state)
print("β Vector validation passed")
except ValueError as e:
print(f"β Vector validation failed: {e}")
sys.exit(1)
# Check feature names
feature_names = psv.get_feature_names()
print(f"β Feature names: {len(feature_names)} features")
# Display sample features
print("\nπ Sample feature values:")
for i in range(0, 24, 4):
end_idx = min(i + 4, 24)
feature_slice = state[i:end_idx]
name_slice = feature_names[i:end_idx]
for j, (name, value) in enumerate(zip(name_slice, feature_slice)):
print(f" {i+j:2d}. {name:25s}: {value:8.4f}")
# Summary statistics
print(f"\nπ Vector statistics:")
print(f" Min: {np.min(state):.4f}")
print(f" Max: {np.max(state):.4f}")
print(f" Mean: {np.mean(state):.4f}")
print(f" Std: {np.std(state):.4f}")
print(f" Range: {np.max(state) - np.min(state):.4f}")
print("\nπ― All verifications passed! 24-dimensional position state vector is working correctly.")