2023-09-24 17:55:44 +00:00
|
|
|
import logging
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
import pandas as pd
|
|
|
|
|
|
|
|
from freqtrade.constants import DATETIME_PRINT_FORMAT, DEFAULT_TRADES_COLUMNS, Config
|
|
|
|
from freqtrade.data.converter.trade_converter import (trades_convert_types,
|
|
|
|
trades_df_remove_duplicates)
|
2024-03-15 05:40:42 +00:00
|
|
|
from freqtrade.data.history import get_datahandler
|
2024-03-02 12:10:57 +00:00
|
|
|
from freqtrade.enums import TradingMode
|
2023-09-25 17:48:09 +00:00
|
|
|
from freqtrade.exceptions import OperationalException
|
2024-02-10 07:48:52 +00:00
|
|
|
from freqtrade.plugins.pairlist.pairlist_helpers import expand_pairlist
|
2023-09-24 17:55:44 +00:00
|
|
|
from freqtrade.resolvers import ExchangeResolver
|
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
KRAKEN_CSV_TRADE_COLUMNS = ['timestamp', 'price', 'amount']
|
|
|
|
|
|
|
|
|
|
|
|
def import_kraken_trades_from_csv(config: Config, convert_to: str):
|
|
|
|
"""
|
|
|
|
Import kraken trades from csv
|
|
|
|
"""
|
|
|
|
if config['exchange']['name'] != 'kraken':
|
2023-09-25 17:48:09 +00:00
|
|
|
raise OperationalException('This function is only for the kraken exchange.')
|
2023-09-24 17:55:44 +00:00
|
|
|
|
|
|
|
datadir: Path = config['datadir']
|
|
|
|
data_handler = get_datahandler(datadir, data_format=convert_to)
|
|
|
|
|
|
|
|
tradesdir: Path = config['datadir'] / 'trades_csv'
|
|
|
|
exchange = ExchangeResolver.load_exchange(config, validate=False)
|
|
|
|
# iterate through directories in this directory
|
|
|
|
data_symbols = {p.stem for p in tradesdir.rglob('*.csv')}
|
|
|
|
|
|
|
|
# create pair/filename mapping
|
|
|
|
markets = {
|
|
|
|
(m['symbol'], m['altname']) for m in exchange.markets.values()
|
|
|
|
if m.get('altname') in data_symbols
|
|
|
|
}
|
2023-09-25 17:45:03 +00:00
|
|
|
logger.info(f"Found csv files for {', '.join(data_symbols)}.")
|
2023-09-24 17:55:44 +00:00
|
|
|
|
2024-02-10 07:48:52 +00:00
|
|
|
if pairs_raw := config.get('pairs'):
|
|
|
|
pairs = expand_pairlist(pairs_raw, [m[0] for m in markets])
|
2024-02-10 08:37:59 +00:00
|
|
|
markets = {m for m in markets if m[0] in pairs}
|
2024-02-10 07:44:16 +00:00
|
|
|
if not markets:
|
2024-02-10 07:48:52 +00:00
|
|
|
logger.info(f"No data found for pairs {', '.join(pairs_raw)}.")
|
2024-02-10 07:44:16 +00:00
|
|
|
return
|
|
|
|
logger.info(f"Converting pairs: {', '.join(m[0] for m in markets)}.")
|
|
|
|
|
2023-09-24 17:55:44 +00:00
|
|
|
for pair, name in markets:
|
2024-02-13 16:47:43 +00:00
|
|
|
logger.debug(f"Converting pair {pair}, files */{name}.csv")
|
2023-09-24 17:55:44 +00:00
|
|
|
dfs = []
|
|
|
|
# Load and combine all csv files for this pair
|
|
|
|
for f in tradesdir.rglob(f"{name}.csv"):
|
|
|
|
df = pd.read_csv(f, names=KRAKEN_CSV_TRADE_COLUMNS)
|
2024-02-14 06:26:23 +00:00
|
|
|
if not df.empty:
|
|
|
|
dfs.append(df)
|
2023-09-24 17:55:44 +00:00
|
|
|
|
2023-09-24 20:47:44 +00:00
|
|
|
# Load existing trades data
|
2023-09-24 17:55:44 +00:00
|
|
|
if not dfs:
|
2023-09-25 17:46:27 +00:00
|
|
|
# edgecase, can only happen if the file was deleted between the above glob and here
|
2023-09-24 20:47:44 +00:00
|
|
|
logger.info(f"No data found for pair {pair}")
|
2023-09-24 17:55:44 +00:00
|
|
|
continue
|
2023-09-24 20:47:44 +00:00
|
|
|
|
2023-09-24 17:55:44 +00:00
|
|
|
trades = pd.concat(dfs, ignore_index=True)
|
2024-02-10 08:37:59 +00:00
|
|
|
del dfs
|
2023-09-24 17:55:44 +00:00
|
|
|
|
|
|
|
trades.loc[:, 'timestamp'] = trades['timestamp'] * 1e3
|
|
|
|
trades.loc[:, 'cost'] = trades['price'] * trades['amount']
|
|
|
|
for col in DEFAULT_TRADES_COLUMNS:
|
|
|
|
if col not in trades.columns:
|
2024-02-10 08:37:59 +00:00
|
|
|
trades.loc[:, col] = ''
|
2023-09-24 17:55:44 +00:00
|
|
|
trades = trades[DEFAULT_TRADES_COLUMNS]
|
|
|
|
trades = trades_convert_types(trades)
|
|
|
|
|
|
|
|
trades_df = trades_df_remove_duplicates(trades)
|
2024-02-10 08:37:59 +00:00
|
|
|
del trades
|
2023-09-24 17:55:44 +00:00
|
|
|
logger.info(f"{pair}: {len(trades_df)} trades, from "
|
|
|
|
f"{trades_df['date'].min():{DATETIME_PRINT_FORMAT}} to "
|
|
|
|
f"{trades_df['date'].max():{DATETIME_PRINT_FORMAT}}")
|
|
|
|
|
2024-03-02 12:10:57 +00:00
|
|
|
data_handler.trades_store(pair, trades_df, TradingMode.SPOT)
|