File size: 9,866 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
"""Tests for option processing in portfolio.py.

This module tests the option processing functionality in portfolio.py to ensure
that refactoring doesn't introduce regressions.
"""

from unittest.mock import patch

import pandas as pd
import pytest

from src.folio.portfolio import process_portfolio_data


@pytest.fixture
def sample_portfolio_with_options():
    """Create a sample portfolio DataFrame with options."""
    data = [
        # Stock position
        {
            "Symbol": "SPY",
            "Description": "SPDR S&P 500 ETF TRUST",
            "Quantity": "10",
            "Last Price": "100.00",
            "Current Value": "1000.00",
            "Percent Of Account": "10.00",
            "Type": "Cash",
            "Average Cost Basis": "95.00",
        },
        # Long call option
        {
            "Symbol": "-SPY",  # Option symbol starts with hyphen
            "Description": "SPY JUN 15 2025 $100 CALL",
            "Quantity": "2",
            "Last Price": "5.00",
            "Current Value": "1000.00",
            "Percent Of Account": "10.00",
            "Type": "Cash",
            "Average Cost Basis": "4.50",
        },
        # Short call option
        {
            "Symbol": "-SPY",
            "Description": "SPY JUN 15 2025 $110 CALL",
            "Quantity": "-1",
            "Last Price": "2.00",
            "Current Value": "-200.00",
            "Percent Of Account": "-2.00",
            "Type": "Cash",
            "Average Cost Basis": "1.80",
        },
        # Long put option
        {
            "Symbol": "-SPY",
            "Description": "SPY JUN 15 2025 $90 PUT",
            "Quantity": "1",
            "Last Price": "2.00",
            "Current Value": "200.00",
            "Percent Of Account": "2.00",
            "Type": "Cash",
            "Average Cost Basis": "1.90",
        },
        # Short put option
        {
            "Symbol": "-SPY",
            "Description": "SPY JUN 15 2025 $80 PUT",
            "Quantity": "-2",
            "Last Price": "1.00",
            "Current Value": "-200.00",
            "Percent Of Account": "-2.00",
            "Type": "Cash",
            "Average Cost Basis": "0.90",
        },
        # Orphaned option (no corresponding stock)
        {
            "Symbol": "-AAPL",
            "Description": "AAPL JUN 15 2025 $200 CALL",
            "Quantity": "1",
            "Last Price": "10.00",
            "Current Value": "1000.00",
            "Percent Of Account": "10.00",
            "Type": "Cash",
            "Average Cost Basis": "9.50",
        },
    ]
    return pd.DataFrame(data)


