Changed InterestMode enum implementation

This commit is contained in:
Sam Germain 2021-07-07 21:04:38 -06:00
parent 86888dbbf0
commit a368dfa7b5
3 changed files with 16 additions and 22 deletions

View File

@ -1,30 +1,24 @@
from enum import Enum, auto from enum import Enum, auto
from decimal import Decimal from decimal import Decimal
from freqtrade.exceptions import OperationalException
one = Decimal(1.0) one = Decimal(1.0)
four = Decimal(4.0) four = Decimal(4.0)
twenty_four = Decimal(24.0) twenty_four = Decimal(24.0)
class FunctionProxy: class InterestMode(Enum):
"""Allow to mask a function as an Object."""
def __init__(self, function): HOURSPERDAY = "HOURSPERDAY"
self.function = function HOURSPER4 = "HOURSPER4" # Hours per 4 hour segment
def __call__(self, *args, **kwargs): def __call__(self, *args, **kwargs):
return self.function(*args, **kwargs)
borrowed, rate, hours = kwargs["borrowed"], kwargs["rate"], kwargs["hours"]
class InterestMode(Enum): if self.name == "HOURSPERDAY":
"""Equations to calculate interest""" return borrowed * rate * max(hours, one)/twenty_four
elif self.name == "HOURSPER4":
# Interest_rate is per day, minimum time of 1 hour return borrowed * rate * (1 + max(0, (hours-four)/four))
HOURSPERDAY = FunctionProxy( else:
lambda borrowed, rate, hours: borrowed * rate * max(hours, one)/twenty_four raise OperationalException(f"Leverage not available on this exchange with freqtrade")
)
# 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))
)

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') interest_rate = get_column_def(cols, 'interest_rate', '0.0')
liquidation_price = get_column_def(cols, 'liquidation_price', 'null') liquidation_price = get_column_def(cols, 'liquidation_price', 'null')
is_short = get_column_def(cols, 'is_short', 'False') 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 ticker-interval existed use that, else null.
if has_column(cols, 'ticker_interval'): if has_column(cols, 'ticker_interval'):
timeframe = get_column_def(cols, 'timeframe', '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, stoploss_order_id, stoploss_last_update,
max_rate, min_rate, sell_reason, sell_order_status, strategy, max_rate, min_rate, sell_reason, sell_order_status, strategy,
timeframe, open_trade_value, close_profit_abs, 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), select id, lower(exchange),
case case
@ -113,7 +114,8 @@ def migrate_trades_table(decl_base, inspector, engine, table_back_name: str, col
{strategy} strategy, {timeframe} timeframe, {strategy} strategy, {timeframe} timeframe,
{open_trade_value} open_trade_value, {close_profit_abs} close_profit_abs, {open_trade_value} open_trade_value, {close_profit_abs} close_profit_abs,
{leverage} leverage, {interest_rate} interest_rate, {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} from {table_back_name}
""")) """))

View File

@ -612,8 +612,6 @@ class LocalTrade():
# If nothing was borrowed # If nothing was borrowed
if self.has_no_leverage: if self.has_no_leverage:
return zero 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) open_date = self.open_date.replace(tzinfo=None)
now = (self.close_date or datetime.now(timezone.utc)).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) rate = Decimal(interest_rate or self.interest_rate)
borrowed = Decimal(self.borrowed) 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, def calc_close_trade_value(self, rate: Optional[float] = None,
fee: Optional[float] = None, fee: Optional[float] = None,