Changed leveragemode to tradingmode, added tests for liqformula enum, reorganized liqformula

This commit is contained in:
Sam Germain 2021-08-01 17:27:26 -06:00
parent 03aa99c5a2
commit d80db3c4db
3 changed files with 118 additions and 40 deletions

View File

@ -1,10 +1,10 @@
# flake8: noqa: F401 # flake8: noqa: F401
from freqtrade.enums.backteststate import BacktestState from freqtrade.enums.backteststate import BacktestState
from freqtrade.enums.interestmode import InterestMode from freqtrade.enums.interestmode import InterestMode
from freqtrade.enums.leveragemode import LeverageMode
from freqtrade.enums.liqformula import LiqFormula from freqtrade.enums.liqformula import LiqFormula
from freqtrade.enums.rpcmessagetype import RPCMessageType from freqtrade.enums.rpcmessagetype import RPCMessageType
from freqtrade.enums.runmode import NON_UTIL_MODES, OPTIMIZE_MODES, TRADING_MODES, RunMode from freqtrade.enums.runmode import NON_UTIL_MODES, OPTIMIZE_MODES, TRADING_MODES, RunMode
from freqtrade.enums.selltype import SellType from freqtrade.enums.selltype import SellType
from freqtrade.enums.signaltype import SignalTagType, SignalType from freqtrade.enums.signaltype import SignalTagType, SignalType
from freqtrade.enums.state import State from freqtrade.enums.state import State
from freqtrade.enums.tradingmode import TradingMode

View File

@ -26,47 +26,60 @@ class LiqFormula(Enum):
raise OperationalException( raise OperationalException(
f"Freqtrade does not support {trading_mode.value} on {self.name}") f"Freqtrade does not support {trading_mode.value} on {self.name}")
else: else:
raise OperationalException(f"{self.name} does not support {trading_mode.value}") raise OperationalException(f"{self.name} does not support {trading_mode.value} trading")
def __call__(self, **k): def __binance(self, trading_mode: TradingMode):
trading_mode: TradingMode = k.trading_mode # TODO-mg: Additional arguments, fill in formulas
# * Cross Margin if trading_mode == TradingMode.CROSS_MARGIN:
if trading_mode == TradingMode.CROSS: # TODO-mg: perform a calculation based on this formula
if self.name == "BINANCE":
# TODO: perform a calculation based on this formula
# https://www.binance.com/en/support/faq/f6b010588e55413aa58b7d63ee0125ed # https://www.binance.com/en/support/faq/f6b010588e55413aa58b7d63ee0125ed
self.__exception(trading_mode) self.__exception(trading_mode)
elif self.name == "KRAKEN": elif trading_mode == TradingMode.ISOLATED_MARGIN:
# TODO: perform a calculation based on this formula self.__exception(trading_mode) # Likely won't be implemented
# https://support.kraken.com/hc/en-us/articles/203325763-Margin-Call-Level-and-Margin-Liquidation-Level
self.__exception(trading_mode)
elif self.name == "FTX":
self.__exception(trading_mode)
# * Isolated Margin
elif trading_mode == TradingMode.ISOLATED:
if self.name == "KRAKEN": # Kraken doesn't have isolated margin
self.__exception(trading_mode, False)
else:
self.__exception(trading_mode)
# * Cross Futures
elif trading_mode == TradingMode.CROSS_FUTURES: elif trading_mode == TradingMode.CROSS_FUTURES:
if self.name == "BINANCE": # TODO-mg: perform a calculation based on this formula
# TODO: perform a calculation based on this formula # https://www.binance.com/en/support/faq/b3c689c1f50a44cabb3a84e663b81d93
self.__exception(trading_mode)
elif trading_mode == TradingMode.ISOLATED_FUTURES:
# TODO-mg: perform a calculation based on this formula
# https://www.binance.com/en/support/faq/b3c689c1f50a44cabb3a84e663b81d93 # https://www.binance.com/en/support/faq/b3c689c1f50a44cabb3a84e663b81d93
self.__exception(trading_mode) self.__exception(trading_mode)
else: else:
self.__exception(trading_mode) self.__exception(trading_mode)
# * Isolated Futures def __kraken(self, trading_mode: TradingMode):
elif trading_mode == TradingMode.ISOLATED_FUTURES: # TODO-mg: Additional arguments, fill in formulas
if self.name == "BINANCE":
# TODO: perform a calculation based on this formula if trading_mode == TradingMode.CROSS_MARGIN:
# https://www.binance.com/en/support/faq/b3c689c1f50a44cabb3a84e663b81d93
self.__exception(trading_mode) self.__exception(trading_mode)
elif self.name == "KRAKEN": # Kraken doesn't have isolated margin # TODO-mg: perform a calculation based on this formula
self.__exception(trading_mode, False) # https://support.kraken.com/hc/en-us/articles/203325763-Margin-Call-Level-and-Margin-Liquidation-Level
elif trading_mode == TradingMode.CROSS_FUTURES:
# TODO-mg: implement
self.__exception(trading_mode)
elif trading_mode == TradingMode.ISOLATED_MARGIN or \
trading_mode == TradingMode.ISOLATED_FUTURES:
self.__exception(trading_mode, True)
else:
self.__exception(trading_mode)
def __ftx(self, trading_mode: TradingMode):
# TODO-mg: Additional arguments, fill in formulas
self.__exception(trading_mode)
def __call__(self, **k):
trading_mode: TradingMode = k['trading_mode']
if trading_mode == TradingMode.SPOT:
return None
if self.name == "BINANCE":
return self.__binance(trading_mode)
elif self.name == "KRAKEN":
return self.__kraken(trading_mode)
elif self.name == "FTX":
return self.__ftx(trading_mode)
else: else:
self.__exception(trading_mode) self.__exception(trading_mode)

