def recalc_rng(self, idx, df):
     split_arr = dh.array_split_by_bar(df, split_list = self.open_period, field = 'min_id')
     win = int(self.lookbacks[idx])
     ddf = split_arr.data
     if win > 0:
         self.cur_rng[idx] = max(max(ddf['high'][-win:])- min(ddf['close'][-win:]), \
                                 max(ddf['close'][-win:]) - min(ddf['low'][-win:]))
     elif win == 0:
         self.cur_rng[idx] = max(max(ddf['high'][-2:])- min(ddf['close'][-2:]), \
                                 max(ddf['close'][-2:]) - min(ddf['low'][-2:]))
         self.cur_rng[idx] = max(self.cur_rng[idx] * 0.5, ddf['high'][-1]-ddf['close'][-1], \
                                 ddf['close'][-1]-ddf['low'][-1])
     else:
         self.cur_rng[idx] = max(ddf['high'][-1] - ddf['low'][-1], abs(ddf['close'][-1] - ddf['close'][-2]))
 def recalc_rng(self, idx, df):
     split_arr = dh.array_split_by_bar(df, split_list = self.open_period, field = 'min_id')
     win = int(self.lookbacks[idx])
     ddf = split_arr.data
     if win > 0:
         self.cur_rng[idx] = max(max(ddf['high'][-win:])- min(ddf['close'][-win:]), \
                                 max(ddf['close'][-win:]) - min(ddf['low'][-win:]))
     elif win == 0:
         self.cur_rng[idx] = max(max(ddf['high'][-2:])- min(ddf['close'][-2:]), \
                                 max(ddf['close'][-2:]) - min(ddf['low'][-2:]))
         self.cur_rng[idx] = max(self.cur_rng[idx] * 0.5, ddf['high'][-1]-ddf['close'][-1], \
                                 ddf['close'][-1]-ddf['low'][-1])
     else:
         self.cur_rng[idx] = max(ddf['high'][-1] - ddf['low'][-1], abs(ddf['close'][-1] - ddf['close'][-2]))
 def initialize(self):
     self.load_state()
     for idx, underlier in enumerate(self.underliers):
         inst = underlier[0]
         self.max_pos[idx] = sum(v > 0.0 for v in self.vol_ratio[idx])
         self.tick_base[idx] = self.agent.instruments[inst].tick_base
         min_id = self.agent.instruments[inst].last_tick_id/1000
         min_id = int(min_id/100)*60 + min_id % 100 - self.daily_close_buffer
         self.last_min_id[idx] = int(min_id/60)*100 + min_id % 60
         ddf = self.agent.day_data[inst].data
         mdf = self.agent.min_data[inst][1].data
         min_date = mdf['date'][-1]
         last_date = ddf['date'][-1]
         if last_date < min_date:
             last_min = mdf['min_id'][-1]
             pid = 0
             for i in range(1, len(self.open_period)):
                 if self.open_period[i] > last_min:
                     pid = i - 1
                     break
             self.open_idx[idx] = pid
             midx = len(mdf)-1
             for i in range(len(mdf)-2, -1, -1):
                 if (mdf['date'][i] < min_date) or (mdf['min_id'][i] < self.open_period[pid]):
                     midx = i + 1
                     break
             self.tday_open[idx] = mdf['open'][midx]
             df = mdf[:midx]
         else:
             self.tday_open[idx] = mdf['close'][-1]
             self.open_idx[idx] = 0
             df = mdf
         self.split_data[idx] = dh.array_split_by_bar(df, split_list = self.open_period, field = 'min_id')
         self.recalc_rng(idx)
     self.update_trade_unit()
     self.save_state()