Added in isolated margin and trading_mode to freqtradebot

This commit is contained in:
Sam Germain 2021-08-02 05:26:56 -06:00
parent 986c6fe17a
commit 1af6d8abca
2 changed files with 42 additions and 19 deletions

View File

@ -16,7 +16,7 @@ from freqtrade.configuration import validate_config_consistency
from freqtrade.data.converter import order_book_to_dataframe from freqtrade.data.converter import order_book_to_dataframe
from freqtrade.data.dataprovider import DataProvider from freqtrade.data.dataprovider import DataProvider
from freqtrade.edge import Edge 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, from freqtrade.exceptions import (DependencyException, ExchangeError, InsufficientFundsError,
InvalidOrderException, PricingError) InvalidOrderException, PricingError)
from freqtrade.exchange import timeframe_to_minutes, timeframe_to_seconds from freqtrade.exchange import timeframe_to_minutes, timeframe_to_seconds
@ -105,11 +105,26 @@ class FreqtradeBot(LoggingMixin):
self._exit_lock = Lock() self._exit_lock = Lock()
LoggingMixin.__init__(self, logger, timeframe_to_seconds(self.strategy.timeframe)) 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 # Start calculating maintenance margin if on cross margin
# TODO: Add margin_mode to freqtrade.configuration? # TODO: Add margin_mode to freqtrade.configuration?
if self.config.get('leverage_type') == "cross" or \ if self.trading_mode == TradingMode.CROSS_MARGIN or \
self.config.get('leverage_type') == "cross_futures": self.trading_mode == TradingMode.CROSS_FUTURES:
self.maintenance_margin = MaintenanceMargin(self.exchange.liq_formula)
self.maintenance_margin = MaintenanceMargin(
liq_formula=self.exchange.liq_formula,
trading_mode=self.trading_mode
)
self.maintenance_margin.run self.maintenance_margin.run
def notify_status(self, msg: str) -> None: def notify_status(self, msg: str) -> None:
@ -633,9 +648,11 @@ class FreqtradeBot(LoggingMixin):
is_short=is_short is_short=is_short
) )
# TODO-mg: if margin == isolated if self.trading_mode == TradingMode.ISOLATED_MARGIN or \
isolated_liq = self.exchange.get_isolated_liq( self.trading_mode == TradingMode.ISOLATED_FUTURES:
pair=pair,
isolated_liq = self.exchange.liq_formula(
trading_mode=self.trading_mode,
open_rate=enter_limit_filled_price, open_rate=enter_limit_filled_price,
amount=amount, amount=amount,
leverage=leverage, leverage=leverage,
@ -664,7 +681,9 @@ class FreqtradeBot(LoggingMixin):
is_short=is_short, is_short=is_short,
interest_rate=interest_rate, interest_rate=interest_rate,
interest_mode=self.exchange.interest_mode, 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) trade.orders.append(order_obj)

View File

@ -1,25 +1,29 @@
from typing import List from typing import List
from freqtrade.enums import LiqFormula from freqtrade.enums import LiqFormula, TradingMode
from freqtrade.persistence import Trade from freqtrade.persistence import Trade
class MaintenanceMargin: class MaintenanceMargin:
trades: List[Trade] trades: List[Trade]
formula: LiqFormula liq_formula: LiqFormula
trading_mode: TradingMode
@property @property
def margin_level(self): 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 @property
def liq_level(self): # This may be a constant value and may not need a function 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 return # If constant, would need to be recalculated after each new trade
def __init__(self, formula: LiqFormula): def __init__(self, liq_formula: LiqFormula, trading_mode: TradingMode):
self.formula = formula self.liq_formula = liq_formula
return self.trading_mode = trading_mode
def add_new_trade(self, trade): def add_new_trade(self, trade):
return return