Changed liquidation_price to isolated_liq

This commit is contained in:
Sam Germain 2021-07-25 02:57:17 -06:00
parent 1918304c5b
commit 4fcae0d927
4 changed files with 47 additions and 47 deletions

View File

@ -50,7 +50,7 @@ def migrate_trades_table(decl_base, inspector, engine, table_back_name: str, col
leverage = get_column_def(cols, 'leverage', '1.0')
interest_rate = get_column_def(cols, 'interest_rate', '0.0')
liquidation_price = get_column_def(cols, 'liquidation_price', 'null')
isolated_liq = get_column_def(cols, 'isolated_liq', 'null')
# sqlite does not support literals for booleans
is_short = get_column_def(cols, 'is_short', '0')
interest_mode = get_column_def(cols, 'interest_mode', 'null')
@ -90,7 +90,7 @@ def migrate_trades_table(decl_base, inspector, engine, table_back_name: str, col
stoploss_order_id, stoploss_last_update,
max_rate, min_rate, sell_reason, sell_order_status, strategy,
timeframe, open_trade_value, close_profit_abs,
leverage, interest_rate, liquidation_price, is_short, interest_mode
leverage, interest_rate, isolated_liq, is_short, interest_mode
)
select id, lower(exchange),
case
@ -115,7 +115,7 @@ def migrate_trades_table(decl_base, inspector, engine, table_back_name: str, col
{strategy} strategy, {timeframe} timeframe,
{open_trade_value} open_trade_value, {close_profit_abs} close_profit_abs,
{leverage} leverage, {interest_rate} interest_rate,
{liquidation_price} liquidation_price, {is_short} is_short,
{isolated_liq} isolated_liq, {is_short} is_short,
{interest_mode} interest_mode
from {table_back_name}
"""))

View File

@ -265,7 +265,7 @@ class LocalTrade():
# Margin trading properties
interest_rate: float = 0.0
liquidation_price: Optional[float] = None
isolated_liq: Optional[float] = None
is_short: bool = False
leverage: float = 1.0
interest_mode: InterestMode = InterestMode.NONE
@ -314,8 +314,8 @@ class LocalTrade():
def __init__(self, **kwargs):
for key in kwargs:
setattr(self, key, kwargs[key])
if self.liquidation_price:
self.set_liquidation_price(self.liquidation_price)
if self.isolated_liq:
self.set_isolated_liq(self.isolated_liq)
self.recalc_open_trade_value()
def set_stop_loss(self, stop_loss: float):
@ -323,11 +323,11 @@ class LocalTrade():
Method you should use to set self.stop_loss.
Assures stop_loss is not passed the liquidation price
"""
if self.liquidation_price is not None:
if self.isolated_liq is not None:
if self.is_short:
sl = min(stop_loss, self.liquidation_price)
sl = min(stop_loss, self.isolated_liq)
else:
sl = max(stop_loss, self.liquidation_price)
sl = max(stop_loss, self.isolated_liq)
else:
sl = stop_loss
@ -335,21 +335,21 @@ class LocalTrade():
self.initial_stop_loss = sl
self.stop_loss = sl
def set_liquidation_price(self, liquidation_price: float):
def set_isolated_liq(self, isolated_liq: float):
"""
Method you should use to set self.liquidation price.
Assures stop_loss is not passed the liquidation price
"""
if self.stop_loss is not None:
if self.is_short:
self.stop_loss = min(self.stop_loss, liquidation_price)
self.stop_loss = min(self.stop_loss, isolated_liq)
else:
self.stop_loss = max(self.stop_loss, liquidation_price)
self.stop_loss = max(self.stop_loss, isolated_liq)
else:
self.initial_stop_loss = liquidation_price
self.stop_loss = liquidation_price
self.initial_stop_loss = isolated_liq
self.stop_loss = isolated_liq
self.liquidation_price = liquidation_price
self.isolated_liq = isolated_liq
def set_is_short(self, is_short: bool):
self.is_short = is_short
@ -425,7 +425,7 @@ class LocalTrade():
'leverage': self.leverage,
'interest_rate': self.interest_rate,
'liquidation_price': self.liquidation_price,
'isolated_liq': self.isolated_liq,
'is_short': self.is_short,
'open_order_id': self.open_order_id,
@ -472,13 +472,13 @@ class LocalTrade():
if self.is_short:
new_loss = float(current_price * (1 + abs(stoploss)))
# If trading on margin, don't set the stoploss below the liquidation price
if self.liquidation_price:
new_loss = min(self.liquidation_price, new_loss)
if self.isolated_liq:
new_loss = min(self.isolated_liq, new_loss)
else:
new_loss = float(current_price * (1 - abs(stoploss)))
# If trading on margin, don't set the stoploss below the liquidation price
if self.liquidation_price:
new_loss = max(self.liquidation_price, new_loss)
if self.isolated_liq:
new_loss = max(self.isolated_liq, new_loss)
# no stop loss assigned yet
if not self.stop_loss:
@ -905,7 +905,7 @@ class Trade(_DECL_BASE, LocalTrade):
# Margin trading properties
leverage = Column(Float, nullable=True, default=1.0)
interest_rate = Column(Float, nullable=False, default=0.0)
liquidation_price = Column(Float, nullable=True)
isolated_liq = Column(Float, nullable=True)
is_short = Column(Boolean, nullable=False, default=False)
interest_mode = Column(Enum(InterestMode), nullable=True)
# End of margin trading properties

View File

@ -109,7 +109,7 @@ def test_rpc_trade_status(default_conf, ticker, fee, mocker) -> None:
'exchange': 'binance',
'leverage': 1.0,
'interest_rate': 0.0,
'liquidation_price': None,
'isolated_liq': None,
'is_short': False,
}
@ -179,7 +179,7 @@ def test_rpc_trade_status(default_conf, ticker, fee, mocker) -> None:
'exchange': 'binance',
'leverage': 1.0,
'interest_rate': 0.0,
'liquidation_price': None,
'isolated_liq': None,
'is_short': False,
}

