add pandas-based converter-functions

This commit is contained in:
Matthias 2019-10-13 19:21:27 +02:00
parent ed9ec402fd
commit 3e4617be37
3 changed files with 27 additions and 10 deletions

View File

@ -114,3 +114,23 @@ def order_book_to_dataframe(bids: list, asks: list) -> DataFrame:
keys=['b_sum', 'b_size', 'bids', 'asks', 'a_size', 'a_sum'])
# logger.info('order book %s', frame )
return frame
def trades_to_ohlcv(trades, timeframe):
"""
Converts trades list to ohlcv list
"""
from freqtrade.exchange import timeframe_to_minutes
ticker_minutes = timeframe_to_minutes(timeframe)
df = pd.DataFrame(trades)
df['datetime'] = pd.to_datetime(df['datetime'])
df = df.set_index('datetime')
df_new = df['price'].resample(f'{ticker_minutes}min').ohlc()
df_new['volume'] = df['amount'].resample(f'{ticker_minutes}min').sum()
df_new['date'] = df_new.index.astype("int64") // 10 ** 6
# Drop 0 volume columns
df_new = df_new.dropna()
columns = ["date", "open", "high", "low", "close", "volume"]
return list(zip(*[df_new[x].values.tolist() for x in columns]))

View File

@ -17,7 +17,7 @@ from pandas import DataFrame
from freqtrade import OperationalException, misc
from freqtrade.configuration import TimeRange
from freqtrade.data.converter import parse_ticker_dataframe
from freqtrade.data.converter import parse_ticker_dataframe, trades_to_ohlcv
from freqtrade.exchange import Exchange, timeframe_to_minutes
logger = logging.getLogger(__name__)
@ -398,7 +398,7 @@ def refresh_backtest_trades_data(exchange: Exchange, pairs: List[str], datadir:
return pairs_not_available
def convert_trades_to_ohlcv(exchange: Exchange, pairs: List[str], timeframes: List[str],
def convert_trades_to_ohlcv(pairs: List[str], timeframes: List[str],
datadir: Path, timerange: TimeRange, erase=False) -> None:
"""
Convert stored trades data to ohlcv data
@ -410,7 +410,7 @@ def convert_trades_to_ohlcv(exchange: Exchange, pairs: List[str], timeframes: Li
if erase and ohlcv_file.exists():
logger.info(f'Deleting existing data for pair {pair}, interval {timeframe}.')
ohlcv_file.unlink()
ohlcv = exchange.build_ohlcv(trades, timeframe)
ohlcv = trades_to_ohlcv(trades, timeframe)
# Store ohlcv
store_tickerdata_file(datadir, pair, timeframe, data=ohlcv)

View File

@ -24,7 +24,6 @@ from freqtrade.data.history import (_load_cached_data_for_updating,
trim_tickerlist)
from freqtrade.exchange import timeframe_to_minutes
from freqtrade.misc import file_dump_json
from freqtrade.resolvers.exchange_resolver import ExchangeResolver
from freqtrade.strategy.default_strategy import DefaultStrategy
from tests.conftest import (get_patched_exchange, log_has, log_has_re,
patch_exchange)
@ -642,6 +641,7 @@ def test_download_trades_history(trades_history, mocker, default_conf, testdatad
def test_convert_trades_to_ohlcv(mocker, default_conf, testdatadir, caplog):
pair = 'XRP/ETH'
file1 = testdatadir / 'XRP_ETH-1m.json'
file5 = testdatadir / 'XRP_ETH-5m.json'
@ -653,18 +653,15 @@ def test_convert_trades_to_ohlcv(mocker, default_conf, testdatadir, caplog):
ticker_interval="5m",
pair=pair)
_backup_file(file1)
_backup_file(file1, copy_file=True)
_backup_file(file5)
exchange = ExchangeResolver('Bittrex', default_conf).exchange
tr = TimeRange.parse_timerange('20191011-20191012')
# mocker.patch.object(Path, "exists", MagicMock(return_value=True))
# mocker.patch.object(Path, "unlink", MagicMock())
convert_trades_to_ohlcv(exchange, [pair], timeframes=['1m', '5m'],
convert_trades_to_ohlcv([pair], timeframes=['1m', '5m'],
datadir=testdatadir, timerange=tr, erase=True)
# assert log_has("Deleting existing data for XRP/ETH, interval 1m.", caplog)
assert log_has("Deleting existing data for pair XRP/ETH, interval 1m.", caplog)
# Load new data
df_1m = history.load_pair_history(datadir=testdatadir,
ticker_interval="1m",