Trailing Stop Buy and Buying Power

Hi all, I’m playing with the traling stop orders. With trailing stop sell all seems to be working ok, but with if i have short position and I want to create a trailing stop buy the system checks if i have enough buying power, but since i have an open short position, if i match the qty of the short position the buying power should not be take in account…or i miss something?
here is my python experimental code, any comment wellcome.

def alpaca_trailing_buy_order(_symbol,_qty,_trailing_pct):
    _order = api.submit_order(symbol=_symbol,qty=_qty,side='buy',type='trailing_stop',trail_percent=_trailing_pct,time_in_force='gtc')
    return _order

def alpaca_trailing_sell_order(_symbol,_qty,_trailing_pct):
    _order = api.submit_order(symbol=_symbol,qty=_qty,side='sell',type='trailing_stop',trail_percent=_trailing_pct,time_in_force='gtc')
    return _order

positions = api.list_positions()
orders = api.list_orders()

submit_trailing_order = True

for position in positions:    
    submit_trailing_order = True    
    for order in orders:
        if (order.type == 'trailing_stop') and (order.symbol == position.symbol):
            print('Trailing position already present ',position.symbol)  
            submit_trailing_order = False 
    if (position.side=='long') and (float(position.unrealized_plpc)>0.01) and (submit_trailing_order == True):
        print('Place stop sell order for per',position.symbol,position.unrealized_plpc) 
        alpaca_trailing_sell_order(position.symbol,position.qty,'0.99')
    if (position.side=='short') and (float(position.unrealized_plpc)>-0.01):
        print('Place stop buy order for ',position.symbol,position.unrealized_plpc)
        alpaca_trailing_buy_order(position.symbol,str(-1*float(position.qty)),'0.99')

I have a SQQQ short position open and the error i receive when i try to place a trailing stop buy is:

---------------------------------------------------------------------------
HTTPError                                 Traceback (most recent call last)
H:\anaconda3\envs\plaid-ml\lib\site-packages\alpaca_trade_api\rest.py in _one_request(self, method, url, opts, retry)
    139         try:
--> 140             resp.raise_for_status()
    141         except HTTPError as http_error:

H:\anaconda3\envs\plaid-ml\lib\site-packages\requests\models.py in raise_for_status(self)
    940         if http_error_msg:
--> 941             raise HTTPError(http_error_msg, response=self)
    942 

HTTPError: 403 Client Error: Forbidden for url: https://paper-api.alpaca.markets/v2/orders

During handling of the above exception, another exception occurred:

APIError                                  Traceback (most recent call last)
<ipython-input-18-19d6f7d934e4> in <module>
     15     if (position.side=='short') and (float(position.unrealized_plpc)>-0.01):
     16         print('Piazzo Trailing stop buy per',position.symbol,position.unrealized_plpc)
---> 17         alpaca_trailing_buy_order(position.symbol,str(-1*float(position.qty)),'0.99')
     18 
     19 

<ipython-input-7-a081f3ce1115> in alpaca_trailing_buy_order(_symbol, _qty, _trailing_pct)
     17 
     18 def alpaca_trailing_buy_order(_symbol,_qty,_trailing_pct):
---> 19     _order = api.submit_order(symbol=_symbol,qty=_qty,side='buy',type='trailing_stop',trail_percent=_trailing_pct,time_in_force='gtc')
     20     return _order
     21 

H:\anaconda3\envs\plaid-ml\lib\site-packages\alpaca_trade_api\rest.py in submit_order(self, symbol, qty, side, type, time_in_force, limit_price, stop_price, client_order_id, extended_hours, order_class, take_profit, stop_loss, trail_price, instructions, trail_percent)
    314         if instructions is not None:
    315             params['instructions'] = instructions
--> 316         resp = self.post('/orders', params)
    317         return Order(resp)
    318 

H:\anaconda3\envs\plaid-ml\lib\site-packages\alpaca_trade_api\rest.py in post(self, path, data)
    157 
    158     def post(self, path, data=None):
--> 159         return self._request('POST', path, data)
    160 
    161     def put(self, path, data=None):

H:\anaconda3\envs\plaid-ml\lib\site-packages\alpaca_trade_api\rest.py in _request(self, method, path, data, base_url, api_version)
    117         while retry >= 0:
    118             try:
--> 119                 return self._one_request(method, url, opts, retry)
    120             except RetryException:
    121                 retry_wait = self._retry_wait

H:\anaconda3\envs\plaid-ml\lib\site-packages\alpaca_trade_api\rest.py in _one_request(self, method, url, opts, retry)
    146                 error = resp.json()
    147                 if 'code' in error:
--> 148                     raise APIError(error, http_error)
    149             else:
    150                 raise

APIError: insufficient buying power

“since i have an open short position, if i match the qty of the short position the buying power should not be take in account”

This isn’t always true. There are several considerations which come into play when determining “buying power”.

When was the existing short position opened? If it was opened on the same day as the trailing stop order was placed, then it is a potential day trade. The system looks at your day trade buying power. If there isn’t enough to cover the trade then the system won’t allow the order to be placed. It assumes the worst case that the trailing stop order will fill, the trade becomes a day trade, and your account goes over your day trading buying power.

There is a bit more explanation in the Alpaca docs.

Thanks Dan, actually the position was quite old, not for sure a day trade.
the only thing i can say is that in the same day i have filled other trailing orders.