Added liquidation_price function

This commit is contained in:
Sam Germain
2021-08-06 01:15:18 -06:00
parent ecdecb02fa
commit 04f254b885
5 changed files with 220 additions and 15 deletions

View File

@@ -0,0 +1,89 @@
# from decimal import Decimal
from freqtrade.enums import Collateral, TradingMode
from freqtrade.leverage import liquidation_price
# from freqtrade.exceptions import OperationalException
binance = "binance"
kraken = "kraken"
ftx = "ftx"
other = "bittrex"
def test_liquidation_price():
spot = TradingMode.SPOT
margin = TradingMode.MARGIN
futures = TradingMode.FUTURES
cross = Collateral.CROSS
isolated = Collateral.ISOLATED
# NONE
assert liquidation_price(exchange_name=other, trading_mode=spot) is None
assert liquidation_price(exchange_name=other, trading_mode=margin,
collateral=cross) is None
assert liquidation_price(exchange_name=other, trading_mode=margin,
collateral=isolated) is None
assert liquidation_price(
exchange_name=other, trading_mode=futures, collateral=cross) is None
assert liquidation_price(exchange_name=other, trading_mode=futures,
collateral=isolated) is None
# Binance
assert liquidation_price(exchange_name=binance, trading_mode=spot) is None
assert liquidation_price(exchange_name=binance, trading_mode=spot,
collateral=cross) is None
assert liquidation_price(exchange_name=binance, trading_mode=spot,
collateral=isolated) is None
# TODO-lev: Uncomment these assertions and make them real calculation tests
# TODO-lev: Replace 1.0 with real value
# assert liquidation_price(
# exchange_name=binance,
# trading_mode=margin,
# collateral=cross
# ) == 1.0
# assert liquidation_price(
# exchange_name=binance,
# trading_mode=margin,
# collateral=isolated
# ) == 1.0
# assert liquidation_price(
# exchange_name=binance,
# trading_mode=futures,
# collateral=cross
# ) == 1.0
# Binance supports isolated margin, but freqtrade likely won't for a while on Binance
# liquidation_price(exchange_name=binance, trading_mode=margin, collateral=isolated)
# assert exception thrown #TODO-lev: Check that exception is thrown
# Kraken
assert liquidation_price(exchange_name=kraken, trading_mode=spot) is None
assert liquidation_price(exchange_name=kraken, trading_mode=spot, collateral=cross) is None
assert liquidation_price(exchange_name=kraken, trading_mode=spot,
collateral=isolated) is None
# TODO-lev: Uncomment these assertions and make them real calculation tests
# assert liquidation_price(kraken, trading_mode=margin, collateral=cross) == 1.0
# assert liquidation_price(kraken, trading_mode=margin, collateral=isolated) == 1.0
# liquidation_price(kraken, trading_mode=futures, collateral=cross)
# assert exception thrown #TODO-lev: Check that exception is thrown
# liquidation_price(kraken, trading_mode=futures, collateral=isolated)
# assert exception thrown #TODO-lev: Check that exception is thrown
# FTX
assert liquidation_price(ftx, trading_mode=spot) is None
assert liquidation_price(ftx, trading_mode=spot, collateral=cross) is None
assert liquidation_price(ftx, trading_mode=spot, collateral=isolated) is None
# TODO-lev: Uncomment these assertions and make them real calculation tests
# assert liquidation_price(ftx, trading_mode=margin, collateral=cross) == 1.0
# assert liquidation_price(ftx, trading_mode=margin, collateral=isolated) == 1.0
# liquidation_price(ftx, trading_mode=futures, collateral=cross)
# assert exception thrown #TODO-lev: Check that exception is thrown
# liquidation_price(ftx, trading_mode=futures, collateral=isolated)
# assert exception thrown #TODO-lev: Check that exception is thrown

View File

