Turned amount into a computed property

This commit is contained in:
Sam Germain 2021-06-28 08:19:20 -06:00
parent da81be9050
commit 2a50f4ff7b
2 changed files with 25 additions and 12 deletions

View File

@ -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)

View File

@ -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):
"""