Created interest function
This commit is contained in:
parent
613eecf16a
commit
b6cc3f02bf
@ -563,6 +563,42 @@ class LocalTrade():
|
|||||||
"""
|
"""
|
||||||
self.open_trade_value = self._calc_open_trade_value()
|
self.open_trade_value = self._calc_open_trade_value()
|
||||||
|
|
||||||
|
def calculate_interest(self) -> Decimal:
|
||||||
|
# TODO-mg: Need to set other conditions because sometimes self.open_date is not defined, but why would it ever not be set
|
||||||
|
if not self.interest_rate or not (self.borrowed):
|
||||||
|
return Decimal(0.0)
|
||||||
|
|
||||||
|
try:
|
||||||
|
open_date = self.open_date.replace(tzinfo=None)
|
||||||
|
now = datetime.now()
|
||||||
|
secPerDay = 86400
|
||||||
|
days = Decimal((now - open_date).total_seconds()/secPerDay) or 0.0
|
||||||
|
hours = days/24
|
||||||
|
except:
|
||||||
|
raise OperationalException("Time isn't calculated properly")
|
||||||
|
|
||||||
|
rate = Decimal(self.interest_rate)
|
||||||
|
borrowed = Decimal(self.borrowed)
|
||||||
|
|
||||||
|
if self.exchange == 'binance':
|
||||||
|
# Rate is per day but accrued hourly or something
|
||||||
|
# binance: https://www.binance.com/en-AU/support/faq/360030157812
|
||||||
|
return borrowed * (rate/24) * max(hours, 1.0) # TODO-mg: Is hours rounded?
|
||||||
|
elif self.exchange == 'kraken':
|
||||||
|
# https://support.kraken.com/hc/en-us/articles/206161568-What-are-the-fees-for-margin-trading-
|
||||||
|
opening_fee = borrowed * rate
|
||||||
|
roll_over_fee = borrowed * rate * max(0, (hours-4)/4)
|
||||||
|
return opening_fee + roll_over_fee
|
||||||
|
elif self.exchange == 'binance_usdm_futures':
|
||||||
|
# ! TODO-mg: This is incorrect, I didn't look it up
|
||||||
|
return borrowed * (rate/24) * max(hours, 1.0)
|
||||||
|
elif self.exchange == 'binance_coinm_futures':
|
||||||
|
# ! TODO-mg: This is incorrect, I didn't look it up
|
||||||
|
return borrowed * (rate/24) * max(hours, 1.0)
|
||||||
|
else:
|
||||||
|
# TODO-mg: make sure this breaks and can't be squelched
|
||||||
|
raise OperationalException("Leverage not available on this exchange")
|
||||||
|
|
||||||
def calc_close_trade_value(self, rate: Optional[float] = None,
|
def calc_close_trade_value(self, rate: Optional[float] = None,
|
||||||
fee: Optional[float] = None) -> float:
|
fee: Optional[float] = None) -> float:
|
||||||
"""
|
"""
|
||||||
@ -578,17 +614,7 @@ class LocalTrade():
|
|||||||
|
|
||||||
close_trade = Decimal(self.amount) * Decimal(rate or self.close_rate) # type: ignore
|
close_trade = Decimal(self.amount) * Decimal(rate or self.close_rate) # type: ignore
|
||||||
fees = close_trade * Decimal(fee or self.fee_close)
|
fees = close_trade * Decimal(fee or self.fee_close)
|
||||||
|
interest = self.calculate_interest()
|
||||||
# TODO: Need to set other conditions because sometimes self.open_date is not defined, but why would it ever not be set
|
|
||||||
try:
|
|
||||||
open = self.open_date.replace(tzinfo=None)
|
|
||||||
now = datetime.now()
|
|
||||||
|
|
||||||
# breakpoint()
|
|
||||||
interest = ((Decimal(self.interest_rate or 0) * Decimal(self.borrowed or 0)) *
|
|
||||||
Decimal((now - open).total_seconds())/86400) or 0 # Interest/day * (seconds in trade)/(seconds per day)
|
|
||||||
except:
|
|
||||||
interest = 0
|
|
||||||
|
|
||||||
if (self.is_short):
|
if (self.is_short):
|
||||||
return float(close_trade + fees + interest)
|
return float(close_trade + fees + interest)
|
||||||
|
Loading…
Reference in New Issue
Block a user