def control_in_p(self, bar): try: for k in self.spread_dict.keys(): s0 = self.spread_dict[k][0] s1 = self.spread_dict[k][1] if s0 not in self.got_dict or s1 not in self.got_dict: continue if self.got_dict[s0] and self.got_dict[ s1] and self.process_dict[k] == False: self.process_dict[k] = True s0 = self.signalDict[s0][0] s1 = self.signalDict[s1][0] bar_s = VtBarData() bar_s.vtSymbol = k bar_s.symbol = k bar_s.exchange = s0.bar.exchange bar_s.date = s0.bar.date bar_s.time = s0.bar.time bar_s.close = s0.bar.close - s1.bar.close bar_s.AskPrice = s0.bar.AskPrice - s1.bar.BidPrice bar_s.BidPrice = s0.bar.BidPrice - s1.bar.AskPrice # print(bar_s.time, bar_s.vtSymbol, bar_s.close, bar_s.AskPrice, bar_s.BidPrice) self.engine.lock.acquire() self.engine.bar_list.append(bar_s) self.engine.lock.release() except Exception as e: s = traceback.format_exc() to_log(s)
def _bc_loadInitBar(self, vtSymbol, initBars): """读取Bar数据,""" r = [] #df = get_stk_hfq(self.dss, vtSymbol, begin_date=None, end_date='2019-07-18') df = get_stk_hfq(self.dss, vtSymbol, begin_date=None, end_date=None) if df is not None: df = df.iloc[:initBars] df = df.sort_values('date') for i, row in df.iterrows(): d = dict(row) #print(d) bar = VtBarData() bar.vtSymbol = vtSymbol bar.symbol = vtSymbol bar.open = float(d['open']) bar.high = float(d['high']) bar.low = float(d['low']) bar.close = float(d['close']) date = d['date'].split('-') #去掉字符串中间的'-' date = ''.join(date) bar.date = date bar.time = '00:00:00' bar.datetime = datetime.strptime(bar.date + ' ' + bar.time, '%Y%m%d %H:%M:%S') bar.volume = float(d['volume']) r.append(bar) return r
def loadData(self, vtSymbol): """加载数据""" df = pd.read_csv(vtSymbol + '.csv') for i, d in df.iterrows(): #print(d) #set_trace() bar = VtBarData() bar.vtSymbol = vtSymbol bar.symbol = vtSymbol bar.open = float(d['open']) bar.high = float(d['high']) bar.low = float(d['low']) bar.close = float(d['close']) date = str(d['date']) if '-' in date: date = date.split('-') date = ''.join(date) bar.date = date bar.time = d['time'] #bar.time = '00:00:00' bar.datetime = datetime.strptime(bar.date + ' ' + bar.time, '%Y%m%d %H:%M:%S') bar.volume = d['volume'] barDict = self.dataDict.setdefault(bar.datetime, OrderedDict()) barDict[bar.vtSymbol] = bar self.output(u'全部数据加载完成')
def loadData(self, dbName): """加载数据""" for vtSymbol in self.vtSymbolList: df = get_stk_hfq(dss, vtSymbol) df = df.sort_values(['date']) for i, d in df.iterrows(): #print(d) #set_trace() bar = VtBarData() bar.vtSymbol = vtSymbol bar.symbol = vtSymbol bar.open = float(d['open']) bar.high = float(d['high']) bar.low = float(d['low']) bar.close = float(d['close']) date = d['date'].split('-') date = ''.join(date) bar.date = date bar.time = '00:00:00' bar.datetime = datetime.strptime(bar.date + ' ' + bar.time, '%Y%m%d %H:%M:%S') bar.volume = d['volume'] barDict = self.dataDict.setdefault(bar.datetime, OrderedDict()) barDict[bar.vtSymbol] = bar self.output(u'全部数据加载完成')
def put_service(self): print('in put_svervice') while True: time.sleep(1) for id in self.vtSymbol_list: try: fname = self.dss + 'fut/put/' + self.minx + '_' + id + '.csv' #print(fname) df = pd.read_csv(fname) d = dict(df.loc[0, :]) #print(d) #print(type(d)) bar = VtBarData() bar.__dict__ = d bar.vtSymbol = id bar.symbol = id if id not in self.vtSymbol_dict: self.vtSymbol_dict[id] = bar elif self.vtSymbol_dict[id].time != bar.time: self.vtSymbol_dict[id] = bar #self.onBar(bar) for p in self.portfolio_list: p.onBar(bar) except Exception as e: print('error,读取文件错误') print(e)
def loadData(self, vtSymbol): """加载数据""" df = pd.read_csv(self.dss + 'fut/bar/min5_' + vtSymbol + '.csv') for i, d in df.iterrows(): #print(d) bar = VtBarData() bar.vtSymbol = vtSymbol bar.symbol = vtSymbol bar.open = float(d['open']) bar.high = float(d['high']) bar.low = float(d['low']) bar.close = float(d['close']) bar.volume = d['volume'] date = str(d['date']) bar.date = date bar.time = str(d['time']) if '-' in date: bar.datetime = datetime.strptime(bar.date + ' ' + bar.time, '%Y-%m-%d %H:%M:%S') else: bar.datetime = datetime.strptime(bar.date + ' ' + bar.time, '%Y%m%d %H:%M:%S') #bar.time = '00:00:00' #bar.datetime = bar.date + ' ' + bar.time bar.datetime = datetime.strftime(bar.datetime, '%Y%m%d %H:%M:%S') self.barDict[bar.datetime] = bar # print(bar.__dict__) # break self.output(u'全部数据加载完成')
def worker_1450(self): """盘中推送bar""" to_log('in TradeEngine.worker_1450') for p in self.portfolio_list: for vtSymbol in p.vtSymbolList: #print(vtSymbol) df = None i = 0 while df is None and i < 2: try: i += 1 df = ts.get_realtime_quotes(vtSymbol) except Exception as e: print('error get_realtime_quotes') print(e) time.sleep(0.1) if df is None: continue code = vtSymbol df1 = None i = 0 while df1 is None and i < 2: try: i += 1 df1 = get_adj_factor(self.dss, code) except Exception as e: print('error adj_factor ' + code) print(e) time.sleep(0.1) if df1 is None: continue factor = float(df1.at[0, 'adj_factor']) d = df.loc[0, :] bar = VtBarData() bar.vtSymbol = vtSymbol bar.symbol = vtSymbol bar.open = float(d['open']) * factor bar.high = float(d['high']) * factor bar.low = float(d['low']) * factor bar.close = float(d['price']) * factor bar.close_bfq = float(d['price']) date = d['date'].split('-') #去掉字符串中间的'-' date = ''.join(date) bar.date = date bar.time = '00:00:00' bar.datetime = datetime.strptime(bar.date + ' ' + bar.time, '%Y%m%d %H:%M:%S') bar.volume = float(d['volume']) p.onBar(bar) send_email(self.dss, 'worker_1450处理完毕', '')
def loadData(self): """加载数据""" for vtSymbol in self.symbol_list: filename = get_dss() + 'fut/bar/min1_' + vtSymbol + '.csv' df = pd.read_csv(filename) for i, d in df.iterrows(): # print(d) # print('here') bar = VtBarData() bar.vtSymbol = vtSymbol bar.symbol = vtSymbol bar.open = float(d['open']) bar.high = float(d['high']) bar.low = float(d['low']) bar.close = float(d['close']) bar.volume = d['volume'] dt = d['datetime'] bar.date = dt[:4] + dt[5:7] + dt[8:10] bar.time = dt[11:19] bar.datetime = bar.date + ' ' + bar.time # print(bar.datetime, bar.date, bar.time) # return # date = str(d['date']) # bar.date = date # bar.time = str(d['time']) # if '-' in date: # bar.datetime = datetime.strptime(bar.date + ' ' + bar.time, '%Y-%m-%d %H:%M:%S') # else: # bar.datetime = datetime.strptime(bar.date + ' ' + bar.time, '%Y%m%d %H:%M:%S') # # #bar.time = '00:00:00' # #bar.datetime = bar.date + ' ' + bar.time # bar.datetime = datetime.strftime(bar.datetime, '%Y%m%d %H:%M:%S') barDict = self.dataDict.setdefault(bar.datetime, OrderedDict()) barDict[bar.vtSymbol] = bar # break self.output(u'全部数据加载完成')
def control_in_p(self, bar): try: for k in self.spread_dict.keys(): s0 = self.spread_dict[k][0] s1 = self.spread_dict[k][1] if s0 not in self.got_dict or s1 not in self.got_dict: continue if self.got_dict[s0] and self.got_dict[ s1] and self.process_dict[k] == False: self.process_dict[k] = True s0 = self.bar_dict[s0] s1 = self.bar_dict[s1] bar_s = VtBarData() bar_s.vtSymbol = k bar_s.symbol = k bar_s.exchange = s0.exchange bar_s.date = s0.date bar_s.time = s0.time bar_s.close = s0.close - s1.close bar_s.AskPrice = s0.AskPrice - s1.BidPrice bar_s.BidPrice = s0.BidPrice - s1.AskPrice # print(bar_s.time, bar_s.vtSymbol, bar_s.close, bar_s.AskPrice, bar_s.BidPrice) df = pd.DataFrame([bar_s.__dict__]) cols = [ 'date', 'time', 'open', 'high', 'low', 'close', 'volume' ] df = df[cols] fname = get_dss() + 'fut/bar/min1_' + k + '.csv' if os.path.exists(fname): df.to_csv(fname, index=False, mode='a', header=False) else: df.to_csv(fname, index=False, mode='a') return bar_s except Exception as e: s = traceback.format_exc() to_log(s) return None
def loadData(self): """加载数据""" for vtSymbol in self.symbol_list: #filename = get_dss( )+ 'fut/bar/min5_' + vtSymbol + '.csv' #filename = get_dss( ) + 'fut/bar/' + self.minx + '_' + vtSymbol + '.csv' #filename = get_dss( ) + 'backtest/bar/' + self.minx + '_' + vtSymbol + '.csv' filename = get_dss( ) + 'backtest/fut/' + vtSymbol + '/' + self.minx + '_' + vtSymbol + '.csv' df = pd.read_csv(filename) for i, d in df.iterrows(): #print(d) bar = VtBarData() bar.vtSymbol = vtSymbol bar.symbol = vtSymbol bar.open = float(d['open']) bar.high = float(d['high']) bar.low = float(d['low']) bar.close = float(d['close']) bar.volume = d['volume'] date = str(d['date']) bar.date = date bar.time = str(d['time']) if '-' in date: bar.datetime = datetime.strptime(bar.date + ' ' + bar.time, '%Y-%m-%d %H:%M:%S') else: bar.datetime = datetime.strptime(bar.date + ' ' + bar.time, '%Y%m%d %H:%M:%S') #bar.time = '00:00:00' #bar.datetime = bar.date + ' ' + bar.time bar.datetime = datetime.strftime(bar.datetime, '%Y%m%d %H:%M:%S') barDict = self.dataDict.setdefault(bar.datetime, OrderedDict()) barDict[bar.vtSymbol] = bar # break print(vtSymbol + '全部数据加载完成,数据量:' + str(len(df)))
def loadData(self): """加载数据""" for vtSymbol in self.vtSymbol_list: # print(vtSymbol) test_list = ['IF2011', 'IO2011-C-4700', 'IO2011-P-4700'] # test_list = ['m2101', 'RM101', 'MA101', 'p2101'] if vtSymbol in test_list: fn = get_dss( )+ 'fut/bar/min1_' + vtSymbol + '.csv' if os.path.exists(fn): df = pd.read_csv(fn) for i, d in df.iterrows(): # print(i) bar = VtBarData() bar.vtSymbol = vtSymbol bar.symbol = vtSymbol bar.open = float(d['open']) bar.high = float(d['high']) bar.low = float(d['low']) bar.close = float(d['close']) bar.volume = d['volume'] date = str(d['date']) bar.date = date bar.time = str(d['time']) if '-' in date: bar.datetime = datetime.strptime(bar.date + ' ' + bar.time, '%Y-%m-%d %H:%M:%S') else: bar.datetime = datetime.strptime(bar.date + ' ' + bar.time, '%Y%m%d %H:%M:%S') bar.datetime = datetime.strftime(bar.datetime, '%Y-%m-%d %H:%M:%S') # barDict = self.dataDict.setdefault(bar.datetime, OrderedDict()) # barDict[bar.vtSymbol] = bar if bar.datetime in self.dataDict: self.dataDict[bar.datetime][bar.vtSymbol] = bar else: self.dataDict[bar.datetime] = {} self.dataDict[bar.datetime][bar.vtSymbol] = bar
def put_service(self): print('in put_svervice') vtSymbol_dict = {} # 缓存中间bar g5 = BarGenerator('min5') while True: time.sleep(1) for id in self.vtSymbol_list: try: fname = self.dss + 'fut/put/min1_' + id + '.csv' #print(fname) df = pd.read_csv(fname) d = dict(df.loc[0,:]) #print(d) #print(type(d)) bar = VtBarData() bar.__dict__ = d bar.vtSymbol = id bar.symbol = id if id not in vtSymbol_dict: vtSymbol_dict[id] = bar elif vtSymbol_dict[id].time != bar.time: vtSymbol_dict[id] = bar bar_min5 = g5.update_bar(bar) if bar_min5 is not None: g5.save_bar(bar_min5) for p in self.portfolio_list: p.onBar(bar_min5, 'min5') for p in self.portfolio_list: p.onBar(bar, 'min1') except Exception as e: # 对文件并发访问,存着读空文件的可能!!! #print('-'*30) #traceback.print_exc() s = traceback.format_exc() to_log(s)
def loadData(self): """加载数据""" for vtSymbol in self.vtSymbol_list: fname = vtSymbol + '.csv' #print(fname) df = pd.read_csv(fname,dtype='str') df = df[df.date>datetime.strftime(self.startDt, '%Y%m%d')] df = df[df.date<datetime.strftime(self.endDt, '%Y%m%d')] df = df.sort_values(by=['date','time']) #print(df) for i, row in df.iterrows(): d = dict(row) bar = VtBarData() bar.vtSymbol = vtSymbol bar.symbol = vtSymbol bar.open = float(d['open']) bar.high = float(d['high']) bar.low = float(d['low']) bar.close = float(d['close']) bar.volume = d['volume'] date = str(d['date']) bar.date = date bar.time = str(d['time']) if '-' in date: bar.datetime = datetime.strptime(bar.date + ' ' + bar.time, '%Y-%m-%d %H:%M:%S') else: bar.datetime = datetime.strptime(bar.date + ' ' + bar.time, '%Y%m%d %H:%M:%S') #bar.datetime = datetime.strftime(bar.datetime, '%Y%m%d %H:%M:%S') barDict = self.dataDict.setdefault(bar.datetime, OrderedDict()) barDict[bar.vtSymbol] = bar self.output(u'%s数据加载完成,总数据量:%s' %(vtSymbol, len(df))) self.output(u'全部数据加载完成')
def worker_1450(self): """盘中推送bar""" to_log('in TradeEngine.worker_1450') for p in self.portfolio_list: for vtSymbol in p.vtSymbolList: #print(vtSymbol) df = None i = 0 while df is None and i < 2: try: i += 1 df = ts.get_realtime_quotes(vtSymbol) except Exception as e: print('error get_realtime_quotes') print(e) time.sleep(0.1) if df is None: to_log('ignore ' + vtSymbol) continue adj_factor = None df1 = None i = 0 while df1 is None and i < 2: try: i += 1 df1 = get_adj_factor(self.dss, vtSymbol) adj_factor = float(df1.at[0, 'adj_factor']) except Exception as e: print('error adj_factor ' + vtSymbol) print(e) time.sleep(0.1) if df1 is None: to_log('ignore ' + vtSymbol) continue hfq_factor = get_hfq_factor(self.dss, vtSymbol) factor = hfq_factor if adj_factor is not None: if abs((adj_factor - hfq_factor) / adj_factor) > 0.01: # 差异大,今天有除权 factor = adj_factor d = df.loc[0, :] bar = VtBarData() bar.vtSymbol = vtSymbol bar.symbol = vtSymbol bar.open = float(d['open']) * factor bar.high = float(d['high']) * factor bar.low = float(d['low']) * factor bar.close = float(d['price']) * factor bar.close_bfq = float(d['price']) date = d['date'].split('-') #去掉字符串中间的'-' date = ''.join(date) bar.date = date bar.time = '00:00:00' bar.datetime = datetime.strptime(bar.date + ' ' + bar.time, '%Y%m%d %H:%M:%S') bar.volume = float(d['volume']) #to_log(vtSymbol+' '+d['price']+' * '+str(factor)+' = '+str(bar.close)) p.onBar(bar) send_email(self.dss, 'worker_1450处理完毕', '')
def worker_1450(self): """盘中推送bar""" to_log('in TradeEngine.worker_1450') print('begin worker_1450') r, dt = self.is_trade_day() if r == False: return self.currentDt = dt for vtSymbol in self.vtSymbolList: #print(vtSymbol) df = None i = 0 while df is None and i < 3: try: i += 1 df = ts.get_realtime_quotes(vtSymbol) except Exception as e: print('error get_realtime_quotes') print(e) time.sleep(1) if df is None: continue code = vtSymbol df1 = None i = 0 while df1 is None and i < 3: try: i += 1 df1 = get_adj_factor(self.dss, code) except Exception as e: print('error adj_factor') print(e) time.sleep(1) if df1 is None: continue factor = float(df1.at[0, 'adj_factor']) d = df.loc[0, :] bar = VtBarData() bar.vtSymbol = vtSymbol bar.symbol = vtSymbol bar.open = float(d['open']) * factor bar.high = float(d['high']) * factor bar.low = float(d['low']) * factor bar.close = float(d['price']) * factor bar.close_bfq = float(d['price']) date = d['date'].split('-') #去掉字符串中间的'-' date = ''.join(date) bar.date = date bar.time = '00:00:00' bar.datetime = datetime.strptime(bar.date + ' ' + bar.time, '%Y%m%d %H:%M:%S') bar.volume = float(d['volume']) barDict = self.dataDict.setdefault(bar.datetime, OrderedDict()) barDict[bar.vtSymbol] = bar self.portfolio.onBar(bar) send_email(self.dss, 'worker_1450处理完毕', '')