Extract funding and mark mergin to separate method

This commit is contained in:
Matthias 2022-01-17 19:39:58 +01:00
parent 84c6d92d4c
commit 82c90c0049
3 changed files with 24 additions and 19 deletions

View File

@ -1922,19 +1922,29 @@ class Exchange:
)
funding_rates = candle_histories[funding_comb]
mark_rates = candle_histories[mark_comb]
funding_mark_rates = self.combine_funding_and_mark(
funding_rates=funding_rates, mark_rates=mark_rates)
return self.calculate_funding_fees(
funding_rates=funding_rates,
mark_rates=mark_rates,
funding_mark_rates,
amount=amount,
open_date=open_date,
close_date=close_date
)
@staticmethod
def combine_funding_and_mark(funding_rates: DataFrame, mark_rates: DataFrame) -> DataFrame:
"""
Combine funding-rates and mark-rates dataframes
:param funding_rates: Dataframe containing Funding rates (Type FUNDING_RATE)
:param mark_rates: Dataframe containing Mark rates (Type mark_ohlcv_price)
"""
return funding_rates.merge(mark_rates, on='date', how="inner", suffixes=["_fund", "_mark"])
def calculate_funding_fees(
self,
funding_rates: DataFrame,
mark_rates: DataFrame,
df: DataFrame,
amount: float,
open_date: datetime,
close_date: Optional[datetime] = None,
@ -1942,8 +1952,8 @@ class Exchange:
) -> float:
"""
calculates the sum of all funding fees that occurred for a pair during a futures trade
:param funding_rates: Dataframe containing Funding rates (Type FUNDING_RATE)
:param mark_rates: Dataframe containing Mark rates (Type mark_ohlcv_price)
:param df: Dataframe containing combined funding and mark rates
as `open_fund` and `open_mark`.
:param amount: The quantity of the trade
:param open_date: The date and time that the trade started
:param close_date: The date and time that the trade ended
@ -1951,7 +1961,6 @@ class Exchange:
"""
fees: float = 0
df = funding_rates.merge(mark_rates, on='date', how="inner", suffixes=["_fund", "_mark"])
if not df.empty:
df = df[(df['date'] >= open_date) & (df['date'] <= close_date)]
fees = sum(df['open_fund'] * df['open_mark'] * amount)

View File

@ -161,8 +161,7 @@ class Kraken(Exchange):
def calculate_funding_fees(
self,
funding_rates: DataFrame,
mark_rates: DataFrame,
df: DataFrame,
amount: float,
open_date: datetime,
close_date: Optional[datetime] = None,
@ -174,8 +173,8 @@ class Kraken(Exchange):
# ! functionality must be added that passes the parameter time_in_ratio to
# ! _get_funding_fee when using Kraken
calculates the sum of all funding fees that occurred for a pair during a futures trade
:param funding_rates: Dataframe containing Funding rates (Type FUNDING_RATE)
:param mark_rates: Dataframe containing Mark rates (Type mark_ohlcv_price)
:param df: Dataframe containing combined funding and mark rates
as `open_fund` and `open_mark`.
:param amount: The quantity of the trade
:param open_date: The date and time that the trade started
:param close_date: The date and time that the trade ended
@ -186,7 +185,6 @@ class Kraken(Exchange):
f"time_in_ratio is required for {self.name}._get_funding_fee")
fees: float = 0
df = funding_rates.merge(mark_rates, on='date', how="inner", suffixes=["_fund", "_mark"])
if not df.empty:
df = df[(df['date'] >= open_date) & (df['date'] <= close_date)]
fees = sum(df['open_fund'] * df['open_mark'] * amount * time_in_ratio)

View File

@ -3540,7 +3540,7 @@ def test_get_max_leverage(default_conf, mocker, pair, nominal_value, max_lev):
(10, 0.0002, 2.0, 0.01, 0.004, 0.00004),
(10, 0.0002, 2.5, None, 0.005, None),
])
def test__calculate_funding_fees(
def test_calculate_funding_fees(
default_conf,
mocker,
size,
@ -3562,10 +3562,10 @@ def test__calculate_funding_fees(
{'date': prior_date, 'open': mark_price},
{'date': trade_date, 'open': mark_price},
])
df = exchange.combine_funding_and_mark(funding_rates, mark_rates)
assert exchange.calculate_funding_fees(
funding_rates=funding_rates,
mark_rates=mark_rates,
df,
amount=size,
open_date=trade_date,
close_date=trade_date,
@ -3575,8 +3575,7 @@ def test__calculate_funding_fees(
if (kraken_fee is None):
with pytest.raises(OperationalException):
kraken.calculate_funding_fees(
funding_rates=funding_rates,
mark_rates=mark_rates,
df,
amount=size,
open_date=trade_date,
close_date=trade_date,
@ -3585,8 +3584,7 @@ def test__calculate_funding_fees(
else:
assert kraken.calculate_funding_fees(
funding_rates=funding_rates,
mark_rates=mark_rates,
df,
amount=size,
open_date=trade_date,
close_date=trade_date,