2020-01-26 12:17:26 +00:00
|
|
|
import logging
|
|
|
|
import sys
|
2020-07-12 07:57:00 +00:00
|
|
|
from collections import defaultdict
|
2023-06-17 13:13:56 +00:00
|
|
|
from typing import Any, Dict
|
2020-01-26 12:17:26 +00:00
|
|
|
|
2020-01-26 12:55:48 +00:00
|
|
|
from freqtrade.configuration import TimeRange, setup_utils_configuration
|
2023-07-09 13:09:03 +00:00
|
|
|
from freqtrade.constants import DATETIME_PRINT_FORMAT, DL_DATA_TIMEFRAMES, Config
|
2024-05-12 13:18:32 +00:00
|
|
|
from freqtrade.data.converter import (
|
|
|
|
convert_ohlcv_format,
|
|
|
|
convert_trades_format,
|
|
|
|
convert_trades_to_ohlcv,
|
|
|
|
)
|
2023-09-24 08:38:25 +00:00
|
|
|
from freqtrade.data.history import download_data_main
|
2024-03-01 18:32:18 +00:00
|
|
|
from freqtrade.enums import CandleType, RunMode, TradingMode
|
2024-03-19 06:06:43 +00:00
|
|
|
from freqtrade.exceptions import ConfigurationError
|
2023-06-17 13:13:56 +00:00
|
|
|
from freqtrade.exchange import timeframe_to_minutes
|
2024-08-13 04:54:35 +00:00
|
|
|
from freqtrade.misc import plural
|
2024-03-02 12:23:03 +00:00
|
|
|
from freqtrade.plugins.pairlist.pairlist_helpers import dynamic_expand_pairlist
|
2020-01-26 12:17:26 +00:00
|
|
|
from freqtrade.resolvers import ExchangeResolver
|
2024-07-06 19:00:34 +00:00
|
|
|
from freqtrade.util import print_rich_table
|
2024-01-04 15:25:40 +00:00
|
|
|
from freqtrade.util.migrations import migrate_data
|
2020-01-26 12:17:26 +00:00
|
|
|
|
2020-09-28 17:39:41 +00:00
|
|
|
|
2020-01-26 12:17:26 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2023-06-17 12:59:01 +00:00
|
|
|
def _check_data_config_download_sanity(config: Config) -> None:
|
2024-05-12 14:27:03 +00:00
|
|
|
if "days" in config and "timerange" in config:
|
|
|
|
raise ConfigurationError(
|
|
|
|
"--days and --timerange are mutually exclusive. "
|
|
|
|
"You can only specify one or the other."
|
|
|
|
)
|
2023-02-25 19:50:26 +00:00
|
|
|
|
2024-05-12 14:27:03 +00:00
|
|
|
if "pairs" not in config:
|
2024-03-19 06:06:43 +00:00
|
|
|
raise ConfigurationError(
|
2023-02-25 19:50:26 +00:00
|
|
|
"Downloading data requires a list of pairs. "
|
2024-05-12 14:27:03 +00:00
|
|
|
"Please check the documentation on how to configure this."
|
|
|
|
)
|
2023-02-25 19:50:26 +00:00
|
|
|
|
|
|
|
|
2020-01-26 12:17:26 +00:00
|
|
|
def start_download_data(args: Dict[str, Any]) -> None:
|
|
|
|
"""
|
|
|
|
Download data (former download_backtest_data.py script)
|
|
|
|
"""
|
|
|
|
config = setup_utils_configuration(args, RunMode.UTIL_EXCHANGE)
|
|
|
|
|
2023-06-17 12:59:01 +00:00
|
|
|
_check_data_config_download_sanity(config)
|
2020-02-08 20:53:34 +00:00
|
|
|
|
2020-01-26 12:17:26 +00:00
|
|
|
try:
|
2023-06-17 13:13:56 +00:00
|
|
|
download_data_main(config)
|
2020-01-26 12:17:26 +00:00
|
|
|
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
sys.exit("SIGINT received, aborting ...")
|
|
|
|
|
2020-01-26 19:31:13 +00:00
|
|
|
|
2021-09-29 14:50:05 +00:00
|
|
|
def start_convert_trades(args: Dict[str, Any]) -> None:
|
|
|
|
config = setup_utils_configuration(args, RunMode.UTIL_EXCHANGE)
|
|
|
|
|
|
|
|
timerange = TimeRange()
|
|
|
|
|
|
|
|
# Remove stake-currency to skip checks which are not relevant for datadownload
|
2024-05-12 14:27:03 +00:00
|
|
|
config["stake_currency"] = ""
|
2021-09-29 14:50:05 +00:00
|
|
|
|
2024-05-12 14:27:03 +00:00
|
|
|
if "timeframes" not in config:
|
|
|
|
config["timeframes"] = DL_DATA_TIMEFRAMES
|
2021-09-29 14:50:05 +00:00
|
|
|
|
|
|
|
# Init exchange
|
2023-05-13 06:27:27 +00:00
|
|
|
exchange = ExchangeResolver.load_exchange(config, validate=False)
|
2021-09-29 14:50:05 +00:00
|
|
|
# Manual validations of relevant settings
|
|
|
|
|
2024-05-12 14:27:03 +00:00
|
|
|
for timeframe in config["timeframes"]:
|
2021-09-29 14:50:05 +00:00
|
|
|
exchange.validate_timeframes(timeframe)
|
2024-03-02 12:23:03 +00:00
|
|
|
available_pairs = [
|
2024-05-12 14:27:03 +00:00
|
|
|
p
|
|
|
|
for p in exchange.get_markets(
|
|
|
|
tradable_only=True, active_only=not config.get("include_inactive")
|
|
|
|
).keys()
|
2024-03-02 12:23:03 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
expanded_pairs = dynamic_expand_pairlist(config, available_pairs)
|
2023-09-24 08:32:03 +00:00
|
|
|
|
2021-09-29 14:50:05 +00:00
|
|
|
# Convert downloaded trade data to different timeframes
|
|
|
|
convert_trades_to_ohlcv(
|
2024-05-12 14:27:03 +00:00
|
|
|
pairs=expanded_pairs,
|
|
|
|
timeframes=config["timeframes"],
|
|
|
|
datadir=config["datadir"],
|
|
|
|
timerange=timerange,
|
|
|
|
erase=bool(config.get("erase")),
|
|
|
|
data_format_ohlcv=config["dataformat_ohlcv"],
|
|
|
|
data_format_trades=config["dataformat_trades"],
|
|
|
|
candle_type=config.get("candle_type_def", CandleType.SPOT),
|
2021-09-29 14:50:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-01-26 19:31:13 +00:00
|
|
|
def start_convert_data(args: Dict[str, Any], ohlcv: bool = True) -> None:
|
|
|
|
"""
|
|
|
|
Convert data from one format to another
|
|
|
|
"""
|
|
|
|
config = setup_utils_configuration(args, RunMode.UTIL_NO_EXCHANGE)
|
|
|
|
if ohlcv:
|
2024-01-04 15:25:40 +00:00
|
|
|
migrate_data(config)
|
2024-05-12 14:27:03 +00:00
|
|
|
convert_ohlcv_format(
|
|
|
|
config,
|
|
|
|
convert_from=args["format_from"],
|
|
|
|
convert_to=args["format_to"],
|
|
|
|
erase=args["erase"],
|
|
|
|
)
|
2020-01-26 19:31:13 +00:00
|
|
|
else:
|
2024-05-12 14:27:03 +00:00
|
|
|
convert_trades_format(
|
|
|
|
config,
|
|
|
|
convert_from=args["format_from_trades"],
|
|
|
|
convert_to=args["format_to"],
|
|
|
|
erase=args["erase"],
|
|
|
|
)
|
2020-07-12 07:57:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
def start_list_data(args: Dict[str, Any]) -> None:
|
|
|
|
"""
|
2024-08-12 18:09:22 +00:00
|
|
|
List available OHLCV data
|
2020-07-12 07:57:00 +00:00
|
|
|
"""
|
|
|
|
|
2024-08-13 04:46:02 +00:00
|
|
|
if args["trades"]:
|
2024-08-13 04:44:28 +00:00
|
|
|
start_list_trades_data(args)
|
|
|
|
return
|
|
|
|
|
2020-07-12 07:57:00 +00:00
|
|
|
config = setup_utils_configuration(args, RunMode.UTIL_NO_EXCHANGE)
|
|
|
|
|
2024-03-15 05:40:42 +00:00
|
|
|
from freqtrade.data.history import get_datahandler
|
2024-05-12 14:27:03 +00:00
|
|
|
|
|
|
|
dhc = get_datahandler(config["datadir"], config["dataformat_ohlcv"])
|
2020-07-12 08:23:09 +00:00
|
|
|
|
2022-03-03 06:06:13 +00:00
|
|
|
paircombs = dhc.ohlcv_get_available_data(
|
2024-05-12 14:27:03 +00:00
|
|
|
config["datadir"], config.get("trading_mode", TradingMode.SPOT)
|
|
|
|
)
|
|
|
|
if args["pairs"]:
|
|
|
|
paircombs = [comb for comb in paircombs if comb[0] in args["pairs"]]
|
2024-07-06 19:00:34 +00:00
|
|
|
title = f"Found {len(paircombs)} pair / timeframe combinations."
|
2024-05-12 14:27:03 +00:00
|
|
|
if not config.get("show_timerange"):
|
2022-08-19 11:44:31 +00:00
|
|
|
groupedpair = defaultdict(list)
|
|
|
|
for pair, timeframe, candle_type in sorted(
|
2024-05-12 14:27:03 +00:00
|
|
|
paircombs, key=lambda x: (x[0], timeframe_to_minutes(x[1]), x[2])
|
2022-08-19 11:44:31 +00:00
|
|
|
):
|
|
|
|
groupedpair[(pair, candle_type)].append(timeframe)
|
|
|
|
|
|
|
|
if groupedpair:
|
2024-07-06 19:00:34 +00:00
|
|
|
print_rich_table(
|
|
|
|
[
|
|
|
|
(pair, ", ".join(timeframes), candle_type)
|
|
|
|
for (pair, candle_type), timeframes in groupedpair.items()
|
|
|
|
],
|
|
|
|
("Pair", "Timeframe", "Type"),
|
|
|
|
title,
|
|
|
|
table_kwargs={"min_width": 50},
|
2024-05-12 14:27:03 +00:00
|
|
|
)
|
2022-08-19 11:44:31 +00:00
|
|
|
else:
|
2024-05-12 14:27:03 +00:00
|
|
|
paircombs1 = [
|
|
|
|
(pair, timeframe, candle_type, *dhc.ohlcv_data_min_max(pair, timeframe, candle_type))
|
|
|
|
for pair, timeframe, candle_type in paircombs
|
|
|
|
]
|
2024-07-06 19:00:34 +00:00
|
|
|
print_rich_table(
|
|
|
|
[
|
|
|
|
(
|
|
|
|
pair,
|
|
|
|
timeframe,
|
|
|
|
candle_type,
|
|
|
|
start.strftime(DATETIME_PRINT_FORMAT),
|
|
|
|
end.strftime(DATETIME_PRINT_FORMAT),
|
|
|
|
str(length),
|
|
|
|
)
|
|
|
|
for pair, timeframe, candle_type, start, end, length in sorted(
|
|
|
|
paircombs1, key=lambda x: (x[0], timeframe_to_minutes(x[1]), x[2])
|
|
|
|
)
|
|
|
|
],
|
|
|
|
("Pair", "Timeframe", "Type", "From", "To", "Candles"),
|
|
|
|
summary=title,
|
|
|
|
table_kwargs={"min_width": 50},
|
2024-05-12 14:27:03 +00:00
|
|
|
)
|
2024-08-13 04:44:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
def start_list_trades_data(args: Dict[str, Any]) -> None:
|
|
|
|
"""
|
|
|
|
List available Trades data
|
|
|
|
"""
|
|
|
|
|
|
|
|
config = setup_utils_configuration(args, RunMode.UTIL_NO_EXCHANGE)
|
|
|
|
|
|
|
|
from freqtrade.data.history import get_datahandler
|
|
|
|
|
|
|
|
dhc = get_datahandler(config["datadir"], config["dataformat_trades"])
|
|
|
|
|
|
|
|
paircombs = dhc.trades_get_available_data(
|
|
|
|
config["datadir"], config.get("trading_mode", TradingMode.SPOT)
|
|
|
|
)
|
|
|
|
|
|
|
|
if args["pairs"]:
|
|
|
|
paircombs = [comb for comb in paircombs if comb in args["pairs"]]
|
|
|
|
|
2024-08-13 05:12:57 +00:00
|
|
|
title = f"Found trades data for {len(paircombs)} {plural(len(paircombs), 'pair')}."
|
2024-08-13 04:44:28 +00:00
|
|
|
if not config.get("show_timerange"):
|
|
|
|
print_rich_table(
|
|
|
|
[(pair, config.get("candle_type_def", CandleType.SPOT)) for pair in sorted(paircombs)],
|
|
|
|
("Pair", "Type"),
|
|
|
|
title,
|
|
|
|
table_kwargs={"min_width": 50},
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
paircombs1 = [
|
|
|
|
(pair, *dhc.trades_data_min_max(pair, config.get("trading_mode", TradingMode.SPOT)))
|
|
|
|
for pair in paircombs
|
|
|
|
]
|
|
|
|
print_rich_table(
|
|
|
|
[
|
|
|
|
(
|
|
|
|
pair,
|
|
|
|
config.get("candle_type_def", CandleType.SPOT),
|
|
|
|
start.strftime(DATETIME_PRINT_FORMAT),
|
|
|
|
end.strftime(DATETIME_PRINT_FORMAT),
|
|
|
|
str(length),
|
|
|
|
)
|
|
|
|
for pair, start, end, length in sorted(paircombs1, key=lambda x: (x[0]))
|
|
|
|
],
|
|
|
|
("Pair", "Type", "From", "To", "Trades"),
|
|
|
|
summary=title,
|
|
|
|
table_kwargs={"min_width": 50},
|
|
|
|
)
|