From e367f84b06896304405a8370f686538ee3c635ac Mon Sep 17 00:00:00 2001 From: Sam Germain Date: Wed, 6 Oct 2021 01:39:02 -0600 Subject: [PATCH] Added more update_funding_fee tests, set exchange of default conf --- freqtrade/exchange/binance.py | 1 + freqtrade/exchange/bybit.py | 9 +++++++- freqtrade/exchange/ftx.py | 2 +- freqtrade/freqtradebot.py | 9 ++++++-- tests/test_freqtradebot.py | 41 +++++++++++++---------------------- 5 files changed, 32 insertions(+), 30 deletions(-) diff --git a/freqtrade/exchange/binance.py b/freqtrade/exchange/binance.py index dc3d4bb5e..d23f84e7b 100644 --- a/freqtrade/exchange/binance.py +++ b/freqtrade/exchange/binance.py @@ -29,6 +29,7 @@ class Binance(Exchange): "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 diff --git a/freqtrade/exchange/bybit.py b/freqtrade/exchange/bybit.py index c4ffcdd0b..df19a671b 100644 --- a/freqtrade/exchange/bybit.py +++ b/freqtrade/exchange/bybit.py @@ -1,7 +1,8 @@ """ Bybit exchange subclass """ import logging -from typing import Dict, List +from typing import Dict, List, Tuple +from freqtrade.enums import Collateral, TradingMode from freqtrade.exchange import Exchange @@ -23,3 +24,9 @@ class Bybit(Exchange): } 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 + # (TradingMode.FUTURES, Collateral.ISOLATED) # TODO-lev: Uncomment once supported + ] diff --git a/freqtrade/exchange/ftx.py b/freqtrade/exchange/ftx.py index ef583de4f..5072d653e 100644 --- a/freqtrade/exchange/ftx.py +++ b/freqtrade/exchange/ftx.py @@ -21,7 +21,7 @@ class Ftx(Exchange): "stoploss_on_exchange": True, "ohlcv_candle_limit": 1500, } - funding_fee_times: List[int] = list(range(0, 23)) + 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 diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 8307dd185..d6734fa43 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -111,10 +111,15 @@ class FreqtradeBot(LoggingMixin): self.trading_mode = TradingMode.SPOT if self.trading_mode == TradingMode.FUTURES: - for time_slot in self.exchange.funding_fee_times: - schedule.every().day.at(str(time(time_slot))).do(self.update_funding_fees) + + def update(): + self.update_funding_fees() self.wallets.update() + for time_slot in self.exchange.funding_fee_times: + t = str(time(time_slot)) + schedule.every().day.at(t).do(update) + def notify_status(self, msg: str) -> None: """ Public method for users of this class (worker, etc.) to send notifications diff --git a/tests/test_freqtradebot.py b/tests/test_freqtradebot.py index 11463f0ee..2353c9f14 100644 --- a/tests/test_freqtradebot.py +++ b/tests/test_freqtradebot.py @@ -6,10 +6,10 @@ import time from copy import deepcopy from math import isclose from unittest.mock import ANY, MagicMock, PropertyMock -import schedule import arrow import pytest +import schedule from freqtrade.constants import CANCEL_REASON, MATH_CLOSE_PREC, UNLIMITED_STAKE_AMOUNT from freqtrade.enums import RPCMessageType, RunMode, SellType, State, TradingMode @@ -4281,40 +4281,29 @@ def test_get_valid_price(mocker, default_conf_usdt) -> None: assert valid_price_at_min_alwd < proposed_price -@pytest.mark.parametrize('exchange,trading_mode,calls', [ - ("ftx", TradingMode.SPOT, 0), - ("ftx", TradingMode.MARGIN, 0), - ("binance", TradingMode.FUTURES, 2), - ("kraken", TradingMode.FUTURES, 3), - ("ftx", TradingMode.FUTURES, 9), +@pytest.mark.parametrize('exchange,trading_mode,calls,t1,t2', [ + ("ftx", TradingMode.SPOT, 0, "2021-09-01 00:00:00", "2021-09-01 08:00:00"), + ("ftx", TradingMode.MARGIN, 0, "2021-09-01 00:00:00", "2021-09-01 08:00:00"), + ("binance", TradingMode.FUTURES, 1, "2021-09-01 00:00:01", "2021-09-01 08:00:00"), + ("kraken", TradingMode.FUTURES, 2, "2021-09-01 00:00:01", "2021-09-01 08:00:00"), + ("ftx", TradingMode.FUTURES, 8, "2021-09-01 00:00:01", "2021-09-01 08:00:00"), + ("binance", TradingMode.FUTURES, 2, "2021-08-31 23:59:59", "2021-09-01 08:00:01"), + ("kraken", TradingMode.FUTURES, 3, "2021-08-31 23:59:59", "2021-09-01 08:00:01"), + ("ftx", TradingMode.FUTURES, 9, "2021-08-31 23:59:59", "2021-09-01 08:00:01"), ]) -def test_update_funding_fees(mocker, default_conf, exchange, trading_mode, calls, time_machine): - time_machine.move_to("2021-09-01 00:00:00 +00:00") +def test_update_funding_fees(mocker, default_conf, exchange, trading_mode, calls, time_machine, + t1, t2): + time_machine.move_to(f"{t1} +00:00") patch_RPCManager(mocker) patch_exchange(mocker, id=exchange) mocker.patch('freqtrade.freqtradebot.FreqtradeBot.update_funding_fees', return_value=True) default_conf['trading_mode'] = trading_mode default_conf['collateral'] = 'isolated' + default_conf['exchange']['name'] = exchange freqtrade = get_patched_freqtradebot(mocker, default_conf) - # trade = Trade( - # id=2, - # pair='ADA/USDT', - # stake_amount=60.0, - # open_rate=2.0, - # amount=30.0, - # is_open=True, - # open_date=arrow.utcnow().datetime, - # fee_open=fee.return_value, - # fee_close=fee.return_value, - # exchange='binance', - # is_short=False, - # leverage=3.0, - # trading_mode=trading_mode - # ) - - time_machine.move_to("2021-09-01 08:00:00 +00:00") + time_machine.move_to(f"{t2} +00:00") schedule.run_pending() assert freqtrade.update_funding_fees.call_count == calls