Skip to content

Commit

Permalink
Merge pull request #14 from ymyke/bugfix/all_pos_neg
Browse files Browse the repository at this point in the history
Fix two bugs related to handling all positive/negative values
  • Loading branch information
tarioch committed Sep 6, 2020
2 parents 0367b05 + a626a40 commit 9d046ac
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
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

0 comments on commit 9d046ac

Please sign in to comment.