def get_prices(numdays=1000): now = Timestamp.utcnow() bundle = load('quantopian-quandl', os.environ, now) all_assets = bundle.asset_finder.retrieve_all(bundle.asset_finder.sids) symbols = set( str(asset.symbol) for asset in bundle.asset_finder.retrieve_all( bundle.asset_finder.equities_sids)) quandl_data = DataPortal( asset_finder=bundle.asset_finder, trading_calendar=get_calendar('NYSE'), first_trading_day=bundle.equity_daily_bar_reader.first_trading_day, equity_minute_reader=bundle.equity_minute_bar_reader, equity_daily_reader=bundle.equity_daily_bar_reader, adjustment_reader=bundle.adjustment_reader) end_dt = pd.Timestamp('2018-03-31', tz='utc') quandl_symbols = [] for ticker in symbols: try: quandl_symbols.append( quandl_data.asset_finder.lookup_symbol(ticker, end_dt)) except: continue quandl_pricing = quandl_data.get_history_window(quandl_symbols, end_dt, numdays, '1d', 'close', 'daily') quandl_pricing.columns = [c.symbol for c in quandl_pricing.columns] return quandl_pricing
def prices(assets, start, end, field='close', offset=0): """ Get price data for assets between start and end. """ start = trading_date(start) end = trading_date(end) bundle = load_sharadar_bundle() trading_calendar = bundle.equity_daily_bar_reader.trading_calendar if offset > 0: start = trading_calendar.sessions_window(start, -offset)[0] bar_count = trading_calendar.session_distance(start, end) data_portal = DataPortal( bundle.asset_finder, trading_calendar=trading_calendar, first_trading_day=start, equity_daily_reader=bundle.equity_daily_bar_reader, adjustment_reader=bundle.adjustment_reader) df = data_portal.get_history_window(assets=assets, end_dt=end, bar_count=bar_count, frequency='1d', field=field, data_frequency='daily') return df if len(assets) > 1 else df.squeeze()
def test_asset_IPOed_same_day(self): # gotta get some minute data up in here. # add sid 4 for a couple of days minutes = self.trading_calendar.minutes_for_sessions_in_range( self.sim_params.sessions[0], self.sim_params.sessions[5] ) tmp_reader = tmp_bcolz_equity_minute_bar_reader( self.trading_calendar, self.trading_calendar.all_sessions, create_minute_bar_data(minutes, [2]), ) with tmp_reader as reader: data_portal = DataPortal( self.env.asset_finder, self.trading_calendar, first_trading_day=reader.first_trading_day, equity_minute_reader=reader, equity_daily_reader=self.bcolz_equity_daily_bar_reader, adjustment_reader=self.adjustment_reader, ) source = BenchmarkSource( 2, self.env, self.trading_calendar, self.sim_params.sessions, data_portal ) days_to_use = self.sim_params.sessions # first value should be 0.0, coming from daily data self.assertAlmostEquals(0.0, source.get_value(days_to_use[0])) manually_calculated = data_portal.get_history_window( [2], days_to_use[-1], len(days_to_use), "1d", "close", "daily", )[2].pct_change() for idx, day in enumerate(days_to_use[1:]): self.assertEqual( source.get_value(day), manually_calculated[idx + 1] )
def test_asset_IPOed_same_day(self): # gotta get some minute data up in here. # add sid 4 for a couple of days minutes = self.trading_calendar.minutes_for_sessions_in_range( self.sim_params.sessions[0], self.sim_params.sessions[5] ) tmp_reader = tmp_bcolz_equity_minute_bar_reader( self.trading_calendar, self.trading_calendar.all_sessions, create_minute_bar_data(minutes, [2]), ) with tmp_reader as reader: data_portal = DataPortal( self.env.asset_finder, self.trading_calendar, first_trading_day=reader.first_trading_day, equity_minute_reader=reader, equity_daily_reader=self.bcolz_equity_daily_bar_reader, adjustment_reader=self.adjustment_reader, ) source = BenchmarkSource( 2, self.env, self.trading_calendar, self.sim_params.sessions, data_portal ) days_to_use = self.sim_params.sessions # first value should be 0.0, coming from daily data self.assertAlmostEquals(0.0, source.get_value(days_to_use[0])) manually_calculated = data_portal.get_history_window( [2], days_to_use[-1], len(days_to_use), "1d", "close", )[2].pct_change() for idx, day in enumerate(days_to_use[1:]): self.assertEqual( source.get_value(day), manually_calculated[idx + 1] )
def get_window_price(end_date): bundle_name = "custom-csv-bundle" window = 30 # 窗口大小 bundle_data = bundles.load(bundle_name) data_por = DataPortal(bundle_data.asset_finder, get_calendar(calendar_name), bundle_data.equity_daily_bar_reader.first_trading_day, equity_minute_reader=bundle_data.equity_minute_bar_reader, equity_daily_reader=bundle_data.equity_daily_bar_reader, adjustment_reader=bundle_data.adjustment_reader) sym = data_por.asset_finder.lookup_symbol(train_symbol_str, end_date) data = data_por.get_history_window(assets=[sym], end_dt=end_date, bar_count=window, frequency='1d', data_frequency='daily', field="close") close = data.iloc[:, 0].values return close
def calc_dividend_ratios(self, dividends): """ Calculate the ratios to apply to equities when looking back at pricing history so that the price is smoothed over the ex_date, when the market adjusts to the change in equity value due to upcoming dividend. Returns ------- DataFrame A frame in the same format as splits and mergers, with keys - sid, the id of the equity - effective_date, the date in seconds on which to apply the ratio. - ratio, the ratio to apply to backwards looking pricing data. """ if dividends is None or dividends.empty: return pd.DataFrame(np.array( [], dtype=[ ('sid', uint64_dtype), ('effective_date', uint32_dtype), ('ratio', float64_dtype), ], )) pricing_reader = self._equity_daily_bar_reader input_sids = dividends.sid.values unique_sids, sids_ix = np.unique(input_sids, return_inverse=True) dates = pricing_reader.sessions.values start = pd.Timestamp(dates[0], tz='UTC') end = pd.Timestamp(dates[-1], tz='UTC') calendar = self._equity_daily_bar_reader.trading_calendar data_portal = DataPortal(self._asset_finder, trading_calendar=calendar, first_trading_day=start, equity_daily_reader=self._equity_daily_bar_reader, adjustment_reader= SQLiteAdjustmentReader(self._filename)) close = data_portal.get_history_window(assets=unique_sids, end_dt=end, bar_count=calendar.session_distance(start, end), frequency='1d', field='close', data_frequency='daily').values date_ix = np.searchsorted(dates, dividends.ex_date.values) mask = date_ix > 0 date_ix = date_ix[mask] sids_ix = sids_ix[mask] input_dates = dividends.ex_date.values[mask] # subtract one day to get the close on the day prior to the merger previous_close = close[date_ix - 1, sids_ix] input_sids = input_sids[mask] amount = dividends.amount.values[mask] ratio = 1.0 - amount / previous_close non_nan_ratio_mask = ~np.isnan(ratio) for ix in np.flatnonzero(~non_nan_ratio_mask): ex_date = pd.Timestamp(input_dates[ix], tz='UTC') start_date = self._asset_finder.retrieve_asset(input_sids[ix]).start_date if ex_date != start_date: log.warn( "Couldn't compute ratio for dividend" " sid={sid}, ex_date={ex_date:%Y-%m-%d}, start_date={start_date:%Y-%m-%d}, amount={amount:.3f}", sid=input_sids[ix], ex_date=ex_date, amount=amount[ix], start_date=start_date ) valid_ratio_mask = non_nan_ratio_mask > 0 for ix in np.flatnonzero(~valid_ratio_mask): log.warn( "Dividend ratio <= 0 for dividend" " sid={sid}, ex_date={ex_date:%Y-%m-%d}, amount={amount:.3f}", sid=input_sids[ix], ex_date=pd.Timestamp(input_dates[ix]), amount=amount[ix], ) return pd.DataFrame({ 'sid': input_sids[valid_ratio_mask], 'effective_date': input_dates[valid_ratio_mask], 'ratio': ratio[valid_ratio_mask], })
def get_benchmark_returns(symbol, bundle_name="quandl-eod", calendar_name="NYSE"): """Use the zipline data portal to return benchmark data Parameters ---------- symbol : str Benchmark symbol string bundle_name : str The name of the zipline bundle to look for data in calendar_name : str The calendar that returns the benchmark prices Returns ------- returns : """ calendar = get_calendar(calendar_name) bundle_data = bundles.load(bundle_name) start_date = pd.Timestamp("1990-01-03", tz="UTC") end_date = pd.Timestamp("today", tz="UTC") bar_count = calendar.session_distance(start_date, end_date) portal = DataPortal( bundle_data.asset_finder, calendar, bundle_data.equity_daily_bar_reader.first_trading_day, equity_minute_reader=bundle_data.equity_minute_bar_reader, equity_daily_reader=bundle_data.equity_daily_bar_reader, adjustment_reader=bundle_data.adjustment_reader, ) prices = portal.get_history_window( assets=[portal.asset_finder.lookup_symbol(symbol, end_date)], end_dt=end_date, bar_count=bar_count, frequency="1d", data_frequency="daily", field="close", ) prices.columns = [symbol] # # api_key = os.environ.get('IEX_API_KEY') # if api_key is None: # raise ValueError( # "Please set your IEX_API_KEY environment variable and retry." # ) # r = requests.get( # "https://cloud.iexapis.com/stable/stock/{}/chart/5y?token={}".format(symbol, api_key) # ) # # if r.status_code != 200: # path = loader.get_data_filepath(loader.get_benchmark_filename(symbol)) # df = pd.read_csv(path, names=["date", "return"]) # df.index = pd.DatetimeIndex(df["date"], tz="UTC") # return df["return"] # # data = r.json() # # df = pd.DataFrame(data) # # df.index = pd.DatetimeIndex(df["date"]) # df = df["close"] return prices.sort_index().dropna().pct_change(1).iloc[1:]
# Define the function for the get_loader parameter def choose_loader(column): if column not in USEquityPricing.columns: raise Exception('Column not in USEquityPricing') return pricing_loader # Set the trading calendar trading_calendar = get_calendar('NYSE') start_date = pd.to_datetime('2020-08-26', utc=True) end_date = pd.to_datetime('2020-09-02', utc=True) bar_count = trading_calendar.session_distance(start_date, end_date) # Create a data portal data_portal = DataPortal(bundle.asset_finder, trading_calendar=trading_calendar, first_trading_day=start_date, equity_daily_reader=bundle.equity_daily_bar_reader, adjustment_reader=bundle.adjustment_reader) equity = bundle.asset_finder.lookup_symbol("AAPL", end_date) history = data_portal.get_history_window(assets=[equity], end_dt=end_date, bar_count=bar_count, frequency='1d', field='close', data_frequency='daily') print(history)