-
Notifications
You must be signed in to change notification settings - Fork 198
Add butterfly, iron condor, and covered strategies #73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0d62857
Add 10 new options strategies: butterflies, iron condors, and covered…
claude 7ce49b6
Remove unused reduce import from core.py
claude 12b3a3f
Add calculated value assertions to new strategy tests
claude aef4a9f
Update README with new supported strategies
claude dbf87a8
Address Copilot review comments
claude 1a9b598
Update tests/test_strategies.py
michaelchu 95e67f2
Update tests/test_strategies.py
michaelchu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,6 @@ | ||
| from typing import Any, Callable, Dict, List, Optional, Tuple | ||
| import pandas as pd | ||
| import numpy as np | ||
| from functools import reduce | ||
| from .definitions import evaluated_cols | ||
| from .checks import _run_checks | ||
|
|
||
|
|
@@ -170,13 +169,25 @@ def _calculate_otm_pct(data: pd.DataFrame) -> pd.DataFrame: | |
| ) | ||
|
|
||
|
|
||
| def _get_leg_quantity(leg: Tuple) -> int: | ||
| """Get quantity for a leg, defaulting to 1 if not specified.""" | ||
| return leg[2] if len(leg) > 2 else 1 | ||
|
|
||
|
|
||
| def _apply_ratios(data: pd.DataFrame, leg_def: List[Tuple]) -> pd.DataFrame: | ||
| """Apply position ratios (long/short multipliers) to entry and exit prices.""" | ||
| """Apply position ratios (long/short multipliers) and quantities to entry and exit prices.""" | ||
| for idx in range(1, len(leg_def) + 1): | ||
| entry_col = f"entry_leg{idx}" | ||
| exit_col = f"exit_leg{idx}" | ||
| entry_kwargs = {entry_col: lambda r: r[entry_col] * leg_def[idx - 1][0].value} | ||
| exit_kwargs = {exit_col: lambda r: r[exit_col] * leg_def[idx - 1][0].value} | ||
| leg = leg_def[idx - 1] | ||
| multiplier = leg[0].value * _get_leg_quantity(leg) | ||
| # Use default arguments to capture values at each iteration (avoid late binding) | ||
| entry_kwargs = { | ||
| entry_col: lambda r, col=entry_col, m=multiplier: r[col] * m | ||
| } | ||
| exit_kwargs = { | ||
| exit_col: lambda r, col=exit_col, m=multiplier: r[col] * m | ||
| } | ||
| data = data.assign(**entry_kwargs).assign(**exit_kwargs) | ||
|
|
||
| return data | ||
|
|
@@ -206,6 +217,16 @@ def _assign_profit( | |
| return data | ||
|
|
||
|
|
||
| def _rename_leg_columns( | ||
| data: pd.DataFrame, leg_idx: int, join_on: List[str] | ||
| ) -> pd.DataFrame: | ||
| """Rename columns with leg suffix, excluding join columns.""" | ||
| rename_map = { | ||
| col: f"{col}_leg{leg_idx}" for col in data.columns if col not in join_on | ||
| } | ||
| return data.rename(columns=rename_map) | ||
|
Comment on lines
+220
to
+227
|
||
|
|
||
|
|
||
| def _strategy_engine( | ||
| data: pd.DataFrame, | ||
| leg_def: List[Tuple], | ||
|
|
@@ -237,19 +258,19 @@ def _rule_func( | |
| ) -> pd.DataFrame: | ||
| return d if r is None else r(d, ld) | ||
|
|
||
| partials = [leg[1](data) for leg in leg_def] | ||
| # Pre-rename columns for each leg to avoid suffix issues with 3+ legs | ||
| partials = [ | ||
| _rename_leg_columns(leg[1](data).copy(), idx, join_on or []) | ||
| for idx, leg in enumerate(leg_def, start=1) | ||
| ] | ||
| suffixes = [f"_leg{idx}" for idx in range(1, len(leg_def) + 1)] | ||
|
|
||
| return ( | ||
| reduce( | ||
| lambda left, right: pd.merge( | ||
| left, right, on=join_on, how="inner", suffixes=suffixes | ||
| ), | ||
| partials, | ||
| ) | ||
| .pipe(_rule_func, rules, leg_def) | ||
| .pipe(_assign_profit, leg_def, suffixes) | ||
| ) | ||
| # Merge all legs sequentially | ||
| result = partials[0] | ||
| for partial in partials[1:]: | ||
| result = pd.merge(result, partial, on=join_on, how="inner") | ||
|
|
||
| return result.pipe(_rule_func, rules, leg_def).pipe(_assign_profit, leg_def, suffixes) | ||
|
|
||
|
|
||
| def _process_strategy(data: pd.DataFrame, **context: Any) -> pd.DataFrame: | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The function docstring should document the expected structure of the
legtuple parameter, including that element [2] represents quantity and what elements [0] and [1] represent for clarity.