Skip to content

Commit

Permalink
✨ Add metrics timeseries
Browse files Browse the repository at this point in the history
This timeseries will have both `std_dev` and `returns` observations

:bug: Validate `returns` and `std_dev` timeseries

:bug:  Sort the index of price series

This fixes an issue where the std_dev ts would be incorrect,
since the rolling window calcs are applied on a decending ordered ts.

This means that the last seven days would not have std_dev observations,
but the first seven would.

:rewind: Reintroduce asset property for frontend
  • Loading branch information
acemasterjb committed Jul 29, 2022
1 parent 43befae commit 3c386ad
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
17 changes: 13 additions & 4 deletions backend/app/endpoints.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import datetime
from dataclasses import asdict, dataclass
from json import loads
from typing import Iterable, Literal, TypeVar, Union

import dateutil
Expand All @@ -21,6 +22,7 @@
class PortfolioAsset:
allocation: float
volatility: float
metrics: dict[str, float]
risk_contribution: float


Expand Down Expand Up @@ -54,13 +56,20 @@ def from_treasury_with_assets(

assets = {
a.token_symbol: PortfolioAsset(
allocation=a.balance_usd / treasury.usd_total,
volatility=None
if histprices[a.token_symbol]["std_dev"].mean() is NaN
else histprices[a.token_symbol]["std_dev"].mean(),
allocation=a.balance / treasury.usd_total,
metrics={}
if histprices[a.token_symbol][["std_dev", "returns"]].empty
else loads(
histprices[a.token_symbol][["std_dev", "returns"]].to_json(
orient="index", date_format="iso"
)
),
risk_contribution=a.risk_contribution
if hasattr(a, "risk_contribution")
else None,
volatility=None
if histprices[a.token_symbol]["std_dev"].mean() is NaN
else histprices[a.token_symbol]["std_dev"].mean(),
)
for a in treasury.assets
}
Expand Down
2 changes: 1 addition & 1 deletion backend/app/libs/price_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def make_returns_df(hist_values: pd.Series, column_name: str) -> pd.DataFrame:
dataframe.returns.replace(-np.inf, 0, inplace=True)
dataframe.returns.iloc[1:].replace(
np.nan, 0, inplace=True
) # Keep first nan (probly useless).
) # Keep first nan (probably useless).
dataframe["std_dev"] = dataframe.returns.rolling(ROLLING_WINDOW_DAYS).std(ddof=0)
return dataframe

Expand Down
2 changes: 1 addition & 1 deletion backend/app/libs/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ def make_hist_price_series(
(price.value for price in prices),
index=pd.Index((price.timestamp for price in prices), name="timestamp"),
name=f"{token_symbol} historical price",
)
).sort_index()

0 comments on commit 3c386ad

Please sign in to comment.