Skip to content

Commit

Permalink
feat: Support get_portfolio_history for TradingClient (#534)
Browse files Browse the repository at this point in the history
* feat: support get_portfolio_history for TradingClient

* chore: fix lint
  • Loading branch information
hiohiohio authored Dec 4, 2024
1 parent 5e21148 commit 554a1f7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
28 changes: 28 additions & 0 deletions alpaca/trading/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
ClosePositionRequest,
GetAssetsRequest,
GetOptionContractsRequest,
GetPortfolioHistoryRequest,
OrderRequest,
GetOrdersRequest,
ReplaceOrderRequest,
Expand All @@ -31,6 +32,7 @@
OptionContract,
OptionContractsResponse,
Order,
PortfolioHistory,
Position,
ClosePositionResponse,
Asset,
Expand Down Expand Up @@ -343,6 +345,32 @@ def exercise_options_position(
f"/positions/{symbol_or_contract_id}/exercise",
)

# ############################## Portfolio ################################# #

def get_portfolio_history(
self,
history_filter: Optional[GetPortfolioHistoryRequest] = None,
) -> Union[PortfolioHistory, RawData]:
"""
Gets the portfolio history statistics for an account.
Args:
account_id (Union[UUID, str]): The ID of the Account to get the portfolio history for.
history_filter: The various portfolio history request parameters.
Returns:
PortfolioHistory: The portfolio history statistics for the account.
"""
response = self.get(
f"/account/portfolio/history",
history_filter.to_request_fields() if history_filter else {},
)

if self._use_raw_data:
return response

return PortfolioHistory(**response)

# ############################## Assets ################################# #

def get_all_assets(
Expand Down
10 changes: 10 additions & 0 deletions alpaca/trading/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,26 @@ class GetPortfolioHistoryRequest(NonEmptyRequest):
week, M for month and A for year. Defaults to 1M.
timeframe (Optional[str]): The resolution of time window. 1Min, 5Min, 15Min, 1H, or 1D. If omitted, 1Min for
less than 7 days period, 15Min for less than 30 days, or otherwise 1D.
intraday_reporting (Optional[str]): this specfies which timestamps to return data points
start (Optional[datetime]): The timestamp the data is returned starting from in RFC3339 format (including timezone specification).
pnl_reset (Optional[str]): efines how we are calculating the baseline values for Profit And Loss (pnl) for queries with timeframe less than 1D (intraday queries).
end (Optional[datetime]): The timestamp the data is returned up to in RFC3339 format (including timezone specification).
date_end (Optional[date]): The date the data is returned up to. Defaults to the current market date (rolls over
at the market open if extended_hours is false, otherwise at 7am ET).
extended_hours (Optional[bool]): If true, include extended hours in the result. This is effective only for
timeframe less than 1D.
cashflow_types (Optional[str]): The cashflow activities to include in the report
"""

period: Optional[str] = None
timeframe: Optional[str] = None
intraday_reporting: Optional[str] = None
start: Optional[datetime] = None
pnl_reset: Optional[str] = None
end: Optional[datetime] = None
date_end: Optional[date] = None
extended_hours: Optional[bool] = None
cashflow_types: Optional[str] = None


class GetCalendarRequest(NonEmptyRequest):
Expand Down

0 comments on commit 554a1f7

Please sign in to comment.