Skip to content

Commit 72afafd

Browse files
committed
Refactor account multiplier logic in quantity calculation.
1 parent 017c34f commit 72afafd

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

allocation.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,25 @@ class PortfolioNotFoundError(Exception):
5353

5454

5555
def calculate_quantities() -> dict[str, float]:
56-
# Only allow cash allocations if multiplier results in zero/sub-zero cash reserve
57-
if (reserved_cash := (1 - Config.account_multiplier) * utils.account_equity()) < 0:
58-
if not Config.account_multiplier > 1: return {} # pure cash based allocation
59-
60-
# Ensure there's enough surplus cash to allocate (i.e. all available not reserved)
61-
if (cash_balance := float(utils.alpaca.get_account().cash)) - reserved_cash < 0:
56+
"""
57+
Invest the cash available after reserving cash according to multiplier.
58+
If the account multiplier is 1 or higher, reserved cash is irrelevant.
59+
Never use margin, these are pure-cash operations.
60+
"""
61+
reserved_cash = (1 - Config.account_multiplier) * utils.account_equity()
62+
63+
# Check for negative cash balance to abort cash operations
64+
if (cash_balance := float(utils.alpaca.get_account().cash)) < 0:
6265
return {}
6366

6467
# Subtract reserved cash if multiplier is under 1, otherwise naked cash allocation
65-
if Config.account_multiplier > 1: tradable_cash = cash_balance
66-
elif Config.account_multiplier <= 1: tradable_cash = cash_balance - reserved_cash
68+
if Config.account_multiplier >= 1: tradable_cash = cash_balance
69+
elif Config.account_multiplier < 1: tradable_cash = cash_balance - reserved_cash
6770

6871
quantities = {}
6972
for alloc in allocation.values():
7073
amount = alloc[0] * tradable_cash
71-
if amount < 2: return {}
74+
if amount <= 2: return {} # if any order is less than $2
7275
quantities[alloc[1]] = round(amount, 2)
7376

7477
return quantities

0 commit comments

Comments
 (0)