kieran-mackle / AutoTrader

A Python-based development platform for automated trading systems - from backtesting to optimisation to livetrading.
https://kieran-mackle.github.io/AutoTrader/
GNU General Public License v3.0
1.01k stars 230 forks source link

Plot Error when running backtest on multiple pairs #75

Closed ziyedbe closed 1 year ago

ziyedbe commented 1 year ago

Describe the bug When I run the backtest on multiple pairs, i get the following error

Traceback (most recent call last):
  File "PATH\Desktop\work\shared\personal\autotrader-demo\runfile.py", line 15, in <module>
    broker = at.run()
             ^^^^^^^^
  File "PATH\AppData\Local\Programs\Python\Python311\Lib\site-packages\autotrader\autotrader.py", line 1142, in run
    self._main()
  File "PATH\AppData\Local\Programs\Python\Python311\Lib\site-packages\autotrader\autotrader.py", line 1660, in _main
    self._trade_update_loop()
  File "PATH\AppData\Local\Programs\Python\Python311\Lib\site-packages\autotrader\autotrader.py", line 2310, in _trade_update_loop
    self.shutdown()
  File "PATH\AppData\Local\Programs\Python\Python311\Lib\site-packages\autotrader\autotrader.py", line 2069, in shutdown
    self.plot_backtest()
  File "PATH\AppData\Local\Programs\Python\Python311\Lib\site-packages\autotrader\autotrader.py", line 1498, in plot_backtest
    portfolio_plot()
  File "PATH\AppData\Local\Programs\Python\Python311\Lib\site-packages\autotrader\autotrader.py", line 1483, in portfolio_plot
    ap._portfolio_plot(self.trade_results)
  File "PATH\AppData\Local\Programs\Python\Python311\Lib\site-packages\autotrader\autoplot.py", line 707, in _portfolio_plot
    self._add_to_autoscale_args(levsource, levfig.y_range)
  File "PATH\AppData\Local\Programs\Python\Python311\Lib\site-packages\autotrader\autoplot.py", line 514, in _add_to_autoscale_args
    if source.data["plot_data"].max() == source.data["plot_data"].min():
       ~~~~~~~~~~~^^^^^^^^^^^^^
KeyError: 'plot_data'

To Reproduce Steps to reproduce the behavior: runfile.py

at = AutoTrader()
at.configure(verbosity=1, show_plot=True, feed='yahoo')#,broker="dydx")
at.add_strategy('ema_crossover')
at.backtest(start='1/1/2022', end='1/10/2022')
at.virtual_account_config(initial_balance=1000, leverage=5,spread=0.00005)
at.run()

ema_crossover.yaml

NAME: 'EMA Crossover'
MODULE: 'ema_crossover'
CLASS: 'EMAcrossOver'
INTERVAL: '1h'
PERIOD: '24h'
RISK_PC: 2
SIZING: 'risk'
PARAMETERS:
  slow_ema: 50
  fast_ema: 21
  RR: 3.0

# Define pairs to monitor
WATCHLIST: ['BTC-USD','ETH-USD','BNB-USD']

Expected behavior Create 3 graphs one for each pair in the watchlist

Version of AutoTrader being used 0.12.0

harsh-phoenix commented 1 year ago

image

even i am facing this issue while plotting MACD, i am unable to resolve error

i have written the code in this

file.py

indicator are define here

from autotrader import autoplot, indicators, autodata, AutoTrader, AutoData import pandas as pd import datetime import yfinance as yf import finta as ta import datetime as dt

data fetching using yfinance

data_yf = yf.download(tickers='AAPL', start=pd.date_range( end=datetime.datetime.today(), periods=360)[0], interval='1d')

data for generating plots

rsi = ta.TA.RSI(data_yf) obv = ta.TA.OBV(data_yf) mfi = ta.TA.MFI(data_yf) cmf = ta.TA.CHAIKIN(data_yf) ema = ta.TA.EMA(data_yf)

macd = ta.TA.MACD(data_yf) macd_co = crossover(macd.MACD, macd.SIGNAL) macd_co_val = cross_values(macd.MACD, macd.SIGNAL, macd_co)

rsi_divergence = indicators.autodetect_divergence( data_yf, rsi, tolerance=15, method=0)

obv_divergence = indicators.autodetect_divergence( data_yf, obv, tolerance=10, method=1)

indicator_dict = {'RSI (14)': {'type': 'RSI', 'data': rsi}, 'obv': {'type': 'below', 'data': obv}, 'mfi': {'type': 'below', 'data': mfi}, 'cmf': {'type': 'below', 'data': cmf},

              'diveregence_Bull': {'type': 'below', 'data': rsi_divergence['regularBull']},
              'diveregence_bear': {'type': 'below', 'data': rsi_divergence['regularBear']},

              'MACD': {'type': 'MACD',

                       'macd': macd.MACD,
                       'signal': macd.SIGNAL,
                       'histogram': macd.MACD-macd.SIGNAL,
                       'crossvals': macd_co_val
                       },

              }

plotting data

ap = autoplot.AutoPlot(data=data_yf) ap.configure(max_indis_below=10, max_indis_over=10) ap.plot(instrument='AAPL', indicators=indicator_dict),