Skip to content

Commit

Permalink
When updating price, use halfway between midpoint
Browse files Browse the repository at this point in the history
Previous we'd pick the midpoint price for an order, then wait a few
seconds, and then pick the midpoint again. This can lead to
less-than-ideal fills on illiquid dogs hit stocks/ETFs. Instead, we'll
take the halfway point between the previous and updated midpoint, which
should give us slightly less bad fills.

Additionally, we never want to switch a credit to a debit or vice versa,
so check that the signs match before proceeding.
  • Loading branch information
brndnmtthws committed Nov 30, 2023
1 parent c61cc29 commit c9325c5
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions thetagang/portfolio_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -1959,8 +1959,13 @@ def adjust_prices(self):
ticker, wait_time=self.api_response_wait_time()
):
(contract, order) = (trade.contract, trade.order)
updated_price = round(ticker.midpoint(), 2)
if order.lmtPrice != updated_price:
updated_price = round((order.lmtPrice + ticker.midpoint()) / 2.0, 2)
# Check if the updated price is actually any different
# before proceeding, and make sure the signs match so we
# don't switch a credit to a debit or vice versa.
if order.lmtPrice != updated_price and np.sign(
order.lmtPrice
) == np.sign(updated_price):
console.print(
f"[green]Resubmitting order for {contract.symbol}"
f" with old lmtPrice={dfmt(order.lmtPrice)} updated lmtPrice={dfmt(updated_price)}"
Expand Down

0 comments on commit c9325c5

Please sign in to comment.