quantstats icon indicating copy to clipboard operation
quantstats copied to clipboard

qs.utils.make_index(symbols, rebalance='1Y')

Open glformanek opened this issue 3 years ago • 1 comments

I have run the code below and it doesn't seem to create good stats on the tearsheet. I have compared it to some R code which rebalances annually and it is quite a bit different (max drawdown and CAGR are 2 items I notice seem way off). If I run the code with rebalance=None, it comes much closer to my results. Are there any suggestions or is this make_index working incorrectly. I also get an error FutureWarning: Dropping of nuisance columns in DataFrame... which was reported elsewhere. I don't believe this is the cause of the incorrect stats but I could be wrong.

import quantstats as qs

qs.extend_pandas()

symbols = {
    'SPY': 0.50,
    'IEF': 0.30,
    'TLT': 0.20
}
port = qs.utils.make_index(symbols, rebalance='1Y')
qs.reports.html(port, 'QQQ', title='Portfolio', output='port.html', download_filename='port.html')

glformanek avatar Apr 28 '22 23:04 glformanek

I suspect something is wrong with make_index indeed. If you make an index that consists for 100% of something you see it differs from the benchmark also consisting for 100% of the same thing. Adding another ticker with 0% makes the difference a lot larger.

%matplotlib inline
import quantstats as qs

# extend pandas functionality with metrics, etc.
qs.extend_pandas()

# fetch the daily returns for a stock
TICKER = 'SPY'
returns = qs.utils.download_returns(TICKER)

# create portfolio returns (identical to 100% TICKER)
portfolio_returns = qs.utils.make_index(
    ticker_weights={
        TICKER: 1.0,
#        'FB': 0.,  # UNCOMMENT THIS LINE
    }
)

# Create report to observe differences.
qs.reports.basic(portfolio_returns, TICKER)

BoudewijnKlijn avatar May 11 '22 18:05 BoudewijnKlijn

Did anyone get anywhere with this, I'm having the same issue - changing the weights doesn't affect returns as it should?

mediastore93 avatar Oct 04 '22 15:10 mediastore93

Same issue here, does anyone have a solution/workaround/suggestions please?

tsunamilx avatar Nov 17 '22 08:11 tsunamilx

Either people are using the library for 1 to 1 comparisons against a benchmark or simply for the stats sheet reports as most are not noticing this -- the make_index function has a few flaws. The most obvious being the sum of all returns retrieved in the index at a certain date, when it should be at the mean for all dates and sum only for the dates where he did the rebalance. But even changing it to the mean, the logic for reballancing wouldn't make too much sense as it assumes a calculation of the specific return * the weight for that ticker on the rebalancing date.

The best way I can think of is to rewrite the function to assume a starting capital, assign the starting capital by dividing it by the weights, buy the stocks at the first date's price and at every rebalance period assume selling everything, redistribute the capital following the weights, and rebuying each ticker with their assigned cash at the price on rebalancing day.

Return the calculation of the pct change on the total PnL instead.

I'm not a dev, but I'll take a stab at this new function

DiabolusBR avatar Jan 11 '23 04:01 DiabolusBR