From 2a50f4ff7bd5ee7680dd61364845b53a75a80dbd Mon Sep 17 00:00:00 2001 From: Sam Germain Date: Mon, 28 Jun 2021 08:19:20 -0600 Subject: [PATCH] Turned amount into a computed property --- freqtrade/persistence/models.py | 16 ++++------------ tests/test_persistence.py | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/freqtrade/persistence/models.py b/freqtrade/persistence/models.py index 29e2f59e3..62a4132d5 100644 --- a/freqtrade/persistence/models.py +++ b/freqtrade/persistence/models.py @@ -280,7 +280,7 @@ class LocalTrade(): @property def amount(self) -> float: - if self.leverage is not None: + if self._leverage is not None: return self._amount * self.leverage else: return self._amount @@ -295,12 +295,6 @@ class LocalTrade(): @leverage.setter def leverage(self, value): - # def set_leverage(self, lev: float, is_short: Optional[bool], amount: Optional[float]): - # TODO: Should this be @leverage.setter, or should it take arguments is_short and amount - # if is_short is None: - # is_short = self.is_short - # if amount is None: - # amount = self.amount if self.is_short is None or self.amount is None: raise OperationalException( 'LocalTrade.amount and LocalTrade.is_short must be assigned before assigning leverage') @@ -308,12 +302,10 @@ class LocalTrade(): self._leverage = value if self.is_short: # If shorting the full amount must be borrowed - self.borrowed = self.amount * value + self.borrowed = self._amount * value else: # If not shorting, then the trader already owns a bit - self.borrowed = self.amount * (value-1) - # TODO: Maybe amount should be a computed property, so we don't have to modify it - self.amount = self.amount * value + self.borrowed = self._amount * (value-1) # End of margin trading properties @@ -878,7 +870,7 @@ class Trade(_DECL_BASE, LocalTrade): close_profit = Column(Float) close_profit_abs = Column(Float) stake_amount = Column(Float, nullable=False) - amount = Column(Float) + _amount = Column(Float) amount_requested = Column(Float) open_date = Column(DateTime, nullable=False, default=datetime.utcnow) close_date = Column(DateTime) diff --git a/tests/test_persistence.py b/tests/test_persistence.py index 40542f943..358b59243 100644 --- a/tests/test_persistence.py +++ b/tests/test_persistence.py @@ -105,6 +105,27 @@ def test_is_opening_closing_trade(fee): assert trade.is_closing_trade('sell') == False +@pytest.mark.usefixtures("init_persistence") +def test_amount(limit_buy_order, limit_sell_order, fee, caplog): + trade = Trade( + id=2, + pair='ETH/BTC', + stake_amount=0.001, + open_rate=0.01, + amount=5, + is_open=True, + open_date=arrow.utcnow().datetime, + fee_open=fee.return_value, + fee_close=fee.return_value, + exchange='binance', + is_short=False + ) + assert trade.amount == 5 + trade.leverage = 3 + assert trade.amount == 15 + assert trade._amount == 5 + + @pytest.mark.usefixtures("init_persistence") def test_update_with_binance(limit_buy_order, limit_sell_order, fee, caplog): """