Hi, I am just starting out using the alpaca_trade_api package in python. I am trying to simply display the orders that I have made on my paper account. I am simply typing
api = tradeapi.REST(config.API_KEY, config.API_SECRET, base_url = “https://paper-api.alpaca.markets”)
orders = api.list_orders
print(orders)
However nothing gets displayed in the terminal window. I have also tried displaying my account information, and that works, so I know python is able to connect to my account. I also have filled orders showing on the alpaca website from last week as well, so I know I have also successfully placed orders.
Any idea what might be happening? Again, I am new to this, so I apologize if it is something simple
Hi @mitchell1551
If using the python alpaca-trade-api-python
SDK one can get a list of all unfilled orders something like this:
# Imports we need. Also import pandas while we're at it. It'll come in handy later on.
import alpaca_trade_api as alpacaapi
import pandas as pd
# Set endpoints for either live or paper trading
ALPACA_API_ENDPOINT = 'https://paper-api.alpaca.markets'
ALPACA_API_KEY_ID = 'XXXXX'
ALPACA_API_SECRET_KEY = 'XXXXXXX'
# Get a reference to a a rest object
api = alpacaapi.REST(ALPACA_API_KEY_ID, ALPACA_API_SECRET_KEY, ALPACA_API_ENDPOINT)
# Get a list of all orders.
open_orders_list = api.list_orders(status='open')
Setting status='open'
will return only unfilled open orders. Setting status
to either ‘closed’ or ‘all’ will return only closed or all orders respectively. The default quantity returned is 50. To get more than that add the parameter limit
. The maximum however is 500. If one has more than 500 orders to return then the list will need to be ‘chunked’ into smaller pieces using the after
and/or until
parameters. Something like this will return up to 100 of the most recently closed orders
all_orders_list = api.list_orders(status='closed', limit=100)
The list is rather cumbersome to work with (IMHO). It can be easily turned into a dataframe like this
all_orders_df = pd.DataFrame([order._raw for order in all_orders_list])
Hope that helps