Call calc_open_price() whenever necessary

This commit is contained in:
Matthias 2019-12-17 07:08:36 +01:00
parent 307ade6251
commit 861a7834fc
3 changed files with 15 additions and 8 deletions

View File

@ -555,6 +555,7 @@ class FreqtradeBot:
order['amount'] = new_amount
# Fee was applied, so set to 0
trade.fee_open = 0
trade.recalc_open_trade_price()
except DependencyException as exception:
logger.warning("Could not update trade amount: %s", exception)
@ -850,6 +851,7 @@ class FreqtradeBot:
trade.amount = new_amount
# Fee was applied, so set to 0
trade.fee_open = 0
trade.recalc_open_trade_price()
except DependencyException as e:
logger.warning("Could not update trade amount: %s", e)

View File

@ -216,7 +216,7 @@ class Trade(_DECL_BASE):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.open_trade_price = self._calc_open_trade_price()
self.recalc_open_trade_price()
def __repr__(self):
open_since = self.open_date.strftime('%Y-%m-%d %H:%M:%S') if self.is_open else 'closed'
@ -310,7 +310,7 @@ class Trade(_DECL_BASE):
# Update open rate and actual amount
self.open_rate = Decimal(order['price'])
self.amount = Decimal(order['amount'])
self.open_trade_price = self._calc_open_trade_price()
self.recalc_open_trade_price()
logger.info('%s_BUY has been fulfilled for %s.', order_type.upper(), self)
self.open_order_id = None
elif order_type in ('market', 'limit') and order['side'] == 'sell':
@ -351,6 +351,13 @@ class Trade(_DECL_BASE):
fees = buy_trade * Decimal(self.fee_open)
return float(buy_trade + fees)
def recalc_open_trade_price(self) -> None:
"""
Recalculate open_trade_price.
Must be called whenever open_rate or fee_open is changed.
"""
self.open_trade_price - self._calc_open_trade_price()
def calc_close_trade_price(self, rate: Optional[float] = None,
fee: Optional[float] = None) -> float:
"""
@ -378,12 +385,11 @@ class Trade(_DECL_BASE):
If rate is not set self.close_rate will be used
:return: profit in stake currency as float
"""
open_trade_price = self._calc_open_trade_price()
close_trade_price = self.calc_close_trade_price(
rate=(rate or self.close_rate),
fee=(fee or self.fee_close)
)
profit = close_trade_price - open_trade_price
profit = close_trade_price - self.open_trade_price
return float(f"{profit:.8f}")
def calc_profit_percent(self, rate: Optional[float] = None,
@ -395,12 +401,11 @@ class Trade(_DECL_BASE):
:param fee: fee to use on the close rate (optional).
:return: profit in percentage as float
"""
open_trade_price = self._calc_open_trade_price()
close_trade_price = self.calc_close_trade_price(
rate=(rate or self.close_rate),
fee=(fee or self.fee_close)
)
profit_percent = (close_trade_price / open_trade_price) - 1
profit_percent = (close_trade_price / self.open_trade_price) - 1
return float(f"{profit_percent:.8f}")
@staticmethod

View File

@ -498,7 +498,7 @@ def test_migrate_old(mocker, default_conf, fee):
assert trade.max_rate == 0.0
assert trade.stop_loss == 0.0
assert trade.initial_stop_loss == 0.0
assert trade.open_trade_price == 0.26758131848350003
assert trade.open_trade_price == trade._calc_open_trade_price()
def test_migrate_new(mocker, default_conf, fee, caplog):
@ -581,7 +581,7 @@ def test_migrate_new(mocker, default_conf, fee, caplog):
assert log_has("trying trades_bak1", caplog)
assert log_has("trying trades_bak2", caplog)
assert log_has("Running database migration - backup available as trades_bak2", caplog)
assert trade.open_trade_price == 0.26758131848350003
assert trade.open_trade_price == trade._calc_open_trade_price()
def test_migrate_mid_state(mocker, default_conf, fee, caplog):