Skip to content

Commit

Permalink
Refactor the net option calc
Browse files Browse the repository at this point in the history
This also fixes an edge case. Added more tests to reflect.
  • Loading branch information
brndnmtthws committed Apr 24, 2024
1 parent 0749b86 commit 7ed0887
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 3 deletions.
64 changes: 64 additions & 0 deletions thetagang/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,70 @@ def test_calculate_net_short_positions() -> None:
"P",
)

# A couple real-world examples
exp9dte = (today + timedelta(days=9)).strftime("%Y%m%d")
exp16dte = (today + timedelta(days=16)).strftime("%Y%m%d")
exp23dte = (today + timedelta(days=23)).strftime("%Y%m%d")
exp30dte = (today + timedelta(days=30)).strftime("%Y%m%d")
exp37dte = (today + timedelta(days=37)).strftime("%Y%m%d")
exp268dte = (today + timedelta(days=268)).strftime("%Y%m%d")

assert 2 == calculate_net_short_positions(
[
con(exp9dte, 77.0, "P", -2),
con(exp16dte, 76.0, "P", -1),
con(exp16dte, 77.0, "P", -1),
con(exp23dte, 77.0, "P", -6),
con(exp30dte, 77.0, "P", -2),
con(exp37dte, 77.0, "P", -5),
con(exp268dte, 77.0, "P", 15),
],
"P",
)

assert 0 == calculate_net_short_positions(
[
con(exp9dte, 77.0, "P", -2),
con(exp16dte, 76.0, "P", -1),
con(exp16dte, 77.0, "P", -1),
con(exp23dte, 77.0, "P", -6),
con(exp30dte, 77.0, "P", -2),
con(exp37dte, 77.0, "P", -5),
con(exp268dte, 77.0, "P", 15),
],
"C",
)

assert 20 == calculate_net_short_positions(
[
con(exp23dte, 72.0, "C", -8),
con(exp30dte, 66.0, "C", -8),
con(exp30dte, 68.0, "C", -9),
con(exp30dte, 69.0, "C", -7),
con(exp30dte, 72.0, "C", -1),
con(exp37dte, 59.5, "C", -8),
con(exp37dte, 68.0, "C", -7),
con(exp268dte, 55.0, "C", 5),
con(exp268dte, 60.0, "C", 23),
],
"C",
)

assert 0 == calculate_net_short_positions(
[
con(exp23dte, 72.0, "C", -8),
con(exp30dte, 66.0, "C", -8),
con(exp30dte, 68.0, "C", -9),
con(exp30dte, 69.0, "C", -7),
con(exp30dte, 72.0, "C", -1),
con(exp37dte, 59.5, "C", -8),
con(exp37dte, 68.0, "C", -7),
con(exp268dte, 55.0, "C", 5),
con(exp268dte, 60.0, "C", 23),
],
"P",
)


def test_weighted_avg_strike() -> None:
today = date.today()
Expand Down
8 changes: 5 additions & 3 deletions thetagang/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,14 @@ def calc_net(short_dte: int, short_strike: float, short_position: float) -> floa
# ignore empty long positions
continue
if long_dte >= short_dte:
if (right.upper().startswith("P") and long_strike >= short_strike) or (
right.upper().startswith("C") and long_strike <= short_strike
if (
math.isclose(short_strike, long_strike)
or (right.upper().startswith("P") and long_strike >= short_strike)
or (right.upper().startswith("C") and long_strike <= short_strike)
):
if short_position + long_position > 0:
short_position = 0
long_position = short_position + long_position
short_position = 0
else:
short_position += long_position
long_position = 0
Expand Down

0 comments on commit 7ed0887

Please sign in to comment.