From 1af6d8abcac17ce1ee7f74eb43f0478883dfc388 Mon Sep 17 00:00:00 2001 From: Sam Germain Date: Mon, 2 Aug 2021 05:26:56 -0600 Subject: [PATCH] Added in isolated margin and trading_mode to freqtradebot --- freqtrade/freqtradebot.py | 45 +++++++++++++++++++++++---------- freqtrade/maintenance_margin.py | 16 +++++++----- 2 files changed, 42 insertions(+), 19 deletions(-) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 56d51ac82..d590c6558 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -16,7 +16,7 @@ from freqtrade.configuration import validate_config_consistency from freqtrade.data.converter import order_book_to_dataframe from freqtrade.data.dataprovider import DataProvider from freqtrade.edge import Edge -from freqtrade.enums import RPCMessageType, SellType, State +from freqtrade.enums import RPCMessageType, SellType, State, TradingMode from freqtrade.exceptions import (DependencyException, ExchangeError, InsufficientFundsError, InvalidOrderException, PricingError) from freqtrade.exchange import timeframe_to_minutes, timeframe_to_seconds @@ -105,11 +105,26 @@ class FreqtradeBot(LoggingMixin): self._exit_lock = Lock() LoggingMixin.__init__(self, logger, timeframe_to_seconds(self.strategy.timeframe)) + if self.config.get('trading_mode') == "cross_margin": + self.trading_mode = TradingMode.CROSS_MARGIN + elif self.config.get('trading_mode') == "isolated_margin": + self.trading_mode = TradingMode.ISOLATED_MARGIN + elif self.config.get('trading_mode') == "cross_futures": + self.trading_mode = TradingMode.CROSS_FUTURES + elif self.config.get('trading_mode') == "isolated_futures": + self.trading_mode = TradingMode.ISOLATED_FUTURES + else: + self.trading_mode = TradingMode.SPOT + # Start calculating maintenance margin if on cross margin # TODO: Add margin_mode to freqtrade.configuration? - if self.config.get('leverage_type') == "cross" or \ - self.config.get('leverage_type') == "cross_futures": - self.maintenance_margin = MaintenanceMargin(self.exchange.liq_formula) + if self.trading_mode == TradingMode.CROSS_MARGIN or \ + self.trading_mode == TradingMode.CROSS_FUTURES: + + self.maintenance_margin = MaintenanceMargin( + liq_formula=self.exchange.liq_formula, + trading_mode=self.trading_mode + ) self.maintenance_margin.run def notify_status(self, msg: str) -> None: @@ -633,14 +648,16 @@ class FreqtradeBot(LoggingMixin): is_short=is_short ) - # TODO-mg: if margin == isolated - isolated_liq = self.exchange.get_isolated_liq( - pair=pair, - open_rate=enter_limit_filled_price, - amount=amount, - leverage=leverage, - is_short=is_short - ) + if self.trading_mode == TradingMode.ISOLATED_MARGIN or \ + self.trading_mode == TradingMode.ISOLATED_FUTURES: + + isolated_liq = self.exchange.liq_formula( + trading_mode=self.trading_mode, + open_rate=enter_limit_filled_price, + amount=amount, + leverage=leverage, + is_short=is_short + ) # Fee is applied twice because we make a LIMIT_BUY and LIMIT_SELL fee = self.exchange.get_fee(symbol=pair, taker_or_maker='maker') @@ -664,7 +681,9 @@ class FreqtradeBot(LoggingMixin): is_short=is_short, interest_rate=interest_rate, interest_mode=self.exchange.interest_mode, - isolated_liq=isolated_liq + isolated_liq=isolated_liq, + trading_mode=self.trading_mode, + liq_formula=self.exchange.liq_formula, ) trade.orders.append(order_obj) diff --git a/freqtrade/maintenance_margin.py b/freqtrade/maintenance_margin.py index e2806e088..64055405b 100644 --- a/freqtrade/maintenance_margin.py +++ b/freqtrade/maintenance_margin.py @@ -1,25 +1,29 @@ from typing import List -from freqtrade.enums import LiqFormula +from freqtrade.enums import LiqFormula, TradingMode from freqtrade.persistence import Trade class MaintenanceMargin: trades: List[Trade] - formula: LiqFormula + liq_formula: LiqFormula + trading_mode: TradingMode @property def margin_level(self): - return self.formula() # TODO: Add args to formula + return self.liq_formula( + trading_mode=self.trading_mode + # TODO: Add args to formula + ) @property def liq_level(self): # This may be a constant value and may not need a function return # If constant, would need to be recalculated after each new trade - def __init__(self, formula: LiqFormula): - self.formula = formula - return + def __init__(self, liq_formula: LiqFormula, trading_mode: TradingMode): + self.liq_formula = liq_formula + self.trading_mode = trading_mode def add_new_trade(self, trade): return