Back to Community
Custom data Bundle

hi guys i'm trying construct a bundle data with BR assets. So to do this I'm following the steps presented in this tutorial: Link -https://towardsdatascience.com/backtesting-trading-strategies-using-custom-data-in-zipline-e6fd65eeaca0

I'm using the code below to download from the yahoo and save de data into directory C:/Users/ivo/PycharmProjects/finance/datas/BVSP.csv

def download_csv_data(ticker, start_date, end_date, freq, path):  
    yahoo_financials = YahooFinancials(ticker)

    df = yahoo_financials.get_historical_price_data(start_date, end_date, freq)  
    df = pd.DataFrame(df[ticker]['prices']).drop(['date'], axis=1) \  
             .rename(columns={'formatted_date': 'date'}) \  
             .loc[:, ['date', 'open', 'high', 'low', 'close', 'volume']] \  
        .set_index('date')  
    df.index = pd.to_datetime(df.index)  
    df['dividend'] = 0  
    df['split'] = 1


    # save data to csv for later ingestion  
    df.to_csv(path, header=True, index=True)

    # plot the time series  
    df.close.plot(title='{} prices --- {}:{}'.format(ticker, start_date, end_date))  
    plt.show()


download_csv_data(ticker='^BVSP',  
                  start_date='2019-01-01',  
                  end_date='2019-12-31',  
                  freq='daily',  
                  path='C:/Users/ivo/PycharmProjects/finance/datas/BVSP.csv')  

And I've add a code in the extensions.py file in the directory: C:\Users\ivo\Anaconda3\envs\z35\Lib\site-packages\zipline

I add the code below into extensions.py

import pandas as pd

from zipline.data.bundles import register  
from zipline.data.bundles.csvdir import csvdir_equities

start_session = pd.Timestamp('2019-1-1', tz='utc')  
end_session = pd.Timestamp('2018-12-31', tz='utc')

register(  
    'br_bundle',  
    csvdir_equities(  
        ['daily'],  
        'C:/Users/ivo/PycharmProjects/finance/datas',  
    ),  
    calendar_name='BVMF', # US equities  
    start_session=start_session,  
    end_session=end_session  
)

but, I've tried to ingest the data bundle running this code in python console:

!zipline ingest -b br_bundle  

I got the error below:

Traceback (most recent call last):  
  File "C:\Users\ivo\Anaconda3\envs\z35\Scripts\zipline-script.py", line 11, in <module>  
    load_entry_point('zipline==1.3.0', 'console_scripts', 'zipline')()  
  File "C:\Users\ivo\Anaconda3\envs\z35\lib\site-packages\pkg_resources\__init__.py", line 484, in load_entry_point  
    return get_distribution(dist).load_entry_point(group, name)  
  File "C:\Users\ivo\Anaconda3\envs\z35\lib\site-packages\pkg_resources\__init__.py", line 2707, in load_entry_point  
    return ep.load()  
  File "C:\Users\ivo\Anaconda3\envs\z35\lib\site-packages\pkg_resources\__init__.py", line 2325, in load  
    return self.resolve()  
  File "C:\Users\ivo\Anaconda3\envs\z35\lib\site-packages\pkg_resources\__init__.py", line 2331, in resolve  
    module = __import__(self.module_name, fromlist=['__name__'], level=0)  
  File "C:\Users\ivo\Anaconda3\envs\z35\lib\site-packages\zipline\__init__.py", line 29, in <module>  
    from .utils.run_algo import run_algorithm  
  File "C:\Users\ivo\Anaconda3\envs\z35\lib\site-packages\zipline\utils\run_algo.py", line 27, in <module>  
    from zipline.algorithm import TradingAlgorithm  
  File "C:\Users\ivo\Anaconda3\envs\z35\lib\site-packages\zipline\algorithm.py", line 68, in <module>  
    from zipline.finance.blotter import SimulationBlotter  
  File "C:\Users\ivo\Anaconda3\envs\z35\lib\site-packages\zipline\finance\blotter\__init__.py", line 16, in <module>  
    from .simulation_blotter import SimulationBlotter  
  File "C:\Users\ivo\Anaconda3\envs\z35\lib\site-packages\zipline\finance\blotter\simulation_blotter.py", line 43, in <module>  
    class SimulationBlotter(Blotter):  
TypeError: 'str' object is not callable  

Plese guys, help me. I need this to solve this to do a task of my university.

7 responses

File "C:\Users\ivo\Anaconda3\envs\z35\Scripts\zipline-script.py", line 11, in
load_entry_point('zipline==1.3.0', 'console_scripts', 'zipline')()
... TypeError: 'str' object is not callable

It sounds like load_entry_point returns a string and you try to call it. Try to remove the second pair of paretheses in line 11 of zipline-script.py

load_entry_point('zipline==1.3.0', 'console_scripts', 'zipline')  

instead of

load_entry_point('zipline==1.3.0', 'console_scripts', 'zipline')()  

thanks, but didn't work. Do you or someone have another sugestion to solve this issue?

No, sorry. But could you try to post that link again? There's no url...

I forgot to attach the link in the post. I've already edited the post and attached the link.

Ok, I just tried it out. The tutorial is not very clear

modify the extension.py file located in the zipline directory

I found another file extension.py in another directory called zipline and it was empty - that was the right one. I'm on ubuntu and in my user folder is a hidden folder called .zipline. In your case it might be in "C:\Users\ivo\" (or just search for extesion.py and make sure to include hidden folders).
There should be another extension.py where you can paste your code above. I had to change the dates to

start_session = pd.Timestamp('2019-1-2', tz='utc')  
end_session = pd.Timestamp('2019-12-30', tz='utc')  

In the folder where I put BVSP.csv I had to make a subfolder called 'daily' and moved the csv file there. After that the ingestion seems to have worked.

EDIT:
There were some NaN rows in the csv file, so I filled them with

import pandas as pd  
df = pd.read_csv('daily/BVSP.csv', index_col=0).ffill()  
df.to_csv('daily/BVSP.csv')

and ingested the bundle again. Now I'm working on the problem that zipline doesn't find the benchmark...

You can find a soution for the benchmark problem here, it's near the end of the page:

Understanding the benchmarking process and how to overcome errors if they creep in

I donwloaded historical SPY data from the yahoo finance website, changed the column names to lowercase and in the file benchmarks.py I just replaced

df = pd.DataFrame(data)  

with

df = pd.read_csv('full/path/to/SPY.csv')  

After all that I successfully ran a backtest with BVSP from the br_bundle

I will do what you said tomorrow and I will get the results. Thanks for help me Tentor.