diff --git a/freqtrade/leverage/interest.py b/freqtrade/leverage/interest.py index 367df5821..ddeea2b42 100644 --- a/freqtrade/leverage/interest.py +++ b/freqtrade/leverage/interest.py @@ -1,20 +1,20 @@ -from decimal import Decimal from math import ceil from freqtrade.exceptions import OperationalException +from freqtrade.util import FtPrecise -one = Decimal(1.0) -four = Decimal(4.0) -twenty_four = Decimal(24.0) +one = FtPrecise(1.0) +four = FtPrecise(4.0) +twenty_four = FtPrecise(24.0) def interest( exchange_name: str, - borrowed: Decimal, - rate: Decimal, - hours: Decimal -) -> Decimal: + borrowed: FtPrecise, + rate: FtPrecise, + hours: FtPrecise +) -> FtPrecise: """ Equation to calculate interest on margin trades @@ -31,13 +31,13 @@ def interest( """ exchange_name = exchange_name.lower() if exchange_name == "binance": - return borrowed * rate * ceil(hours) / twenty_four + return borrowed * rate * FtPrecise(ceil(hours)) / twenty_four elif exchange_name == "kraken": # Rounded based on https://kraken-fees-calculator.github.io/ - return borrowed * rate * (one + ceil(hours / four)) + return borrowed * rate * (one + FtPrecise(ceil(hours / four))) elif exchange_name == "ftx": # As Explained under #Interest rates section in # https://help.ftx.com/hc/en-us/articles/360053007671-Spot-Margin-Trading-Explainer - return borrowed * rate * ceil(hours) / twenty_four + return borrowed * rate * FtPrecise(ceil(hours)) / twenty_four else: raise OperationalException(f"Leverage not available on {exchange_name} with freqtrade") diff --git a/tests/leverage/test_interest.py b/tests/leverage/test_interest.py index 6b189ce50..7bdf4c2f0 100644 --- a/tests/leverage/test_interest.py +++ b/tests/leverage/test_interest.py @@ -1,14 +1,14 @@ -from decimal import Decimal from math import isclose import pytest from freqtrade.leverage import interest +from freqtrade.util import FtPrecise -ten_mins = Decimal(1 / 6) -five_hours = Decimal(5.0) -twentyfive_hours = Decimal(25.0) +ten_mins = FtPrecise(1 / 6) +five_hours = FtPrecise(5.0) +twentyfive_hours = FtPrecise(25.0) @pytest.mark.parametrize('exchange,interest_rate,hours,expected', [ @@ -28,11 +28,11 @@ twentyfive_hours = Decimal(25.0) ('ftx', 0.00025, twentyfive_hours, 0.015625), ]) def test_interest(exchange, interest_rate, hours, expected): - borrowed = Decimal(60.0) + borrowed = FtPrecise(60.0) assert isclose(interest( exchange_name=exchange, borrowed=borrowed, - rate=Decimal(interest_rate), + rate=FtPrecise(interest_rate), hours=hours ), expected) diff --git a/tests/rpc/test_rpc_apiserver.py b/tests/rpc/test_rpc_apiserver.py index 25343ead6..eb3172704 100644 --- a/tests/rpc/test_rpc_apiserver.py +++ b/tests/rpc/test_rpc_apiserver.py @@ -892,7 +892,7 @@ def test_api_performance(botclient, fee): assert_response(rc) assert len(rc.json()) == 2 assert rc.json() == [{'count': 1, 'pair': 'LTC/ETH', 'profit': 7.61, 'profit_pct': 7.61, - 'profit_ratio': 0.07609203, 'profit_abs': 0.01872279}, + 'profit_ratio': 0.07609203, 'profit_abs': 0.0187228}, {'count': 1, 'pair': 'XRP/ETH', 'profit': -5.57, 'profit_pct': -5.57, 'profit_ratio': -0.05570419, 'profit_abs': -0.1150375}]