Skip to content
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

Include pending orders in cash balance #338

Merged
merged 1 commit into from
Dec 12, 2023
Merged
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
30 changes: 25 additions & 5 deletions thetagang/portfolio_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -1755,6 +1755,21 @@ def vix_calls_should_be_closed() -> (

console.print(Panel(Group(*to_print), title="VIX call hedging"))

def calc_pending_cash_balance(self):
return sum(
[
order.lmtPrice * order.totalQuantity * float(contract.multiplier)
for (contract, order) in self.orders
if order.action == "SELL"
]
) - sum(
[
order.lmtPrice * order.totalQuantity * float(contract.multiplier)
for (contract, order) in self.orders
if order.action == "BUY"
]
)

def do_cashman(self, account_summary, portfolio_positions):
to_print = []

Expand All @@ -1769,6 +1784,7 @@ def inner_handler():
buy_threshold = self.config["cash_management"]["buy_threshold"]
sell_threshold = self.config["cash_management"]["sell_threshold"]
cash_balance = math.floor(float(account_summary["TotalCashValue"].value))
pending_balance = self.calc_pending_cash_balance()

try:

Expand All @@ -1791,14 +1807,16 @@ def make_order() -> tuple[Optional[Ticker], Optional[LimitOrder]]:
else self.config["orders"]["algo"]
)

amount = cash_balance - target_cash_balance
amount = cash_balance - (target_cash_balance + pending_balance)
price = ticker.ask if amount > 0 else ticker.bid
qty = amount // price

if qty > 0:
to_print.append(
f"[green]cash_balance={cash_balance} which exceeds "
f"(target_cash_balance + buy_threshold)={(target_cash_balance + buy_threshold)}"
f"[green]cash_balance={dfmt(cash_balance)} which exceeds "
f"(target_cash_balance + pending_balance + buy_threshold)="
f"{(dfmt(target_cash_balance + pending_balance + buy_threshold))} "
f"with pending_balance={dfmt(pending_balance)}"
)
to_print.append(
f"[green]Will buy {symbol} with qty={qty} shares at price={price}"
Expand All @@ -1809,8 +1827,10 @@ def make_order() -> tuple[Optional[Ticker], Optional[LimitOrder]]:
# subtract 1 to keep cash balance above target
qty -= 1
to_print.append(
f"[green]cash_balance={cash_balance} which is less than "
f"(target_cash_balance - sell_threshold)={(target_cash_balance - sell_threshold)}"
f"[green]cash_balance={dfmt(cash_balance)} which is less than "
f"(target_cash_balance + pending_balance - sell_threshold)="
f"{(dfmt(target_cash_balance + pending_balance - sell_threshold))} "
f"with pending_balance={dfmt(pending_balance)}"
)
if symbol not in portfolio_positions:
# we don't have any positions to sell
Expand Down