| 1 | |
| 2 | from copy import deepcopy |
| 3 | from pathlib import Path |
| 4 | from unittest.mock import MagicMock |
| 5 | |
| 6 | import pandas as pd |
| 7 | import plotly.graph_objects as go |
| 8 | import pytest |
| 9 | from plotly.subplots import make_subplots |
| 10 | |
| 11 | from freqtrade.commands import start_plot_dataframe, start_plot_profit |
| 12 | from freqtrade.configuration import TimeRange |
| 13 | from freqtrade.data import history |
| 14 | from freqtrade.data.btanalysis import create_cum_profit, load_backtest_data |
| 15 | from freqtrade.exceptions import OperationalException |
| 16 | from freqtrade.plot.plotting import (add_areas, add_indicators, add_profit, create_plotconfig, |
| 17 | generate_candlestick_graph, generate_plot_filename, |
| 18 | generate_profit_graph, init_plotscript, load_and_plot_trades, |
| 19 | plot_profit, plot_trades, store_plot_file) |
| 20 | from freqtrade.resolvers import StrategyResolver |
| 21 | from tests.conftest import get_args, log_has, log_has_re, patch_exchange |
| 22 | |
| 23 | |
| 24 | def fig_generating_mock(fig, *args, **kwargs): |
| 25 | """ Return Fig - used to mock add_indicators and plot_trades""" |
| 26 | return fig |
| 27 | |
| 28 | |
| 29 | def find_trace_in_fig_data(data, search_string: str): |
| 30 | matches = (d for d in data if d.name == search_string) |
| 31 | return next(matches) |
| 32 | |
| 33 | |
| 34 | def generate_empty_figure(): |
| 35 | return make_subplots( |
| 36 | rows=3, |
| 37 | cols=1, |
| 38 | shared_xaxes=True, |
| 39 | row_width=[1, 1, 4], |
| 40 | vertical_spacing=0.0001, |
| 41 | ) |
| 42 | |
| 43 | |
| 44 | def test_init_plotscript(default_conf, mocker, testdatadir): |
| 45 | default_conf['timerange'] = "20180110-20180112" |
| 46 | default_conf['trade_source'] = "file" |
| 47 | default_conf['timeframe'] = "5m" |
| 48 | default_conf["datadir"] = testdatadir |
| 49 | default_conf['exportfilename'] = testdatadir / "backtest-result_test.json" |
| 50 | supported_markets = ["TRX/BTC", "ADA/BTC"] |
| 51 | ret = init_plotscript(default_conf, supported_markets) |
| 52 | assert "ohlcv" in ret |
| 53 | assert "trades" in ret |
| 54 | assert "pairs" in ret |
| 55 | assert 'timerange' in ret |
| 56 | |
| 57 | default_conf['pairs'] = ["TRX/BTC", "ADA/BTC"] |
| 58 | ret = init_plotscript(default_conf, supported_markets, 20) |
| 59 | assert "ohlcv" in ret |
| 60 | assert "TRX/BTC" in ret["ohlcv"] |
| 61 | assert "ADA/BTC" in ret["ohlcv"] |
| 62 | |
| 63 | |
| 64 | def test_add_indicators(default_conf, testdatadir, caplog): |
| 65 | pair = "UNITTEST/BTC" |
| 66 | timerange = TimeRange(None, 'line', 0, -1000) |
| 67 | |
| 68 | data = history.load_pair_history(pair=pair, timeframe='1m', |
| 69 | datadir=testdatadir, timerange=timerange) |
| 70 | indicators1 = {"ema10": {}} |
| 71 | indicators2 = {"macd": {"color": "red"}} |
| 72 | |
| 73 | default_conf.update({'strategy': 'DefaultStrategy'}) |
| 74 | strategy = StrategyResolver.load_strategy(default_conf) |
| 75 | |
| 76 | # Generate buy/sell signals and indicators |
| 77 | data = strategy.analyze_ticker(data, {'pair': pair}) |
| 78 | fig = generate_empty_figure() |
| 79 | |
| 80 | # Row 1 |
| 81 | fig1 = add_indicators(fig=deepcopy(fig), row=1, indicators=indicators1, data=data) |
| 82 | figure = fig1.layout.figure |
| 83 | ema10 = find_trace_in_fig_data(figure.data, "ema10") |
| 84 | assert isinstance(ema10, go.Scatter) |
| 85 | assert ema10.yaxis == "y" |
| 86 | |
| 87 | fig2 = add_indicators(fig=deepcopy(fig), row=3, indicators=indicators2, data=data) |
| 88 | figure = fig2.layout.figure |
| 89 | macd = find_trace_in_fig_data(figure.data, "macd") |
| 90 | assert isinstance(macd, go.Scatter) |
| 91 | assert macd.yaxis == "y3" |
| 92 | assert macd.line.color == "red" |
| 93 | |
| 94 | # No indicator found |
| 95 | fig3 = add_indicators(fig=deepcopy(fig), row=3, indicators={'no_indicator': {}}, data=data) |
| 96 | assert fig == fig3 |
| 97 | assert log_has_re(r'Indicator "no_indicator" ignored\..*', caplog) |
| 98 | |
| 99 | |
| 100 | def test_add_areas(default_conf, testdatadir, caplog): |
| 101 | pair = "UNITTEST/BTC" |
| 102 | timerange = TimeRange(None, 'line', 0, -1000) |
| 103 | |
| 104 | data = history.load_pair_history(pair=pair, timeframe='1m', |
| 105 | datadir=testdatadir, timerange=timerange) |
| 106 | indicators = {"macd": {"color": "red", |
| 107 | "fill_color": "black", |
| 108 | "fill_to": "macdhist", |
| 109 | "fill_label": "MACD Fill"}} |
| 110 | |
| 111 | ind_no_label = {"macd": {"fill_color": "red", |
| 112 | "fill_to": "macdhist"}} |
| 113 | |
| 114 | ind_plain = {"macd": {"fill_to": "macdhist"}} |
| 115 | default_conf.update({'strategy': 'DefaultStrategy'}) |
| 116 | strategy = StrategyResolver.load_strategy(default_conf) |
| 117 | |
| 118 | # Generate buy/sell signals and indicators |
| 119 | data = strategy.analyze_ticker(data, {'pair': pair}) |
| 120 | fig = generate_empty_figure() |
| 121 | |
| 122 | # indicator mentioned in fill_to does not exist |
| 123 | fig1 = add_areas(fig, 1, data, {'ema10': {'fill_to': 'no_fill_indicator'}}) |
| 124 | assert fig == fig1 |
| 125 | assert log_has_re(r'fill_to: "no_fill_indicator" ignored\..*', caplog) |
| 126 | |
| 127 | # indicator does not exist |
| 128 | fig2 = add_areas(fig, 1, data, {'no_indicator': {'fill_to': 'ema10'}}) |
| 129 | assert fig == fig2 |
| 130 | assert log_has_re(r'Indicator "no_indicator" ignored\..*', caplog) |
| 131 | |
| 132 | # everythin given in plot config, row 3 |
| 133 | fig3 = add_areas(fig, 3, data, indicators) |
| 134 | figure = fig3.layout.figure |
| 135 | fill_macd = find_trace_in_fig_data(figure.data, "MACD Fill") |
| 136 | assert isinstance(fill_macd, go.Scatter) |
| 137 | assert fill_macd.yaxis == "y3" |
| 138 | assert fill_macd.fillcolor == "black" |
| 139 | |
| 140 | # label missing, row 1 |
| 141 | fig4 = add_areas(fig, 1, data, ind_no_label) |
| 142 | figure = fig4.layout.figure |
| 143 | fill_macd = find_trace_in_fig_data(figure.data, "macd<>macdhist") |
| 144 | assert isinstance(fill_macd, go.Scatter) |
| 145 | assert fill_macd.yaxis == "y" |
| 146 | assert fill_macd.fillcolor == "red" |
| 147 | |
| 148 | # fit_to only |
| 149 | fig5 = add_areas(fig, 1, data, ind_plain) |
| 150 | figure = fig5.layout.figure |
| 151 | fill_macd = find_trace_in_fig_data(figure.data, "macd<>macdhist") |
| 152 | assert isinstance(fill_macd, go.Scatter) |
| 153 | assert fill_macd.yaxis == "y" |
| 154 | |
| 155 | |
| 156 | def test_plot_trades(testdatadir, caplog): |
| 157 | fig1 = generate_empty_figure() |
| 158 | # nothing happens when no trades are available |
| 159 | fig = plot_trades(fig1, None) |
| 160 | assert fig == fig1 |
| 161 | assert log_has("No trades found.", caplog) |
| 162 | pair = "ADA/BTC" |
| 163 | filename = testdatadir / "backtest-result_test.json" |
| 164 | trades = load_backtest_data(filename) |
| 165 | trades = trades.loc[trades['pair'] == pair] |
| 166 | |
| 167 | fig = plot_trades(fig, trades) |
| 168 | figure = fig1.layout.figure |
| 169 | |
| 170 | # Check buys - color, should be in first graph, ... |
| 171 | trade_buy = find_trace_in_fig_data(figure.data, 'Trade buy') |
| 172 | assert isinstance(trade_buy, go.Scatter) |
| 173 | assert trade_buy.yaxis == 'y' |
| 174 | assert len(trades) == len(trade_buy.x) |
| 175 | assert trade_buy.marker.color == 'cyan' |
| 176 | assert trade_buy.marker.symbol == 'circle-open' |
| 177 | assert trade_buy.text[0] == '4.0%, roi, 15 min' |
| 178 | |
| 179 | trade_sell = find_trace_in_fig_data(figure.data, 'Sell - Profit') |
| 180 | assert isinstance(trade_sell, go.Scatter) |
| 181 | assert trade_sell.yaxis == 'y' |
| 182 | assert len(trades.loc[trades['profit_ratio'] > 0]) == len(trade_sell.x) |
| 183 | assert trade_sell.marker.color == 'green' |
| 184 | assert trade_sell.marker.symbol == 'square-open' |
| 185 | assert trade_sell.text[0] == '4.0%, roi, 15 min' |
| 186 | |
| 187 | trade_sell_loss = find_trace_in_fig_data(figure.data, 'Sell - Loss') |
| 188 | assert isinstance(trade_sell_loss, go.Scatter) |
| 189 | assert trade_sell_loss.yaxis == 'y' |
| 190 | assert len(trades.loc[trades['profit_ratio'] <= 0]) == len(trade_sell_loss.x) |
| 191 | assert trade_sell_loss.marker.color == 'red' |
| 192 | assert trade_sell_loss.marker.symbol == 'square-open' |
| 193 | assert trade_sell_loss.text[5] == '-10.4%, stop_loss, 720 min' |
| 194 | |
| 195 | |
| 196 | def test_generate_candlestick_graph_no_signals_no_trades(default_conf, mocker, testdatadir, caplog): |
| 197 | row_mock = mocker.patch('freqtrade.plot.plotting.add_indicators', |
| 198 | MagicMock(side_effect=fig_generating_mock)) |
| 199 | trades_mock = mocker.patch('freqtrade.plot.plotting.plot_trades', |
| 200 | MagicMock(side_effect=fig_generating_mock)) |
| 201 | |
| 202 | pair = "UNITTEST/BTC" |
| 203 | timerange = TimeRange(None, 'line', 0, -1000) |
| 204 | data = history.load_pair_history(pair=pair, timeframe='1m', |
| 205 | datadir=testdatadir, timerange=timerange) |
| 206 | data['buy'] = 0 |
| 207 | data['sell'] = 0 |
| 208 | |
| 209 | indicators1 = [] |
| 210 | indicators2 = [] |
| 211 | fig = generate_candlestick_graph(pair=pair, data=data, trades=None, |
| 212 | indicators1=indicators1, indicators2=indicators2) |
| 213 | assert isinstance(fig, go.Figure) |
| 214 | assert fig.layout.title.text == pair |
| 215 | figure = fig.layout.figure |
| 216 | |
| 217 | assert len(figure.data) == 2 |
| 218 | # Candlesticks are plotted first |
| 219 | candles = find_trace_in_fig_data(figure.data, "Price") |
| 220 | assert isinstance(candles, go.Candlestick) |
| 221 | |
| 222 | volume = find_trace_in_fig_data(figure.data, "Volume") |
| 223 | assert isinstance(volume, go.Bar) |
| 224 | |
| 225 | assert row_mock.call_count == 2 |
| 226 | assert trades_mock.call_count == 1 |
| 227 | |
| 228 | assert log_has("No buy-signals found.", caplog) |
| 229 | assert log_has("No sell-signals found.", caplog) |
| 230 | |
| 231 | |
| 232 | def test_generate_candlestick_graph_no_trades(default_conf, mocker, testdatadir): |
| 233 | row_mock = mocker.patch('freqtrade.plot.plotting.add_indicators', |
| 234 | MagicMock(side_effect=fig_generating_mock)) |
| 235 | trades_mock = mocker.patch('freqtrade.plot.plotting.plot_trades', |
| 236 | MagicMock(side_effect=fig_generating_mock)) |
| 237 | pair = 'UNITTEST/BTC' |
| 238 | timerange = TimeRange(None, 'line', 0, -1000) |
| 239 | data = history.load_pair_history(pair=pair, timeframe='1m', |
| 240 | datadir=testdatadir, timerange=timerange) |
| 241 | |
| 242 | default_conf.update({'strategy': 'DefaultStrategy'}) |
| 243 | strategy = StrategyResolver.load_strategy(default_conf) |
| 244 | |
| 245 | # Generate buy/sell signals and indicators |
| 246 | data = strategy.analyze_ticker(data, {'pair': pair}) |
| 247 | |
| 248 | indicators1 = [] |
| 249 | indicators2 = [] |
| 250 | fig = generate_candlestick_graph(pair=pair, data=data, trades=None, |
| 251 | indicators1=indicators1, indicators2=indicators2) |
| 252 | assert isinstance(fig, go.Figure) |
| 253 | assert fig.layout.title.text == pair |
| 254 | figure = fig.layout.figure |
| 255 | |
| 256 | assert len(figure.data) == 6 |
| 257 | # Candlesticks are plotted first |
| 258 | candles = find_trace_in_fig_data(figure.data, "Price") |
| 259 | assert isinstance(candles, go.Candlestick) |
| 260 | |
| 261 | volume = find_trace_in_fig_data(figure.data, "Volume") |
| 262 | assert isinstance(volume, go.Bar) |
| 263 | |
| 264 | buy = find_trace_in_fig_data(figure.data, "buy") |
| 265 | assert isinstance(buy, go.Scatter) |
| 266 | # All buy-signals should be plotted |
| 267 | assert int(data.buy.sum()) == len(buy.x) |
| 268 | |
| 269 | sell = find_trace_in_fig_data(figure.data, "sell") |
| 270 | assert isinstance(sell, go.Scatter) |
| 271 | # All buy-signals should be plotted |
| 272 | assert int(data.sell.sum()) == len(sell.x) |
| 273 | |
| 274 | assert find_trace_in_fig_data(figure.data, "Bollinger Band") |
| 275 | |
| 276 | assert row_mock.call_count == 2 |
| 277 | assert trades_mock.call_count == 1 |
| 278 | |
| 279 | |
| 280 | def test_generate_Plot_filename(): |
| 281 | fn = generate_plot_filename("UNITTEST/BTC", "5m") |
| 282 | assert fn == "freqtrade-plot-UNITTEST_BTC-5m.html" |
| 283 | |
| 284 | |
| 285 | def test_generate_plot_file(mocker, caplog): |
| 286 | fig = generate_empty_figure() |
| 287 | plot_mock = mocker.patch("freqtrade.plot.plotting.plot", MagicMock()) |
| 288 | store_plot_file(fig, filename="freqtrade-plot-UNITTEST_BTC-5m.html", |
| 289 | directory=Path("user_data/plot")) |
| 290 | |
| 291 | expected_fn = str(Path("user_data/plot/freqtrade-plot-UNITTEST_BTC-5m.html")) |
| 292 | assert plot_mock.call_count == 1 |
| 293 | assert plot_mock.call_args[0][0] == fig |
| 294 | assert (plot_mock.call_args_list[0][1]['filename'] |
| 295 | == expected_fn) |
| 296 | assert log_has(f"Stored plot as {expected_fn}", |
| 297 | caplog) |
| 298 | |
| 299 | |
| 300 | def test_add_profit(testdatadir): |
| 301 | filename = testdatadir / "backtest-result_test.json" |
| 302 | bt_data = load_backtest_data(filename) |
| 303 | timerange = TimeRange.parse_timerange("20180110-20180112") |
| 304 | |
| 305 | df = history.load_pair_history(pair="TRX/BTC", timeframe='5m', |
| 306 | datadir=testdatadir, timerange=timerange) |
| 307 | fig = generate_empty_figure() |
| 308 | |
| 309 | cum_profits = create_cum_profit(df.set_index('date'), |
| 310 | bt_data[bt_data["pair"] == 'TRX/BTC'], |
| 311 | "cum_profits", timeframe="5m") |
| 312 | |
| 313 | fig1 = add_profit(fig, row=2, data=cum_profits, column='cum_profits', name='Profits') |
| 314 | figure = fig1.layout.figure |
| 315 | profits = find_trace_in_fig_data(figure.data, "Profits") |
| 316 | assert isinstance(profits, go.Scatter) |
| 317 | assert profits.yaxis == "y2" |
| 318 | |
| 319 | |
| 320 | def test_generate_profit_graph(testdatadir): |
| 321 | filename = testdatadir / "backtest-result_test.json" |
| 322 | trades = load_backtest_data(filename) |
| 323 | timerange = TimeRange.parse_timerange("20180110-20180112") |
| 324 | pairs = ["TRX/BTC", "XLM/BTC"] |
| 325 | trades = trades[trades['close_date'] < pd.Timestamp('2018-01-12', tz='UTC')] |
| 326 | |
| 327 | data = history.load_data(datadir=testdatadir, |
| 328 | pairs=pairs, |
| 329 | timeframe='5m', |
| 330 | timerange=timerange) |
| 331 | |
| 332 | trades = trades[trades['pair'].isin(pairs)] |
| 333 | |
| 334 | fig = generate_profit_graph(pairs, data, trades, timeframe="5m") |
| 335 | assert isinstance(fig, go.Figure) |
| 336 | |
| 337 | assert fig.layout.title.text == "Freqtrade Profit plot" |
| 338 | assert fig.layout.yaxis.title.text == "Price" |
| 339 | assert fig.layout.yaxis2.title.text == "Profit" |
| 340 | assert fig.layout.yaxis3.title.text == "Profit" |
| 341 | |
| 342 | figure = fig.layout.figure |
| 343 | assert len(figure.data) == 5 |
| 344 | |
| 345 | avgclose = find_trace_in_fig_data(figure.data, "Avg close price") |
| 346 | assert isinstance(avgclose, go.Scatter) |
| 347 | |
| 348 | profit = find_trace_in_fig_data(figure.data, "Profit") |
| 349 | assert isinstance(profit, go.Scatter) |
| 350 | profit = find_trace_in_fig_data(figure.data, "Max drawdown 10.45%") |
| 351 | assert isinstance(profit, go.Scatter) |
| 352 | |
| 353 | for pair in pairs: |
| 354 | profit_pair = find_trace_in_fig_data(figure.data, f"Profit {pair}") |
| 355 | assert isinstance(profit_pair, go.Scatter) |
| 356 | |
| 357 | with pytest.raises(OperationalException, match=r"No trades found.*"): |
| 358 | # Pair cannot be empty - so it's an empty dataframe. |
| 359 | generate_profit_graph(pairs, data, trades.loc[trades['pair'].isnull()], timeframe="5m") |
| 360 | |
| 361 | |
| 362 | def test_start_plot_dataframe(mocker): |
| 363 | aup = mocker.patch("freqtrade.plot.plotting.load_and_plot_trades", MagicMock()) |
| 364 | args = [ |
| 365 | "plot-dataframe", |
| 366 | "--config", "config_bittrex.json.example", |
| 367 | "--pairs", "ETH/BTC" |
| 368 | ] |
| 369 | start_plot_dataframe(get_args(args)) |
| 370 | |
| 371 | assert aup.call_count == 1 |
| 372 | called_config = aup.call_args_list[0][0][0] |
| 373 | assert "pairs" in called_config |
| 374 | assert called_config['pairs'] == ["ETH/BTC"] |
| 375 | |
| 376 | |
| 377 | def test_load_and_plot_trades(default_conf, mocker, caplog, testdatadir): |
| 378 | patch_exchange(mocker) |
| 379 | |
| 380 | default_conf['trade_source'] = 'file' |
| 381 | default_conf["datadir"] = testdatadir |
| 382 | default_conf['exportfilename'] = testdatadir / "backtest-result_test.json" |
| 383 | default_conf['indicators1'] = ["sma5", "ema10"] |
| 384 | default_conf['indicators2'] = ["macd"] |
| 385 | default_conf['pairs'] = ["ETH/BTC", "LTC/BTC"] |
| 386 | |
| 387 | candle_mock = MagicMock() |
| 388 | store_mock = MagicMock() |
| 389 | mocker.patch.multiple( |
| 390 | "freqtrade.plot.plotting", |
| 391 | generate_candlestick_graph=candle_mock, |
| 392 | store_plot_file=store_mock |
| 393 | ) |
| 394 | load_and_plot_trades(default_conf) |
| 395 | |
| 396 | # Both mocks should be called once per pair |
| 397 | assert candle_mock.call_count == 2 |
| 398 | assert store_mock.call_count == 2 |
| 399 | |
| 400 | assert candle_mock.call_args_list[0][1]['indicators1'] == ['sma5', 'ema10'] |
| 401 | assert candle_mock.call_args_list[0][1]['indicators2'] == ['macd'] |
| 402 | |
| 403 | assert log_has("End of plotting process. 2 plots generated", caplog) |
| 404 | |
| 405 | |
| 406 | def test_start_plot_profit(mocker): |
| 407 | aup = mocker.patch("freqtrade.plot.plotting.plot_profit", MagicMock()) |
| 408 | args = [ |
| 409 | "plot-profit", |
| 410 | "--config", "config_bittrex.json.example", |
| 411 | "--pairs", "ETH/BTC" |
| 412 | ] |
| 413 | start_plot_profit(get_args(args)) |
| 414 | |
| 415 | assert aup.call_count == 1 |
| 416 | called_config = aup.call_args_list[0][0][0] |
| 417 | assert "pairs" in called_config |
| 418 | assert called_config['pairs'] == ["ETH/BTC"] |
| 419 | |
| 420 | |
| 421 | def test_start_plot_profit_error(mocker): |
| 422 | |
| 423 | args = [ |
| 424 | 'plot-profit', |
| 425 | '--pairs', 'ETH/BTC' |
| 426 | ] |
| 427 | argsp = get_args(args) |
| 428 | # Make sure we use no config. Details: #2241 |
| 429 | # not resetting config causes random failures if config.json exists |
| 430 | argsp['config'] = [] |
| 431 | with pytest.raises(OperationalException): |
| 432 | start_plot_profit(argsp) |
| 433 | |
| 434 | |
| 435 | def test_plot_profit(default_conf, mocker, testdatadir): |
| 436 | patch_exchange(mocker) |
| 437 | default_conf['trade_source'] = 'file' |
| 438 | default_conf['datadir'] = testdatadir |
| 439 | default_conf['exportfilename'] = testdatadir / 'backtest-result_test_nofile.json' |
| 440 | default_conf['pairs'] = ['ETH/BTC', 'LTC/BTC'] |
| 441 | |
| 442 | profit_mock = MagicMock() |
| 443 | store_mock = MagicMock() |
| 444 | mocker.patch.multiple( |
| 445 | "freqtrade.plot.plotting", |
| 446 | generate_profit_graph=profit_mock, |
| 447 | store_plot_file=store_mock |
| 448 | ) |
| 449 | with pytest.raises(OperationalException, |
| 450 | match=r"No trades found, cannot generate Profit-plot.*"): |
| 451 | plot_profit(default_conf) |
| 452 | |
| 453 | default_conf['exportfilename'] = testdatadir / "backtest-result_test.json" |
| 454 | |
| 455 | plot_profit(default_conf) |
| 456 | |
| 457 | # Plot-profit generates one combined plot |
| 458 | assert profit_mock.call_count == 1 |
| 459 | assert store_mock.call_count == 1 |
| 460 | |
| 461 | assert profit_mock.call_args_list[0][0][0] == default_conf['pairs'] |
| 462 | assert store_mock.call_args_list[0][1]['auto_open'] is True |
| 463 | |
| 464 | |
| 465 | @pytest.mark.parametrize("ind1,ind2,plot_conf,exp", [ |
| 466 | # No indicators, use plot_conf |
| 467 | ([], [], {}, |
| 468 | {'main_plot': {'sma': {}, 'ema3': {}, 'ema5': {}}, |
| 469 | 'subplots': {'Other': {'macd': {}, 'macdsignal': {}}}}), |
| 470 | # use indicators |
| 471 | (['sma', 'ema3'], ['macd'], {}, |
| 472 | {'main_plot': {'sma': {}, 'ema3': {}}, 'subplots': {'Other': {'macd': {}}}}), |
| 473 | # only main_plot - adds empty subplots |
| 474 | ([], [], {'main_plot': {'sma': {}}}, |
| 475 | {'main_plot': {'sma': {}}, 'subplots': {}}), |
| 476 | # Main and subplots |
| 477 | ([], [], {'main_plot': {'sma': {}}, 'subplots': {'RSI': {'rsi': {'color': 'red'}}}}, |
| 478 | {'main_plot': {'sma': {}}, 'subplots': {'RSI': {'rsi': {'color': 'red'}}}}), |
| 479 | # no main_plot, adds empty main_plot |
| 480 | ([], [], {'subplots': {'RSI': {'rsi': {'color': 'red'}}}}, |
| 481 | {'main_plot': {}, 'subplots': {'RSI': {'rsi': {'color': 'red'}}}}), |
| 482 | # indicator 1 / 2 should have prevelance |
| 483 | (['sma', 'ema3'], ['macd'], |
| 484 | {'main_plot': {'sma': {}}, 'subplots': {'RSI': {'rsi': {'color': 'red'}}}}, |
| 485 | {'main_plot': {'sma': {}, 'ema3': {}}, 'subplots': {'Other': {'macd': {}}}} |
| 486 | ), |
| 487 | # indicator 1 - overrides plot_config main_plot |
| 488 | (['sma', 'ema3'], [], |
| 489 | {'main_plot': {'sma': {}}, 'subplots': {'RSI': {'rsi': {'color': 'red'}}}}, |
| 490 | {'main_plot': {'sma': {}, 'ema3': {}}, 'subplots': {'RSI': {'rsi': {'color': 'red'}}}} |
| 491 | ), |
| 492 | # indicator 2 - overrides plot_config subplots |
| 493 | ([], ['macd', 'macd_signal'], |
| 494 | {'main_plot': {'sma': {}}, 'subplots': {'RSI': {'rsi': {'color': 'red'}}}}, |
| 495 | {'main_plot': {'sma': {}}, 'subplots': {'Other': {'macd': {}, 'macd_signal': {}}}} |
| 496 | ), |
| 497 | ]) |
| 498 | def test_create_plotconfig(ind1, ind2, plot_conf, exp): |
| 499 | |
| 500 | res = create_plotconfig(ind1, ind2, plot_conf) |
| 501 | assert 'main_plot' in res |
| 502 | assert 'subplots' in res |
| 503 | assert isinstance(res['main_plot'], dict) |
| 504 | assert isinstance(res['subplots'], dict) |
| 505 | |
| 506 | assert res == exp |