Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add Metrics TimeSeries #90

Merged
merged 1 commit into from
Aug 1, 2022
Merged

✨ Add Metrics TimeSeries #90

merged 1 commit into from
Aug 1, 2022

Conversation

acemasterjb
Copy link
Contributor

Summary

Fixes #80

Details

This PR exposes the historical 7-day avg standard deviation and historical returns to the portfolio endpoint.

The volatility property of each asset in this endpoint response will be replaced with metrics containing the historical std_dev as volatility and returns as returns as timeseries.

Further Improvements

  • Frontend changes to plot

The graph on the frontend will need to also plot these timeseries on the three timeframes (1-yr, 3-months, 1-month).

@acemasterjb acemasterjb requested review from lajarre and vaughnmck July 25, 2022 14:08
@lajarre lajarre temporarily deployed to review-to-pr-feature-me-684rad July 27, 2022 18:58 Inactive
@@ -20,7 +20,7 @@
@dataclass
class PortfolioAsset:
allocation: float
volatility: float
metrics: dict[str, float]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a good thing that this PR suggests a change on the backend only, so let's try to make it mergeable by itself, without needing to wait for the frontend changes.

To keep this new backend version compatible with the frontend, we need to keep the volatility key here. Let's just add metrics next to it.

@@ -24,4 +24,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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for my knowledge, did you notice that the indices were not sorted before introducing this .sort_index()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, they were sorted in descending order for Prices.

The biggest consequence being that the std_dev calculation was incorrect.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is the summary representation of a given price dataframe before and after the change:

Before:
Screenshot 2022-07-29 at 14 17 30

After:
Screenshot 2022-07-29 at 14 19 20

Indeed the std_dev calcuation, albeit being very similar, is not the same. Nevertheless, I guess the volatility calculation stays the same.

This index-sorting fix has fairly wide-ranging consequences. We might need to look into git history to look for when a bug was introduced (think git bisect). A good rule of thumb is thus to always move such a fix to a separate independent commit.

@lajarre lajarre temporarily deployed to review-to-pr-feature-me-684rad July 28, 2022 23:31 Inactive
@lajarre lajarre temporarily deployed to review-to-pr-feature-me-684rad July 29, 2022 11:49 Inactive
@lajarre lajarre temporarily deployed to review-to-pr-feature-me-684rad July 29, 2022 11:51 Inactive
@acemasterjb acemasterjb removed the request for review from vaughnmck July 29, 2022 11:52
@@ -24,4 +24,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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is the summary representation of a given price dataframe before and after the change:

Before:
Screenshot 2022-07-29 at 14 17 30

After:
Screenshot 2022-07-29 at 14 19 20

Indeed the std_dev calcuation, albeit being very similar, is not the same. Nevertheless, I guess the volatility calculation stays the same.

This index-sorting fix has fairly wide-ranging consequences. We might need to look into git history to look for when a bug was introduced (think git bisect). A good rule of thumb is thus to always move such a fix to a separate independent commit.

@acemasterjb acemasterjb enabled auto-merge (rebase) August 1, 2022 16:51
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
@acemasterjb acemasterjb merged commit 539f900 into main Aug 1, 2022
@acemasterjb acemasterjb deleted the feature/metrics_ts branch August 1, 2022 16:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Risk metrics over time
2 participants