Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Get multiple fields in Prices function in research notebooks

I would like to get both 'open' and 'close' price data using a single call to the prices function in my research notebook. Can anyone advise how to do this? Or is the only solution to use Pipelines?

In the reference docs, the prices function documentation says the following:

prices(assets, start, end, frequency='daily', price_field='price', symbol_reference_date=None, start_offset=0)  
...

price_field ({'open', 'high', 'low', 'close', 'price'}, optional) – Price field to load. ‘price’ produces the same data as ‘close’, but forward-fills over missing data. Default is ‘price’.  

But when I use the following code:

price_data_close = prices(  
    assets=asset,  
    start=period_start,  
    end=period_end,  
    price_field={'close','open'},  
    frequency=freq)  

I get this error:

/build/src/qexec_repo/zipline_repo/zipline/utils/input_validation.pyc
in _check(func, argname, argvalue)
444 'funcname': get_funcname(func),
445 'argname': argname,
--> 446 'actual': actual(argvalue),
447 },
448 )

ValueError: qexec.research.api.prices() expected a value in
('close_price', 'high', 'low', 'open_price', 'price') for argument
'price_field', but got set(['close_price', 'open_price']) instead.

What's going wrong?

4 responses

Wrong syntax. Use brackets rather than curly braces. Not this

    price_field={'close','open'},  

But rather this

    price_field=['close','open'],  

I took your suggestion and tried the following:

asset = [symbols('SPY'),]  
period_start = '2013-06-01'  
period_end = '2018-06-01'  
freq = 'daily'

price_data = prices(  
    assets=asset,  
    start=period_start,  
    end=period_end,  
    price_field=['close','open'],  
    frequency=freq)  

Now, I get the following errors:

/usr/local/lib/python2.7/dist-packages/toolz/functoolz.pyc in
call(self, *args, **kwargs)
281 def call(self, *args, **kwargs):
282 try:
--> 283 return self._partial(*args, **kwargs)
284 except TypeError as exc:
285 if self._should_curry(args, kwargs, exc):

TypeError: unhashable type: 'list'

Any other suggestions?

oops, my mistake. I didn't notice you were using the 'prices' method. I thought it was the 'get_pricing' method.

The 'pricing' method only returns a single field. One cannot get multiple fields (ie 'open' and 'close') at the same time. That is what's causing the error. Just put a single value in for 'price_field' and it will work. If you want to return multiple fields use 'get_pricing' like this.

spy_prices = get_pricing(symbols = asset,  
                        start_date = period_start,  
                        end_date = period_end,  
                        frequency = freq,  
                        fields=['open_price', 'close_price']  
                        )

See attached notebook.

Thank you very much!