Handling "insufficient buying power"

My current strategy is looking at a number of stocks overnight and providing buy and sell signals along with bull and bear indicators. My goal is to place bracket orders for all of them in accordance with the buy_power divided by the number of stocks I found in the evening and prior to the market opening.


My code returns 14 stocks that meet my model’s parameters to buy/short.

I look at the current buying power and divide that by 10. That will be my rough purchase amount for each position. (I build in an edge case in the event the last purchase has less than the slipt amount).

As I am establishing the orders, I am printing out the current paper accounts buying power and cash value:

Current Buying Power going into next order: 377224.3, Purchase Value: 26944.592857142856, Current Cash: 77224.3
Buy Order of 578 Shares of: XLRE, at 45.99
Current Buying Power going into next order: 350642.08, Purchase Value: 25045.862857142856, Current Cash: 77224.3
Buy Order of 161 Shares of: XLK, at 152.68
Current Buying Power going into next order: 326060.6, Purchase Value: 23290.042857142853, Current Cash: 77224.3
Buy Order of 338 Shares of: XLU, at 66.71
Current Buying Power going into next order: 326288.32, Purchase Value: 23306.30857142857, Current Cash: 100014.22
Buy Order of 483 Shares of: XLE, at 48.06
Current Buying Power going into next order: 303075.34, Purchase Value: 21648.238571428574, Current Cash: 100014.22

As you can see there is enough Buying Power going into the next order after “XLE”, but I get the following error.

~\anaconda3\lib\site-packages\alpaca_trade_api\rest.py in _one_request(self, method, url, opts, retry)
    166                 error = resp.json()
    167                 if 'code' in error:
--> 168                     raise APIError(error, http_error)
    169             else:
    170                 raise

APIError: insufficient buying power

I have looked around on slack and here, but none of them can give me a good answer as to why I cannot complete the other 10 orders when I am deliberately trying to account for the buying power prior to starting the order-making process.

Here is my bracket order function in case that helps.

    def buy_bracket_order(self, symbol, qty_value, buy_value, sell_value):
        limit_price_value = 1 - (((sell_value - buy_value) / buy_value) / 2)
        stop_price_value = 1.01 - (((sell_value - buy_value) / buy_value) / 2)
                        limit_price = buy_value, 
                        stop_loss={'stop_price': buy_value * stop_price_value,
                                   'limit_price':  buy_value * limit_price_value},
                        take_profit={'limit_price': sell_value}