def on_data_update(self, data): if data.type == "Bar_1day": #用日度交易数据来收集数据并确定SEP #增加最新数据 self.history = np.append(self.history, data.close) self.date = np.append(self.date, data.date) if (len(self.history)-10>0): for i in range(max(4,len(self.history)-10),len(self.history)-5): if(self.history[i] > max(self.history[i-4:i]) and self.history[i] > max(self.history[i+1:i+5]) and (self.date[i+5] not in self.Trade_date)): ## 判断SEP条件 self.Trade_date.append(self.date[i+5]) if data.date in self.Trade_date: cal = chrono.Calendar("ChinaStocks") self.date0 = data.date print "由SEP确定的交易日为:",cal.add(data.date, 1) ## SEP 之后第五个交易日被记录,其下一天为交易日期 # 集合竞价 9:25 self.t0 = self.add_alarm(chrono.str_to_time(str(cal.add(self.date0, 1))+"01:25:00.000000")) self.t1 = self.add_alarm(chrono.str_to_time(str(cal.add(self.date0, 1))+"01:31:00.000000")) self.t2 = self.add_alarm(chrono.str_to_time(str(cal.add(self.date0, 1))+"01:37:00.000000")) self.t3 = self.add_alarm(chrono.str_to_time(str(cal.add(self.date0, 1))+"01:43:00.000000")) self.t4 = self.add_alarm(chrono.str_to_time(str(cal.add(self.date0, 1))+"01:49:00.000000")) self.t5 = self.add_alarm(chrono.str_to_time(str(cal.add(self.date0, 1))+"01:55:00.000000")) cal = chrono.Calendar("ChinaStocks") if cal.add(data.date,-1) in self.Trade_date: print "五次下单分时价格为:",self.bar1,self.bar2,self.bar3,self.bar4,self.bar5 if data.type == "Bar_1min": cal = chrono.Calendar("ChinaStocks") if chrono.time_to_date(data.time) == cal.add(self.date0, 1): if chrono.str_to_time(str(cal.add(self.date0, 1))+"01:31:00.000000") == data.time: self.bar1 = data.close if chrono.str_to_time(str(cal.add(self.date0, 1))+"01:37:00.000000") == data.time: self.bar2 = data.close if chrono.str_to_time(str(cal.add(self.date0, 1))+"01:43:00.000000") == data.time: self.bar3 = data.close if chrono.str_to_time(str(cal.add(self.date0, 1))+"01:49:00.000000") == data.time: self.bar4 = data.close if chrono.str_to_time(str(cal.add(self.date0, 1))+"01:55:00.000000") == data.time: self.bar5 = data.close
def on_data_update(self, data): if data.type == "Bar_1day": #用日度交易数据来收集数据并确定SEP #增加最新数据,去掉最旧数据 self.history = np.append(self.history, data.close) self.date = np.append(self.date, data.date) if (len(self.history) - 10 > 0): for i in range(max(4, len(self.history) - 10), len(self.history) - 5): if (self.history[i] > max(self.history[i - 4:i]) and self.history[i] > max(self.history[i + 1:i + 5]) and (self.date[i + 5] not in self.Trade_date)): ## 判断SEP条件 self.Trade_date.append(self.date[i + 5]) if data.date in self.Trade_date: cal = chrono.Calendar("ChinaStocks") print "由SEP确定的交易日为:", cal.add(data.date, 1) # 集合竞价 9:25 self.t0 = self.add_alarm( chrono.str_to_time( str(cal.add(data.date, 1)) + "01:25:00.000000")) self.t1 = self.add_alarm( chrono.str_to_time( str(cal.add(data.date, 1)) + "01:30:00.000000")) self.t2 = self.add_alarm( chrono.str_to_time( str(cal.add(data.date, 1)) + "01:36:00.000000")) self.t3 = self.add_alarm( chrono.str_to_time( str(cal.add(data.date, 1)) + "01:42:00.000000")) self.t4 = self.add_alarm( chrono.str_to_time( str(cal.add(data.date, 1)) + "01:48:00.000000")) self.t5 = self.add_alarm( chrono.str_to_time( str(cal.add(data.date, 1)) + "01:54:00.000000"))
def on_data_update(self, data): for instr in self.list['name']: if str(data.instrument) == instr: if data.type == 'Bar_1day': self.report['report'][instr]['end_time'].append( chrono.time_to_str(get_current_time())) cal = chrono.Calendar("ChinaStocks") self.df['history'][instr] = np.append( self.df['history'][instr], data.close) self.df['date'][instr] = np.append(self.df['date'][instr], data.date) print 'Today is:', data.date, 'Stock id: ', instr self.report['report'][instr]['date'].append( cal.add(data.date, 1)) self.report['report'][instr]['stock_id'].append(str(instr)) self.report['report'][instr]['history'].append(data.close) print "日期记录:", self.report['report'][instr][ 'date'], '\n', "交易价记录:", self.report['report'][instr][ 'price'], '\n', "每日收盘价记录:", self.report['report'][instr][ 'history'], '\n', 'value:', self.report['report'][ instr]['value'], 'size:', self.report['report'][ instr]['size'], 'order id: ', self.report[ 'report'][instr][ 'states'], '\n', "订单时间:", self.report[ 'report'][instr][ 'order_time'], '\n', 'data返回终止时间:', self.report[ 'report'][instr][ 'end_time'], '\n' self.df['t0'][instr] = self.add_alarm( chrono.str_to_time( str(cal.add(data.date, 1)) + "01:55:00.000000")) self.df['t1'][instr] = self.add_alarm( chrono.str_to_time( str(cal.add(data.date, 1)) + "06:55:00.000000"))
def on_data_update(self, data): for instr in self.list['name']: ##遍历股票池中的股票 if str(data.instrument) == instr: ##对于某只特定的股票: self.date[instr] = [] ## 字典中对应于该只股票的空列表,存档每只股票的交易日,因为不同股票可能不一样 self.Trade_date[instr] = [ ] ##字典中对应于该只股票的空列表,搜集SEP后的第五个交易日期,每一个列表中的交易日,我们将在下一个交易日交易 self.history[instr] = [] ## 字典中对应于该只股票的空列表, 存档每日收盘价 ## 这几个变量都得用字典形式存储,防止数据跑窜了地方 self.order0[instr] = 0 ##6个订单编号 self.order1[instr] = 0 self.order2[instr] = 0 self.order3[instr] = 0 self.order4[instr] = 0 self.order5[instr] = 0 self.bar0[instr] = 0 ##6个收集bartype收盘价的全局变量 self.bar1[instr] = 0 self.bar2[instr] = 0 self.bar3[instr] = 0 self.bar4[instr] = 0 self.bar5[instr] = 0 self.date0[instr] = self.default_date if data.type == "Bar_1day": #用日度交易数据来收集数据并确定SEP #增加最新数据 self.history[instr] = np.append(self.history[instr], data.close) self.date[instr] = np.append(self.date[instr], data.date) if (len(self.history[instr]) - 10 > 0): for i in range(max(4, len(self.history[instr]) - 10), len(self.history[instr]) - 5): if (self.history[instr][i] > max( self.history[instr][i - 4:i]) and self.history[instr][i] > max( self.history[instr][i + 1:i + 5]) and (self.date[instr][i + 5] not in self.Trade_date[instr])): ## 判断SEP条件 self.Trade_date[instr].append( self.date[instr][i + 5]) if data.date in self.Trade_date[instr]: ## 如果某天是 该只股票的某个SEP日期的后五日: cal = chrono.Calendar("ChinaStocks") self.date0[instr] = data.date print "由SEP确定的交易日为:", cal.add( data.date, 1) ## SEP 之后第五个交易日被记录,其下一天为交易日期 ## 设定不同的闹钟进行买入交易 self.t0[instr] = self.add_alarm( chrono.str_to_time( str(cal.add(data.date, 1)) + "01:25:00.000000")) self.t1[instr] = self.add_alarm( chrono.str_to_time( str(cal.add(data.date, 1)) + "01:31:00.000000")) self.t2[instr] = self.add_alarm( chrono.str_to_time( str(cal.add(data.date, 1)) + "01:37:00.000000")) self.t3[instr] = self.add_alarm( chrono.str_to_time( str(cal.add(data.date, 1)) + "01:43:00.000000")) self.t4[instr] = self.add_alarm( chrono.str_to_time( str(cal.add(data.date, 1)) + "01:49:00.000000")) self.t5[instr] = self.add_alarm( chrono.str_to_time( str(cal.add(data.date, 1)) + "01:55:00.000000")) cal = chrono.Calendar("ChinaStocks") if cal.add(data.date, -1) in self.Trade_date[instr]: ## 如果某一天的前一天是 instr该只股票SEP之后第五个交易日被记录(已被记录在self.Trade_date[instr]),那么他这一天将是买入的日期 ## 在这里打印是为了对应 data.type=="Bar_1day",只需要每天打一次就行 print "五次下单分时价格为:", self.bar1[instr], self.bar2[ instr], self.bar3[instr], self.bar4[ instr], self.bar5[instr] if data.type == "Bar_1min": cal = chrono.Calendar("ChinaStocks") if chrono.time_to_date(data.time) == cal.add( self.date0[instr], 1): ## 对应某一只股票,改天1Min bar线对应的日期为交易日的话: if chrono.str_to_time( str(cal.add(self.date0[instr], 1)) + "01:31:00.000000") == data.time: self.bar1[instr] = data.close if chrono.str_to_time( str(cal.add(self.date0[instr], 1)) + "01:37:00.000000") == data.time: self.bar2[instr] = data.close if chrono.str_to_time( str(cal.add(self.date0[instr], 1)) + "01:43:00.000000") == data.time: self.bar3[instr] = data.close if chrono.str_to_time( str(cal.add(self.date0[instr], 1)) + "01:49:00.000000") == data.time: self.bar4[instr] = data.close if chrono.str_to_time( str(cal.add(self.date0[instr], 1)) + "01:55:00.000000") == data.time: self.bar5[instr] = data.close
def on_data_update(self, data): for instr in self.list['name']: if str(data.instrument) == instr: if data.type == "Bar_1day": self.df['history'][instr] = np.append( self.df['history'][instr], data.close) self.df['date'][instr] = np.append(self.df['date'][instr], data.date) if (len(self.df['history'][instr]) - 10 > 0): for i in range( max(4, len(self.df['history'][instr]) - 10), len(self.df['history'][instr]) - 5): if (self.df['history'][instr][i] > max( self.df['history'][instr][i - 4:i]) and self.df['history'][instr][i] > max( self.df['history'][instr][i + 1:i + 5]) and (self.df['date'][instr][i + 5] not in self.df['Trade_date'][instr])): ## 判断SEP条件 self.df['Trade_date'][instr].append( self.df['date'][instr][i + 5]) if data.date in self.df['Trade_date'][ instr]: ########################################################################################## ## 如果某天是 该只股票的某个SEP日期的后五日: # cal = chrono.Calendar("ChinaStocks") # self.df['date0'][instr] = data.date # print instr, "由SEP确定的交易日为:", cal.add( self.df['date0'][instr], 1 ) ## SEP 之后第五个交易日被记录,其下一天为交易日期 # # self.df['t0'][instr] = self.add_alarm( chrono.str_to_time( str(cal.add(self.df['date0'][instr], 1)) + "01:25:00.000000")) # self.df['t1'][instr] = self.add_alarm( chrono.str_to_time( str(cal.add(self.df['date0'][instr], 1)) + "01:31:00.000000")) # self.df['t2'][instr] = self.add_alarm( chrono.str_to_time( str(cal.add(self.df['date0'][instr], 1)) + "01:37:00.000000")) # self.df['t3'][instr] = self.add_alarm( chrono.str_to_time( str(cal.add(self.df['date0'][instr], 1)) + "01:43:00.000000")) # self.df['t4'][instr] = self.add_alarm( chrono.str_to_time( str(cal.add(self.df['date0'][instr], 1)) + "01:49:00.000000")) # self.df['t5'][instr] = self.add_alarm( chrono.str_to_time( str(cal.add(self.df['date0'][instr], 1)) + "01:55:00.000000")) # # cal = chrono.Calendar("ChinaStocks") # # if cal.add(data.date, -1) in self.df['Trade_date'][ instr]: ############################################################################# ## 如果某一天的前一天是 instr该只股票SEP之后第五个交易日被记录(已被记录在self.Trade_date[instr]),那么他这一天将是买入的日期 ## 在这里打印是为了对应 data.type=="Bar_1day",只需要每天打一次就行 print instr, "五次下单分时价格为:", self.df['bar1'][ instr], self.df['bar2'][instr], self.df['bar3'][ instr], self.df['bar4'][instr], self.df[ 'bar5'][instr] if data.type == "Bar_1min": cal = chrono.Calendar("ChinaStocks") if chrono.time_to_date(data.time) == cal.add( self.df['date0'][instr], 1): ## 对应某一只股票,该天1Min bar线对应的日期为交易日的话: if chrono.str_to_time( str(cal.add(self.df['date0'][instr], 1)) + "01:31:00.000000") == data.time: self.df['bar1'][instr] = data.close if chrono.str_to_time( str(cal.add(self.df['date0'][instr], 1)) + "01:37:00.000000") == data.time: self.df['bar2'][instr] = data.close if chrono.str_to_time( str(cal.add(self.df['date0'][instr], 1)) + "01:43:00.000000") == data.time: self.df['bar3'][instr] = data.close if chrono.str_to_time( str(cal.add(self.df['date0'][instr], 1)) + "01:49:00.000000") == data.time: self.df['bar4'][instr] = data.close if chrono.str_to_time( str(cal.add(self.df['date0'][instr], 1)) + "01:55:00.000000") == data.time: self.df['bar5'][instr] = data.close
def on_data_update(self, data): for instr in self.list['name']: ##遍历股票池中的股票 if str(data.instrument) == instr: ##对于某只特定的股票: if data.type == "Bar_1day": #用日度交易数据来收集数据并确定SEP #增加最新数据 self.df['history'][instr]= np.append(self.df['history'][instr], data.close) self.df['date'][instr]= np.append(self.df['date'][instr], data.date) if (len(self.df['history'][instr])-10>0): for i in range(max(4,len(self.df['history'][instr])-10),len(self.df['history'][instr])-5): if(self.df['history'][instr][i] > max(self.df['history'][instr][i-4:i]) and self.df['history'][instr][i] > max(self.df['history'][instr][i+1:i+5]) and (self.df['history'][instr][i+5] not in self.df['Trade_date'][instr])): ## 判断SEP条件 self.df['Trade_date'][instr].append(self.df['date'][instr][i+5]) if data.date in self.df['Trade_date'][instr]: ## 如果某天是 该只股票的某个SEP日期的后五日: cal = chrono.Calendar("ChinaStocks") self.df['date0'][instr] = data.date print "由SEP确定的交易日为:",cal.add(data.date, 1) ## SEP 之后第五个交易日被记录,其下一天为交易日期 ## 设定不同的闹钟进行买入交易 self.df['t0'][instr] = self.add_alarm(chrono.str_to_time(str(cal.add(data.date, 1))+"01:25:00.000000")) self.df['t1'][instr] = self.add_alarm(chrono.str_to_time(str(cal.add(data.date, 1))+"01:31:00.000000")) self.df['t2'][instr] = self.add_alarm(chrono.str_to_time(str(cal.add(data.date, 1))+"01:37:00.000000")) self.df['t3'][instr] = self.add_alarm(chrono.str_to_time(str(cal.add(data.date, 1))+"01:43:00.000000")) self.df['t4'][instr] = self.add_alarm(chrono.str_to_time(str(cal.add(data.date, 1))+"01:49:00.000000")) self.df['t5'][instr] = self.add_alarm(chrono.str_to_time(str(cal.add(data.date, 1))+"01:55:00.000000")) cal = chrono.Calendar("ChinaStocks") if cal.add(data.date,-1) in self.df['Trade_date'][instr]: ## 如果某一天的前一天是 instr该只股票SEP之后第五个交易日被记录(已被记录在self.Trade_date[instr]),那么他这一天将是买入的日期 ## 在这里打印是为了对应 data.type=="Bar_1day",只需要每天打一次就行 print "五次下单分时价格为:",self.df['bar1'][instr],self.df['bar2'][instr],self.df['bar3'][instr],self.df['bar4'][instr],self.df['bar5'][instr] if data.type == "Bar_1min": cal = chrono.Calendar("ChinaStocks") if chrono.time_to_date(data.time) == cal.add(self.df['date0'][instr], 1): ## 对应某一只股票,改天1Min bar线对应的日期为交易日的话: if chrono.str_to_time(str(cal.add(self.df['date0'][instr], 1))+"01:31:00.000000") == data.time: self.df['bar1'][instr] = data.close if chrono.str_to_time(str(cal.add(self.df['date0'][instr], 1))+"01:37:00.000000") == data.time: self.df['bar2'][instr] = data.close if chrono.str_to_time(str(cal.add(self.df['date0'][instr], 1))+"01:43:00.000000") == data.time: self.df['bar3'][instr] = data.close if chrono.str_to_time(str(cal.add(self.df['date0'][instr], 1))+"01:49:00.000000") == data.time: self.df['bar4'][instr] = data.close if chrono.str_to_time(str(cal.add(self.df['date0'][instr], 1))+"01:55:00.000000") == data.time: self.df['bar5'][instr] = data.close def on_alarm(self,id): for instr in self.list['name']: ##遍历股票池中的股票 if id == self.df['t0'][instr] : self.df['order0'][instr] = self.place_stock_order(instr, 5 , trade.MarketPrice()) self.df['total_volumn'][instr] = 0 self.df['total_size'][instr] = 0 ## 每到一个新的指定交易日,清零之前的累加(用来求新的交易均价) if id == self.df['t1'][instr]: self.df['order1'][instr] = self.place_stock_order(instr, 1 , trade.MarketPrice()) if id == self.df['t2'][instr]: self.df['order2'][instr] = self.place_stock_order(instr, 1 , trade.MarketPrice()) if id == self.df['t3'][instr]: self.df['order3'][instr] = self.place_stock_order(instr, 1 , trade.MarketPrice()) if id == self.df['t4'][instr]: self.df['order4'][instr] = self.place_stock_order(instr, 1, trade.MarketPrice()) if id == self.df['t5'][instr]: self.df['order5'][instr] = self.place_stock_order(instr, 1, trade.MarketPrice()) def on_order_update(self, order): for instr in self.list['name']: ##遍历股票池中的股票 if order.id == self.df['order0'][instr] and order.state == OrderState.FINAL: ## 集合竞价得到的平均购入价格 self.df['average_price_once'][instr] = order.filled_value / order.filled_size if order.id != self.df['order0'][instr] and order.state == OrderState.FINAL: ## 累积加和分次下单的总价和下单总量 self.df['total_volumn'][instr] += order.filled_value self.df['total_size'][instr] += order.filled_size if order.id == self.df['order5'][instr] and order.state ==OrderState.FINAL: ## 最后一单交易完成之后,核算平均成交价格 self.df['average_price'][instr] = self.df['total_volumn'][instr] / self.df['total_size'][instr] print "Stock_id:",instr,"分次下单总交易量:",self.df['total_volumn'][instr],"分次下单总股数:",self.df['total_size'][instr] ,"分次下单均价:",self.df['average_price'][instr],"集合竞价均价:",self.df['average_price_once'][instr]
def on_data_update(self, data): for instr in self.list['name']: if str(data.instrument) == instr: if data.type == "Bar_1day": cal = chrono.Calendar("ChinaStocks") self.df['history'][instr] = np.append( self.df['history'][instr], data.close) self.df['date'][instr] = np.append(self.df['date'][instr], data.date) if (len(self.df['history'][instr]) - 10 > 0): for i in range( max(4, len(self.df['history'][instr]) - 10), len(self.df['history'][instr]) - 5): if (self.df['history'][instr][i] > max( self.df['history'][instr][i - 4:i]) and self.df['history'][instr][i] > max( self.df['history'][instr][i + 1:i + 5]) and (self.df['date'][instr][i + 5] not in self.df['Trade_date'][instr])): ## 判断SEP条件 self.df['Trade_date'][instr].append( self.df['date'][instr][i + 5]) if data.date in self.df['Trade_date'][instr]: self.df['date0'][instr] = data.date self.df['t0'][instr] = self.add_alarm( chrono.str_to_time( str(cal.add(self.df['date0'][instr], 1)) + "01:25:00.000000")) self.df['t1'][instr] = self.add_alarm( chrono.str_to_time( str(cal.add(self.df['date0'][instr], 1)) + "01:31:00.000000")) self.df['t2'][instr] = self.add_alarm( chrono.str_to_time( str(cal.add(self.df['date0'][instr], 1)) + "01:37:00.000000")) self.df['t3'][instr] = self.add_alarm( chrono.str_to_time( str(cal.add(self.df['date0'][instr], 1)) + "01:43:00.000000")) self.df['t4'][instr] = self.add_alarm( chrono.str_to_time( str(cal.add(self.df['date0'][instr], 1)) + "01:49:00.000000")) self.df['t5'][instr] = self.add_alarm( chrono.str_to_time( str(cal.add(self.df['date0'][instr], 1)) + "01:55:00.000000")) if cal.add(data.date, -1) in self.df['Trade_date'][instr]: ##记录交易日##### self.report['report'][instr]['date'].append(data.date) self.report['report'][instr]['stock_id'].append(instr) ###$$$$$####### print instr, "由SEP确定的交易日为:", data.date, "五次下单分时价格为:", self.df[ 'bar1'][instr], self.df['bar2'][instr], self.df[ 'bar3'][instr], self.df['bar4'][ instr], self.df['bar5'][instr] print self.report['order_record'][instr] if abs((self.df['average_price'][instr] - self.df['average_price_once'][instr]) / self.df['average_price_once'][instr]) >= 0.02: self.report['report'][instr][ 'possible_error0'].append('Error') #记录可能出现的错误:集合竞价与分次下单平均价格差别太大。 else: self.report['report'][instr][ 'possible_error0'].append('Correct') if abs( (self.df['bar1'][instr] - self.df['price1'][instr]) / self.df['bar1'][instr]) + abs( (self.df['bar2'][instr] - self.df['price2'][instr]) / self.df['bar2'][instr]) + abs( (self.df['bar3'][instr] - self.df['price3'][instr]) / self.df['bar3'][instr]) + abs( (self.df['bar4'][instr] - self.df['price4'][instr]) / self.df['bar4'][instr]) + abs( (self.df['bar5'][instr] - self.df['price5'][instr]) / self.df['bar5'][instr]) >= 0.1: #记录可能出现的错误:bar线数据与实际成交数据差别太大。 self.report['report'][instr][ 'possible_error1'].append('Error') else: self.report['report'][instr][ 'possible_error1'].append('Correct') if data.type == "Bar_1min": cal = chrono.Calendar("ChinaStocks") if chrono.time_to_date(data.time) == cal.add( self.df['date0'][instr], 1): ## 对应某一只股票,该天1Min bar线对应的日期为交易日的话: if chrono.str_to_time( str(cal.add(self.df['date0'][instr], 1)) + "01:31:00.000000") == data.time: self.df['bar1'][instr] = data.close if chrono.str_to_time( str(cal.add(self.df['date0'][instr], 1)) + "01:37:00.000000") == data.time: self.df['bar2'][instr] = data.close if chrono.str_to_time( str(cal.add(self.df['date0'][instr], 1)) + "01:43:00.000000") == data.time: self.df['bar3'][instr] = data.close if chrono.str_to_time( str(cal.add(self.df['date0'][instr], 1)) + "01:49:00.000000") == data.time: self.df['bar4'][instr] = data.close if chrono.str_to_time( str(cal.add(self.df['date0'][instr], 1)) + "01:55:00.000000") == data.time: self.df['bar5'][instr] = data.close