def load_local_positions(self, tday): pos_date = tday logfile = self.file_prefix + 'EODPos_' + pos_date.strftime( '%y%m%d') + '.csv' if not os.path.isfile(logfile): pos_date = workdays.workday(pos_date, -1, CHN_Holidays) logfile = self.file_prefix + 'EODPos_' + pos_date.strftime( '%y%m%d') + '.csv' if not os.path.isfile(logfile): logContent = "no prior position file is found" self.onLog(logContent, level=logging.INFO) return False else: self.eod_flag = True with open(logfile, 'rb') as f: reader = csv.reader(f) for idx, row in enumerate(reader): if row[0] == 'capital': self.account_info['prev_capital'] = float(row[1]) elif row[0] == 'pos': inst = row[1] if inst in self.instruments: if inst not in self.positions: self.positions[inst] = order.Position( self.agent.instruments[inst], self) self.positions[inst].pos_yday.long = int(row[2]) self.positions[inst].pos_yday.short = int(row[3]) return True
def day_finalize(self, tday): self.save_local_positions(tday) eod_pos = {} for inst in self.positions: pos = self.positions[inst] eod_pos[inst] = [pos.curr_pos.long, pos.curr_pos.short] self.id2order = {} self.positions = {} self.order_stats = { 'total_submit': 0, 'total_failure': 0, 'total_cancel': 0 } for inst in self.instruments: self.order_stats[inst] = { 'submit': 0, 'cancel': 0, 'failure': 0, 'status': True } self.positions[inst] = order.Position(self.agent.instruments[inst], self) self.positions[inst].pos_yday.long = eod_pos[inst][0] self.positions[inst].pos_yday.short = eod_pos[inst][1] self.positions[inst].re_calc() self.account_info['prev_capital'] = self.account_info['curr_capital']
def add_instrument(self, instID): if instID not in self.positions: self.positions[instID] = order.Position(self.agent.instruments[instID], self) if instID not in self.order_stats: self.order_stats[instID] = {'submit': 0, 'cancel':0, 'failure': 0, 'status': True } if instID not in self.qry_pos: self.qry_pos[instID] = {'tday': [0, 0], 'yday': [0, 0]}