zipline icon indicating copy to clipboard operation
zipline copied to clipboard

Ingest custom data as a CSV file

Open niklasamslgruber opened this issue 8 years ago • 22 comments

Dear Zipline Maintainers, I have some equity price data (CSV file) in the OHLCV + Dividends + Splits format and tried to ingest the data using zipline ingest -b bundle_name. However, I always get the following error code:

Traceback (most recent call last):
  File "/Users/niklasamslgruber/anaconda3/envs/py35/bin/zipline", line 11, in <module>
    sys.exit(main())
  File "/Users/niklasamslgruber/anaconda3/envs/py35/lib/python3.5/site-packages/click/core.py", line 722, in __call__
    return self.main(*args, **kwargs)
  File "/Users/niklasamslgruber/anaconda3/envs/py35/lib/python3.5/site-packages/click/core.py", line 697, in main
    rv = self.invoke(ctx)
  File "/Users/niklasamslgruber/anaconda3/envs/py35/lib/python3.5/site-packages/click/core.py", line 1066, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/Users/niklasamslgruber/anaconda3/envs/py35/lib/python3.5/site-packages/click/core.py", line 895, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/Users/niklasamslgruber/anaconda3/envs/py35/lib/python3.5/site-packages/click/core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "/Users/niklasamslgruber/anaconda3/envs/py35/lib/python3.5/site-packages/zipline/__main__.py", line 331, in ingest
    show_progress,
  File "/Users/niklasamslgruber/anaconda3/envs/py35/lib/python3.5/site-packages/zipline/data/bundles/core.py", line 451, in ingest
    pth.data_path([name, timestr], environ=environ),
  File "/Users/niklasamslgruber/anaconda3/envs/py35/lib/python3.5/site-packages/zipline/data/bundles/csvdir.py", line 94, in ingest
    self.csvdir)
  File "/Users/niklasamslgruber/anaconda3/envs/py35/lib/python3.5/site-packages/zipline/data/bundles/csvdir.py", line 120, in csvdir_bundle
    raise ValueError("%s is not a directory" % csvdir)
ValueError: /Users/niklasamslgruber/Desktop/Daedalus/csvdir/daily/WCH_01.05.18_21.25.csv is not a directory

I registered the bundle in the .zipline/extension.py file:

import pandas as pd
from zipline.data.bundles import register
from zipline.data.bundles.csvdir import csvdir_equities

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

register(
    'custom1',
    csvdir_equities(
        ['daily'],
        "/Users/niklasamslgruber/Desktop/Daedalus/csvdir/daily/WCH_01.05.18_21.25.csv"))

I hope you can help me!

Sincerely, Niklas

niklasamslgruber avatar May 01 '18 19:05 niklasamslgruber

Your directory to your daily data should be:

"/Users/niklasamslgruber/Desktop/Daedalus/csvdir"

From the Zipline docs:

And then we can register() our bundle, and pass the location of the directory 
in which our .csv files exist

I figured the word directory would be clear but maybe that's not clear enough?

freddiev4 avatar May 01 '18 19:05 freddiev4

I still get the same error:

ValueError: /Users/niklasamslgruber/Desktop/Daedalus/csvdir/WCH_01.05.18_21.25.csv is not a directory

niklasamslgruber avatar May 01 '18 19:05 niklasamslgruber

Hmm.. not sure why that piece of code in csvdir would be looking at your csv file to check if it's a directory.

This is what my custom bundle looks like right now:

register(
    'custom-csvdir-bundle',
    csvdir_equities(
        ["daily"],
        '/Users/freddiev4/Documents/csvdir'
    ),
    start_session=start_session,
    end_session=end_session
)

freddiev4 avatar May 01 '18 19:05 freddiev4

Changed that and not get this error:

Traceback (most recent call last):
  File "/Users/niklasamslgruber/anaconda3/envs/py35/bin/zipline", line 11, in <module>
    sys.exit(main())
  File "/Users/niklasamslgruber/anaconda3/envs/py35/lib/python3.5/site-packages/click/core.py", line 722, in __call__
    return self.main(*args, **kwargs)
  File "/Users/niklasamslgruber/anaconda3/envs/py35/lib/python3.5/site-packages/click/core.py", line 697, in main
    rv = self.invoke(ctx)
  File "/Users/niklasamslgruber/anaconda3/envs/py35/lib/python3.5/site-packages/click/core.py", line 1066, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/Users/niklasamslgruber/anaconda3/envs/py35/lib/python3.5/site-packages/click/core.py", line 895, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/Users/niklasamslgruber/anaconda3/envs/py35/lib/python3.5/site-packages/click/core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "/Users/niklasamslgruber/anaconda3/envs/py35/lib/python3.5/site-packages/zipline/__main__.py", line 331, in ingest
    show_progress,
  File "/Users/niklasamslgruber/anaconda3/envs/py35/lib/python3.5/site-packages/zipline/data/bundles/core.py", line 400, in ingest
    end_session,
  File "/Users/niklasamslgruber/anaconda3/envs/py35/lib/python3.5/site-packages/zipline/data/us_equity_pricing.py", line 201, in __init__
    "Start session %s is invalid!" % start_session
