@@ -53,22 +53,25 @@ class PortfolioNotFoundError(Exception):
53
53
54
54
55
55
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 :
62
65
return {}
63
66
64
67
# 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
67
70
68
71
quantities = {}
69
72
for alloc in allocation .values ():
70
73
amount = alloc [0 ] * tradable_cash
71
- if amount < 2 : return {}
74
+ if amount <= 2 : return {} # if any order is less than $2
72
75
quantities [alloc [1 ]] = round (amount , 2 )
73
76
74
77
return quantities
0 commit comments