Trailing orders error

Environment

Language
Python 3.8.5

Alpaca SDK Version
alpaca trade api 0.51

Other Environment Details
Both windows 10 and linux

Problem

Submitting a trailing order returns error for quantity on paper trading:
I have the following position open:

Position({   'asset_class': 'us_equity',
 'asset_id': 'f9700a20-b0fe-4516-87c5-9d7b096d5539',
 'avg_entry_price': '29.15',
 'change_today': '0.0013490725126476',
 'cost_basis': '2157.1',
 'current_price': '29.69',
 'exchange': 'NYSE',
 'lastday_price': '29.65',
 'market_value': '2197.06',
 'qty': '74',
 'side': 'long',
 'symbol': 'WFC',
 'unrealized_intraday_pl': '2.96',
 'unrealized_intraday_plpc': '0.0013490725126476',
 'unrealized_pl': '39.96',
 'unrealized_plpc': '0.01852487135506'})

and i want to fill a trailing stop order on that position:

order = api.submit_order(symbol='WFC',qty='74',side='sell',type='trailing_stop',trail_percent='1',time_in_force='gtc')

I receive an error on the quantity
APIError: insufficient qty available for order (requested: 74, available: 0)

But as you can see from the position the quantity of 74 is in the position, for that position i have also a cascading order open (take profit and stop loss).

 Order({   'asset_class': 'us_equity',
 'asset_id': 'f9700a20-b0fe-4516-87c5-9d7b096d5539',
 'canceled_at': None,
 'client_order_id': '0c22053e-a918-488a-9711-d19de34d9209',
 'created_at': '2020-12-10T15:18:42.75244Z',
 'expired_at': None,
 'extended_hours': False,
 'failed_at': None,
 'filled_at': None,
 'filled_avg_price': None,
 'filled_qty': '0',
 'hwm': None,
 'id': '3216de28-505b-44a1-842c-f4d0d1dac8af',
 'legs': None,
 'limit_price': '35.016',
 'order_class': 'bracket',
 'order_type': 'limit',
 'qty': '74',
 'replaced_at': None,
 'replaced_by': None,
 'replaces': None,
 'side': 'sell',
 'status': 'new',
 'stop_price': None,
 'submitted_at': '2020-12-10T15:18:42.923835Z',
 'symbol': 'WFC',
 'time_in_force': 'gtc',
 'trail_percent': None,
 'trail_price': None,
 'type': 'limit',
 'updated_at': '2020-12-10T15:18:42.939057Z'})

Let us know if you’re using the paper trading simulator or the live trading API.

Example Code
This is the trailing order function

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

The total number of shares in open orders cannot exceed the actual shares held. Since there are currently 74 shares held, and there is already a limit order to sell 74 shares (as part of the cascading or ‘bracket’ order), one cannot place another order to sell an additional 74 shares. That is what the error APIError: insufficient qty available for order (requested: 74, available: 0) is saying. There are 0 shares available for new orders.

The rational for this is to prevent a situation where both orders filled and one would therefore be trying to sell more shares than they have.

If you really want the new trailing stop order in place of the existing limit order, then simply cancel the limit order. Then you will be able to submit the trailing stop order. Note if you cancel one of the legs of a bracket order (eg the limit order) then the other leg automatically gets cancelled to.

Hope that helps explain what’s going on.

Thanks Dan, this actually make sense ! I would suggest to state it somware in the docs.
When do you think that bracket orders can include a trailing sell ? I think will be a great function.

Thanks.

Allowing a trailing stop order as a leg in a bracket order has been requested by other users too. It’s definitely on the list (but no ETA). One can always replace the stop price of the stop order. Your algo can adjust that price up or down as desired.

@Riodda

Feel free to make an issue stating this feature request on the Alpaca API Github repo if there is not already an open issue for it

Jason

But what should I do if I cannot cancel the order?
I’m getting this message:
Could not cancel orders
There was a problem trying to cancel your orders. Please try again.