Hello everyone,
I’m running into an error while trying to update bracket orders based on new market conditions. My code is supposed to access the legs attribute of an existing bracket order to update the take profit and stop-loss parameters dynamically for existing bracket orders. However, I’m getting an AttributeError stating that a ‘list’ object has no attribute ‘legs’. Below is the relevant portion of my code and the traceback of the error:
elif(buy < buy_threshold or sell < buy):
pos_info = alpaca_functions.position(account, handle)
if(pos_info != 0):
try:
buy_handle = alpaca_functions.quote(handle, 1)
exit_target = predictions["Exit Target (Buy)"].item()
stop_loss = predictions["Stop-Loss (Buy)"].item()
print(f"the entire set of data is: {pos_info} \n the handle is {handle} the exit target is {exit_target} and the stop loss is {stop_loss}")
alpaca_functions.update_bracket(account, pos_info, handle, predictions["Exit Target (Buy)"].item(), predictions["Stop-Loss (Buy)"].item(), buy_handle)
except Exception as e:
# Fetch the traceback as a string
traceback_str = traceback.format_exc()
error_message = f"Error when updatng update_bracket {handle}: {e}\nTraceback:\n{traceback_str}."
which calls the script update bracket:
def update_bracket(account: TradingClient, pos_info, symbol, exit_target, stop_loss, buy_handle):
if (pos_info == 0):
return 0
print(f"The symbol is {symbol}, position contains {pos_info.qty} shares, the updated exit target is ${exit_target}, the updated stop loss is ${stop_loss}.")
exit_target = round(buy_handle+float(exit_target),2)
stop_loss = round(buy_handle+float(stop_loss),2)
extant_order=get_Order(account, symbol)
print(extant_order)
take_profit_leg = extant_order.legs[0]
stop_loss_leg = extant_order.legs[1]
updates_to_take_profit = ReplaceOrderRequest(limit_price=exit_target)
updates_to_stop_loss = ReplaceOrderRequest(stop_price=stop_loss)
account.replace_order_by_id(take_profit_leg.id, updates_to_take_profit)
account.replace_order_by_id(stop_loss_leg.id, updates_to_stop_loss)
def get_Order(account: TradingClient, handle):
"""Converts handle into list[str] then fetches all orders on that handle."""
# get a list of orders including closed (e.g. filled) orders by specifying symbol
req = GetOrdersRequest(
status = QueryOrderStatus.OPEN,
symbols = [handle]
)
return account.get_orders(req)
Traceback (most recent call last):
File “c:\Users\XXXXXXXXX\main.py”, line 352, in stock_loop
alpaca_functions.update_bracket(account, pos_info, handle, predictions[“Exit Target (Buy)”].item(), predictions[“Stop-Loss (Buy)”].item(), buy_handle)
File “c:\Users\XXXXXXXXX\alpaca_functions.py”, line 357, in update_bracket
take_profit_leg = extant_order.legs[0]
^^^^^^^^^^^^^^^^^
AttributeError: ‘list’ object has no attribute ‘legs’
To explain the code a little more, the code runs through a list of potential positions and for some with a strong signal, a buy order is created, others are sold, and the rest of the symbols, I check if I have an open position and update the bracket order (the structure will need to be tweaked, but it works for now, I think). It is the last case where the issues are coming up. I already check to make sure that I am in position before I try and update the bracket.
Based on the Alpaca API documentation, this approach should work as the bracket order should be an object with a ‘legs’ attribute containing the individual legs of the bracket. Could there be something I’m missing in how bracket orders are handled or retrieved? Has anyone else encountered this issue or have insights on what might be causing this error?
Any help or guidance would be greatly appreciated!
Thank you!