fix cagr on zero or negative values
This commit is contained in:
@@ -296,7 +296,7 @@ def calculate_cagr(days_passed: int, starting_balance: float, final_balance: flo
|
|||||||
:param final_balance: Final balance to calculate CAGR against
|
:param final_balance: Final balance to calculate CAGR against
|
||||||
:return: CAGR
|
:return: CAGR
|
||||||
"""
|
"""
|
||||||
if final_balance < 0:
|
if (final_balance < 0) or (starting_balance <= 0) or (days_passed <= 0):
|
||||||
# With leveraged trades, final_balance can become negative.
|
# With leveraged trades, final_balance can become negative.
|
||||||
return 0
|
return 0
|
||||||
return (final_balance / starting_balance) ** (1 / (days_passed / 365)) - 1
|
return (final_balance / starting_balance) ** (1 / (days_passed / 365)) - 1
|
||||||
|
|||||||
@@ -516,6 +516,8 @@ def test_calculate_sqn_cases(profits, starting_balance, expected_sqn, descriptio
|
|||||||
(1000, 1500, 365, 0.5),
|
(1000, 1500, 365, 0.5),
|
||||||
(1000, 1500, 100, 3.3927), # sub year
|
(1000, 1500, 100, 3.3927), # sub year
|
||||||
(0.01000000, 0.01762792, 120, 4.6087), # sub year BTC values
|
(0.01000000, 0.01762792, 120, 4.6087), # sub year BTC values
|
||||||
|
(1000, 1010, 0, 0.0), # zero days
|
||||||
|
(-100, 100, 365, 0.0), # negative starting balance
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
def test_calculate_cagr(start, end, days, expected):
|
def test_calculate_cagr(start, end, days, expected):
|
||||||
|
|||||||
Reference in New Issue
Block a user