Open carstenf opened 4 years ago
Dear Zipline Maintainers,
this should do the job of get_pricing.
# Create a data portal
data_portal = DataPortal(bundle_data.asset_finder,
trading_calendar = trading_calendar,
first_trading_day = bundle_data.equity_daily_bar_reader.first_trading_day,
equity_daily_reader = bundle_data.equity_daily_bar_reader,
adjustment_reader = bundle_data.adjustment_reader)
def get_pricing(data_portal, trading_calendar, assets, start_date, end_date, field='close'):
# Set the given start and end dates to Timestamps. The frequency string C is used to
# indicate that a CustomBusinessDay DateOffset is used
end_dt = pd.Timestamp(end_date, tz='UTC', freq='C')
start_dt = pd.Timestamp(start_date, tz='UTC', freq='C')`
# Get the locations of the start and end dates
end_loc = trading_calendar.closes.index.get_loc(end_dt)
start_loc = trading_calendar.closes.index.get_loc(start_dt)
# return the historical data for the given window
return data_portal.get_history_window(assets=assets, end_dt=end_dt, bar_count=end_loc - start_loc,
frequency='1d',
field=field,
data_frequency='daily')
could you please include it into the zipline library
best Carsten
hey @carstenf. Apologies for the long delay in responding here.
The get_pricing
function that's available on Quantopian lives in the quantopian codebase, not in zipline. As you've observed, it's mostly a thin wrapper over the DataPortal
class that does some input normalization. The full logic is a bit more complex than your example, mostly because get_pricing
can be called with either sids or tickers, and it will use the AssetFinder
to look up the stock tickers, with various configurable behaviors if the lookup fails.
I think the more general feature request here is probably something like "Make it easier to work interactively with data that's been ingested into zipline." Is that a fair assessment?
hey @ssanderson
no worries.
I was using Alphalens and one important step is to get the pricing....so basically, yes please make live easier :) But It would be very helpful if the code would be easily exchange able between Quantopian platform and zipline. I'm using zipline, because its way faster than your platform but sometimes i use as well the platform.
I found it as well useful to programm the backtest like you suggest in your tutorials with make pipeline. most examples in the internet don't use make pipeline. In that way on can easily exchange them between the platform an zipline.
Thats an argument to include a get_pricing function.
There is as well an other small part missing, please see #2650. I found was well a workaround. Would be cool to implement that as well, just to make things easier.
...and I found a difference between Quantopian and Zipline, I do not understand that... https://www.quantopian.com/posts/differences-between-zipline-and-quantopian I wanted to hedge/get a beta neutral portfolio but it was always way of the benchmark(S&P). But not so much on the platform. Then I made it simpler and simpler. Finally I just was comparing an asset to the same asset as benchmark. I got a significant difference in zipline - but not on Quantopian platform. (You just can use the code on both)
Thank you Carsten
I tried implementing the reverse-engineered version of get_pricing by @carstenf but encountered some errors. This a version that works for me with current zipline master:
import os
from zipline.data import bundles
from zipline.data.data_portal import DataPortal
from trading_calendars import get_calendar
bundle = 'quandl'
environ = os.environ
# Load ingested data bundle
bundle_data = bundles.load(bundle) # , environ, timestamp)
# Create a data portal
data_portal = DataPortal(
bundle_data.asset_finder,
trading_calendar=trading_calendar,
first_trading_day=bundle_data.equity_daily_bar_reader.first_trading_day,
equity_daily_reader=bundle_data.equity_daily_bar_reader,
adjustment_reader=bundle_data.adjustment_reader)
# Implement Quantopian's get_pricing
def get_pricing(assets, start_date, end_date, fields='close', trading_calendar=None):
"""
Approximate Quantopian function `get_pricing` available
in online environment at quantopian.com
"""
if trading_calendar is None:
trading_calendar = get_calendar("NYSE")
# Set the given start and end dates to Timestamps.
end_date = pd.Timestamp(end_date, tz='utc')
start_date = pd.Timestamp(start_date, tz='utc')
# Get the locations of the start and end dates
sessions = trading_calendar.sessions_in_range(start_date, end_date)
bar_count = len(sessions)
# Get identifiers for asset symbols
equities = bundle_data.asset_finder.lookup_symbols(assets, start_date)
# return the historical data for the given window
return data_portal.get_history_window(
assets=equities,
end_dt=end_date,
bar_count=bar_count,
frequency='1d',
field=fields,
data_frequency='daily')
Dear Zipline Maintainers,
I'm trying to build a tear sheet for Alphalens. I use the zipline pipeline to get the data from the bundle. Everthink works nice, unfortunately I do not find the zipline counterpart for the "get_pricing" function from from quantopian.pipeline import Pipeline
pricing_data = get_pricing( symbols=factor_data.index.levels[1], start_date='2014-1-1', end_date='2016-2-1', fields='open_price')
I'm using a workaround, but would prefer to use the build in function to get the price information.
Could someone point me the direction to look for?
best Carsten