@patch("src.folio.portfolio.get_beta")
@patch("src.folio.portfolio.data_fetcher")
def test_option_processing(
    mock_data_fetcher, mock_get_beta, sample_portfolio_with_options
):
    """Test that option processing works correctly."""
    # Mock the data fetcher to return stock prices
    # Create a mock DataFrame with the expected structure
    mock_df = pd.DataFrame({"Close": [100.0]}, index=[pd.Timestamp("2025-01-01")])
    mock_aapl_df = pd.DataFrame({"Close": [200.0]}, index=[pd.Timestamp("2025-01-01")])

    # Set up the fetch_data method to return the mock DataFrame
    mock_data_fetcher.fetch_data.side_effect = (
        lambda symbol, period=None, interval=None: {
            "SPY": mock_df,
            "AAPL": mock_aapl_df,
        }.get(symbol, mock_df)
    )

    # Mock the beta function to return a fixed beta
    mock_get_beta.return_value = 1.0

    # Process the portfolio data
    groups, summary, cash_like = process_portfolio_data(sample_portfolio_with_options)

    # Verify that we have the expected number of groups
    assert len(groups) == 2, "Should have 2 groups (SPY and AAPL)"

    # Find the SPY group
    spy_group = next((g for g in groups if g.ticker == "SPY"), None)
    assert spy_group is not None, "Should have a group for SPY"

    # Verify that the SPY group has the expected number of options
    assert len(spy_group.option_positions) == 4, "SPY group should have 4 options"

    # Verify that each option has the expected attributes
    for opt in spy_group.option_positions:
        assert hasattr(opt, "delta"), "Option should have delta"
        assert hasattr(opt, "delta_exposure"), "Option should have delta_exposure"
        assert hasattr(opt, "beta_adjusted_exposure"), (
            "Option should have beta_adjusted_exposure"
        )
        assert hasattr(opt, "notional_value"), "Option should have notional_value"

    # Verify that the options have the correct signs for delta and exposure
    long_call = next(
        (
            o
            for o in spy_group.option_positions
            if o.option_type == "CALL" and o.quantity > 0
        ),
        None,
    )
    assert long_call is not None, "Should have a long call option"
    assert long_call.delta > 0, "Long call should have positive delta"
    assert long_call.delta_exposure > 0, "Long call should have positive delta exposure"

    short_call = next(
        (
            o
            for o in spy_group.option_positions
            if o.option_type == "CALL" and o.quantity < 0
        ),
        None,
    )
    assert short_call is not None, "Should have a short call option"
    assert short_call.delta < 0, "Short call should have negative delta"
    assert short_call.delta_exposure < 0, (
        "Short call should have negative delta exposure"
    )

    long_put = next(
        (
            o
            for o in spy_group.option_positions
            if o.option_type == "PUT" and o.quantity > 0
        ),
        None,
    )
    assert long_put is not None, "Should have a long put option"
    assert long_put.delta < 0, "Long put should have negative delta"
    assert long_put.delta_exposure < 0, "Long put should have negative delta exposure"

    short_put = next(
        (
            o
            for o in spy_group.option_positions
            if o.option_type == "PUT" and o.quantity < 0
        ),
        None,
    )
    assert short_put is not None, "Should have a short put option"
    assert short_put.delta > 0, "Short put should have positive delta"
    assert short_put.delta_exposure > 0, "Short put should have positive delta exposure"

    # Find the AAPL group (orphaned option)
    aapl_group = next((g for g in groups if g.ticker == "AAPL"), None)
    assert aapl_group is not None, "Should have a group for AAPL"

    # Verify that the AAPL group has the expected number of options
    assert len(aapl_group.option_positions) == 1, "AAPL group should have 1 option"

    # Verify that the orphaned option has the expected attributes
    aapl_option = aapl_group.option_positions[0]
    assert hasattr(aapl_option, "delta"), "Option should have delta"
    assert hasattr(aapl_option, "delta_exposure"), "Option should have delta_exposure"
    assert hasattr(aapl_option, "beta_adjusted_exposure"), (
        "Option should have beta_adjusted_exposure"
    )
    assert hasattr(aapl_option, "notional_value"), "Option should have notional_value"

    # Verify that the summary has option exposures
    assert hasattr(summary, "long_exposure"), "Summary should have long_exposure"
    assert hasattr(summary, "short_exposure"), "Summary should have short_exposure"
    assert hasattr(summary, "options_exposure"), "Summary should have options_exposure"

    # Verify that the option exposures are included in the summary
    assert summary.long_exposure.option_delta_exposure > 0, (
        "Summary should have positive long option exposure"
    )
    assert summary.short_exposure.option_delta_exposure < 0, (
        "Summary should have negative short option exposure"
    )
    assert summary.options_exposure.option_delta_exposure != 0, (
        "Summary should have non-zero net option exposure"
    )


@patch("src.folio.portfolio.get_beta")
@patch("src.folio.portfolio.data_fetcher")
def test_option_processing_with_errors(
    mock_data_fetcher, mock_get_beta, sample_portfolio_with_options
):
    """Test that option processing handles errors gracefully."""
    # Add an invalid option to the portfolio
    invalid_option = {
        "Symbol": "-SPY",
        "Description": "Invalid option description",  # Invalid description
        "Quantity": "1",
        "Last Price": "5.00",
        "Current Value": "500.00",
        "Percent Of Account": "5.00",
        "Type": "Cash",
    }
    df_with_invalid = pd.concat(
        [sample_portfolio_with_options, pd.DataFrame([invalid_option])]
    )

    # Mock the data fetcher to return stock prices
    # Create a mock DataFrame with the expected structure
    mock_df = pd.DataFrame({"Close": [100.0]}, index=[pd.Timestamp("2025-01-01")])
    mock_aapl_df = pd.DataFrame({"Close": [200.0]}, index=[pd.Timestamp("2025-01-01")])

    # Set up the fetch_data method to return the mock DataFrame
    mock_data_fetcher.fetch_data.side_effect = (
        lambda symbol, period=None, interval=None: {
            "SPY": mock_df,
            "AAPL": mock_aapl_df,
        }.get(symbol, mock_df)
    )

    # Mock the beta function to return a fixed beta
    mock_get_beta.return_value = 1.0

    # Process the portfolio data
    groups, summary, cash_like = process_portfolio_data(df_with_invalid)

    # Verify that we have the expected number of groups
    # Note: The invalid option creates a separate group with ticker '-SPY'
    assert len(groups) == 3, "Should have 3 groups (SPY, -SPY, and AAPL)"

    # Find the SPY group
    spy_group = next((g for g in groups if g.ticker == "SPY"), None)
    assert spy_group is not None, "Should have a group for SPY"

    # Verify that the SPY group still has the expected number of options (invalid one should be skipped)
    assert len(spy_group.option_positions) == 4, (
        "SPY group should have 4 options (invalid one should be skipped)"
    )