View File

@ -112,7 +112,7 @@ def test_is_opening_closing_trade(fee):
@pytest.mark.usefixtures("init_persistence")
def test_set_stop_loss_liquidation_price(fee):
def test_set_stop_loss_isolated_liq(fee):
trade = Trade(
id=2,
pair='ETH/BTC',
@ -127,36 +127,36 @@ def test_set_stop_loss_liquidation_price(fee):
is_short=False,
leverage=2.0
)
trade.set_liquidation_price(0.09)
assert trade.liquidation_price == 0.09
trade.set_isolated_liq(0.09)
assert trade.isolated_liq == 0.09
assert trade.stop_loss == 0.09
assert trade.initial_stop_loss == 0.09
trade.set_stop_loss(0.1)
assert trade.liquidation_price == 0.09
assert trade.isolated_liq == 0.09
assert trade.stop_loss == 0.1
assert trade.initial_stop_loss == 0.09
trade.set_liquidation_price(0.08)
assert trade.liquidation_price == 0.08
trade.set_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_liquidation_price(0.11)
assert trade.liquidation_price == 0.11
trade.set_isolated_liq(0.11)
assert trade.isolated_liq == 0.11
assert trade.stop_loss == 0.11
assert trade.initial_stop_loss == 0.09
trade.set_stop_loss(0.1)
assert trade.liquidation_price == 0.11
assert trade.isolated_liq == 0.11
assert trade.stop_loss == 0.11
assert trade.initial_stop_loss == 0.09
trade.stop_loss = None
trade.liquidation_price = None
trade.isolated_liq = None
trade.initial_stop_loss = None
trade.set_stop_loss(0.07)
assert trade.liquidation_price is None
assert trade.isolated_liq is None
assert trade.stop_loss == 0.07
assert trade.initial_stop_loss == 0.07
@ -164,28 +164,28 @@ def test_set_stop_loss_liquidation_price(fee):
trade.stop_loss = None
trade.initial_stop_loss = None
trade.set_liquidation_price(0.09)
assert trade.liquidation_price == 0.09
trade.set_isolated_liq(0.09)
assert trade.isolated_liq == 0.09
assert trade.stop_loss == 0.09
assert trade.initial_stop_loss == 0.09
trade.set_stop_loss(0.08)
assert trade.liquidation_price == 0.09
assert trade.isolated_liq == 0.09
assert trade.stop_loss == 0.08
assert trade.initial_stop_loss == 0.09
trade.set_liquidation_price(0.1)
assert trade.liquidation_price == 0.1
trade.set_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_liquidation_price(0.07)
assert trade.liquidation_price == 0.07
trade.set_isolated_liq(0.07)
assert trade.isolated_liq == 0.07
assert trade.stop_loss == 0.07
assert trade.initial_stop_loss == 0.09
trade.set_stop_loss(0.1)
assert trade.liquidation_price == 0.07
assert trade.isolated_liq == 0.07
assert trade.stop_loss == 0.07
assert trade.initial_stop_loss == 0.09
@ -1616,10 +1616,10 @@ def test_adjust_stop_loss_short(fee):
assert trade.initial_stop_loss == 1.05
assert trade.initial_stop_loss_pct == 0.05
assert trade.stop_loss_pct == 0.1
trade.set_liquidation_price(0.63)
trade.set_isolated_liq(0.63)
trade.adjust_stop_loss(0.59, -0.1)
assert trade.stop_loss == 0.63
assert trade.liquidation_price == 0.63
assert trade.isolated_liq == 0.63
def test_adjust_min_max_rates(fee):
@ -1744,7 +1744,7 @@ def test_to_json(default_conf, fee):
'exchange': 'binance',
'leverage': None,
'interest_rate': None,
'liquidation_price': None,
'isolated_liq': None,
'is_short': None,
}
@ -1813,7 +1813,7 @@ def test_to_json(default_conf, fee):
'exchange': 'binance',
'leverage': None,
'interest_rate': None,
'liquidation_price': None,
'isolated_liq': None,
'is_short': None,
}
@ -1932,7 +1932,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_liquidation_price(1.0)
trade_adj.set_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