 Investing a percentage of balance

Is there a way to only invest 1% of your Alpaca balance instead having the qty as a whole number?
something like
“qty”: balance * .01 ,
“qty”: 1,

1 Like

I agree, will be a very usefoul function, I would consider 3 options pct of whole account, pct of buying power, pct of cash

Hey Mike_Oxsmall (condolences BTW ),

You could do something like this:

trade_amount = round(float(account.cash) * _percentage, 2)
# This will calculate the given percentage of your account cash (0.02 = 2%, 0.1 = 10% etc.)
# The calculated number is rounded to 2 decimal places.

def get_latest_price(_symbol):
bar = api.get_latest_bar(_symbol)
# return the latest Close price
return bar.c

# Call this to place the order. If the asset can be traded with fractional quantities, just use the calculated trade amount. Otherwise, use integer division to find the largest whole number that your funds will allow.
asset = api.get_asset(_symbol)
if asset.fractionable: # Then check if the asset allows fractional orders.
order = api.submit_order(
symbol=_symbol,
type='market',
time_in_force='gtc')
else:
order = api.submit_order(
symbol=_symbol,