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

Fix two bugs related to handling all positive/negative values #14

Merged
merged 2 commits into from
Sep 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/xirr/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ def xirr(valuesPerDate):
>>> xirr(valuesPerDate)
-0.645363882724717
'''
if not valuesPerDate or len(valuesPerDate) < 2:
if not valuesPerDate:
return None

if all(v >= 0 for v in valuesPerDate.values()):
return -float("inf")
if all(v <= 0 for v in valuesPerDate.values()):
return float("inf")
if all(v <= 0 for v in valuesPerDate.values()):
return -float("inf")

result = None
try:
Expand Down
7 changes: 4 additions & 3 deletions tests/test_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@
({'2020-03-12': 65209.6, '2019-12-31': -80005.8}, -0.6454),
({'2019-12-31': -100082.76, '2020-03-05': 82671.24}, -0.6581),
({}, None),
({'2019-12-31': -100082.76}, None),
({'2019-12-31': -100082.76}, -float("inf")),
({'2022-10-12': 200}, float("inf")),
({'2019-12-31': -0.00001, '2020-03-05': 0.00001}, 0.0),
({'2019-12-31': -100, '2020-03-05': 100}, 0.0),
({'2019-12-31': -100, '2020-03-05': 1000}, 412461.6383),
({'2017-12-16': -2236.3994659663, '2017-12-26': -47.3417585212, '2017-12-29': -46.52619316339632, '2017-12-31': 10424.74612565936, '2017-12-20': -13.077972551952}, 1.2238535289956518e+16),
({'2018-05-09': -200, '2018-06-09': 30, '2018-11-09': 50, '2018-12-09': 20}, -0.8037),
({'2011-01-01': -1, '2011-01-02': 0, '2012-01-01': -1}, float("inf")),
({'2011-01-01': 1, '2011-01-02': 0, '2012-01-01': 1}, -float("inf")),
({'2011-01-01': -1, '2011-01-02': 0, '2012-01-01': -1}, -float("inf")),
({'2011-01-01': 1, '2011-01-02': 0, '2012-01-01': 1}, float("inf")),
({'2011-07-01': -10000, '2014-07-01': 1}, -0.9535),
({'2011-07-01': 10000, '2014-07-01': -1}, -0.9535),
])
Expand Down