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

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 + c0.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)

@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.

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

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.

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.

@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.

    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.

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

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)

That seemed to work! thank you very much!