Skip to content

Commit 74c11e7

Browse files
authored
fix: Change PortfolioHistory.base_value to Optional[float] (#597)
* fix: change PortfolioHistory.base_value to Optional[float] * fix: add test_get_portfolio_history_with_null_base_value
1 parent 3b0ff3a commit 74c11e7

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

alpaca/trading/models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ class PortfolioHistory(BaseModel):
310310
equity (List[float]): Equity value of the account in dollar amount as of the end of each time window.
311311
profit_loss (List[float]): Profit/loss in dollar from the base value.
312312
profit_loss_pct (List[Optional[float]]): Profit/loss in percentage from the base value.
313-
base_value (float): Basis in dollar of the profit loss calculation.
313+
base_value (Optional[float]): Basis in dollar of the profit loss calculation.
314314
timeframe (str): Time window size of each data element.
315315
cashflow (Dict[ActivityType, List[float]]): Cash flow amounts per activity type, if any.
316316
"""
@@ -319,7 +319,7 @@ class PortfolioHistory(BaseModel):
319319
equity: List[float]
320320
profit_loss: List[float]
321321
profit_loss_pct: List[Optional[float]]
322-
base_value: float
322+
base_value: Optional[float] = None
323323
timeframe: str
324324
cashflow: Dict[ActivityType, List[float]] = {}
325325

tests/broker/broker_client/test_trading_routes.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,30 @@ def test_get_portfolio_history_with_null_pl_pct(reqmock, client: BrokerClient):
563563
assert isinstance(portfolio_history, PortfolioHistory)
564564

565565

566+
def test_get_portfolio_history_with_null_base_value(reqmock, client: BrokerClient):
567+
account_id = "2a87c088-ffb6-472b-a4a3-cd9305c8605c"
568+
569+
reqmock.get(
570+
f"{BaseURL.BROKER_SANDBOX.value}/v1/trading/accounts/{account_id}/account/portfolio/history",
571+
text="""
572+
{
573+
"timestamp": [1580826600000, 1580827500000, 1580828400000],
574+
"equity": [27423.73, 27408.19, 27515.97],
575+
"profit_loss": [11.8, -3.74, 104.04],
576+
"profit_loss_pct": [11.8, -3.74, 104.04],
577+
"base_value": null,
578+
"timeframe": "15Min"
579+
}
580+
""",
581+
)
582+
583+
portfolio_history = client.get_portfolio_history_for_account(account_id)
584+
585+
assert reqmock.called_once
586+
assert reqmock.request_history[0].qs == {}
587+
assert isinstance(portfolio_history, PortfolioHistory)
588+
589+
566590
def test_get_portfolio_history_with_filter(reqmock, client: BrokerClient):
567591
account_id = "2a87c088-ffb6-472b-a4a3-cd9305c8605c"
568592

0 commit comments

Comments
 (0)