Changed InterestMode enum implementation

This commit is contained in:
Sam Germain 2021-07-07 21:04:38 -06:00
parent 60572c9e0d
commit 52def4e826
3 changed files with 16 additions and 22 deletions

View File

@ -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")

View File

@ -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}
"""))

View File

@ -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,