Split tradingmode into tradingmode and collateral, moved functions out of liqformula class

This commit is contained in:
Sam Germain 2021-08-02 20:05:48 -06:00
parent 923e4ead49
commit 6c489f18fe
4 changed files with 18 additions and 20 deletions

View File

@ -9,6 +9,9 @@ from freqtrade.exceptions import OperationalException
# from math import ceil
# from math import ceil
class LiqFormula(Enum):
"""Equations to calculate liquidation price"""

View File

@ -1,11 +1,12 @@
from freqtrade.enums import LiqFormula, TradingMode
from freqtrade.exceptions import OperationalException
from typing import List
from freqtrade.enums import LiqFormula
from freqtrade.persistence import Trade
class MaintenanceMargin:
trades: list[Trade]
trades: List[Trade]
formula: LiqFormula
@property
@ -16,12 +17,8 @@ class MaintenanceMargin:
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, trading_mode: TradingMode):
if (
trading_mode != TradingMode.CROSS_MARGIN or
trading_mode != TradingMode.CROSS_FUTURES
):
raise OperationalException("Maintenance margin should only be used for cross trading")
def __init__(self, formula: LiqFormula):
self.formula = formula
return
def add_new_trade(self, trade):

View File

@ -697,8 +697,7 @@ class LocalTrade():
if trading_mode == TradingMode.SPOT:
return float(self._calc_base_close(amount, rate, fee))
elif (trading_mode == TradingMode.CROSS_MARGIN or
trading_mode == TradingMode.ISOLATED_MARGIN):
elif (trading_mode == TradingMode.MARGIN):
interest = self.calculate_interest(interest_rate)
@ -709,8 +708,7 @@ class LocalTrade():
# Currency already owned for longs, no need to purchase
return float(self._calc_base_close(amount, rate, fee) - interest)
elif (trading_mode == TradingMode.CROSS_FUTURES or
trading_mode == TradingMode.ISOLATED_FUTURES):
elif (trading_mode == TradingMode.FUTURES):
# TODO-lev: implement
raise OperationalException("Futures is not yet available using freqtrade")
else:

View File

@ -238,7 +238,7 @@ def test_interest(market_buy_order_usdt, fee):
leverage=3.0,
interest_rate=0.0005,
interest_mode=InterestMode.HOURSPERDAY,
trading_mode=TradingMode.CROSS_MARGIN
trading_mode=TradingMode.MARGIN
)
# 10min, 3x leverage
@ -552,7 +552,7 @@ def test_update_limit_order(limit_buy_order_usdt, limit_sell_order_usdt, fee, ca
leverage=3.0,
interest_rate=0.0005,
interest_mode=InterestMode.HOURSPERDAY,
trading_mode=TradingMode.CROSS_MARGIN
trading_mode=TradingMode.MARGIN
)
trade.open_order_id = 'something'
trade.update(limit_sell_order_usdt)
@ -646,7 +646,7 @@ def test_calc_open_close_trade_price(limit_buy_order_usdt, limit_sell_order_usdt
assert trade.calc_profit_ratio() == round(0.0945137157107232, 8)
# 3x leverage, binance
trade.trading_mode = TradingMode.ISOLATED_MARGIN
trade.trading_mode = TradingMode.MARGIN
trade.leverage = 3
trade.interest_mode = InterestMode.HOURSPERDAY
assert trade._calc_open_trade_value() == 60.15
@ -807,7 +807,7 @@ def test_calc_open_trade_value(limit_buy_order_usdt, fee):
assert trade._calc_open_trade_value() == 60.15
# Margin
trade.trading_mode = TradingMode.CROSS_MARGIN
trade.trading_mode = TradingMode.MARGIN
trade.is_short = True
trade.recalc_open_trade_value()
assert trade._calc_open_trade_value() == 59.85
@ -855,7 +855,7 @@ def test_calc_close_trade_price(limit_buy_order_usdt, limit_sell_order_usdt, fee
assert trade.calc_close_trade_value(fee=0.005) == 65.67
# 3x leverage binance
trade.trading_mode = TradingMode.CROSS_MARGIN
trade.trading_mode = TradingMode.MARGIN
trade.leverage = 3.0
assert round(trade.calc_close_trade_value(rate=2.5), 8) == 74.81166667
assert round(trade.calc_close_trade_value(rate=2.5, fee=0.003), 8) == 74.77416667
@ -1057,7 +1057,7 @@ def test_calc_profit(limit_buy_order_usdt, limit_sell_order_usdt, fee):
trade.open_trade_value = trade._calc_open_trade_value()
# Margin
trade.trading_mode = TradingMode.CROSS_MARGIN
trade.trading_mode = TradingMode.MARGIN
# 3x leverage, long ###################################################
trade.leverage = 3.0
# Higher than open rate - 2.1 quote
@ -1162,7 +1162,7 @@ def test_calc_profit_ratio(limit_buy_order_usdt, limit_sell_order_usdt, fee):
trade.open_trade_value = trade._calc_open_trade_value()
# Margin
trade.trading_mode = TradingMode.CROSS_MARGIN
trade.trading_mode = TradingMode.MARGIN
# 3x leverage, long ###################################################
trade.leverage = 3.0
# 2.1 quote - Higher than open rate