calmar() raises ZeroDivisionError when returns have no drawdown

Open Beginner friendly
#590 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
75/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
numpy, pandas, python
Domain
backend, data

Research direction

Start with the calmar implementation in quantstats.stats and trace how it computes max_dd and divides the CAGR ratio for Series and DataFrame inputs. Run the two examples from the issue, then add or find tests covering a return series with no drawdown in both shapes. Done means neither input raises and both produce the same result, either inf or nan.

Written by the indexing model from the issue text.

Description

qs.stats.calmar crashes on a Series that never goes down, for example a short sample where every day is positive.

import numpy as np, pandas as pd, quantstats as qs

rising = pd.Series(np.linspace(0.001, 0.01, 20), index=pd.bdate_range("2024-01-01", periods=20))

qs.stats.calmar(rising)                 # ZeroDivisionError: float division by zero
qs.stats.calmar(rising.to_frame("a"))   # a    inf

The max drawdown is 0, and calmar ends with return cagr_ratio / abs(max_dd). A Series and a one-column DataFrame of the same data also behave differently.

Expected: both inputs give the same result, either inf or nan, without an exception.

Dominant language
Python
Stars
7.9k
Forks
1.3k
PR merge metrics
No merged PRs in 30d

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from ranaroussi/quantstats

All issues in ranaroussi/quantstats

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.