def handle_data(self, data, *args, **kwargs): """ Point of entry. Process an event frame. """ # extract dates dts = [event.datetime for event in data._data.itervalues()] # we have to provide the event with a dt. This is only for # checking if the event is outside the window or not so a # couple of seconds shouldn't matter. We don't add it to # the data parameter, because it would mix dt with the # sid keys. event = Event() event.dt = max(dts) event.data = {k: v.__dict__ for k, v in data._data.iteritems() # Need to check if data has a 'length' to filter # out sids without trade data available. # TODO: expose more of 'no trade available' # functionality to zipline if len(v)} # only modify the trailing window if this is # a new event. This is intended to make handle_data # idempotent. if self.last_dt < event.dt: self.updated = True self._append_to_window(event) else: self.updated = False # return newly computed or cached value return self.get_transform_value(*args, **kwargs)
def create_commission(sid, value, datetime): txn = Event({ 'dt': datetime, 'type': DATASOURCE_TYPE.COMMISSION, 'cost': value, 'sid': sid }) return txn
def create_txn(sid, price, amount, datetime): txn = Event({ 'sid': sid, 'amount': amount, 'dt': datetime, 'price': price, 'type': DATASOURCE_TYPE.TRANSACTION }) return txn
def create_dividend(sid, payment, declared_date, ex_date, pay_date): div = Event({ 'sid': sid, 'gross_amount': payment, 'net_amount': payment, 'dt': declared_date.replace(hour=0, minute=0, second=0, microsecond=0), 'ex_date': ex_date.replace(hour=0, minute=0, second=0, microsecond=0), 'pay_date': pay_date.replace(hour=0, minute=0, second=0, microsecond=0), 'type': DATASOURCE_TYPE.DIVIDEND }) return div
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. """ if self.benchmark_return_source is None: benchmark_return_source = [ Event({ 'dt': dt, 'returns': ret, 'type': alephnull.protocol.DATASOURCE_TYPE.BENCHMARK, 'source_id': 'benchmarks' }) for dt, ret in trading.environment.benchmark_returns.iterkv() 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 = 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_trade(sid, price, amount, datetime, source_id="test_factory"): trade = Event() trade.source_id = source_id trade.type = DATASOURCE_TYPE.TRADE trade.sid = sid trade.dt = datetime trade.price = price trade.close_price = price trade.open_price = price trade.low = price * .95 trade.high = price * 1.05 trade.volume = amount return trade
def mapped_data(self): for row in self.raw_data: yield Event(self.apply_mapping(row))