def compute(self):
     '''
     compute the strategy
     @raise CoreError: 
     '''
        
     compute_date = self.first_date
     
     while compute_date <= self.last_date:
         
         # check if it is instrument trading day
         for instrument in self.instruments.values():
             value = instrument.get_close_value(compute_date)
             
             # if there is a value then send trading time event
             if value:
                 trading_time_event = Event(Event.TRADING_TIME_EVENT)
                 trading_time_event.datas['date'] = compute_date
                 trading_time_event.datas['instrument'] = instrument
                 
                 if self.logger.isEnabledFor(logging.DEBUG):
                     self.logger.debug('%s: send event %s'%(compute_date,trading_time_event.type))
                 
                 self.event_dispatcher.dispatch_event(trading_time_event)
         
             
         
         # build valorization event and send it
         valo_event = Event(Event.VALORIZATION_EVENT)
         valo_event.datas['date'] = compute_date
         
         self.event_dispatcher.dispatch_event(valo_event)
         
         compute_date = self.calendar_util.get_next_business_date(compute_date)
 def process_event(self, event):
     '''
     get an event (trading time event), check if date is a rolling day and command
     computation of signals
     @param event: trading time event 
     '''
     event_dispatcher = self.strategy.event_dispatcher
     date = event.datas['date']
     instrument = event.datas['instrument']
     
     # check if it is a portfolio rolling time
     if self._is_rolling(event):
         rolling_event = Event(Event.ROLLING_EVENT)
         # transfer the date from trading time event to rolling event
         rolling_event.datas['date'] = date 
         rolling_event.datas['instrument'] = instrument
         event_dispatcher.dispatch_event(rolling_event)
     else:
         # check if there are some contract to roll
         if instrument.get_reference_month(date):
             back_date = self.strategy.calendar_util.get_previous_quote_date(date, instrument)
             if instrument.get_reference_month(date) != instrument.get_reference_month(back_date):
                 future_rolling_event = Event(Event.ROLLING_EVENT)
                 future_rolling_event.datas['date'] = date
                 future_rolling_event.datas['instrument'] = instrument
         
                 event_dispatcher.dispatch_event(future_rolling_event)
     
     # check condition and send signal event
     signals_active = False
     for block in self.strategy.trading_blocks[instrument]:
         
         is_block_active = block.is_active(date)
         
         if self.logger.isEnabledFor(logging.DEBUG):
             self.logger.debug('%s %i'%(block,is_block_active))
         
         # if signal configuration change store the block
         if is_block_active != self.strategy.trading_blocks_state[block]:
             signals_active = True
             # update strategy trading block
             self.strategy.trading_blocks_state[block] = block.is_active(date)
     
     # send event with changing blocks        
     if signals_active:
         signal_event = Event(Event.SIGNALS_EVENT)
         signal_event.datas['instrument'] = instrument
         signal_event.datas['date'] = date
         event_dispatcher.dispatch_event(signal_event)
 def process_event(self, event):
     '''
     get an event (signal event) which contains changing trading blocks and send trade event
     @param event: the event (signal event)
     @raise CoreError:  
     '''
     instrument = event.datas['instrument']
     date = event.datas['date']
     
     # map instruments and quantities
     trades = {}
     
     # fill the map
     for block in self.strategy.trading_blocks[instrument]:
         qty = 0.0
         # if block is active trade a new signal else close an old signal
         if block.is_active(date):
             # compute target quantity
             qty = block.get_quantity_to_trade(date)
             
         if instrument in trades:
             trades[instrument] = trades[instrument] + qty
         else:
             trades[instrument] = qty
     
     # compute real quantity to trade and send event    
     for instrument in trades:
         qty_to_trade =  trades[instrument] - self.strategy.portfolio.get_instrument_quantity(instrument)
         # if trade already performed in the rolling
         if qty_to_trade != 0:
             trade_event = Event(Event.TRADE_EVENT)
             trade_event.datas['date'] = date
             trade_event.datas['instrument'] = instrument
             trade_event.datas['closing'] = False
             trade_event.datas['quantity'] = qty_to_trade
             trade_event.datas['price'] = instrument.get_close_value(date)
             self.strategy.event_dispatcher.dispatch_event(trade_event) 
 def process_event(self, event):
     '''
     get a rolling event, build the trade event
     @param event: the rolling event
     '''
     date = event.datas['date']
     instrument = event.datas['instrument']
     
     # select block to roll and map raw quantity with related instrument
     quantity = 0.
     for block in self.strategy.trading_blocks[instrument]:
         if block.is_active(date):
             quantity += block.get_quantity_to_trade(date)
     
     event_dispatcher = self.strategy.event_dispatcher
     
     
     close_trade_event = Event(Event.TRADE_EVENT)
     close_trade_event.datas['date'] = date
     close_trade_event.datas['quantity'] = 0.
     close_trade_event.datas['instrument'] = instrument
     close_trade_event.datas['price'] = self._get_close_trade_price(instrument, date)
     close_trade_event.datas['closing'] = True
     event_dispatcher.dispatch_event(close_trade_event)
         
     # the new trade event
     new_trade_event = Event(Event.TRADE_EVENT)
     new_trade_event.datas['date'] = date
     new_trade_event.datas['quantity'] = quantity
     new_trade_event.datas['instrument'] = instrument
     new_trade_event.datas['price'] = instrument.get_close_value(date)
     new_trade_event.datas['closing'] = False
     event_dispatcher.dispatch_event(new_trade_event)