ranaroussi / quantstats

Portfolio analytics for quants, written in Python
Apache License 2.0
4.91k stars 851 forks source link

inconsistent days treatment for returns vs pv #235

Closed drusakov778 closed 1 year ago

drusakov778 commented 1 year ago

consider the code similar to #233

import pandas as pd
import quantstats as qs

pv_raw = [100, 101, 99, 98, 97, 101, 99]
dates = pd.date_range("2022-12-01", periods=7)
pv = pd.DataFrame(pv_raw, index=dates, columns=["pv"])
vol = qs.stats.volatility(pv)
sharpe = qs.stats.sharpe(pv)

# calculate daily returns, no first day return as since data is daily (assumed EOD) 
rt = pv.pct_change()
rt = rt[1:]
vol2 = qs.stats.volatility(rt)
sharpe2 = qs.stats.sharpe(rt)

printouts

>>> vol[0], vol2[0]
(0.3417106479175939, 0.3742046996695246)
>>> sharpe[0], sharpe2[0]
(-0.9136086220829638, -0.9733213582901914)

volatility, sharpe and almost every other metric differs b/c when calculated from pv quantstats calculates statistics for the one extra first day as well (where returns are NaN). The results differ only a little bit, but it is quite annoying especially when trying to debug and compare results vs external stats valuation

on the other hand, if correct, trimmed returns (like rt above) are supplied to qs.reports.html(), then plots do not start at 0/100% point, which is also confusing