Repository: tarioch/xirr

Issue:
Problematic example
Here's an interesting example of numbers:

Values:

```
[5046.0, 5037.299999999999, 4995.25, 5795.5, -1085.6, 4998.0, 4557.8, 4815.0, 4928.0, -2197.05, 5424.0, -2565.0, -2872.8, 10085.0, 9500.0, 9976.8, 14880.000000000002, -6094.7, 19522.359999999997, 18035.0, 10477.44]
```

Dates:

```
[Timestamp('2015-08-03 00:00:00+0000', tz='UTC'), Timestamp('2015-10-20 00:00:00+0000', tz='UTC'), Timestamp('2016-01-11 00:00:00+0000', tz='UTC'), Timestamp('2016-04-06 00:00:00+0000', tz='UTC'), Timestamp('2016-04-26 00:00:00+0000', tz='UTC'), Timestamp('2016-07-19 00:00:00+0000', tz='UTC'), Timestamp('2016-10-11 00:00:00+0000', tz='UTC'), Timestamp('2017-01-11 00:00:00+0000', tz='UTC'), Timestamp('2017-04-11 00:00:00+0000', tz='UTC'), Timestamp('2017-04-25 00:00:00+0000', tz='UTC'), Timestamp('2017-10-12 00:00:00+0000', tz='UTC'), Timestamp('2018-04-24 00:00:00+0000', tz='UTC'), Timestamp('2019-04-23 00:00:00+0000', tz='UTC'), Timestamp('2020-02-25 00:00:00+0000', tz='UTC'), Timestamp('2020-03-03 00:00:00+0000', tz='UTC'), Timestamp('2020-03-09 00:00:00+0000', tz='UTC'), Timestamp('2020-04-06 00:00:00+0000', tz='UTC'), Timestamp('2020-04-23 00:00:00+0000', tz='UTC'), Timestamp('2020-06-05 00:00:00+0000', tz='UTC'), Timestamp('2020-08-05 00:00:00+0000', tz='UTC'), Timestamp('2020-08-19 00:00:00+0000', tz='UTC')]
```

It produces an exception in xirr: `ValueError: f(a) and f(b) must have different signs`

It also produces an error in Google Sheets.

Excel, however, produces a result: 

![image](https://user-images.githubusercontent.com/8012482/92327892-26b14d00-f05d-11ea-9bf9-d102c3ed394f.png)

Any idea what is going on here?

Discussion:

Patch:
diff --git a/src/xirr/math.py b/src/xirr/math.py
index 92ca4de..14bf286 100644
--- a/src/xirr/math.py
+++ b/src/xirr/math.py
@@ -50,7 +50,7 @@ def xirr(valuesPerDate):
     try:
         result = scipy.optimize.newton(lambda r: xnpv(valuesPerDate, r), 0)
     except (RuntimeError, OverflowError):    # Failed to converge?
-        result = scipy.optimize.brentq(lambda r: xnpv(valuesPerDate, r), -0.999999999999999, 1e20)
+        result = scipy.optimize.brentq(lambda r: xnpv(valuesPerDate, r), -0.999999999999999, 1e20, maxiter=10**6)
 
     if not isinstance(result, complex):
         return result


Test patch:
diff --git a/tests/test_math.py b/tests/test_math.py
index 0c78f3f..48b0a1f 100644
--- a/tests/test_math.py
+++ b/tests/test_math.py
@@ -21,6 +21,18 @@ from xirr.math import xirr, cleanXirr, xnpv
     ({'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),
+    ({
+        '2016-04-06': 18902.0,
+        '2016-05-04': 83600.0,
+        '2016-05-12': -5780.0,
+        '2017-05-08': -4080.0,
+        '2017-07-03': -56780.0,
+        '2018-05-07': -2210.0,
+        '2019-05-06': -2380.0,
+        '2019-10-01': 33975.0,
+        '2020-03-13': 23067.98,
+        '2020-05-07': -1619.57,
+    }, -1),
 ])
 def test_xirr(valuesPerDateString, expected):
     valuesPerDate = {datetime.fromisoformat(k).date(): v for k, v in valuesPerDateString.items()}