freqtrade_origin/freqtrade/leverage/interest.py

44 lines
1.4 KiB
Python
Raw Normal View History

2021-08-06 07:15:18 +00:00
from decimal import Decimal
from math import ceil
from freqtrade.exceptions import OperationalException
one = Decimal(1.0)
four = Decimal(4.0)
twenty_four = Decimal(24.0)
def interest(
exchange_name: str,
borrowed: Decimal,
rate: Decimal,
hours: Decimal
) -> Decimal:
2021-08-09 06:00:50 +00:00
"""
2021-11-09 18:22:29 +00:00
Equation to calculate interest on margin trades
2021-08-06 07:15:18 +00:00
2021-11-09 18:22:29 +00:00
:param exchange_name: The exchanged being trading on
:param borrowed: The amount of currency being borrowed
:param rate: The rate of interest (i.e daily interest rate)
:param hours: The time in hours that the currency has been borrowed for
2021-08-06 07:15:18 +00:00
2021-11-09 18:22:29 +00:00
Raises:
OperationalException: Raised if freqtrade does
not support margin trading for this exchange
2021-08-06 07:15:18 +00:00
2021-11-09 18:22:29 +00:00
Returns: The amount of interest owed (currency matches borrowed)
2021-08-06 07:15:18 +00:00
"""
exchange_name = exchange_name.lower()
if exchange_name == "binance":
return borrowed * rate * ceil(hours)/twenty_four
elif exchange_name == "kraken":
# Rounded based on https://kraken-fees-calculator.github.io/
return borrowed * rate * (one+ceil(hours/four))
elif exchange_name == "ftx":
2021-09-07 06:58:23 +00:00
# As Explained under #Interest rates section in
# https://help.ftx.com/hc/en-us/articles/360053007671-Spot-Margin-Trading-Explainer
return borrowed * rate * ceil(hours)/twenty_four
2021-08-06 07:15:18 +00:00
else:
2021-09-19 06:12:29 +00:00
raise OperationalException(f"Leverage not available on {exchange_name} with freqtrade")