diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 179c99d2c..e797f28c4 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -16,10 +16,11 @@ 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 +from freqtrade.maintenance_margin import MaintenanceMargin from freqtrade.misc import safe_value_fallback, safe_value_fallback2 from freqtrade.mixins import LoggingMixin from freqtrade.persistence import Order, PairLocks, Trade, cleanup_db, init_db @@ -102,6 +103,17 @@ class FreqtradeBot(LoggingMixin): self._sell_lock = Lock() LoggingMixin.__init__(self, logger, timeframe_to_seconds(self.strategy.timeframe)) + if self.config.get("trading_mode"): + self.trading_mode = TradingMode(self.config.get("trading_mode")) + + # Start calculating maintenance margin if on cross margin + # TODO: Add margin_mode to freqtrade.configuration? + if self.config.get('collateral') == "cross": + self.maintenance_margin = MaintenanceMargin( + exchange_name=self.exchange.name, + trading_mode=self.trading_mode) + self.maintenance_margin.run + def notify_status(self, msg: str) -> None: """ Public method for users of this class (worker, etc.) to send notifications diff --git a/freqtrade/maintenance_margin.py b/freqtrade/maintenance_margin.py new file mode 100644 index 000000000..10d89debb --- /dev/null +++ b/freqtrade/maintenance_margin.py @@ -0,0 +1,52 @@ +from typing import List + +from freqtrade.enums import TradingMode +from freqtrade.leverage import liquidation_price +from freqtrade.persistence import Trade + + +class MaintenanceMargin: + + trades: List[Trade] + exchange_name: str + trading_mode: TradingMode + + @property + def margin_level(self): + # This is the current value of all assets, + # and if you pass below liq_level, you are liquidated + # TODO: Add args to formula + return liquidation_price( + trading_mode=self.trading_mode, + exchange_name=self.exchange_name + ) + + @property + def liq_level(self): # This may be a constant value and may not need a function + # TODO-lev: The is the value that you are liquidated at + return # If constant, would need to be recalculated after each new trade + + def __init__(self, exchange_name: str, trading_mode: TradingMode): + self.exchange_name = exchange_name + self.trading_mode = trading_mode + return + + def add_new_trade(self, trade): + self.trades.append(trade) + + def remove_trade(self, trade): + self.trades.remove(trade) + + # ? def update_trade_pric(self): + + def sell_all(self): + # TODO-lev + return + + def run(self): + # TODO-lev: implement a thread that constantly updates with every price change, + # TODO-lev: must update at least every few seconds or so + # while true: + # if self.margin_level <= self.liq_level: + # self.sell_all() + return