Take Profit must be < Stop Loss when selling Long Position?

I am getting strange error where the API returns error of {"code":42210000,"message":"take_profit.limit_price must be \u003c stop_loss.stop_price"} when I try to place SELL order with bracket.

This is paper trading environment.

Here is my code:

# Code to get existing positions
 # Fetch all open positions
positions = trading_client.get_all_positions()

# Print details for each position
for position in positions:
    print(f"Symbol: {position.symbol}")
    print(f"Quantity: {position.qty}")
    print(f"Market Value: {position.market_value}")
    print(f"Unrealized P/L: {position.unrealized_pl}")
    print(f"Current Price: {position.current_price}")
    print(f"Average Entry Price: {position.avg_entry_price}")
    print(f"Position Side: {position.side}")
    print("-" * 30)

The existing position is:
Symbol: NVDA
Quantity: 976.46268837
Market Value: 101278.710038
Unrealized P/L: 1283.710039
Current Price: 103.72
Average Entry Price: 102.405347
Position Side: PositionSide.LONG

However, the following code resulted in the confusing error, which is to my understanding, as if I were trying to close a short position, but it’s not.

sell_nvda_position = MarketOrderRequest(
    symbol='NVDA',
    qty=976,
    side=OrderSide.SELL,
    time_in_force=TimeInForce.GTC,
    order_class=OrderClass.BRACKET,
    stop_loss=StopLossRequest(stop_price=101.38),
    take_profit=TakeProfitRequest(limit_price=105.5)
)

order = trading_client.submit_order(sell_nvda_position)

I got error APIError: {"code":42210000,"message":"take_profit.limit_price must be \u003c stop_loss.stop_price"} when I tried to submit the order.

Can I get help on what I am missing/misunderstanding here?

seems like you might be misunderstanding bracket orders.

as far as I understand, bracket orders are three orders:
entry, stop loss and take profit.

as you have an existing position it sounds like you don’t want the entry part of the order. You probably want to create an OCO (one cancels the other) order, which just has stop loss and take profit. (order_class OCO)

See Orders at Alpaca

1 Like