Environment
Language
Python 3.11.9
Alpaca SDK Version
Alpaca-py version 0.43.2
Other Environment Details
Running on windows and linux.
Problem
Summary
Equity data doesn’t make sense. I’m seeing huge random dips in the data that don’t have any reasonable explanation. More than that, when getting equity data for different periods, the data from Alpaca is inconsistent.
You can see in the following images that the 1D history looks fine, however the 1W and the 1M shows a huge dip, such that my current equity should be sitting at about 60k. The equity for 1D is at about 170k which agrees with the Alpaca dashboard.
Is this an issue with the API? Or am I doing something wrong?
In addition, while this huge drop doesn’t seem to show in the Alpaca dashboard, there are clearly other random drops. Why would this be? I was not trading or even interacting with my account during the time of those drops.
Thanks in advance!
Paper or Live Tradng?
Paper Trading
Example Code
from alpaca.data import Trade
from src.base.singleton import SingletonMeta
from alpaca.trading.requests import GetPortfolioHistoryRequest
from src.base.alpaca_client import AlpacaClient
from alpaca.trading.client import TradingClient
from datetime import datetime
from alpaca.trading.models import Order, Position, TradeAccount
from typing import List
class AccountService(metaclass=SingletonMeta):
def __init__(self):
self.alpaca_client = AlpacaClient()
self.trading_client: TradingClient = self.alpaca_client.trading_client
self.update_account()
def get_equity_history(self, **kwargs):
"""
Fetches the historical equity/portfolio value for the account using
dynamic keyword arguments.
Args:
**kwargs: Arbitrary keyword arguments passed to GetPortfolioHistoryRequest.
Common keys include:
- period (str): Duration (e.g., '1D', '1M', '1Y', 'all').
- timeframe (str): Resolution (e.g., '1Min', '5Min', '1H', '1D').
- start (datetime): Specific start date.
- end (datetime): Specific end date.
- extended_hours (bool): Include pre/post market data.
Returns:
PortfolioHistory: Object containing equity, timestamp, and P/L data.
"""
# Unpack kwargs directly into the request object
history_params = GetPortfolioHistoryRequest(**kwargs)
# Execute the query
return self.trading_client.get_portfolio_history(history_params)
def get_all_positions(self) -> List[Position]:
"""Open stock and crypto positions for the account."""
return self.trading_client.get_all_positions()
def get_history_1d(self):
"""Last 24 hours at 1-minute resolution."""
return self.get_equity_history(period="1D", timeframe="1Min", intraday_reporting="continuous")
def get_history_1w(self):
"""Last 7 days at 1-hour resolution."""
return self.get_equity_history(period="1W", timeframe="1H", intraday_reporting="continuous")
def get_history_1m(self):
"""Last 30 days at 1-hour resolution."""
return self.get_equity_history(period="28D", timeframe="1H", intraday_reporting="continuous")
def get_history_3m(self):
"""Last 90 days at 1-day resolution."""
return self.get_equity_history(period="3M", timeframe="1D")
def get_history_ytd(self):
"""From Jan 1st of the current year to now at 1-day resolution."""
start_of_year = datetime(datetime.now().year, 1, 1)
# Note: Alpaca calculates YTD best by passing the 'start' date
return self.get_equity_history(start=start_of_year, timeframe="1D")
def get_history_1y(self):
"""Last 365 days at 1-day resolution."""
return self.get_equity_history(period="12M", timeframe="1D")
def get_history_all(self):
"""Entire account history at 1-day resolution."""
return self.get_equity_history(period="all", timeframe="1D")
