stable/freqtrade/leverage/interest.py

40 lines
1.2 KiB
Python
Raw Normal View History

2021-08-06 07:15:18 +00:00
from math import ceil
from freqtrade.exceptions import OperationalException
2022-08-10 18:56:04 +00:00
from freqtrade.util import FtPrecise
2021-08-06 07:15:18 +00:00
2022-08-10 18:56:04 +00:00
one = FtPrecise(1.0)
four = FtPrecise(4.0)
twenty_four = FtPrecise(24.0)
2021-08-06 07:15:18 +00:00
def interest(
exchange_name: str,
2022-08-10 18:56:04 +00:00
borrowed: FtPrecise,
rate: FtPrecise,
hours: FtPrecise
) -> FtPrecise:
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":
2022-08-10 18:56:04 +00:00
return borrowed * rate * FtPrecise(ceil(hours)) / twenty_four
2021-08-06 07:15:18 +00:00
elif exchange_name == "kraken":
# Rounded based on https://kraken-fees-calculator.github.io/
2022-08-10 18:56:04 +00:00
return borrowed * rate * (one + FtPrecise(ceil(hours / 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")