Added tests and docstring to exchange funding_fee methods, removed utils

This commit is contained in:
Sam Germain
2021-09-09 01:43:05 -06:00
parent d54117990b
commit dfb9937436
9 changed files with 134 additions and 24 deletions

View File

@@ -1,5 +1,6 @@
""" Binance exchange subclass """
import logging
from datetime import datetime
from typing import Dict, List, Optional
import ccxt
@@ -91,6 +92,13 @@ class Binance(Exchange):
except ccxt.BaseError as e:
raise OperationalException(e) from e
def _get_funding_rate(self, pair: str, when: datetime) -> Optional[float]:
"""
Get's the funding_rate for a pair at a specific date and time in the past
"""
# TODO-lev: implement
raise OperationalException("_get_funding_rate has not been implement on binance")
def _get_funding_fee(
self,
contract_size: float,

View File

@@ -1555,7 +1555,7 @@ class Exchange:
except ccxt.BaseError as e:
raise OperationalException(e) from e
def get_mark_price(self, pair: str, when: datetime):
def _get_mark_price(self, pair: str, when: datetime):
"""
Get's the value of the underlying asset for a futures contract
at a specific date and time in the past
@@ -1563,7 +1563,7 @@ class Exchange:
# TODO-lev: implement
raise OperationalException(f"get_mark_price has not been implemented for {self.name}")
def get_funding_rate(self, pair: str, when: datetime):
def _get_funding_rate(self, pair: str, when: datetime):
"""
Get's the funding_rate for a pair at a specific date and time in the past
"""
@@ -1587,7 +1587,7 @@ class Exchange:
"""
raise OperationalException(f"Funding fee has not been implemented for {self.name}")
def get_funding_fee_dates(self, open_date: datetime, close_date: datetime):
def _get_funding_fee_dates(self, open_date: datetime, close_date: datetime):
"""
Get's the date and time of every funding fee that happened between two datetimes
"""
@@ -1619,9 +1619,9 @@ class Exchange:
"""
fees: float = 0
for date in self.get_funding_fee_dates(open_date, close_date):
funding_rate = self.get_funding_rate(pair, date)
mark_price = self.get_mark_price(pair, date)
for date in self._get_funding_fee_dates(open_date, close_date):
funding_rate = self._get_funding_rate(pair, date)
mark_price = self._get_mark_price(pair, date)
fees += self._get_funding_fee(amount, mark_price, funding_rate)
return fees

View File

@@ -3,7 +3,7 @@ import logging
from typing import Any, Dict, List, Optional
import ccxt
from datetime import datetime
from freqtrade.exceptions import (DDosProtection, InsufficientFundsError, InvalidOrderException,
OperationalException, TemporaryError)
from freqtrade.exchange import Exchange
@@ -154,6 +154,10 @@ class Ftx(Exchange):
return safe_value_fallback2(order, order, 'id_stop', 'id')
return order['id']
def _get_funding_rate(self, pair: str, when: datetime) -> Optional[float]:
"""FTX doesn't use this"""
return None
def _get_funding_fee(
self,
contract_size: float,
@@ -162,9 +166,9 @@ class Ftx(Exchange):
) -> float:
"""
Calculates a single funding fee
Always paid in USD on FTX # TODO: How do we account for this
:param contract_size: The amount/quanity
:param mark_price: The price of the asset that the contract is based off of
:param funding_rate: Must be None on ftx
Always paid in USD on FTX # TODO: How do we account for this
: param contract_size: The amount/quanity
: param mark_price: The price of the asset that the contract is based off of
: param funding_rate: Must be None on ftx
"""
return (contract_size * mark_price) / 24