Looking for sample code of how to pull This info over a given fixed time frame or Date window with Python Alpaca-Py. Seems the older version of Python was buggy for this, but see no code examples and looking to solve. Thanks in advance.
@KentP Probably what you are looking for is the portfolio_history
endpoint. That has end of day equity and a few other portfolio details. Check out the docs here. There are some known issues (which are being fixed) but generally a 1Day timeframe and no extended hours should work.
This endpoint isn’t implemented directly in the alpaca-py SDK. However, there is a generic get
method which can be used for any endpoint. The get method takes 2 arguments 1) the endpoint path and 2) a dict of parameter:value pairs. The parameter names are exactly as documented in the API endpoint.
So, using this method to get, for example the last 60 days of portfolio stats, one could do this
!pip install -q alpaca-py from alpaca.trading.client import TradingClient ALPACA_API_KEY = 'xxxxx' ALPACA_API_SECRET_KEY = 'xxxxx' trading_client = TradingClient(ALPACA_API_KEY, ALPACA_API_SECRET_KEY, paper=True, raw_data=True) portfolio_hist = trading_client.get("/account/portfolio/history", {'period':'60D', 'timeframe':'1D'})
I find it always easier to use pandas dataframes, so convert this to a dataframe and while we’re at it, convert the epoch timestamp to a datetime for easier reading.
portfolio_hist_df = pd.DataFrame(portfolio_hist) portfolio_hist_df['timestamp'] = pd.to_datetime(portfolio_hist_df.timestamp, unit='s')
This is the result
Does that help?
Perfect. Pandas df is also my go to. Will try this, much appreciated