@@ -91,7 +91,7 @@ def test_enter_exit_side(fee):
@pytest.mark.usefixtures("init_persistence")
def test__set_stop_loss_isolated_liq(fee):
def test_set_stop_loss_isolated_liq(fee):
trade = Trade(
id=2,
pair='ADA/USDT',
@@ -106,7 +106,7 @@ def test__set_stop_loss_isolated_liq(fee):
is_short=False,
leverage=2.0
)
trade.set_isolated_liq(0.09)
trade.set_isolated_liq(isolated_liq=0.09)
assert trade.isolated_liq == 0.09
assert trade.stop_loss == 0.09
assert trade.initial_stop_loss == 0.09
@@ -116,12 +116,12 @@ def test__set_stop_loss_isolated_liq(fee):
assert trade.stop_loss == 0.1
assert trade.initial_stop_loss == 0.09
trade.set_isolated_liq(0.08)
trade.set_isolated_liq(isolated_liq=0.08)
assert trade.isolated_liq == 0.08
assert trade.stop_loss == 0.1
assert trade.initial_stop_loss == 0.09
trade.set_isolated_liq(0.11)
trade.set_isolated_liq(isolated_liq=0.11)
assert trade.isolated_liq == 0.11
assert trade.stop_loss == 0.11
assert trade.initial_stop_loss == 0.09
@@ -145,7 +145,7 @@ def test__set_stop_loss_isolated_liq(fee):
trade.stop_loss = None
trade.initial_stop_loss = None
trade.set_isolated_liq(0.09)
trade.set_isolated_liq(isolated_liq=0.09)
assert trade.isolated_liq == 0.09
assert trade.stop_loss == 0.09
assert trade.initial_stop_loss == 0.09
@@ -155,12 +155,12 @@ def test__set_stop_loss_isolated_liq(fee):
assert trade.stop_loss == 0.08
assert trade.initial_stop_loss == 0.09
trade.set_isolated_liq(0.1)
trade.set_isolated_liq(isolated_liq=0.1)
assert trade.isolated_liq == 0.1
assert trade.stop_loss == 0.08
assert trade.initial_stop_loss == 0.09
trade.set_isolated_liq(0.07)
trade.set_isolated_liq(isolated_liq=0.07)
assert trade.isolated_liq == 0.07
assert trade.stop_loss == 0.07
assert trade.initial_stop_loss == 0.09
@@ -234,7 +234,7 @@ def test_interest(market_buy_order_usdt, fee):
open_date=datetime.utcnow() - timedelta(hours=0, minutes=10),
fee_open=fee.return_value,
fee_close=fee.return_value,
exchange='kraken',
exchange='binance',
leverage=3.0,
interest_rate=0.0005,
interest_mode=InterestMode.HOURSPERDAY
@@ -506,7 +506,7 @@ def test_update_limit_order(limit_buy_order_usdt, limit_sell_order_usdt, fee, ca
open_date=arrow.utcnow().datetime,
fee_open=fee.return_value,
fee_close=fee.return_value,
exchange='binance',
exchange='binance'
)
assert trade.open_order_id is None
assert trade.close_profit is None
@@ -1575,11 +1575,11 @@ def test_adjust_stop_loss_short(fee):
assert trade.initial_stop_loss_pct == 0.05
# Initial is true but stop_loss set - so doesn't do anything
trade.adjust_stop_loss(0.3, -0.1, True)
assert round(trade.stop_loss, 8) == 0.66 # TODO-mg: What is this test?
assert round(trade.stop_loss, 8) == 0.66
assert trade.initial_stop_loss == 1.05
assert trade.initial_stop_loss_pct == 0.05
assert trade.stop_loss_pct == 0.1
trade.set_isolated_liq(0.63)
trade.set_isolated_liq(isolated_liq=0.63)
trade.adjust_stop_loss(0.59, -0.1)
assert trade.stop_loss == 0.63
assert trade.isolated_liq == 0.63
@@ -1899,7 +1899,7 @@ def test_stoploss_reinitialization_short(default_conf, fee):
assert trade_adj.initial_stop_loss == 1.04
assert trade_adj.initial_stop_loss_pct == 0.04
# Stoploss can't go above liquidation price
trade_adj.set_isolated_liq(1.0)
trade_adj.set_isolated_liq(isolated_liq=1.0)
trade.adjust_stop_loss(0.97, -0.04)
assert trade_adj.stop_loss == 1.0
assert trade_adj.stop_loss == 1.0