adjusted funding fee formula binance

This commit is contained in:
Sam Germain 2021-10-05 17:25:09 -06:00
parent 2a26c6fbed
commit cba0a8cee6
2 changed files with 7 additions and 33 deletions

View File

@ -218,34 +218,21 @@ class Binance(Exchange):
except ccxt.BaseError as e: except ccxt.BaseError as e:
raise OperationalException(e) from e raise OperationalException(e) from e
def _get_premium_index(self, pair: str, date: datetime) -> float:
raise OperationalException(f'_get_premium_index has not been implemented on {self.name}')
def _get_mark_price(self, pair: str, date: datetime) -> float: def _get_mark_price(self, pair: str, date: datetime) -> float:
raise OperationalException(f'_get_mark_price has not been implemented on {self.name}') raise OperationalException(f'_get_mark_price has not been implemented on {self.name}')
def _get_funding_interest_rates(self): def _get_funding_rate(self, pair: str, premium_index: float) -> Optional[float]:
rates = self._api.fetch_funding_rates()
interest_rates = {}
for pair, data in rates.items():
interest_rates[pair] = data['interestRate']
return interest_rates
def _calculate_funding_rate(self, pair: str, premium_index: float) -> Optional[float]:
""" """
Get's the funding_rate for a pair at a specific date and time in the past Get's the funding_rate for a pair at a specific date and time in the past
""" """
return ( raise OperationalException(f'_get_mark_price has not been implemented on {self.name}')
premium_index +
max(min(self._funding_interest_rates[pair] - premium_index, 0.0005), -0.0005)
)
def _get_funding_fee( def _get_funding_fee(
self, self,
pair: str, pair: str,
contract_size: float, contract_size: float,
mark_price: float, mark_price: float,
premium_index: Optional[float], funding_rate: Optional[float],
) -> float: ) -> float:
""" """
Calculates a single funding fee Calculates a single funding fee

View File

@ -1604,14 +1604,6 @@ class Exchange:
self._async_get_trade_history(pair=pair, since=since, self._async_get_trade_history(pair=pair, since=since,
until=until, from_id=from_id)) until=until, from_id=from_id))
# https://www.binance.com/en/support/faq/360033525031
def fetch_funding_rate(self, pair):
if not self.exchange_has("fetchFundingHistory"):
raise OperationalException(
f"fetch_funding_history() has not been implemented on ccxt.{self.name}")
return self._api.fetch_funding_rates()
@retrier @retrier
def get_funding_fees_from_exchange(self, pair: str, since: Union[datetime, int]) -> float: def get_funding_fees_from_exchange(self, pair: str, since: Union[datetime, int]) -> float:
""" """
@ -1667,9 +1659,6 @@ class Exchange:
else: else:
return 1.0 return 1.0
def _get_premium_index(self, pair: str, date: datetime) -> float:
raise OperationalException(f'_get_premium_index has not been implemented on {self.name}')
def _get_mark_price(self, pair: str, date: datetime) -> float: def _get_mark_price(self, pair: str, date: datetime) -> float:
raise OperationalException(f'_get_mark_price has not been implemented on {self.name}') raise OperationalException(f'_get_mark_price has not been implemented on {self.name}')
@ -1685,9 +1674,7 @@ class Exchange:
pair: str, pair: str,
contract_size: float, contract_size: float,
mark_price: float, mark_price: float,
premium_index: Optional[float], funding_rate: Optional[float]
# index_price: float,
# interest_rate: float)
) -> float: ) -> float:
""" """
Calculates a single funding fee Calculates a single funding fee
@ -1740,7 +1727,7 @@ class Exchange:
def set_margin_mode(self, pair: str, collateral: Collateral, params: dict = {}): def set_margin_mode(self, pair: str, collateral: Collateral, params: dict = {}):
''' '''
Set's the margin mode on the exchange to cross or isolated for a specific pair Set's the margin mode on the exchange to cross or isolated for a specific pair
:param symbol: base/quote currency pair (e.g. "ADA/USDT") :param pair: base/quote currency pair (e.g. "ADA/USDT")
''' '''
if self._config['dry_run'] or not self.exchange_has("setMarginMode"): if self._config['dry_run'] or not self.exchange_has("setMarginMode"):
# Some exchanges only support one collateral type # Some exchanges only support one collateral type
@ -1773,13 +1760,13 @@ class Exchange:
fees: float = 0 fees: float = 0
for date in self._get_funding_fee_dates(open_date, close_date): for date in self._get_funding_fee_dates(open_date, close_date):
premium_index = self._get_premium_index(pair, date) funding_rate = self._get_funding_rate(pair, date)
mark_price = self._get_mark_price(pair, date) mark_price = self._get_mark_price(pair, date)
fees += self._get_funding_fee( fees += self._get_funding_fee(
pair=pair, pair=pair,
contract_size=amount, contract_size=amount,
mark_price=mark_price, mark_price=mark_price,
premium_index=premium_index funding_rate=funding_rate
) )
return fees return fees