Turned amount into a computed property

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

View File

@ -280,7 +280,7 @@ class LocalTrade():
@property @property
def amount(self) -> float: def amount(self) -> float:
if self.leverage is not None: if self._leverage is not None:
return self._amount * self.leverage return self._amount * self.leverage
else: else:
return self._amount return self._amount
@ -295,12 +295,6 @@ class LocalTrade():
@leverage.setter @leverage.setter
def leverage(self, value): 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: if self.is_short is None or self.amount is None:
raise OperationalException( raise OperationalException(
'LocalTrade.amount and LocalTrade.is_short must be assigned before assigning leverage') 'LocalTrade.amount and LocalTrade.is_short must be assigned before assigning leverage')
@ -308,12 +302,10 @@ class LocalTrade():
self._leverage = value self._leverage = value
if self.is_short: if self.is_short:
# If shorting the full amount must be borrowed # If shorting the full amount must be borrowed
self.borrowed = self.amount * value self.borrowed = self._amount * value
else: else:
# If not shorting, then the trader already owns a bit # If not shorting, then the trader already owns a bit
self.borrowed = self.amount * (value-1) 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
# End of margin trading properties # End of margin trading properties
@ -878,7 +870,7 @@ class Trade(_DECL_BASE, LocalTrade):
close_profit = Column(Float) close_profit = Column(Float)
close_profit_abs = Column(Float) close_profit_abs = Column(Float)
stake_amount = Column(Float, nullable=False) stake_amount = Column(Float, nullable=False)
amount = Column(Float) _amount = Column(Float)
amount_requested = Column(Float) amount_requested = Column(Float)
open_date = Column(DateTime, nullable=False, default=datetime.utcnow) open_date = Column(DateTime, nullable=False, default=datetime.utcnow)
close_date = Column(DateTime) close_date = Column(DateTime)

View File

@ -105,6 +105,27 @@ def test_is_opening_closing_trade(fee):
assert trade.is_closing_trade('sell') == False 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") @pytest.mark.usefixtures("init_persistence")
def test_update_with_binance(limit_buy_order, limit_sell_order, fee, caplog): def test_update_with_binance(limit_buy_order, limit_sell_order, fee, caplog):
""" """