extract combine_tickers to btanalysis

This commit is contained in:
Matthias 2019-06-30 10:04:43 +02:00
parent 348513c151
commit 6b387d320e
3 changed files with 38 additions and 7 deletions

View File

@ -3,6 +3,7 @@ Helpers when analyzing backtest data
"""
import logging
from pathlib import Path
from typing import Dict
import numpy as np
import pandas as pd
@ -125,7 +126,23 @@ def extract_trades_of_period(dataframe: pd.DataFrame, trades: pd.DataFrame) -> p
return trades
def create_cum_profit(df: pd.DataFrame, trades: pd.DataFrame, col_name: str):
def combine_tickers_with_mean(tickers: Dict[str, pd.DataFrame], column: str = "close"):
"""
Combine multiple dataframes "column"
:param tickers: Dict of Dataframes, dict key should be pair.
:param column: Column in the original dataframes to use
:return: DataFrame with the column renamed to the dict key, and a column
named mean, containing the mean of all pairs.
"""
df_comb = pd.concat([tickers[pair].set_index('date').rename(
{column: pair}, axis=1)[pair] for pair in tickers], axis=1)
df_comb['mean'] = df_comb.mean(axis=1)
return df_comb
def create_cum_profit(df: pd.DataFrame, trades: pd.DataFrame, col_name: str) -> pd.DataFrame:
"""
Adds a column `col_name` with the cumulative profit for the given trades array.
:param df: DataFrame with date index

View File

@ -5,11 +5,14 @@ from arrow import Arrow
from pandas import DataFrame, to_datetime
from freqtrade.arguments import Arguments, TimeRange
from freqtrade.data.btanalysis import (BT_DATA_COLUMNS, create_cum_profit,
from freqtrade.data.btanalysis import (BT_DATA_COLUMNS,
combine_tickers_with_mean,
create_cum_profit,
extract_trades_of_period,
load_backtest_data, load_trades,
load_trades_from_db)
from freqtrade.data.history import load_pair_history, make_testdata_path
from freqtrade.data.history import (load_data, load_pair_history,
make_testdata_path)
from freqtrade.tests.test_persistence import create_mock_trades
@ -97,6 +100,19 @@ def test_load_trades(default_conf, mocker):
assert bt_mock.call_count == 1
def test_combine_tickers_with_mean():
pairs = ["ETH/BTC", "XLM/BTC"]
tickers = load_data(datadir=None,
pairs=pairs,
ticker_interval='5m'
)
df = combine_tickers_with_mean(tickers)
assert isinstance(df, DataFrame)
assert "ETH/BTC" in df.columns
assert "XLM/BTC" in df.columns
assert "mean" in df.columns
def test_create_cum_profit():
filename = make_testdata_path(None) / "backtest-result_test.json"
bt_data = load_backtest_data(filename)

View File

@ -13,7 +13,7 @@ import plotly.graph_objs as go
from plotly import tools
from freqtrade.arguments import ARGS_PLOT_PROFIT, Arguments
from freqtrade.data.btanalysis import create_cum_profit
from freqtrade.data.btanalysis import create_cum_profit, combine_tickers_with_mean
from freqtrade.optimize import setup_configuration
from freqtrade.plot.plotting import FTPlots, store_plot_file
from freqtrade.state import RunMode
@ -36,9 +36,7 @@ def plot_profit(config: Dict[str, Any]) -> None:
# this could be useful to gauge the overall market trend
# Combine close-values for all pairs, rename columns to "pair"
df_comb = pd.concat([plot.tickers[pair].set_index('date').rename(
{'close': pair}, axis=1)[pair] for pair in plot.tickers], axis=1)
df_comb['mean'] = df_comb.mean(axis=1)
df_comb = combine_tickers_with_mean(plot.tickers, "close")
# Add combined cumulative profit
df_comb = create_cum_profit(df_comb, trades, 'cum_profit')