View File

@ -0,0 +1,65 @@
from freqtrade.enums import LiqFormula, TradingMode
# from freqtrade.exceptions import OperationalException
def test_liquidation_formula():
spot = TradingMode.SPOT
# cross_margin = TradingMode.CROSS_MARGIN
# isolated_margin = TradingMode.ISOLATED_MARGIN
# cross_futures = TradingMode.CROSS_FUTURES
# isolated_futures = TradingMode.ISOLATED_FUTURES
assert LiqFormula.BINANCE(
trading_mode=spot
) is None
# TODO-mg: Uncomment these assertions and make them real calculation tests
# assert LiqFormula.BINANCE(
# trading_mode=cross_margin
# ) == 1.0 #Replace 1.0 with real value
# assert LiqFormula.BINANCE(
# trading_mode=isolated_margin
# ) == 1.0
# assert LiqFormula.BINANCE(
# trading_mode=cross_futures
# ) == 1.0
# assert LiqFormula.BINANCE(
# trading_mode=isolated_futures
# ) == 1.0
assert LiqFormula.KRAKEN(
trading_mode=spot
) is None
# TODO-mg: Uncomment these assertions and make them real calculation tests
# assert LiqFormula.KRAKEN(
# trading_mode=cross_margin
# ) == 1.0
# LiqFormula.KRAKEN(
# trading_mode=isolated_margin
# )
# asset exception thrown #TODO-mg: Check that exception is thrown
# assert LiqFormula.KRAKEN(
# trading_mode=cross_futures
# ) == 1.0
# LiqFormula.KRAKEN(
# trading_mode=isolated_futures
# )
# asset exception thrown #TODO-mg: Check that exception is thrown
assert LiqFormula.FTX(
trading_mode=spot
) is None
# TODO-mg: Uncomment these assertions and make them real calculation tests
# assert LiqFormula.FTX(
# trading_mode=cross_margin
# ) == 1.0
# assert LiqFormula.FTX(
# trading_mode=isolated_margin
# ) == 1.0
# assert LiqFormula.FTX(
# trading_mode=cross_futures
# ) == 1.0
# assert LiqFormula.FTX(
# trading_mode=isolated_futures
# ) == 1.0