stefan-jansen / machine-learning-for-trading

Code for Machine Learning for Algorithmic Trading, 2nd edition.
https://ml4trading.io
12.57k stars 4.03k forks source link

Chapter 8 02_backtesting_with_zipline #305

Closed lyubomir-vasilev closed 11 months ago

lyubomir-vasilev commented 11 months ago
I am using the code from Chapter 8 of the ML4T book by Stefan Jansen. The file I am using is 02_backtesting_with_zipline

Unfortunately, when running this code:

start = time()
results = run_algorithm(start=start_date,
                       end=end_date,
                       initialize=initialize,
                       before_trading_start=before_trading_start,
                       capital_base=1e6,
                       data_frequency='daily',
                       bundle='quandl',
                       custom_loader=signal_loader) # need to modify zipline

print('Duration: {:.2f}s'.format(time() - start))

` I get this error:

``AttributeError                            Traceback (most recent call last)
c:\Lyubo\Python lessons\Machine-Learning-for-Algorithmic-Trading-Second-Edition-master\08_ml4t_workflow\04_ml4t_workflow_with_zipline\02_backtesting_with_zipline.ipynb Cell 55 in ()
      1 start = time()
----> 2 results = run_algorithm(start=start_date,
      3                        end=end_date,
      4                        initialize=initialize,
      5                        before_trading_start=before_trading_start,
      6                        capital_base=1e6,
      7                        data_frequency='daily',
      8                        bundle='quandl',
      9                        custom_loader=signal_loader) # need to modify zipline
     11 print('Duration: {:.2f}s'.format(time() - start))

File c:\ProgramData\anaconda3\envs\py38class\lib\site-packages\zipline\utils\run_algo.py:397, in run_algorithm(start, end, initialize, capital_base, handle_data, before_trading_start, analyze, data_frequency, bundle, bundle_timestamp, trading_calendar, metrics_set, benchmark_returns, default_extension, extensions, strict_extensions, environ, custom_loader, blotter)
    393 load_extensions(default_extension, extensions, strict_extensions, environ)
    395 benchmark_spec = BenchmarkSpec.from_returns(benchmark_returns)
--> 397 return _run(
    398     handle_data=handle_data,
    399     initialize=initialize,
    400     before_trading_start=before_trading_start,
    401     analyze=analyze,
    402     algofile=None,
    403     algotext=None,
    404     defines=(),
...
   5987 ):
   5988     return self[name]
-> 5989 return object.__getattribute__(self, name)

AttributeError: 'Series' object has no attribute 'append'

``

I followed the link to the file that gives the error:


File c:\ProgramData\anaconda3\envs\py38class\lib\site-packages\zipline\utils\run_algo.py:397, in run_algorithm(start, end, initialize, capital_base, handle_data, before_trading_start, analyze, data_frequency, bundle, bundle_timestamp, trading_calendar, metrics_set, benchmark_returns, default_extension, extensions, strict_extensions, environ, custom_loader, blotter)

However, I can't see append anywhere in the function itself, so I guess the funciton is dependent on another function in a different package. 

Does anyone know how I can fix this? 

I tried to change the code in the source and basically changed the code of any .py file in the pacakge which has append in it but that didn't work either. 
stefan-jansen commented 11 months ago

.append() has been removed in pandas 2.0, please adjust the code to use pd.concat instead docs. A simple word search in the referenced file surfaces two instances where .append() is used.