# Python - use api.list\_orders() to cancel stop loss before selling profit

**URL:** <https://forum.alpaca.markets/t/python-use-api-list-orders-to-cancel-stop-loss-before-selling-profit/1168>\
**Category:** Alpaca Integration Applications\
**Created:** [April 14, 2020, 1:43am UTC](https://forum.alpaca.markets/t/python-use-api-list-orders-to-cancel-stop-loss-before-selling-profit/1168 "2020-04-14T01:43:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Capo](https://avatars.discourse-cdn.com/v4/letter/c/ec9cab/32.png) [@Capo](https://forum.alpaca.markets/u/Capo)\
**Post date:** [April 14, 2020, 1:43am UTC](https://forum.alpaca.markets/t/python-use-api-list-orders-to-cancel-stop-loss-before-selling-profit/1168/1 "2020-04-14T01:43:13Z")

</div>

I am submitting OTO bracket orders that uses a limit buy with a stop loss. I use another strategy indicator to trigger when to sell therefore a simple Bracket take\_profit will not be appropriate for my solution.

My understanding is that I cannot liquidate a position (DELETE/v2/positions/{symbol} ) with an open stop\_loss order? For this reason I am trying to get a list of open orders and parse through the list returned by the API to find the order id for my symbol, and then cancel the order using the order id.

How do I parse through the orders list? Can anyone include a good code example? I imagine this requires json.dumps or similar.

Pseudocode of what i’m trying to do:  
#Need to sell ‘symbol’: ‘AAPL’  
check\_orders = api.list\_orders(status=‘open’)  
print(check\_orders)

#Parse order data returned by API, look for open orders matching ‘AAPL’, return order\_id  
api.cancel\_order(order\_id)  
api.submit\_order(‘sell’, ‘AAPL’)

---

<div class="post-metadata">

**Author:** ![hitoshi](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/hitoshi/32/69_2.png) [@hitoshi](https://forum.alpaca.markets/u/hitoshi)\
**Post date:** [April 14, 2020, 4:39am UTC](https://forum.alpaca.markets/t/python-use-api-list-orders-to-cancel-stop-loss-before-selling-profit/1168/2 "2020-04-14T04:39:43Z")

</div>

Something like this couuld do.

```auto
#Need to sell ‘symbol’: ‘AAPL’
check_orders = api.list_orders(status=‘open’)
print(check_orders)

for o in check_orders:
  if o.symbol == 'AAPL':
    api.cancel_order(o.id)
api.submit_order('AAPL', side='sell', ...)

```

You can also use `api.close_position('AAPL')` if market order works for you.

---

<div class="post-metadata">

**Author:** ![Capo](https://avatars.discourse-cdn.com/v4/letter/c/ec9cab/32.png) [@Capo](https://forum.alpaca.markets/u/Capo)\
**Post date:** [April 14, 2020, 5:05am UTC](https://forum.alpaca.markets/t/python-use-api-list-orders-to-cancel-stop-loss-before-selling-profit/1168/3 "2020-04-14T05:05:05Z")

</div>

Thank you @hitoshi !!! This is exactly what I was looking for. For Market orders, will api.close\_position(‘AAPL’) still work even if I have a pending stop\_loss order from OTO?

---

<div class="post-metadata">

**Author:** ![hitoshi](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/hitoshi/32/69_2.png) [@hitoshi](https://forum.alpaca.markets/u/hitoshi)\
**Post date:** [April 14, 2020, 5:50am UTC](https://forum.alpaca.markets/t/python-use-api-list-orders-to-cancel-stop-loss-before-selling-profit/1168/4 "2020-04-14T05:50:33Z")

</div>

No, you still need to cancel the order first.
