MCPcopy
hub / github.com/ranaroussi/quantstats / _prepare_benchmark

Function _prepare_benchmark

quantstats/utils.py:255–288  ·  view source on GitHub ↗

Fetch benchmark if ticker is provided, and pass through _prepare_returns() period can be options or (expected) _pd.DatetimeIndex range

(benchmark=None, period="max", rf=0.0, prepare_returns=True)

Source from the content-addressed store, hash-verified

253
254
255def _prepare_benchmark(benchmark=None, period="max", rf=0.0, prepare_returns=True):
256 """
257 Fetch benchmark if ticker is provided, and pass through
258 _prepare_returns()
259
260 period can be options or (expected) _pd.DatetimeIndex range
261 """
262 if benchmark is None:
263 return None
264
265 if isinstance(benchmark, str):
266 benchmark = download_returns(benchmark)
267
268 elif isinstance(benchmark, _pd.DataFrame):
269 benchmark = benchmark[benchmark.columns[0]].copy()
270
271 if isinstance(period, _pd.DatetimeIndex) and set(period) != set(benchmark.index):
272
273 # Adjust Benchmark to Strategy frequency
274 benchmark_prices = to_prices(benchmark, base=1)
275 new_index = _pd.date_range(start=period[0], end=period[-1], freq="D")
276 benchmark = (
277 benchmark_prices.reindex(new_index, method="bfill")
278 .reindex(period)
279 .pct_change()
280 .fillna(0)
281 )
282 benchmark = benchmark[benchmark.index.isin(period)]
283
284 benchmark = benchmark.tz_localize(None)
285
286 if prepare_returns:
287 return _prepare_returns(benchmark.dropna(), rf=rf)
288 return benchmark.dropna()
289
290
291def _round_to_closest(val, res, decimals=None):

Callers

nothing calls this directly

Calls 3

download_returnsFunction · 0.85
to_pricesFunction · 0.85
_prepare_returnsFunction · 0.85

Tested by

no test coverage detected