From d80db3c4dbea892c10c86261c3e6898047c964af Mon Sep 17 00:00:00 2001 From: Sam Germain Date: Sun, 1 Aug 2021 17:27:26 -0600 Subject: [PATCH] Changed leveragemode to tradingmode, added tests for liqformula enum, reorganized liqformula --- freqtrade/enums/__init__.py | 2 +- freqtrade/enums/liqformula.py | 91 ++++++++++++++---------- tests/leverage/test_liquidation_price.py | 65 +++++++++++++++++ 3 files changed, 118 insertions(+), 40 deletions(-) create mode 100644 tests/leverage/test_liquidation_price.py diff --git a/freqtrade/enums/__init__.py b/freqtrade/enums/__init__.py index df1bc1dc8..87e9dfb6b 100644 --- a/freqtrade/enums/__init__.py +++ b/freqtrade/enums/__init__.py @@ -1,10 +1,10 @@ # flake8: noqa: F401 from freqtrade.enums.backteststate import BacktestState from freqtrade.enums.interestmode import InterestMode -from freqtrade.enums.leveragemode import LeverageMode from freqtrade.enums.liqformula import LiqFormula from freqtrade.enums.rpcmessagetype import RPCMessageType from freqtrade.enums.runmode import NON_UTIL_MODES, OPTIMIZE_MODES, TRADING_MODES, RunMode from freqtrade.enums.selltype import SellType from freqtrade.enums.signaltype import SignalTagType, SignalType from freqtrade.enums.state import State +from freqtrade.enums.tradingmode import TradingMode diff --git a/freqtrade/enums/liqformula.py b/freqtrade/enums/liqformula.py index e7892d7ed..cb021d8ca 100644 --- a/freqtrade/enums/liqformula.py +++ b/freqtrade/enums/liqformula.py @@ -26,47 +26,60 @@ class LiqFormula(Enum): raise OperationalException( f"Freqtrade does not support {trading_mode.value} on {self.name}") 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 __binance(self, trading_mode: TradingMode): + # TODO-mg: Additional arguments, fill in formulas + + if trading_mode == TradingMode.CROSS_MARGIN: + # TODO-mg: perform a calculation based on this formula + # https://www.binance.com/en/support/faq/f6b010588e55413aa58b7d63ee0125ed + self.__exception(trading_mode) + elif trading_mode == TradingMode.ISOLATED_MARGIN: + self.__exception(trading_mode) # Likely won't be implemented + elif trading_mode == TradingMode.CROSS_FUTURES: + # TODO-mg: 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 + self.__exception(trading_mode) + else: + self.__exception(trading_mode) + + def __kraken(self, trading_mode: TradingMode): + # TODO-mg: Additional arguments, fill in formulas + + if trading_mode == TradingMode.CROSS_MARGIN: + self.__exception(trading_mode) + # TODO-mg: perform a calculation based on this formula + # 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 - # * Cross Margin - if trading_mode == TradingMode.CROSS: - if self.name == "BINANCE": - # TODO: perform a calculation based on this formula - # https://www.binance.com/en/support/faq/f6b010588e55413aa58b7d63ee0125ed - self.__exception(trading_mode) - elif self.name == "KRAKEN": - # TODO: perform a calculation based on this formula - # 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) + trading_mode: TradingMode = k['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) + if trading_mode == TradingMode.SPOT: + return None - # * Cross Futures - elif trading_mode == TradingMode.CROSS_FUTURES: - if self.name == "BINANCE": - # TODO: perform a calculation based on this formula - # https://www.binance.com/en/support/faq/b3c689c1f50a44cabb3a84e663b81d93 - self.__exception(trading_mode) - else: - self.__exception(trading_mode) - - # * Isolated Futures - elif trading_mode == TradingMode.ISOLATED_FUTURES: - if self.name == "BINANCE": - # TODO: perform a calculation based on this formula - # https://www.binance.com/en/support/faq/b3c689c1f50a44cabb3a84e663b81d93 - self.__exception(trading_mode) - elif self.name == "KRAKEN": # Kraken doesn't have isolated margin - self.__exception(trading_mode, False) - else: - self.__exception(trading_mode) + 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: + self.__exception(trading_mode) diff --git a/tests/leverage/test_liquidation_price.py b/tests/leverage/test_liquidation_price.py new file mode 100644 index 000000000..babe6a513 --- /dev/null +++ b/tests/leverage/test_liquidation_price.py @@ -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