From 52def4e8269465f96f9870c5978de3f849053f68 Mon Sep 17 00:00:00 2001 From: Sam Germain Date: Wed, 7 Jul 2021 21:04:38 -0600 Subject: [PATCH] Changed InterestMode enum implementation --- freqtrade/enums/interestmode.py | 28 +++++++++++----------------- freqtrade/persistence/migrations.py | 6 ++++-- freqtrade/persistence/models.py | 4 +--- 3 files changed, 16 insertions(+), 22 deletions(-) diff --git a/freqtrade/enums/interestmode.py b/freqtrade/enums/interestmode.py index c95f4731f..f35573f1f 100644 --- a/freqtrade/enums/interestmode.py +++ b/freqtrade/enums/interestmode.py @@ -1,30 +1,24 @@ from enum import Enum, auto from decimal import Decimal +from freqtrade.exceptions import OperationalException one = Decimal(1.0) four = Decimal(4.0) twenty_four = Decimal(24.0) -class FunctionProxy: - """Allow to mask a function as an Object.""" +class InterestMode(Enum): - def __init__(self, function): - self.function = function + HOURSPERDAY = "HOURSPERDAY" + HOURSPER4 = "HOURSPER4" # Hours per 4 hour segment def __call__(self, *args, **kwargs): - return self.function(*args, **kwargs) + borrowed, rate, hours = kwargs["borrowed"], kwargs["rate"], kwargs["hours"] -class InterestMode(Enum): - """Equations to calculate interest""" - - # Interest_rate is per day, minimum time of 1 hour - HOURSPERDAY = FunctionProxy( - lambda borrowed, rate, hours: borrowed * rate * max(hours, one)/twenty_four - ) - - # Interest_rate is per 4 hours, minimum time of 4 hours - HOURSPER4 = FunctionProxy( - lambda borrowed, rate, hours: borrowed * rate * (1 + max(0, (hours-four)/four)) - ) + if self.name == "HOURSPERDAY": + return borrowed * rate * max(hours, one)/twenty_four + elif self.name == "HOURSPER4": + return borrowed * rate * (1 + max(0, (hours-four)/four)) + else: + raise OperationalException(f"Leverage not available on this exchange with freqtrade") diff --git a/freqtrade/persistence/migrations.py b/freqtrade/persistence/migrations.py index 69ffc544e..b7c969945 100644 --- a/freqtrade/persistence/migrations.py +++ b/freqtrade/persistence/migrations.py @@ -52,6 +52,7 @@ def migrate_trades_table(decl_base, inspector, engine, table_back_name: str, col interest_rate = get_column_def(cols, 'interest_rate', '0.0') liquidation_price = get_column_def(cols, 'liquidation_price', 'null') is_short = get_column_def(cols, 'is_short', 'False') + interest_mode = get_column_def(cols, 'interest_mode', 'null') # If ticker-interval existed use that, else null. if has_column(cols, 'ticker_interval'): timeframe = get_column_def(cols, 'timeframe', 'ticker_interval') @@ -88,7 +89,7 @@ def migrate_trades_table(decl_base, inspector, engine, table_back_name: str, col stoploss_order_id, stoploss_last_update, max_rate, min_rate, sell_reason, sell_order_status, strategy, timeframe, open_trade_value, close_profit_abs, - leverage, interest_rate, liquidation_price, is_short + leverage, interest_rate, liquidation_price, is_short, interest_mode ) select id, lower(exchange), case @@ -113,7 +114,8 @@ def migrate_trades_table(decl_base, inspector, engine, table_back_name: str, col {strategy} strategy, {timeframe} timeframe, {open_trade_value} open_trade_value, {close_profit_abs} close_profit_abs, {leverage} leverage, {interest_rate} interest_rate, - {liquidation_price} liquidation_price, {is_short} is_short + {liquidation_price} liquidation_price, {is_short} is_short, + {interest_mode} interest_mode from {table_back_name} """)) diff --git a/freqtrade/persistence/models.py b/freqtrade/persistence/models.py index b9c1b89a8..9aa340fdc 100644 --- a/freqtrade/persistence/models.py +++ b/freqtrade/persistence/models.py @@ -612,8 +612,6 @@ class LocalTrade(): # If nothing was borrowed if self.has_no_leverage: return zero - elif not self.interest_mode: - raise OperationalException(f"Leverage not available on {self.exchange} using freqtrade") open_date = self.open_date.replace(tzinfo=None) now = (self.close_date or datetime.now(timezone.utc)).replace(tzinfo=None) @@ -624,7 +622,7 @@ class LocalTrade(): rate = Decimal(interest_rate or self.interest_rate) borrowed = Decimal(self.borrowed) - return self.interest_mode.value(borrowed, rate, hours) + return self.interest_mode(borrowed=borrowed, rate=rate, hours=hours) def calc_close_trade_value(self, rate: Optional[float] = None, fee: Optional[float] = None,