# While loop doesn't wait for the trailing stop order to be filled before re looping

**URL:** https://forum.alpaca.markets/t/while-loop-doesnt-wait-for-the-trailing-stop-order-to-be-filled-before-re-looping/13058
**Category:** Getting Started with Alpaca
**Created:** [October 26, 2023, 2:28pm UTC](https://forum.alpaca.markets/t/while-loop-doesnt-wait-for-the-trailing-stop-order-to-be-filled-before-re-looping/13058 "2023-10-26T14:28:40Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![AASAAQWE](https://avatars.discourse-cdn.com/v4/letter/a/b782af/32.png) [@AASAAQWE](https://forum.alpaca.markets/u/AASAAQWE)
#### Post date: [October 26, 2023, 2:28pm UTC](https://forum.alpaca.markets/t/while-loop-doesnt-wait-for-the-trailing-stop-order-to-be-filled-before-re-looping/13058/1 "2023-10-26T14:28:40Z")

</div>

import time

import yfinance as yf  
from alpaca.trading.client import TradingClient  
from alpaca.trading.enums import OrderSide, TimeInForce  
from alpaca.trading.requests import MarketOrderRequest, StopLossRequest, TrailingStopOrderRequest

import config

API\_KEY = config.API\_KEY  
API\_SECRET = config.API\_SECRET  
x = “AMC”  
stock\_data = yf.Ticker(x)  
historical\_data = stock\_data.history(period=‘1d’)  
current\_price = round(historical\_data[‘Close’].iloc[0], 2)  
c = current\_price  
Limit\_Round\_Up = round(float(c + c_0.01), 2)  
Stop\_Round\_Dwn = round(float(c - c_0.01), 2)

trading\_client = TradingClient(API\_KEY, API\_SECRET, paper=True)

# Get the start time of the trading operation

start\_time = time.time()

# Getting account information

account = trading\_client.get\_account()

while float(account.cash) \> 0:  
account = trading\_client.get\_account()  
print(f"Cash before Order : ${account.cash}“)  
print(f"Current Price of {x} is {current\_price}”)

```
# preparing market order
market_order_data = MarketOrderRequest(
    symbol=x,
    qty=1,
    side=OrderSide.BUY,
    time_in_force=TimeInForce.DAY
)

# Market order
market_order = trading_client.submit_order(
    order_data=market_order_data
)

while not market_order.status.FILLED:
    time.sleep(1)

trailing_percent_data = TrailingStopOrderRequest(
    symbol="SPY",
    qty=1,
    side=OrderSide.SELL,
    time_in_force=TimeInForce.GTC,
    trail_percent=1.00, # hwm * 0.99
    stop_loss=StopLossRequest(stop_price=Stop_Round_Dwn)
)

trailing_percent_order = trading_client.submit_order(
    order_data=trailing_percent_data
)

while not trailing_percent_order.status.FILLED:
    time.sleep(1)

```

---

<div class="post-metadata">

### Author: ![Dan\_Whitnable\_Alpaca](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/dan_whitnable_alpaca/32/1658_2.png) [@Dan\_Whitnable\_Alpaca](https://forum.alpaca.markets/u/Dan_Whitnable_Alpaca)
#### Post date: [October 26, 2023, 2:41pm UTC](https://forum.alpaca.markets/t/while-loop-doesnt-wait-for-the-trailing-stop-order-to-be-filled-before-re-looping/13058/2 "2023-10-26T14:41:43Z")

</div>

@AASAAQWE Try fetching the updated status of the order inside the while loop. The order exists on the Alpaca servers and to get the latest order status you need to explicitly get the order to refresh the status. Something like this.

```auto
while not market_order.status.FILLED:
    time.sleep(1)
    market_order = trading_client.get_order_by_id(market_order.id)

```

---

<div class="post-metadata">

### Author: ![AASAAQWE](https://avatars.discourse-cdn.com/v4/letter/a/b782af/32.png) [@AASAAQWE](https://forum.alpaca.markets/u/AASAAQWE)
#### Post date: [October 27, 2023, 5:17am UTC](https://forum.alpaca.markets/t/while-loop-doesnt-wait-for-the-trailing-stop-order-to-be-filled-before-re-looping/13058/3 "2023-10-27T05:17:30Z")

</div>

Thank you for your response.

The new while not place holder for the market order is now:

```
while not market_order.status.FILLED:
    time.sleep(1)
    market_order = trading_client.get_order_by_id(market_order.id)

```

The new while not place holder for the Trailing percent sell is now:

```
while not trailing_percent_order.status.FILLED:
    time.sleep(1)
    trailing_percent_order = trading_client.get_order_by_id(trailing_percent_order.id)

```

I will run it when the market opens and hopefully it works! Thanks again.

---

<div class="post-metadata">

### Author: ![AASAAQWE](https://avatars.discourse-cdn.com/v4/letter/a/b782af/32.png) [@AASAAQWE](https://forum.alpaca.markets/u/AASAAQWE)
#### Post date: [October 27, 2023, 1:51pm UTC](https://forum.alpaca.markets/t/while-loop-doesnt-wait-for-the-trailing-stop-order-to-be-filled-before-re-looping/13058/4 "2023-10-27T13:51:43Z")

</div>

just ran the program with your recommendations and the loop still ignored the while not function. I don’t mean to in convince you but if you have any other ideas id really appreciate them.

---

<div class="post-metadata">

### Author: ![Dan\_Whitnable\_Alpaca](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/dan_whitnable_alpaca/32/1658_2.png) [@Dan\_Whitnable\_Alpaca](https://forum.alpaca.markets/u/Dan_Whitnable_Alpaca)
#### Post date: [October 27, 2023, 3:41pm UTC](https://forum.alpaca.markets/t/while-loop-doesnt-wait-for-the-trailing-stop-order-to-be-filled-before-re-looping/13058/5 "2023-10-27T15:41:29Z")

</div>

@AASAAQWE Another issue which may be the problem. The value `trailing_percent_order.status.FILLED` is not a Boolean value. So, your while condition won’t work as expected.

```
      while not trailing_percent_order.status.FILLED:

```

Change that to something like this (you may need to import OrderStatus first)

```
      from alpaca.trading.enums import OrderStatus
      while market_order.status != OrderStatus.FILLED:

```

Give that a try.

---

<div class="post-metadata">

### Author: ![AASAAQWE](https://avatars.discourse-cdn.com/v4/letter/a/b782af/32.png) [@AASAAQWE](https://forum.alpaca.markets/u/AASAAQWE)
#### Post date: [October 30, 2023, 5:11pm UTC](https://forum.alpaca.markets/t/while-loop-doesnt-wait-for-the-trailing-stop-order-to-be-filled-before-re-looping/13058/6 "2023-10-30T17:11:11Z")

</div>

```
    while trailing_percent_order.status != OrderStatus.FILLED:
        time.sleep(1)

```

Is this correct? I’ve noticed the opposite is happening now. it’s getting stuck in the new while loop and not moving passed it. I added so strings to show what was happening and the program repeated the string over and over. it looks to me that the new while loop just gets stuck updating the order status and doesn’t really get updated whether to order is filled or not.

---

<div class="post-metadata">

### Author: ![Dan\_Whitnable\_Alpaca](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/dan_whitnable_alpaca/32/1658_2.png) [@Dan\_Whitnable\_Alpaca](https://forum.alpaca.markets/u/Dan_Whitnable_Alpaca)
#### Post date: [October 30, 2023, 5:44pm UTC](https://forum.alpaca.markets/t/while-loop-doesnt-wait-for-the-trailing-stop-order-to-be-filled-before-re-looping/13058/7 "2023-10-30T17:44:03Z")

</div>

@AASAAQWE Now your issue is back to needing to check the order for updates. Try this

```auto
while trailing_percent_order.status != OrderStatus.FILLED:
  time.sleep(1)
  # get the order status to see if it's changed
  trailing_percent_order = trading_client.get_order_by_id(trailing_percent_order.id)

```

---

<div class="post-metadata">

### Author: ![AASAAQWE](https://avatars.discourse-cdn.com/v4/letter/a/b782af/32.png) [@AASAAQWE](https://forum.alpaca.markets/u/AASAAQWE)
#### Post date: [October 30, 2023, 8:14pm UTC](https://forum.alpaca.markets/t/while-loop-doesnt-wait-for-the-trailing-stop-order-to-be-filled-before-re-looping/13058/8 "2023-10-30T20:14:06Z")

</div>

That seemed to work! thank you very much!
