Skip to content

Make the library more Options friendly #754

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

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions lumibot/components/options_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -1086,7 +1086,7 @@ def aggregate_portfolio_greeks(self, positions: List, underlying_asset: Asset) -
self.strategy.log_message(f"Aggregated Greeks: {aggregated}", color="blue")
return aggregated

def check_spread_profit(self, initial_cost: float, orders: List[Order]) -> Optional[float]:
def check_spread_profit(self, initial_cost: float, orders: List[Order], contract_multiplier: int = 100) -> Optional[float]:
"""
Calculate the current profit or loss percentage of a spread based on updated market prices.

Expand All @@ -1096,6 +1096,8 @@ def check_spread_profit(self, initial_cost: float, orders: List[Order]) -> Optio
The initial net cost (or credit) of establishing the spread.
orders : List[Order]
The list of orders that constitute the spread.
contract_multiplier : int, optional
The Option contract multiplier to use (default is 100)

Returns
-------
Expand All @@ -1110,11 +1112,11 @@ def check_spread_profit(self, initial_cost: float, orders: List[Order]) -> Optio
self.strategy.log_message(f"Price unavailable for {order.asset.symbol}; cannot calculate spread profit.", color="red")
return None
multiplier = -1 if order.side.lower() == "buy" else 1
current_value += price * order.quantity * 100 # Options standard multiplier
current_value += price * order.quantity * contract_multiplier
profit_pct = ((current_value - initial_cost) / initial_cost) * 100
self.strategy.log_message(f"Spread profit percentage: {profit_pct:.2f}%", color="blue")
return profit_pct

# ============================================================
# End of OptionsHelper Component
# ============================================================
# ============================================================
6 changes: 5 additions & 1 deletion lumibot/entities/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -1096,7 +1096,11 @@ def to_position(self, quantity):
def get_increment(self):
increment = self.quantity
if self.side == SELL:
increment = -increment
if not self.is_option():
increment = -increment
if self.side == BUY:
if self.is_option():
increment = -increment
return float(increment)

def is_option(self):
Expand Down
4 changes: 2 additions & 2 deletions lumibot/strategies/strategy_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -630,9 +630,9 @@ def _on_filled_order(self, position, order, price, quantity, multiplier):
# Calculate the value of the position
order_value = price * float(quantity)

# If option, multiply % of portfolio by 100
# If option, multiply % of portfolio by multiplier
if order.asset.asset_type == Asset.AssetType.OPTION:
order_value = order_value * 100
order_value = order_value * multiplier

# Calculate the percent of the portfolio that this position represents
percent_of_portfolio = order_value / portfolio_value
Expand Down
Loading