File size: 20,477 Bytes
ce4bc73 |
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 |
"""Tests for option exposure calculations.
This module focuses specifically on testing that option exposures are calculated correctly
for different option types (calls/puts) and positions (long/short).
"""
from datetime import datetime
import pytest
from src.folio.options import OptionContract, calculate_option_delta
@pytest.fixture
def option_fixtures():
"""Create a set of option fixtures for testing exposures."""
# Use a fixed expiry date for consistent test results
expiry_date = datetime(2025, 6, 15) # About 6 months from now
# Create options with different types and positions
return {
# Call options
"long_call_atm": OptionContract(
underlying="SPY",
expiry=expiry_date,
strike=100.0,
option_type="CALL",
quantity=1, # Long position
current_price=5.0,
description="SPY JUN 15 2025 $100 CALL",
),
"short_call_atm": OptionContract(
underlying="SPY",
expiry=expiry_date,
strike=100.0,
option_type="CALL",
quantity=-1, # Short position
current_price=5.0,
description="SPY JUN 15 2025 $100 CALL",
),
# Put options
"long_put_atm": OptionContract(
underlying="SPY",
expiry=expiry_date,
strike=100.0,
option_type="PUT",
quantity=1, # Long position
current_price=5.0,
description="SPY JUN 15 2025 $100 PUT",
),
"short_put_atm": OptionContract(
underlying="SPY",
expiry=expiry_date,
strike=100.0,
option_type="PUT",
quantity=-1, # Short position
current_price=5.0,
description="SPY JUN 15 2025 $100 PUT",
),
# Different quantities
"long_call_multiple": OptionContract(
underlying="SPY",
expiry=expiry_date,
strike=100.0,
option_type="CALL",
quantity=3, # Multiple contracts
current_price=5.0,
description="SPY JUN 15 2025 $100 CALL",
),
"short_put_multiple": OptionContract(
underlying="SPY",
expiry=expiry_date,
strike=100.0,
option_type="PUT",
quantity=-2, # Multiple contracts
current_price=5.0,
description="SPY JUN 15 2025 $100 PUT",
),
# Different moneyness levels
"long_call_itm": OptionContract(
underlying="SPY",
expiry=expiry_date,
strike=90.0, # In-the-money
option_type="CALL",
quantity=1,
current_price=15.0,
description="SPY JUN 15 2025 $90 CALL",
),
"long_call_otm": OptionContract(
underlying="SPY",
expiry=expiry_date,
strike=110.0, # Out-of-the-money
option_type="CALL",
quantity=1,
current_price=2.0,
description="SPY JUN 15 2025 $110 CALL",
),
"long_put_itm": OptionContract(
underlying="SPY",
expiry=expiry_date,
strike=110.0, # In-the-money
option_type="PUT",
quantity=1,
current_price=15.0,
description="SPY JUN 15 2025 $110 PUT",
),
"long_put_otm": OptionContract(
underlying="SPY",
expiry=expiry_date,
strike=90.0, # Out-of-the-money
option_type="PUT",
quantity=1,
current_price=2.0,
description="SPY JUN 15 2025 $90 PUT",
),
}
def calculate_exposure(option, underlying_price, iv=0.3):
"""Calculate the exposure for an option position.
Args:
option: The option position
underlying_price: The price of the underlying asset
iv: Implied volatility to use for the calculation
Returns:
The calculated exposure (delta * notional value)
"""
# Set the underlying price on the option object to ensure notional_value property works
option.underlying_price = underlying_price
# Calculate delta using Black-Scholes
delta = calculate_option_delta(option, underlying_price, implied_volatility=iv)
# Calculate exposure as delta * notional value
# This is the key calculation we're testing
exposure = delta * option.notional_value
# Print detailed information for debugging
return delta, exposure
def test_call_option_exposures(option_fixtures):
"""Test that call option exposures are calculated correctly."""
underlying_price = 100.0
# Test long call (should have positive delta and positive exposure)
long_call = option_fixtures["long_call_atm"]
long_delta, long_exposure = calculate_exposure(long_call, underlying_price)
# Delta should be positive for a call option
assert long_delta > 0, "Long call delta should be positive"
# Exposure should be positive for a long call
assert long_exposure > 0, "Long call exposure should be positive"
# Test short call (should have negative delta and negative exposure)
short_call = option_fixtures["short_call_atm"]
short_delta, short_exposure = calculate_exposure(short_call, underlying_price)
# Delta should be negative for a short call
assert short_delta < 0, "Short call delta should be negative"
# Exposure should be negative for a short call
assert short_exposure < 0, "Short call exposure should be negative"
# The absolute values of delta and exposure should be the same for long and short
# positions with the same parameters (just opposite signs)
assert abs(long_delta) == pytest.approx(abs(short_delta), rel=1e-10)
assert abs(long_exposure) == pytest.approx(abs(short_exposure), rel=1e-10)
def test_put_option_exposures(option_fixtures):
"""Test that put option exposures are calculated correctly."""
underlying_price = 100.0
# Test long put (should have negative delta and negative exposure)
long_put = option_fixtures["long_put_atm"]
long_delta, long_exposure = calculate_exposure(long_put, underlying_price)
# Delta should be negative for a put option
assert long_delta < 0, "Long put delta should be negative"
# Exposure should be negative for a long put
assert long_exposure < 0, "Long put exposure should be negative"
# Test short put (should have positive delta and positive exposure)
short_put = option_fixtures["short_put_atm"]
short_delta, short_exposure = calculate_exposure(short_put, underlying_price)
# Delta should be positive for a short put
assert short_delta > 0, "Short put delta should be positive"
# Exposure should be positive for a short put
assert short_exposure > 0, "Short put exposure should be positive"
# The absolute values of delta and exposure should be the same for long and short
# positions with the same parameters (just opposite signs)
assert abs(long_delta) == pytest.approx(abs(short_delta), rel=1e-10)
assert abs(long_exposure) == pytest.approx(abs(short_exposure), rel=1e-10)
def test_exposure_scales_with_quantity(option_fixtures):
"""Test that exposure scales linearly with quantity."""
underlying_price = 100.0
# Test with a single contract
single_call = option_fixtures["long_call_atm"]
_, single_exposure = calculate_exposure(single_call, underlying_price)
# Test with multiple contracts
multiple_call = option_fixtures["long_call_multiple"]
_, multiple_exposure = calculate_exposure(multiple_call, underlying_price)
# Exposure should scale linearly with quantity
expected_ratio = multiple_call.quantity / single_call.quantity
actual_ratio = multiple_exposure / single_exposure
assert actual_ratio == pytest.approx(expected_ratio, rel=1e-10)
# Test with short positions
single_put = option_fixtures["short_put_atm"]
_, single_exposure = calculate_exposure(single_put, underlying_price)
multiple_put = option_fixtures["short_put_multiple"]
_, multiple_exposure = calculate_exposure(multiple_put, underlying_price)
# Exposure should scale linearly with quantity
expected_ratio = multiple_put.quantity / single_put.quantity
actual_ratio = multiple_exposure / single_exposure
assert actual_ratio == pytest.approx(expected_ratio, rel=1e-10)
def test_moneyness_affects_delta(option_fixtures):
"""Test that moneyness affects delta as expected."""
underlying_price = 100.0
# For call options:
# - ITM calls should have higher delta
# - OTM calls should have lower delta
itm_call = option_fixtures["long_call_itm"]
atm_call = option_fixtures["long_call_atm"]
otm_call = option_fixtures["long_call_otm"]
itm_delta, _ = calculate_exposure(itm_call, underlying_price)
atm_delta, _ = calculate_exposure(atm_call, underlying_price)
otm_delta, _ = calculate_exposure(otm_call, underlying_price)
assert itm_delta > atm_delta > otm_delta, (
"Call delta should decrease as strike increases"
)
# For put options:
# - ITM puts should have more negative delta
# - OTM puts should have less negative delta
itm_put = option_fixtures["long_put_itm"]
atm_put = option_fixtures["long_put_atm"]
otm_put = option_fixtures["long_put_otm"]
itm_delta, _ = calculate_exposure(itm_put, underlying_price)
atm_delta, _ = calculate_exposure(atm_put, underlying_price)
otm_delta, _ = calculate_exposure(otm_put, underlying_price)
assert itm_delta < atm_delta < otm_delta, (
"Put delta should increase as strike decreases"
)
def test_portfolio_level_exposure():
"""Test that portfolio-level option exposure is calculated correctly."""
# Create a simple portfolio with different option positions
expiry_date = datetime(2025, 6, 15)
underlying_price = 100.0
portfolio = [
# Long call - positive exposure
OptionContract(
underlying="SPY",
expiry=expiry_date,
strike=100.0,
option_type="CALL",
quantity=2,
current_price=5.0,
description="SPY JUN 15 2025 $100 CALL",
),
# Short call - negative exposure
OptionContract(
underlying="SPY",
expiry=expiry_date,
strike=110.0,
option_type="CALL",
quantity=-1,
current_price=2.0,
description="SPY JUN 15 2025 $110 CALL",
),
# Long put - negative exposure
OptionContract(
underlying="SPY",
expiry=expiry_date,
strike=90.0,
option_type="PUT",
quantity=1,
current_price=2.0,
description="SPY JUN 15 2025 $90 PUT",
),
# Short put - positive exposure
OptionContract(
underlying="SPY",
expiry=expiry_date,
strike=80.0,
option_type="PUT",
quantity=-2,
current_price=1.0,
description="SPY JUN 15 2025 $80 PUT",
),
]
# Calculate individual exposures
exposures = []
for option in portfolio:
_, exposure = calculate_exposure(option, underlying_price)
exposures.append(exposure)
# Calculate total exposure
total_exposure = sum(exposures)
# Verify the signs of individual exposures
assert exposures[0] > 0, "Long call should have positive exposure"
assert exposures[1] < 0, "Short call should have negative exposure"
assert exposures[2] < 0, "Long put should have negative exposure"
assert exposures[3] > 0, "Short put should have positive exposure"
# The total exposure should equal the sum of individual exposures
assert total_exposure == pytest.approx(sum(exposures), rel=1e-10)
# Verify that the calculation matches what's done in portfolio.py
# In portfolio.py, the calculation is: delta * option.notional_value
# Let's verify this is correct for each option type and position
# For a long call (positive delta, positive exposure)
long_call = portfolio[0]
long_call_delta = calculate_option_delta(long_call, underlying_price)
long_call_exposure = long_call_delta * long_call.notional_value
assert long_call_exposure > 0, "Long call exposure should be positive"
# For a short call (negative delta, negative exposure)
short_call = portfolio[1]
short_call_delta = calculate_option_delta(short_call, underlying_price)
short_call_exposure = short_call_delta * short_call.notional_value
assert short_call_exposure < 0, "Short call exposure should be negative"
# For a long put (negative delta, negative exposure)
long_put = portfolio[2]
long_put_delta = calculate_option_delta(long_put, underlying_price)
long_put_exposure = long_put_delta * long_put.notional_value
assert long_put_exposure < 0, "Long put exposure should be negative"
# For a short put (positive delta, positive exposure)
short_put = portfolio[3]
short_put_delta = calculate_option_delta(short_put, underlying_price)
short_put_exposure = short_put_delta * short_put.notional_value
assert short_put_exposure > 0, "Short put exposure should be positive"
def test_calculate_option_exposure(option_fixtures):
"""Test the calculate_option_exposure function."""
# Import calculate_option_exposure from options module
from src.folio.options import calculate_option_exposure
underlying_price = 100.0
beta = 1.2
# Test with a long call
long_call = option_fixtures["long_call_atm"]
long_call_exposures = calculate_option_exposure(long_call, underlying_price, beta)
# Verify the keys in the result
assert "delta" in long_call_exposures
assert "delta_exposure" in long_call_exposures
assert "beta_adjusted_exposure" in long_call_exposures
# Verify the values
delta = long_call_exposures["delta"]
delta_exposure = long_call_exposures["delta_exposure"]
beta_adjusted_exposure = long_call_exposures["beta_adjusted_exposure"]
# Delta should be positive for a long call
assert delta > 0, "Delta should be positive for a long call"
# Delta exposure should be positive for a long call
assert delta_exposure > 0, "Delta exposure should be positive for a long call"
# Beta-adjusted exposure should be delta_exposure * beta
assert beta_adjusted_exposure == pytest.approx(delta_exposure * beta)
# Test with a short call
short_call = option_fixtures["short_call_atm"]
short_call_exposures = calculate_option_exposure(short_call, underlying_price, beta)
# Delta should be negative for a short call
assert short_call_exposures["delta"] < 0, (
"Delta should be negative for a short call"
)
# Delta exposure should be negative for a short call
assert short_call_exposures["delta_exposure"] < 0, (
"Delta exposure should be negative for a short call"
)
# Test with a long put
long_put = option_fixtures["long_put_atm"]
long_put_exposures = calculate_option_exposure(long_put, underlying_price, beta)
# Delta should be negative for a long put
assert long_put_exposures["delta"] < 0, "Delta should be negative for a long put"
# Delta exposure should be negative for a long put
assert long_put_exposures["delta_exposure"] < 0, (
"Delta exposure should be negative for a long put"
)
# Test with a short put
short_put = option_fixtures["short_put_atm"]
short_put_exposures = calculate_option_exposure(short_put, underlying_price, beta)
# Delta should be positive for a short put
assert short_put_exposures["delta"] > 0, "Delta should be positive for a short put"
# Delta exposure should be positive for a short put
assert short_put_exposures["delta_exposure"] > 0, (
"Delta exposure should be positive for a short put"
)
def test_process_options():
"""Test the process_options function."""
# Import process_options from options module
from src.folio.options import process_options
# Create test data
options_data = [
{
"description": "SPY JUN 15 2025 $100 CALL",
"quantity": 1,
"price": 5.0,
"symbol": "SPY250615C00100000",
},
{
"description": "SPY JUN 15 2025 $100 CALL",
"quantity": -1,
"price": 5.0,
"symbol": "SPY250615C00100000",
},
{
"description": "SPY JUN 15 2025 $100 PUT",
"quantity": 1,
"price": 5.0,
"symbol": "SPY250615P00100000",
},
{
"description": "SPY JUN 15 2025 $100 PUT",
"quantity": -1,
"price": 5.0,
"symbol": "SPY250615P00100000",
},
]
prices = {"SPY": 100.0}
betas = {"SPY": 1.2}
# Process the options
processed_options = process_options(options_data, prices, betas)
# Verify the results
assert len(processed_options) == 4, "Should have processed all 4 options"
# Check the first option (long call)
long_call = processed_options[0]
assert long_call["ticker"] == "SPY"
assert long_call["option_type"] == "CALL"
assert long_call["quantity"] == 1
assert long_call["delta"] > 0, "Delta should be positive for a long call"
assert long_call["delta_exposure"] > 0, (
"Delta exposure should be positive for a long call"
)
# Check the second option (short call)
short_call = processed_options[1]
assert short_call["ticker"] == "SPY"
assert short_call["option_type"] == "CALL"
assert short_call["quantity"] == -1
assert short_call["delta"] < 0, "Delta should be negative for a short call"
assert short_call["delta_exposure"] < 0, (
"Delta exposure should be negative for a short call"
)
# Check the third option (long put)
long_put = processed_options[2]
assert long_put["ticker"] == "SPY"
assert long_put["option_type"] == "PUT"
assert long_put["quantity"] == 1
assert long_put["delta"] < 0, "Delta should be negative for a long put"
assert long_put["delta_exposure"] < 0, (
"Delta exposure should be negative for a long put"
)
# Check the fourth option (short put)
short_put = processed_options[3]
assert short_put["ticker"] == "SPY"
assert short_put["option_type"] == "PUT"
assert short_put["quantity"] == -1
assert short_put["delta"] > 0, "Delta should be positive for a short put"
assert short_put["delta_exposure"] > 0, (
"Delta exposure should be positive for a short put"
)
def test_process_options_with_missing_price():
"""Test process_options with a missing price."""
# Import process_options from options module
from src.folio.options import process_options
options_data = [
{
"description": "SPY JUN 15 2025 $100 CALL",
"quantity": 1,
"price": 5.0,
},
{
"description": "AAPL JUN 15 2025 $200 CALL", # No price for AAPL
"quantity": 1,
"price": 10.0,
},
]
prices = {"SPY": 100.0} # No price for AAPL
# Process the options
processed_options = process_options(options_data, prices)
# Verify that only the SPY option was processed
assert len(processed_options) == 1, "Should have processed only the SPY option"
assert processed_options[0]["ticker"] == "SPY"
def test_process_options_with_error():
"""Test process_options with an error in the option data."""
# Import process_options from options module
from src.folio.options import process_options
options_data = [
{
"description": "SPY JUN 15 2025 $100 CALL",
"quantity": 1,
"price": 5.0,
},
{
"description": "Invalid option description", # Invalid description
"quantity": 1,
"price": 5.0,
},
]
prices = {"SPY": 100.0}
# Process the options
processed_options = process_options(options_data, prices)
# Verify that only the valid option was processed
assert len(processed_options) == 1, "Should have processed only the valid option"
assert processed_options[0]["ticker"] == "SPY"
|