def _create_data_generator(self, source_filter): """ Create a merged data generator using the sources and transforms attached to this algorithm. ::source_filter:: is a method that receives events in date sorted order, and returns True for those events that should be processed by the zipline, and False for those that should be skipped. """ benchmark_return_source = [ Event({'dt': ret.date, 'returns': ret.returns, 'type': zipline.protocol.DATASOURCE_TYPE.BENCHMARK, 'source_id': 'benchmarks'}) for ret in trading.environment.benchmark_returns if ret.date.date() >= self.sim_params.period_start.date() and ret.date.date() <= self.sim_params.period_end.date() ] date_sorted = date_sorted_sources(*self.sources) if source_filter: date_sorted = ifilter(source_filter, date_sorted) with_tnfms = sequential_transforms(date_sorted, *self.transforms) with_alias_dt = alias_dt(with_tnfms) with_benchmarks = date_sorted_sources(benchmark_return_source, with_alias_dt) # Group together events with the same dt field. This depends on the # events already being sorted. return groupby(with_benchmarks, attrgetter('dt'))
def _create_data_generator(self, source_filter, sim_params): """ Create a merged data generator using the sources and transforms attached to this algorithm. ::source_filter:: is a method that receives events in date sorted order, and returns True for those events that should be processed by the zipline, and False for those that should be skipped. """ benchmark_return_source = [ Event({'dt': ret.date, 'returns': ret.returns, 'type': zipline.protocol.DATASOURCE_TYPE.BENCHMARK, 'source_id': 'benchmarks'}) for ret in trading.environment.benchmark_returns if ret.date.date() >= sim_params.period_start.date() and ret.date.date() <= sim_params.period_end.date() ] date_sorted = date_sorted_sources(*self.sources) if source_filter: date_sorted = ifilter(source_filter, date_sorted) with_tnfms = sequential_transforms(date_sorted, *self.transforms) with_alias_dt = alias_dt(with_tnfms) with_benchmarks = date_sorted_sources(benchmark_return_source, with_alias_dt) # Group together events with the same dt field. This depends on the # events already being sorted. return groupby(with_benchmarks, attrgetter('dt'))
def _create_data_generator(self, source_filter, sim_params=None): """ Create a merged data generator using the sources and transforms attached to this algorithm. ::source_filter:: is a method that receives events in date sorted order, and returns True for those events that should be processed by the zipline, and False for those that should be skipped. """ if sim_params is None: sim_params = self.sim_params if self.benchmark_return_source is None: env = trading.environment if sim_params.data_frequency == 'minute' or \ sim_params.emission_rate == 'minute': def update_time(date): return env.get_open_and_close(date)[1] else: def update_time(date): return date benchmark_return_source = [ Event({'dt': update_time(dt), 'returns': ret, 'type': zipline.protocol.DATASOURCE_TYPE.BENCHMARK, 'source_id': 'benchmarks'}) for dt, ret in trading.environment.benchmark_returns.iteritems() if dt.date() >= sim_params.period_start.date() and dt.date() <= sim_params.period_end.date() ] else: benchmark_return_source = self.benchmark_return_source date_sorted = date_sorted_sources(*self.sources) if source_filter: date_sorted = filter(source_filter, date_sorted) with_tnfms = sequential_transforms(date_sorted, *self.transforms) with_benchmarks = date_sorted_sources(benchmark_return_source, with_tnfms) # Group together events with the same dt field. This depends on the # events already being sorted. return groupby(with_benchmarks, attrgetter('dt'))
def _create_generator(self, environment): """ Create a basic generator setup using the sources and transforms attached to this algorithm. """ self.date_sorted = date_sorted_sources(*self.sources) self.with_tnfms = sequential_transforms(self.date_sorted, *self.transforms) self.trading_client = tsc(self, environment) transact_method = transact_partial(self.slippage, self.commission) self.set_transact(transact_method) return self.trading_client.simulate(self.with_tnfms)
def _create_data_generator(self, source_filter, sim_params=None): """ Create a merged data generator using the sources and transforms attached to this algorithm. ::source_filter:: is a method that receives events in date sorted order, and returns True for those events that should be processed by the zipline, and False for those that should be skipped. """ if sim_params is None: sim_params = self.sim_params if self.benchmark_return_source is None: env = trading.environment if (sim_params.data_frequency == 'minute' or sim_params.emission_rate == 'minute'): update_time = lambda date: env.get_open_and_close(date)[1] else: update_time = lambda date: date benchmark_return_source = [ Event({'dt': update_time(dt), 'returns': ret, 'type': zipline.protocol.DATASOURCE_TYPE.BENCHMARK, 'source_id': 'benchmarks'}) for dt, ret in trading.environment.benchmark_returns.iteritems() if dt.date() >= sim_params.period_start.date() and dt.date() <= sim_params.period_end.date() ] else: benchmark_return_source = self.benchmark_return_source date_sorted = date_sorted_sources(*self.sources) if source_filter: date_sorted = filter(source_filter, date_sorted) with_tnfms = sequential_transforms(date_sorted, *self.transforms) with_benchmarks = date_sorted_sources(benchmark_return_source, with_tnfms) # Group together events with the same dt field. This depends on the # events already being sorted. return groupby(with_benchmarks, attrgetter('dt'))
def _create_generator(self, environment): """ Create a basic generator setup using the sources and transforms attached to this algorithm. """ self.date_sorted = date_sorted_sources(*self.sources) self.with_tnfms = sequential_transforms(self.date_sorted, *self.transforms) # Group together events with the same dt field. This depends on the # events already being sorted. self.grouped_by_date = groupby(self.with_tnfms, attrgetter('dt')) self.trading_client = tsc(self, environment) transact_method = transact_partial(self.slippage, self.commission) self.set_transact(transact_method) return self.trading_client.simulate(self.grouped_by_date)
def _create_data_generator(self, source_filter): """ Create a merged data generator using the sources and transforms attached to this algorithm. ::source_filter:: is a method that receives events in date sorted order, and returns True for those events that should be processed by the zipline, and False for those that should be skipped. """ date_sorted = date_sorted_sources(*self.sources) if source_filter: date_sorted = ifilter(source_filter, date_sorted) with_tnfms = sequential_transforms(date_sorted, *self.transforms) with_alias_dt = alias_dt(with_tnfms) # Group together events with the same dt field. This depends on the # events already being sorted. return groupby(with_alias_dt, attrgetter('dt'))