Skip to content

Commit

Permalink
Don't close long contracts.
Browse files Browse the repository at this point in the history
Also fixed the quantity handling.
  • Loading branch information
brndnmtthws committed Dec 20, 2023
1 parent c3b7af4 commit b39cd1f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
5 changes: 4 additions & 1 deletion thetagang.toml
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,15 @@ min_pnl = 0.0
# Optional: Don't roll contracts when the current DTE is greater than this
# number of days. This helps avoid cases where you end up rolling out to LEAPs.
# max_dte = 180

# Optional: Create a closing order when the P&L reaches this threshold. This
# overrides the other parameters, i.e., it ignores DTE and everything else.
# If not specified, it has no effect. This can handle the case where you have
# long-dated options that have slowly become worthless and you just want to get
# them out of your portfolio.
# them out of your portfolio. This only applies to short contract positions,
# long positions are ignored.
# close_at_pnl = 0.99

[roll_when.calls]
# Roll calls to the next expiration even if they're in the money. Defaults to
# true if not specified.
Expand Down
17 changes: 9 additions & 8 deletions thetagang/portfolio_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ def orderStatusEvent(self, trade):
f" status={trade.orderStatus.status}",
)

def get_calls(self, portfolio_positions):
return self.get_options(portfolio_positions, "C")
def get_short_calls(self, portfolio_positions):
return self.get_short_contracts(portfolio_positions, "C")

def get_puts(self, portfolio_positions):
return self.get_options(portfolio_positions, "P")
def get_short_puts(self, portfolio_positions):
return self.get_short_contracts(portfolio_positions, "P")

def get_options(
def get_short_contracts(
self, portfolio_positions: dict[str, list[PortfolioItem]], right: str
):
ret = []
Expand All @@ -97,6 +97,7 @@ def get_options(
isinstance(p.contract, Option)
and p.contract.right.startswith(right)
and p.contract.symbol in symbols
and p.position < 0 # short positions only
),
portfolio_positions[symbol],
)
Expand Down Expand Up @@ -647,7 +648,7 @@ def manage(self):

def check_puts(self, portfolio_positions):
# Check for puts which may be rolled to the next expiration or a better price
puts = self.get_puts(portfolio_positions)
puts = self.get_short_puts(portfolio_positions)

# find puts eligible to be rolled or closed
rollable_puts = []
Expand Down Expand Up @@ -681,7 +682,7 @@ def check_puts(self, portfolio_positions):

def check_calls(self, portfolio_positions):
# Check for calls which may be rolled to the next expiration or a better price
calls = self.get_calls(portfolio_positions)
calls = self.get_short_calls(portfolio_positions)

# find calls eligible to be rolled
rollable_calls = []
Expand Down Expand Up @@ -1152,7 +1153,7 @@ def close_positions(self, positions):
# if the price is near zero, use the minimum price
price = ticker.minTick

qty = -position.position
qty = abs(position.position)
order = LimitOrder(
"BUY" if is_short else "SELL",
qty,
Expand Down

0 comments on commit b39cd1f

Please sign in to comment.