ValueError: Start session 2017-01-01 00:00:00+00:00 is invalid!

The CSV looks like that:

Date,Open,High,Low,Close,Volume,Dividend,Split
2017-01-02,98.84,101.15,98.5,100.2,63638.0,0,0
2017-01-03,100.45,103.2,100.35,102.7,146035.0,0,0
2017-01-04,102.7,102.95,100.9,101.75,114505.0,0,0
2017-01-05,101.6,102.35,101.25,102.0,80067.0,0,0

niklasamslgruber avatar May 01 '18 20:05 niklasamslgruber

Is it fair to guess that, based off your WCH csv and your location, you're looking at German stocks?

If that's the case, then that means there are probably dates in your csv file where stocks won't line up with the NYSE calendar, which is the default calendar. If that's the case, you're going to want to look into building your own Trading Calendar.

The NYSE does not run on January 1st, which is why 2017-01-01 00:00:00+00:00 is not a valid start date for that.

freddiev4 avatar May 01 '18 20:05 freddiev4

Yes, I'm using german stocks. I just downloaded the data for AAPL from Yahoo Finance and the error still exists

niklasamslgruber avatar May 01 '18 20:05 niklasamslgruber

What happens if you change your start_session to 2017-1-2?

freddiev4 avatar May 01 '18 20:05 freddiev4

Then I get the first error again:

ValueError: /Users/niklasamslgruber/Desktop/Daedalus/csvdir/AAPL.csv is not a directory

niklasamslgruber avatar May 01 '18 20:05 niklasamslgruber

Can you post the first 5 or 10 rows of the AAPL.csv? I'm just going to try and reproduce this; that's strange to me that you're seeing that error by only adjusting the start_session.

freddiev4 avatar May 01 '18 20:05 freddiev4

Date;Open;High;Low;Close;Volume;Dividend;Split
2017-01-03;115.800003;116.330002;114.760002;116.150002;28781900;0;0
2017-01-04;115.849998;116.510002;115.750000;116.019997;21118100;0;0
2017-01-05;115.919998;116.860001;115.809998;116.610001;22193600;0;0
2017-01-06;116.779999;118.160004;116.470001;117.910004;31751900;0;0
2017-01-09;117.949997;119.430000;117.940002;118.989998;33561900;0;0
2017-01-10;118.769997;119.379997;118.300003;119.110001;24462100;0;0
2017-01-11;118.739998;119.930000;118.599998;119.750000;27588600;0;0
2017-01-12;118.900002;119.300003;118.209999;119.250000;27086200;0;0
2017-01-13;119.110001;119.620003;118.809998;119.040001;26111900;0;0
2017-01-17;118.339996;120.239998;118.220001;120.000000;34439800;0;0

niklasamslgruber avatar May 01 '18 20:05 niklasamslgruber

So I was able to reproduce the start_session error, but not the ValueError.

Here's my setup, which worked with AAPL.csv:

My AAPL.csv file is located in: '/Users/freddiev4/Documents/csvdir/daily/

My ~/.zipline/extensions.py

import pandas as pd
from zipline.data.bundles import register
from zipline.data.bundles.csvdir import csvdir_equities

start_session = pd.Timestamp('2017-1-3', tz='utc')
end_session = pd.Timestamp('2017-1-17', tz='utc')

register(
    'niklas-bundle',
    csvdir_equities(
        ["daily"],
        '/Users/freddiev4/Documents/csvdir'
    ),
    start_session=start_session,
    end_session=end_session
)

Some things to keep in mind:

  • The start_session date needs to be aligned with the first date that appears in the csv; either the same date or after that date; in this case 2017-1-3 or between the start and end session
  • The names of the columns need to all be lowercase (should probably just open a PR for csvdir to ignore letter cases)

Also, the csv files need to be comma-separated, hence the name csv. So the data sample you posted above will cause an error because each data is separated by ;

freddiev4 avatar May 01 '18 20:05 freddiev4

Edit to comment above:

The names of the columns need to all be lowercase (should probably just open a PR for csvdir to ignore letter cases)

The error I saw actually comes from us_equity_pricing.py / pandas:

 File "/Users/freddiev4/Documents/quantopian/zipline/zipline/data/us_equity_pricing.py", line 251, in <genexpr>
    for sid, df in data
  File "/Users/freddiev4/Documents/quantopian/zipline/zipline/data/us_equity_pricing.py", line 416, in to_ctable
    # we already have a ctable so do nothing
  File "/Users/freddiev4/Documents/quantopian/zipline/zipline/data/us_equity_pricing.py", line 419, in to_ctable
    winsorise_uint32(raw_data, invalid_data_behavior, 'volume', *OHLC)
  File "/Users/freddiev4/Documents/quantopian/zipline/zipline/data/us_equity_pricing.py", line 121, in winsorise_uint32
    
  File "/Users/freddiev4/Documents/quantopian/zipline/zipline/data/us_equity_pricing.py", line 137, in winsorise_uint32
    mask = df[columns] > UINT32_MAX

. . .

    indexer = self.ix._convert_to_indexer(key, axis=1)
  File "/Users/freddiev4/.virtualenvs/zip35/lib/python3.5/site-packages/pandas/core/indexing.py", line 1214, in _convert_to_indexer
    raise KeyError('%s not in index' % objarr[mask])
KeyError: "['volume' 'open' 'high' 'close' 'low'] not in index"

freddiev4 avatar May 01 '18 21:05 freddiev4

@niklay14 Let me know if you get it to work (based off my https://github.com/quantopian/zipline/issues/2162#issuecomment-385788553)

freddiev4 avatar May 01 '18 21:05 freddiev4

So I changed everything (CSV with lowercases and comma-separated, etc.), but I still get this ValueError


 File "/Users/niklasamslgruber/anaconda3/envs/py35/bin/zipline", line 11, in <module>
    sys.exit(main())
  File "/Users/niklasamslgruber/anaconda3/envs/py35/lib/python3.5/site-packages/click/core.py", line 722, in __call__
    return self.main(*args, **kwargs)
  File "/Users/niklasamslgruber/anaconda3/envs/py35/lib/python3.5/site-packages/click/core.py", line 697, in main
    rv = self.invoke(ctx)
  File "/Users/niklasamslgruber/anaconda3/envs/py35/lib/python3.5/site-packages/click/core.py", line 1066, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/Users/niklasamslgruber/anaconda3/envs/py35/lib/python3.5/site-packages/click/core.py", line 895, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/Users/niklasamslgruber/anaconda3/envs/py35/lib/python3.5/site-packages/click/core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "/Users/niklasamslgruber/anaconda3/envs/py35/lib/python3.5/site-packages/zipline/__main__.py", line 331, in ingest
    show_progress,
  File "/Users/niklasamslgruber/anaconda3/envs/py35/lib/python3.5/site-packages/zipline/data/bundles/core.py", line 451, in ingest
    pth.data_path([name, timestr], environ=environ),
  File "/Users/niklasamslgruber/anaconda3/envs/py35/lib/python3.5/site-packages/zipline/data/bundles/csvdir.py", line 94, in ingest
    self.csvdir)
  File "/Users/niklasamslgruber/anaconda3/envs/py35/lib/python3.5/site-packages/zipline/data/bundles/csvdir.py", line 120, in csvdir_bundle
    raise ValueError("%s is not a directory" % csvdir)
ValueError: /Users/niklasamslgruber/Desktop/Daedalus/csvdir/daily/AAPL.csv is not a directory

niklasamslgruber avatar May 01 '18 21:05 niklasamslgruber

Hmm... the only way I can get a ValueError:

    pth.data_path([name, timestr], environ=environ),
  File "/Users/freddiev4/Documents/quantopian/zipline/zipline/data/bundles/csvdir.py", line 94, in ingest
    self.csvdir)
  File "/Users/freddiev4/Documents/quantopian/zipline/zipline/data/bundles/csvdir.py", line 120, in csvdir_bundle
    raise ValueError("%s is not a directory" % csvdir)
ValueError: /Users/freddiev4/Documents/csvdir/daily/AAPL.csv is not a directory

this is if I changed

register(
    'niklas-bundle',
    csvdir_equities(
        ["daily"],
        '/Users/freddiev4/Documents/csvdir'
    ),
    start_session=start_session,
    end_session=end_session
)

to:

