alpacahq / alpaca-py

The Official Python SDK for Alpaca API
https://alpaca.markets/sdks/python/getting_started.html
Apache License 2.0
600 stars 147 forks source link

[Bug]: Order filled status is not displayed #509

Open avrenli2 opened 1 month ago

avrenli2 commented 1 month ago

Is there an existing issue for this?

Current Behavior

When I place a sell order, I would like to check if the sell order is filled or not. But when paper trading, the filled_at attribute of the order remains None and is not updated. For instance, I place an order for buying Bitcoin:

CLIENT = TradingClient( api_key='  ', secret_key='  ', paper=True)

BITCOIN_MONEY  = 2000

from alpaca.trading.requests import MarketOrderRequest

from alpaca.trading.enums import OrderSide, TimeInForce

BITCOIN_BUY_DATA = MarketOrderRequest( symbol="BTC/USD" , notional=BITCOIN_MONEY , side=OrderSide.BUY , 
                                      time_in_force=TimeInForce.GTC)

BUY_BITCOIN = CLIENT.submit_order(order_data=BITCOIN_BUY_DATA)

After buying the asset, I submit a sell order:

for X in CLIENT.get_all_positions():

  if X.symbol == "BTCUSD" :

     BITCOIN_QTY = float(X.qty)

     print( BITCOIN_QTY)

BITCOIN_SELL_DATA = MarketOrderRequest( symbol="BTCUSD" , qty=BITCOIN_QTY, side=OrderSide.SELL , 
                                      time_in_force=TimeInForce.GTC)

SELL_BITCOIN = CLIENT.submit_order(order_data=BITCOIN_SELL_DATA)

from datetime import sleep

sleep(5)

print(SELL_BITCOIN)

As seen, this prints the filled_at attribute as:

filled_at': None

How do we check if an order is filled or not?

Expected Behavior

When you run the following code with your API keys, the filled_at attribute of SELL_BITCOIN must differ from None. However, it remains None even after the order is filled. How do we trace if the order is filled or not?

CLIENT = TradingClient( api_key='  ', secret_key='  ', paper=True)

BITCOIN_MONEY  = 2000

from alpaca.trading.requests import MarketOrderRequest

from alpaca.trading.enums import OrderSide, TimeInForce

BITCOIN_BUY_DATA = MarketOrderRequest( symbol="BTC/USD" , notional=BITCOIN_MONEY , side=OrderSide.BUY , 
                                      time_in_force=TimeInForce.GTC)

BUY_BITCOIN = CLIENT.submit_order(order_data=BITCOIN_BUY_DATA)

from datetime import sleep

sleep(10)

for X in CLIENT.get_all_positions():

  if X.symbol == "BTCUSD" :

     BITCOIN_QTY = float(X.qty)

     print( BITCOIN_QTY)

BITCOIN_SELL_DATA = MarketOrderRequest( symbol="BTCUSD" , qty=BITCOIN_QTY, side=OrderSide.SELL , 
                                      time_in_force=TimeInForce.GTC)

SELL_BITCOIN = CLIENT.submit_order(order_data=BITCOIN_SELL_DATA)

from datetime import sleep

sleep(5)

print(SELL_BITCOIN)

Why is the filled_at attribute of SELL_BITCOIN None?

SDK Version I encountered this issue in

alpaca-py 0.30.1

Steps To Reproduce

Run the following code with your API keys:


CLIENT = TradingClient( api_key=' ', secret_key=' ', paper=True)
BITCOIN_MONEY  = 2000
from alpaca.trading.requests import MarketOrderRequest
from alpaca.trading.enums import OrderSide, TimeInForce
BITCOIN_BUY_DATA = MarketOrderRequest( symbol="BTC/USD" , notional=BITCOIN_MONEY , side=OrderSide.BUY , 
                                      time_in_force=TimeInForce.GTC)
BUY_BITCOIN = CLIENT.submit_order(order_data=BITCOIN_BUY_DATA)
from datetime import sleep
sleep(10)
for X in CLIENT.get_all_positions():
  if X.symbol == "BTCUSD" :
     BITCOIN_QTY = float(X.qty)
     print( BITCOIN_QTY)
BITCOIN_SELL_DATA = MarketOrderRequest( symbol="BTCUSD" , qty=BITCOIN_QTY, side=OrderSide.SELL , 
                                      time_in_force=TimeInForce.GTC)
SELL_BITCOIN = CLIENT.submit_order(order_data=BITCOIN_SELL_DATA)
sleep(10)
print(SELL_BITCOIN)

Why is the filled_at attribute of the sell order None?

Filled out the Steps to Reproduce section?

Anything else?

No response

hiohiohio commented 1 month ago

@avrenli2 to check the latest status of an order after submitting, could you please use CLIENT.get_order_by_id(order_id= SELL_BITCOIN .id) [1] to fetch the latest order info from server?

*1 https://alpaca.markets/sdks/python/api_reference/trading/orders.html#get-order-by-id