diff --git a/freqtrade/data/dataprovider.py b/freqtrade/data/dataprovider.py index 7ada4f642..ef03307cc 100644 --- a/freqtrade/data/dataprovider.py +++ b/freqtrade/data/dataprovider.py @@ -5,7 +5,7 @@ including ticker and orderbook data, live and historical candle (OHLCV) data Common Interface for bot and strategy to access data. """ import logging -from typing import Any, Dict, List, Optional, Tuple +from typing import Any, Dict, List, Optional from pandas import DataFrame @@ -13,6 +13,8 @@ from freqtrade.data.history import load_pair_history from freqtrade.exceptions import DependencyException, OperationalException from freqtrade.exchange import Exchange from freqtrade.state import RunMode +from freqtrade.typing import ListPairsWithTimeframes + logger = logging.getLogger(__name__) @@ -25,8 +27,8 @@ class DataProvider: self._pairlists = pairlists def refresh(self, - pairlist: List[Tuple[str, str]], - helping_pairs: List[Tuple[str, str]] = None) -> None: + pairlist: ListPairsWithTimeframes, + helping_pairs: ListPairsWithTimeframes = None) -> None: """ Refresh data, called with each cycle """ @@ -36,7 +38,7 @@ class DataProvider: self._exchange.refresh_latest_ohlcv(pairlist) @property - def available_pairs(self) -> List[Tuple[str, str]]: + def available_pairs(self) -> ListPairsWithTimeframes: """ Return a list of tuples containing (pair, timeframe) for which data is currently cached. Should be whitelist + open trades. diff --git a/freqtrade/exchange/exchange.py b/freqtrade/exchange/exchange.py index 68022662a..c60eb766a 100644 --- a/freqtrade/exchange/exchange.py +++ b/freqtrade/exchange/exchange.py @@ -23,6 +23,7 @@ from freqtrade.exceptions import (DependencyException, InvalidOrderException, OperationalException, TemporaryError) from freqtrade.exchange.common import BAD_EXCHANGES, retrier, retrier_async from freqtrade.misc import deep_merge_dicts, safe_value_fallback +from freqtrade.typing import ListPairsWithTimeframes CcxtModuleType = Any @@ -675,7 +676,7 @@ class Exchange: logger.info("Downloaded data for %s with length %s.", pair, len(data)) return data - def refresh_latest_ohlcv(self, pair_list: List[Tuple[str, str]]) -> List[Tuple[str, List]]: + def refresh_latest_ohlcv(self, pair_list: ListPairsWithTimeframes) -> List[Tuple[str, List]]: """ Refresh in-memory OHLCV asynchronously and set `_klines` with the result Loops asynchronously over pair_list and downloads all pairs async (semi-parallel). diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index a6d32d8fe..d5d512e78 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -7,7 +7,7 @@ import traceback from datetime import datetime from math import isclose from threading import Lock -from typing import Any, Dict, List, Optional, Tuple +from typing import Any, Dict, List, Optional import arrow from cachetools import TTLCache @@ -84,7 +84,7 @@ class FreqtradeBot: self.edge = Edge(self.config, self.exchange, self.strategy) if \ self.config.get('edge', {}).get('enabled', False) else None - self.active_pair_whitelist = self._refresh_whitelist() + self.active_pair_whitelist = self._refresh_active_whitelist() # Set initial bot state from config initial_state = self.config.get('initial_state') @@ -145,10 +145,10 @@ class FreqtradeBot: # Query trades from persistence layer trades = Trade.get_open_trades() - self.active_pair_whitelist = self._refresh_whitelist(trades) + self.active_pair_whitelist = self._refresh_active_whitelist(trades) # Refreshing candles - self.dataprovider.refresh(self._create_pair_whitelist(self.active_pair_whitelist), + self.dataprovider.refresh(self.pairlists.create_pair_list(self.active_pair_whitelist), self.strategy.informative_pairs()) with self._sell_lock: @@ -175,9 +175,10 @@ class FreqtradeBot: if self.config['cancel_open_orders_on_exit']: self.cancel_all_open_orders() - def _refresh_whitelist(self, trades: List[Trade] = []) -> List[str]: + def _refresh_active_whitelist(self, trades: List[Trade] = []) -> List[str]: """ - Refresh whitelist from pairlist or edge and extend it with trades. + Refresh active whitelist from pairlist or edge and extend it with + pairs that have open trades. """ # Refresh whitelist self.pairlists.refresh_pairlist() @@ -194,12 +195,6 @@ class FreqtradeBot: _whitelist.extend([trade.pair for trade in trades if trade.pair not in _whitelist]) return _whitelist - def _create_pair_whitelist(self, pairs: List[str]) -> List[Tuple[str, str]]: - """ - Create pair-whitelist tuple with (pair, ticker_interval) - """ - return [(pair, self.config['ticker_interval']) for pair in pairs] - def get_free_open_trades(self): """ Return the number of free open trades slots or 0 if diff --git a/freqtrade/pairlist/pairlistmanager.py b/freqtrade/pairlist/pairlistmanager.py index b01f1342b..78a6a9668 100644 --- a/freqtrade/pairlist/pairlistmanager.py +++ b/freqtrade/pairlist/pairlistmanager.py @@ -10,6 +10,7 @@ from cachetools import TTLCache, cached from freqtrade.exceptions import OperationalException from freqtrade.pairlist.IPairList import IPairList from freqtrade.resolvers import PairListResolver +from freqtrade.typing import ListPairsWithTimeframes logger = logging.getLogger(__name__) @@ -107,3 +108,9 @@ class PairListManager(): pairlist.remove(p) return pairlist + + def create_pair_list(self, pairs: List[str], timeframe: str = None) -> ListPairsWithTimeframes: + """ + Create list of pair tuples with (pair, ticker_interval) + """ + return [(pair, timeframe or self._config['ticker_interval']) for pair in pairs] diff --git a/freqtrade/strategy/interface.py b/freqtrade/strategy/interface.py index 6268b8a43..8031c7932 100644 --- a/freqtrade/strategy/interface.py +++ b/freqtrade/strategy/interface.py @@ -7,7 +7,7 @@ import warnings from abc import ABC, abstractmethod from datetime import datetime, timezone from enum import Enum -from typing import Dict, List, NamedTuple, Optional, Tuple +from typing import Dict, NamedTuple, Optional, Tuple import arrow from pandas import DataFrame @@ -17,8 +17,10 @@ from freqtrade.exceptions import StrategyError from freqtrade.exchange import timeframe_to_minutes from freqtrade.persistence import Trade from freqtrade.strategy.strategy_wrapper import strategy_safe_wrapper +from freqtrade.typing import ListPairsWithTimeframes from freqtrade.wallets import Wallets + logger = logging.getLogger(__name__) @@ -185,7 +187,7 @@ class IStrategy(ABC): """ return False - def informative_pairs(self) -> List[Tuple[str, str]]: + def informative_pairs(self) -> ListPairsWithTimeframes: """ Define additional, informative pair/interval combinations to be cached from the exchange. These pair/interval combinations are non-tradeable, unless they are part diff --git a/freqtrade/typing.py b/freqtrade/typing.py new file mode 100644 index 000000000..3cb7cf816 --- /dev/null +++ b/freqtrade/typing.py @@ -0,0 +1,8 @@ +""" +Common Freqtrade types +""" + +from typing import List, Tuple + +# List of pairs with their timeframes +ListPairsWithTimeframes = List[Tuple[str, str]] diff --git a/tests/conftest.py b/tests/conftest.py index d15cba1de..971f7a5fa 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -92,7 +92,7 @@ def patch_wallet(mocker, free=999.9) -> None: def patch_whitelist(mocker, conf) -> None: - mocker.patch('freqtrade.freqtradebot.FreqtradeBot._refresh_whitelist', + mocker.patch('freqtrade.freqtradebot.FreqtradeBot._refresh_active_whitelist', MagicMock(return_value=conf['exchange']['pair_whitelist']))