register(
    'niklas-bundle',
    csvdir_equities(
        ["daily"],
        '/Users/freddiev4/Documents/csvdir/daily/AAPL.csv' # this line is incorrect
    ),
    start_session=start_session,
    end_session=end_session
)
  1. Can you post your updated extensions.py?
  2. Do you have a CSVDIR environment variable set anywhere? (you shouldn't need one here though)

freddiev4 avatar May 01 '18 21:05 freddiev4

The error is fixed, however I cannot use the data while running run_algorithm. I always get this error: zipline.errors.SymbolNotFound: Symbol 'AAPL' was not found. Do I need to tell zipline that the data in the CSV file is the apple price data or how can I solve this error?

niklasamslgruber avatar May 02 '18 10:05 niklasamslgruber

Just getting to your next question (https://github.com/quantopian/zipline/issues/2172) now. Closing this issue as we solved your ingest problem.

freddiev4 avatar May 04 '18 00:05 freddiev4

Greetings,
I see this issue is closed but found this when I got same error for ingesting data for Indian stocks. Error and source is exactly same as mentioned in https://github.com/quantopian/zipline/issues/2162#issuecomment-385790239. Can you please point me to what else I can try to resolve this issue ?

Sample CSV (head) o/p attached: date,open,high,low,close,volume,dividend,split 2018-04-02,19400.2500,20199.0000,19400.2500,19973.9000,2563,0.0,1.0 2018-04-03,20182.0500,20300.1500,19921.0000,20111.3000,1118,0.0,1.0 2018-04-04,20111.3000,21000.0500,19851.0000,20105.0000,3744,0.0,1.0 2018-04-05,20459.0000,20720.0000,20340.0000,20638.7000,874,0.0,1.0 2018-04-06,20672.0500,20812.0000,20600.0000,20715.8000,772,0.0,1.0 2018-04-09,20999.0000,21000.0000,20286.0500,20484.5000,3109,0.0,1.0 2018-04-10,20310.0000,20750.0000,20310.0000,20663.4500,571,0.0,1.0 2018-04-11,20614.5500,20850.0000,20175.0000,20356.2500,2503,0.0,1.0 2018-04-12,20595.0000,20800.0000,20300.0000,20720.2500,1199,0.0,1.0

Excerpt from ~/.zipline/extension.py as follows:

start_session = pd.Timestamp('2018-4-2', tz='utc')
end_session = pd.Timestamp('2019-10-17', tz='utc')
register(
    'nse_stocks',
     csvdir_equities(
     ['daily'],
     'path-to-csv-files-directory',
  ),
 calendar_name='XBOM', # IN equities
 start_session=start_session,
 end_session=end_session)

Error extract :

  File "/Applications/anaconda3/envs/env_zipline/lib/python3.5/site-packages/zipline/data/us_equity_pricing.py", line 414, in to_ctable
    # we already have a ctable so do nothing
  File "/Applications/anaconda3/envs/env_zipline/lib/python3.5/site-packages/zipline/data/us_equity_pricing.py", line 417, in to_ctable
    winsorise_uint32(raw_data, invalid_data_behavior, 'volume', *OHLC)
  File "/Applications/anaconda3/envs/env_zipline/lib/python3.5/site-packages/zipline/data/us_equity_pricing.py", line 121, in winsorise_uint32

  File "/Applications/anaconda3/envs/env_zipline/lib/python3.5/site-packages/zipline/data/us_equity_pricing.py", line 137, in winsorise_uint32
    mask = df[columns] > UINT32_MAX
  File "/Applications/anaconda3/envs/env_zipline/lib/python3.5/site-packages/pandas/core/frame.py", line 2133, in __getitem__
    return self._getitem_array(key)
  File "/Applications/anaconda3/envs/env_zipline/lib/python3.5/site-packages/pandas/core/frame.py", line 2177, in _getitem_array
    indexer = self.loc._convert_to_indexer(key, axis=1)
  File "/Applications/anaconda3/envs/env_zipline/lib/python3.5/site-packages/pandas/core/indexing.py", line 1269, in _convert_to_indexer
    .format(mask=objarr[mask]))
KeyError: "['volume' 'close' 'open' 'low' 'high'] not in index"

bhushanjawle avatar Oct 18 '19 10:10 bhushanjawle

Hi @freddiev4 , Is it recommended I open a fresh issue for this. Please let me know

bhushanjawle avatar Oct 21 '19 11:10 bhushanjawle

Hello bhushanjawle, I hope the issue might have got resolved. But just in case,check the 'csv' file. Probably the column names would have Upper case letters. Make all these OHLCV columns to lower case and try.. it should fix.

ghost avatar Nov 08 '19 15:11 ghost