New method for combining all funding fees within a time period

This commit is contained in:
Sam Germain
2021-11-13 04:45:23 -06:00
parent 6b40792f80
commit 3c509a1f9b
13 changed files with 93 additions and 213 deletions

View File

@@ -24,5 +24,3 @@ class Bibox(Exchange):
def _ccxt_config(self) -> Dict:
# Parameters to add directly to ccxt sync/async initialization.
return {"has": {"fetchCurrencies": False}}
funding_fee_times: List[int] = [0, 8, 16] # hours of the day

View File

@@ -29,8 +29,6 @@ class Binance(Exchange):
"trades_pagination_arg": "fromId",
"l2_limit_range": [5, 10, 20, 50, 100, 500, 1000],
}
funding_fee_times: List[int] = [0, 8, 16] # hours of the day
# but the schedule won't check within this timeframe
_supported_trading_mode_collateral_pairs: List[Tuple[TradingMode, Collateral]] = [
# TradingMode.SPOT always supported and not required in this list

View File

@@ -23,8 +23,6 @@ class Bybit(Exchange):
"ohlcv_candle_limit": 200,
}
funding_fee_times: List[int] = [0, 8, 16] # hours of the day
_supported_trading_mode_collateral_pairs: List[Tuple[TradingMode, Collateral]] = [
# TradingMode.SPOT always supported and not required in this list
# (TradingMode.FUTURES, Collateral.CROSS), # TODO-lev: Uncomment once supported

View File

@@ -73,10 +73,6 @@ class Exchange:
}
_ft_has: Dict = {}
# funding_fee_times is currently unused, but should ideally be used to properly
# schedule refresh times
funding_fee_times: List[int] = [] # hours of the day
_supported_trading_mode_collateral_pairs: List[Tuple[TradingMode, Collateral]] = [
# TradingMode.SPOT always supported and not required in this list
]
@@ -1711,21 +1707,6 @@ class Exchange:
"""
return open_date.minute > 0 or open_date.second > 0
def _get_funding_fee_dates(self, start: datetime, end: datetime):
if self.funding_fee_cutoff(start):
start += timedelta(hours=1)
start = datetime(start.year, start.month, start.day, start.hour, tzinfo=timezone.utc)
end = datetime(end.year, end.month, end.day, end.hour, tzinfo=timezone.utc)
results = []
iterator = start
while iterator <= end:
if iterator.hour in self.funding_fee_times:
results.append(iterator)
iterator += timedelta(hours=1)
return results
@retrier
def set_margin_mode(self, pair: str, collateral: Collateral, params: dict = {}):
"""
@@ -1808,6 +1789,16 @@ class Exchange:
:param close_date: The date and time that the trade ended
"""
if self.funding_fee_cutoff(open_date):
open_date += timedelta(hours=1)
open_date = datetime(
open_date.year,
open_date.month,
open_date.day,
open_date.hour,
tzinfo=timezone.utc
)
fees: float = 0
if not close_date:
close_date = datetime.now(timezone.utc)
@@ -1821,29 +1812,20 @@ class Exchange:
pair,
open_timestamp
)
funding_fee_dates = self._get_funding_fee_dates(open_date, close_date)
for date in funding_fee_dates:
timestamp = int(date.timestamp()) * 1000
if timestamp in funding_rate_history:
funding_rate = funding_rate_history[timestamp]
else:
logger.warning(
f"Funding rate for {pair} at {date} not found in funding_rate_history"
f"Funding fee calculation may be incorrect"
)
for timestamp in funding_rate_history.keys():
funding_rate = funding_rate_history[timestamp]
if timestamp in mark_price_history:
mark_price = mark_price_history[timestamp]
else:
logger.warning(
f"Mark price for {pair} at {date} not found in funding_rate_history"
f"Funding fee calculation may be incorrect"
)
if funding_rate and mark_price:
fees += self._get_funding_fee(
size=amount,
mark_price=mark_price,
funding_rate=funding_rate
)
else:
logger.warning(
f"Mark price for {pair} at timestamp {timestamp} not found in "
f"funding_rate_history Funding fee calculation may be incorrect"
)
return fees

View File

@@ -22,7 +22,6 @@ class Ftx(Exchange):
"ohlcv_candle_limit": 1500,
"mark_ohlcv_price": "index"
}
funding_fee_times: List[int] = list(range(0, 24))
_supported_trading_mode_collateral_pairs: List[Tuple[TradingMode, Collateral]] = [
# TradingMode.SPOT always supported and not required in this list

View File

@@ -26,8 +26,6 @@ class Gateio(Exchange):
_headers = {'X-Gate-Channel-Id': 'freqtrade'}
funding_fee_times: List[int] = [0, 8, 16] # hours of the day
_supported_trading_mode_collateral_pairs: List[Tuple[TradingMode, Collateral]] = [
# TradingMode.SPOT always supported and not required in this list
# (TradingMode.MARGIN, Collateral.CROSS), # TODO-lev: Uncomment once supported

View File

@@ -21,5 +21,3 @@ class Hitbtc(Exchange):
"ohlcv_candle_limit": 1000,
"ohlcv_params": {"sort": "DESC"}
}
funding_fee_times: List[int] = [0, 8, 16] # hours of the day

View File

@@ -23,7 +23,6 @@ class Kraken(Exchange):
"trades_pagination": "id",
"trades_pagination_arg": "since",
}
funding_fee_times: List[int] = [0, 4, 8, 12, 16, 20] # hours of the day
_supported_trading_mode_collateral_pairs: List[Tuple[TradingMode, Collateral]] = [
# TradingMode.SPOT always supported and not required in this list

View File

@@ -24,5 +24,3 @@ class Kucoin(Exchange):
"order_time_in_force": ['gtc', 'fok', 'ioc'],
"time_in_force_parameter": "timeInForce",
}
funding_fee_times: List[int] = [4, 12, 20] # hours of the day

View File

@@ -17,7 +17,6 @@ class Okex(Exchange):
_ft_has: Dict = {
"ohlcv_candle_limit": 100,
}
funding_fee_times: List[int] = [0, 8, 16] # hours of the day
_supported_trading_mode_collateral_pairs: List[Tuple[TradingMode, Collateral]] = [
# TradingMode.SPOT always supported and not required in this list