Exemple #1
0
 def min_switch(self, inst, forced=False):
     prev_bar = self.cur_min[inst]['bar_id']
     bar_id = self.conv_bar_id(self.cur_min[inst]['tick_min'])
     if self.cur_min[inst]['min_id'] == 0:
         return bar_id
     ra = self.min_data[inst][1]
     if self.cur_min[inst]['high'] > self.cur_min[inst]['low']:
         ra.append_by_dict(self.cur_min[inst])
     for m in sorted(self.min_data_func[inst]):
         ra_m = self.min_data[inst][m]
         if ((int(bar_id / m) > int(prev_bar / m)) or forced):
             if m > 1:
                 if (int(prev_bar / m) == int(ra_m.data['bar_id'][-1] / m)
                     ) and (ra.data['date'][-1] == data_handler.conv_date(
                         ra_m.data['date'][-1])):
                     ra_m.remove_lastn(1)
                 last_bar = ra_m.data['bar_id'][-1]
                 last_date = data_handler.conv_date(ra_m.data['date'][-1])
                 rlen = len(ra)
                 idx = 0
                 for i in range(rlen):
                     if (ra.data['date'][rlen - i - 1] <= last_date) and (
                             ra.data['bar_id'][rlen - i - 1] <= last_bar):
                         idx = i
                         break
                 if idx > 0:
                     new_data = {'datetime':ra.data['datetime'][-idx], 'open':ra.data['open'][-idx], 'high':max(ra.data['high'][-idx:]), \
                             'low': min(ra.data['low'][-idx:]), 'close': ra.data['close'][-1],\
                             'volume': sum(ra.data['volume'][-idx:]), 'openInterest':ra.data['openInterest'][-1],\
                             'min_id': ra.data['min_id'][-1], 'bar_id': ra.data['bar_id'][-1], 'date':ra.data['date'][-1]}
                     if new_data['high'] > new_data['low']:
                         ra_m.append_by_dict(new_data)
             for fobj in self.min_data_func[inst][m]:
                 fobj.rfunc(self.min_data[inst][m].data)
     if self.save_flag:
         event1 = Event(type=EVENT_DB_WRITE, priority=500)
         event1.dict['data'] = self.tick_data[inst]
         event1.dict['type'] = EVENT_TICK
         event1.dict['instID'] = inst
         self.eventEngine.put(event1)
         if self.cur_min[inst]['close'] > 0:
             event2 = Event(type=EVENT_DB_WRITE, priority=500)
             event2.dict['data'] = self.cur_min[inst]
             event2.dict['type'] = EVENT_MIN_BAR
             event2.dict['instID'] = inst
             self.eventEngine.put(event2)
     return bar_id
Exemple #2
0
 def min_switch(self, inst, forced = False):
     prev_bar = self.cur_min[inst]['bar_id']
     bar_id = self.conv_bar_id(self.cur_min[inst]['tick_min'])
     if self.cur_min[inst]['min_id'] == 0:
         return bar_id
     ra = self.min_data[inst][1]
     if self.cur_min[inst]['high']>self.cur_min[inst]['low']:
         ra.append_by_dict(self.cur_min[inst])
     for m in sorted(self.min_data_func[inst]):
         ra_m = self.min_data[inst][m]
         if ((int(bar_id/m)>int(prev_bar/m)) or forced):
             if m > 1:
                 if (int(prev_bar/m) == int(ra_m.data['bar_id'][-1]/m)) and (ra.data['date'][-1] == data_handler.conv_date(ra_m.data['date'][-1])):
                     ra_m.remove_lastn(1)
                 last_bar = ra_m.data['bar_id'][-1]
                 last_date = data_handler.conv_date(ra_m.data['date'][-1])
                 rlen = len(ra)
                 idx = 0
                 for i in range(rlen):
                     if (ra.data['date'][rlen-i-1] <= last_date) and (ra.data['bar_id'][rlen-i-1] <= last_bar):
                         idx = i
                         break
                 if idx > 0:
                     new_data = {'datetime':ra.data['datetime'][-idx], 'open':ra.data['open'][-idx], 'high':max(ra.data['high'][-idx:]), \
                             'low': min(ra.data['low'][-idx:]), 'close': ra.data['close'][-1],\
                             'volume': sum(ra.data['volume'][-idx:]), 'openInterest':ra.data['openInterest'][-1],\
                             'min_id': ra.data['min_id'][-1], 'bar_id': ra.data['bar_id'][-1], 'date':ra.data['date'][-1]}
                     if new_data['high'] > new_data['low']:
                         ra_m.append_by_dict(new_data)
             for fobj in self.min_data_func[inst][m]:
                 fobj.rfunc(self.min_data[inst][m].data)
     if self.save_flag:
         event1 = Event(type=EVENT_DB_WRITE, priority = 500)
         event1.dict['data'] = self.tick_data[inst]
         event1.dict['type'] = EVENT_TICK
         event1.dict['instID'] = inst
         self.eventEngine.put(event1)
         if self.cur_min[inst]['close'] > 0:
             event2 = Event(type=EVENT_DB_WRITE, priority = 500)
             event2.dict['data'] = self.cur_min[inst]
             event2.dict['type'] = EVENT_MIN_BAR
             event2.dict['instID'] = inst
             self.eventEngine.put(event2)
     return bar_id