stable/freqtrade/tests/test_plotting.py

271 lines
9.9 KiB
Python
Raw Normal View History

2019-06-10 18:17:23 +00:00
from copy import deepcopy
2019-06-10 18:17:23 +00:00
from unittest.mock import MagicMock
import plotly.graph_objects as go
from plotly.subplots import make_subplots
2019-06-10 18:17:23 +00:00
from freqtrade.configuration import Arguments, TimeRange
2019-06-10 18:17:23 +00:00
from freqtrade.data import history
2019-06-30 08:25:48 +00:00
from freqtrade.data.btanalysis import create_cum_profit, load_backtest_data
from freqtrade.plot.plotting import (add_indicators, add_profit,
generate_candlestick_graph,
2019-06-30 08:47:55 +00:00
generate_plot_filename,
2019-06-30 11:01:12 +00:00
generate_profit_graph, init_plotscript,
plot_trades, store_plot_file)
2019-06-11 04:45:36 +00:00
from freqtrade.strategy.default_strategy import DefaultStrategy
from freqtrade.tests.conftest import log_has, log_has_re
2019-06-10 18:17:23 +00:00
2019-06-16 08:32:12 +00:00
2019-06-10 18:17:23 +00:00
def fig_generating_mock(fig, *args, **kwargs):
2019-06-30 07:44:50 +00:00
""" Return Fig - used to mock add_indicators and plot_trades"""
2019-06-10 18:17:23 +00:00
return fig
2019-06-11 04:45:36 +00:00
def find_trace_in_fig_data(data, search_string: str):
2019-07-14 18:14:35 +00:00
matches = (d for d in data if d.name == search_string)
2019-06-11 04:45:36 +00:00
return next(matches)
2019-06-16 08:32:12 +00:00
def generage_empty_figure():
return make_subplots(
2019-06-16 08:32:12 +00:00
rows=3,
cols=1,
shared_xaxes=True,
row_width=[1, 1, 4],
vertical_spacing=0.0001,
)
2019-06-16 12:03:55 +00:00
2019-06-30 11:01:12 +00:00
def test_init_plotscript(default_conf, mocker):
default_conf['timerange'] = "20180110-20180112"
default_conf['trade_source'] = "file"
default_conf['ticker_interval'] = "5m"
default_conf["datadir"] = history.make_testdata_path(None)
default_conf['exportfilename'] = str(
history.make_testdata_path(None) / "backtest-result_test.json")
ret = init_plotscript(default_conf)
assert "tickers" in ret
assert "trades" in ret
assert "pairs" in ret
assert "strategy" in ret
default_conf['pairs'] = "POWR/BTC,XLM/BTC"
ret = init_plotscript(default_conf)
assert "tickers" in ret
assert "POWR/BTC" in ret["tickers"]
assert "XLM/BTC" in ret["tickers"]
2019-06-30 07:44:50 +00:00
def test_add_indicators(default_conf, caplog):
2019-06-16 08:32:12 +00:00
pair = "UNITTEST/BTC"
timerange = TimeRange(None, 'line', 0, -1000)
data = history.load_pair_history(pair=pair, ticker_interval='1m',
datadir=None, timerange=timerange)
indicators1 = ["ema10"]
indicators2 = ["macd"]
# Generate buy/sell signals and indicators
strat = DefaultStrategy(default_conf)
data = strat.analyze_ticker(data, {'pair': pair})
fig = generage_empty_figure()
# Row 1
2019-06-30 07:44:50 +00:00
fig1 = add_indicators(fig=deepcopy(fig), row=1, indicators=indicators1, data=data)
2019-06-16 08:32:12 +00:00
figure = fig1.layout.figure
ema10 = find_trace_in_fig_data(figure.data, "ema10")
assert isinstance(ema10, go.Scatter)
assert ema10.yaxis == "y"
2019-06-30 07:44:50 +00:00
fig2 = add_indicators(fig=deepcopy(fig), row=3, indicators=indicators2, data=data)
2019-06-16 08:32:12 +00:00
figure = fig2.layout.figure
macd = find_trace_in_fig_data(figure.data, "macd")
assert isinstance(macd, go.Scatter)
assert macd.yaxis == "y3"
# No indicator found
2019-06-30 07:44:50 +00:00
fig3 = add_indicators(fig=deepcopy(fig), row=3, indicators=['no_indicator'], data=data)
2019-06-16 08:32:12 +00:00
assert fig == fig3
assert log_has_re(r'Indicator "no_indicator" ignored\..*', caplog.record_tuples)
2019-06-10 18:17:23 +00:00
2019-06-22 13:45:20 +00:00
def test_plot_trades(caplog):
2019-06-16 08:32:12 +00:00
fig1 = generage_empty_figure()
# nothing happens when no trades are available
fig = plot_trades(fig1, None)
assert fig == fig1
2019-06-22 13:45:20 +00:00
assert log_has("No trades found.", caplog.record_tuples)
2019-06-16 17:53:48 +00:00
pair = "ADA/BTC"
filename = history.make_testdata_path(None) / "backtest-result_test.json"
trades = load_backtest_data(filename)
trades = trades.loc[trades['pair'] == pair]
2019-06-16 08:32:12 +00:00
2019-06-16 17:53:48 +00:00
fig = plot_trades(fig, trades)
figure = fig1.layout.figure
# Check buys - color, should be in first graph, ...
trade_buy = find_trace_in_fig_data(figure.data, "trade_buy")
assert isinstance(trade_buy, go.Scatter)
assert trade_buy.yaxis == 'y'
assert len(trades) == len(trade_buy.x)
assert trade_buy.marker.color == 'green'
trade_sell = find_trace_in_fig_data(figure.data, "trade_sell")
assert isinstance(trade_sell, go.Scatter)
assert trade_sell.yaxis == 'y'
assert len(trades) == len(trade_sell.x)
assert trade_sell.marker.color == 'red'
2019-06-10 18:17:23 +00:00
def test_generate_candlestick_graph_no_signals_no_trades(default_conf, mocker, caplog):
2019-06-30 07:44:50 +00:00
row_mock = mocker.patch('freqtrade.plot.plotting.add_indicators',
2019-06-10 18:17:23 +00:00
MagicMock(side_effect=fig_generating_mock))
trades_mock = mocker.patch('freqtrade.plot.plotting.plot_trades',
MagicMock(side_effect=fig_generating_mock))
2019-06-11 04:45:36 +00:00
pair = "UNITTEST/BTC"
timerange = TimeRange(None, 'line', 0, -1000)
data = history.load_pair_history(pair=pair, ticker_interval='1m',
datadir=None, timerange=timerange)
data['buy'] = 0
data['sell'] = 0
indicators1 = []
indicators2 = []
fig = generate_candlestick_graph(pair=pair, data=data, trades=None,
indicators1=indicators1, indicators2=indicators2)
2019-06-11 04:45:36 +00:00
assert isinstance(fig, go.Figure)
assert fig.layout.title.text == pair
figure = fig.layout.figure
assert len(figure.data) == 2
# Candlesticks are plotted first
candles = find_trace_in_fig_data(figure.data, "Price")
assert isinstance(candles, go.Candlestick)
volume = find_trace_in_fig_data(figure.data, "Volume")
assert isinstance(volume, go.Bar)
assert row_mock.call_count == 2
assert trades_mock.call_count == 1
assert log_has("No buy-signals found.", caplog.record_tuples)
assert log_has("No sell-signals found.", caplog.record_tuples)
def test_generate_candlestick_graph_no_trades(default_conf, mocker):
2019-06-30 07:44:50 +00:00
row_mock = mocker.patch('freqtrade.plot.plotting.add_indicators',
2019-06-11 04:45:36 +00:00
MagicMock(side_effect=fig_generating_mock))
trades_mock = mocker.patch('freqtrade.plot.plotting.plot_trades',
MagicMock(side_effect=fig_generating_mock))
pair = 'UNITTEST/BTC'
timerange = TimeRange(None, 'line', 0, -1000)
data = history.load_pair_history(pair=pair, ticker_interval='1m',
2019-06-10 18:17:23 +00:00
datadir=None, timerange=timerange)
2019-06-11 04:45:36 +00:00
# Generate buy/sell signals and indicators
strat = DefaultStrategy(default_conf)
data = strat.analyze_ticker(data, {'pair': pair})
2019-06-10 18:17:23 +00:00
indicators1 = []
indicators2 = []
fig = generate_candlestick_graph(pair=pair, data=data, trades=None,
indicators1=indicators1, indicators2=indicators2)
2019-06-10 18:17:23 +00:00
assert isinstance(fig, go.Figure)
2019-06-11 04:45:36 +00:00
assert fig.layout.title.text == pair
2019-06-10 18:17:23 +00:00
figure = fig.layout.figure
2019-06-11 04:45:36 +00:00
assert len(figure.data) == 6
2019-06-10 18:17:23 +00:00
# Candlesticks are plotted first
2019-06-11 04:45:36 +00:00
candles = find_trace_in_fig_data(figure.data, "Price")
assert isinstance(candles, go.Candlestick)
volume = find_trace_in_fig_data(figure.data, "Volume")
assert isinstance(volume, go.Bar)
buy = find_trace_in_fig_data(figure.data, "buy")
assert isinstance(buy, go.Scatter)
# All buy-signals should be plotted
assert int(data.buy.sum()) == len(buy.x)
sell = find_trace_in_fig_data(figure.data, "sell")
assert isinstance(sell, go.Scatter)
# All buy-signals should be plotted
assert int(data.sell.sum()) == len(sell.x)
2019-06-10 18:17:23 +00:00
2019-06-11 04:45:36 +00:00
assert find_trace_in_fig_data(figure.data, "BB lower")
assert find_trace_in_fig_data(figure.data, "BB upper")
2019-06-10 18:17:23 +00:00
assert row_mock.call_count == 2
assert trades_mock.call_count == 1
2019-06-16 12:03:55 +00:00
def test_generate_Plot_filename():
fn = generate_plot_filename("UNITTEST/BTC", "5m")
assert fn == "freqtrade-plot-UNITTEST_BTC-5m.html"
2019-06-16 12:03:55 +00:00
def test_generate_plot_file(mocker, caplog):
fig = generage_empty_figure()
plot_mock = mocker.patch("freqtrade.plot.plotting.plot", MagicMock())
store_plot_file(fig, filename="freqtrade-plot-UNITTEST_BTC-5m.html")
2019-06-16 12:03:55 +00:00
assert plot_mock.call_count == 1
assert plot_mock.call_args[0][0] == fig
assert (plot_mock.call_args_list[0][1]['filename']
== "user_data/plots/freqtrade-plot-UNITTEST_BTC-5m.html")
assert log_has("Stored plot as user_data/plots/freqtrade-plot-UNITTEST_BTC-5m.html",
caplog.record_tuples)
2019-06-30 08:24:10 +00:00
def test_add_profit():
filename = history.make_testdata_path(None) / "backtest-result_test.json"
bt_data = load_backtest_data(filename)
timerange = Arguments.parse_timerange("20180110-20180112")
df = history.load_pair_history(pair="POWR/BTC", ticker_interval='5m',
datadir=None, timerange=timerange)
fig = generage_empty_figure()
cum_profits = create_cum_profit(df.set_index('date'),
bt_data[bt_data["pair"] == 'POWR/BTC'],
"cum_profits")
fig1 = add_profit(fig, row=2, data=cum_profits, column='cum_profits', name='Profits')
figure = fig1.layout.figure
profits = find_trace_in_fig_data(figure.data, "Profits")
assert isinstance(profits, go.Scattergl)
assert profits.yaxis == "y2"
2019-06-30 08:47:55 +00:00
def test_generate_profit_graph():
filename = history.make_testdata_path(None) / "backtest-result_test.json"
trades = load_backtest_data(filename)
timerange = Arguments.parse_timerange("20180110-20180112")
pairs = ["POWR/BTC", "XLM/BTC"]
tickers = history.load_data(datadir=None,
pairs=pairs,
ticker_interval='5m',
timerange=timerange
)
trades = trades[trades['pair'].isin(pairs)]
fig = generate_profit_graph(pairs, tickers, trades)
assert isinstance(fig, go.Figure)
assert fig.layout.title.text == "Profit plot"
figure = fig.layout.figure
assert len(figure.data) == 4
avgclose = find_trace_in_fig_data(figure.data, "Avg close price")
assert isinstance(avgclose, go.Scattergl)
profit = find_trace_in_fig_data(figure.data, "Profit")
assert isinstance(profit, go.Scattergl)
for pair in pairs:
profit_pair = find_trace_in_fig_data(figure.data, f"Profit {pair}")
assert isinstance(profit_pair, go.Scattergl)