Made borrowed a computed property

This commit is contained in:
Sam Germain 2021-06-28 08:31:05 -06:00
parent 2a50f4ff7b
commit 876386d2db

View File

@ -269,8 +269,8 @@ class LocalTrade():
interest_rate: float = 0.0
liquidation_price: float = None
is_short: bool = False
borrowed: float = 0.0
_leverage: float = None # * You probably want to use LocalTrade.leverage instead
_borrowed: float = 0.0
leverage: float = None # * You probably want to use LocalTrade.leverage instead
# @property
# def base_currency(self) -> str:
@ -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
@ -290,22 +290,21 @@ class LocalTrade():
self._amount = value
@property
def leverage(self) -> float:
return self._leverage
@leverage.setter
def leverage(self, value):
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')
self._leverage = value
def borrowed(self) -> float:
if self.leverage is not None:
if self.is_short:
# If shorting the full amount must be borrowed
self.borrowed = self._amount * value
return self._amount * self.leverage
else:
# If not shorting, then the trader already owns a bit
self.borrowed = self._amount * (value-1)
return self._amount * (self.leverage-1)
else:
return self._borrowed
@borrowed.setter
def borrowed(self, value):
self._borrowed = value
self.leverage = None
# End of margin trading properties
@ -897,8 +896,8 @@ class Trade(_DECL_BASE, LocalTrade):
timeframe = Column(Integer, nullable=True)
# Margin trading properties
_leverage: float = None # * You probably want to use LocalTrade.leverage instead
borrowed = Column(Float, nullable=False, default=0.0)
leverage: float = None # * You probably want to use LocalTrade.leverage instead
_borrowed = Column(Float, nullable=False, default=0.0)
interest_rate = Column(Float, nullable=False, default=0.0)
liquidation_price = Column(Float, nullable=True)
is_short = Column(Boolean, nullable=False, default=False)