Skip to content

Commit

Permalink
Refactor list-timeframes command with the use of the Exchange class m…
Browse files Browse the repository at this point in the history
…ethods
  • Loading branch information
hroff-1902 committed Sep 29, 2019
1 parent 448b09d commit 75446d8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 20 deletions.
18 changes: 10 additions & 8 deletions freqtrade/exchange/exchange.py
Expand Up @@ -133,6 +133,9 @@ def __init__(self, config: dict) -> None:

logger.info('Using Exchange "%s"', self.name)

# Check if timeframe is available
self.validate_timeframes(config.get('ticker_interval'))

# Converts the interval provided in minutes in config to seconds
self.markets_refresh_interval: int = exchange_config.get(
"markets_refresh_interval", 60) * 60
Expand All @@ -144,10 +147,6 @@ def __init__(self, config: dict) -> None:
self.validate_ordertypes(config.get('order_types', {}))
self.validate_order_time_in_force(config.get('order_time_in_force', {}))

if config.get('ticker_interval'):
# Check if timeframe is available
self.validate_timeframes(config['ticker_interval'])

def __del__(self):
"""
Destructor - clean up async stuff
Expand Down Expand Up @@ -199,6 +198,10 @@ def id(self) -> str:
"""exchange ccxt id"""
return self._api.id

@property
def timeframes(self) -> List[str]:
return list((self._api.timeframes or {}).keys())

@property
def markets(self) -> Dict:
"""exchange ccxt markets"""
Expand Down Expand Up @@ -291,7 +294,7 @@ def get_valid_pair_combination(self, curr_1, curr_2) -> str:
return pair
raise DependencyException(f"Could not combine {curr_1} and {curr_2} to get a valid pair.")

def validate_timeframes(self, timeframe: List[str]) -> None:
def validate_timeframes(self, timeframe: Optional[str]) -> None:
"""
Checks if ticker interval from config is a supported timeframe on the exchange
"""
Expand All @@ -304,10 +307,9 @@ def validate_timeframes(self, timeframe: List[str]) -> None:
f"for the exchange \"{self.name}\" and this exchange "
f"is therefore not supported. ccxt fetchOHLCV: {self.exchange_has('fetchOHLCV')}")

timeframes = self._api.timeframes
if timeframe not in timeframes:
if timeframe and (timeframe not in self.timeframes):
raise OperationalException(
f'Invalid ticker {timeframe}, this Exchange supports {timeframes}')
f"Invalid ticker interval '{timeframe}'. This exchange supports: {self.timeframes}")

def validate_ordertypes(self, order_types: Dict) -> None:
"""
Expand Down
17 changes: 6 additions & 11 deletions freqtrade/utils.py
Expand Up @@ -104,19 +104,14 @@ def start_list_timeframes(args: Dict[str, Any]) -> None:
Print ticker intervals (timeframes) available on Exchange
"""
config = setup_utils_configuration(args, RunMode.OTHER)
# Do not use ticker_interval set in the config
config['ticker_interval'] = None

# Init exchange
exchange = ExchangeResolver(config['exchange']['name'], config).exchange

timeframes = list((exchange._api.timeframes or {}).keys())

if not timeframes:
logger.warning("List of timeframes available for exchange "
f"`{config['exchange']['name']}` is empty. "
"This exchange does not support fetching OHLCV data.")
if args['print_one_column']:
print('\n'.join(exchange.timeframes))
else:
if args['print_one_column']:
print('\n'.join(timeframes))
else:
print(f"Timeframes available for the exchange `{config['exchange']['name']}`: "
f"{', '.join(timeframes)}")
print(f"Timeframes available for the exchange `{config['exchange']['name']}`: "
f"{', '.join(exchange.timeframes)}")
3 changes: 2 additions & 1 deletion tests/exchange/test_exchange.py
Expand Up @@ -409,7 +409,8 @@ def test_validate_timeframes_failed(default_conf, mocker):
mocker.patch('freqtrade.exchange.Exchange._init_ccxt', MagicMock(return_value=api_mock))
mocker.patch('freqtrade.exchange.Exchange._load_markets', MagicMock(return_value={}))
mocker.patch('freqtrade.exchange.Exchange.validate_pairs', MagicMock())
with pytest.raises(OperationalException, match=r'Invalid ticker 3m, this Exchange supports.*'):
with pytest.raises(OperationalException,
match=r"Invalid ticker interval '3m'. This exchange supports.*"):
Exchange(default_conf)


Expand Down

0 comments on commit 75446d8

Please sign in to comment.