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 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 _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 onBar(self, bar, minx='min1'): """引擎新推送过来bar,传递给每个signal""" # 不处理不相关的品种 if bar.vtSymbol not in self.vtSymbolList: return if minx != 'min15': # 本策略为min15 return if self.result.date != bar.date + ' ' + bar.time: previousResult = self.result self.result = DailyResult(bar.date + ' ' + bar.time) self.resultList.append(self.result) if previousResult: self.result.updateClose(previousResult.closeDict) # 将bar推送给signal for signal in self.signalDict[bar.vtSymbol]: signal.onBar(bar, minx) # print(bar.vtSymbol, bar.time) """ 在此实现P层的业务控制逻辑 为每一个品种来检查是否触发下单条件 # 开始处理组合关心的bar , 尤其是品种对价差的加工和处理 """ if self.got_dict[self.symbol_g] == True and self.got_dict[self.symbol_d] == True: for signal in self.signalDict[self.dual_name]: for s_g in self.signalDict[self.symbol_g]: price_g = s_g.bar.close for s_d in self.signalDict[self.symbol_d]: price_d = s_d.bar.close price_gap = price_g - price_d bar_dual = VtBarData() bar_dual.vtSymbol = self.dual_name bar_dual.open = price_gap bar_dual.high = price_gap bar_dual.low = price_gap bar_dual.close = price_gap bar_dual.date = bar.date bar_dual.time = bar.time bar_dual.datetime = bar.date + ' ' + bar.time # 将bar推送给signal signal.onBar(bar_dual, minx) self.result.updateBar(bar_dual) # print(bar_dual.vtSymbol, bar_dual.datetime ) self.got_dict[self.symbol_g] = False self.got_dict[self.symbol_d] = False self.result.updateBar(bar) self.result.updatePos(self.posDict)
def onBar(self, bar, minx='min1'): """引擎新推送过来bar,传递给每个signal""" # 不处理不相关的品种 if bar.vtSymbol not in self.vtSymbolList: return if minx != 'min5': # 本策略为min5 return if self.result.date != bar.date + ' ' + bar.time: previousResult = self.result self.result = DailyResult(bar.date + ' ' + bar.time) self.resultList.append(self.result) if previousResult: self.result.updateClose(previousResult.closeDict) # 将bar推送给signal for signal in self.signalDict[bar.vtSymbol]: signal.onBar(bar, minx) # print(bar.vtSymbol, bar.time) """ 在此实现P层的业务控制逻辑 为每一个品种来检查是否触发下单条件 # 开始处理组合关心的bar , 尤其是品种对价差的加工和处理 """ if self.got_dict[self.symbol_a] == True and self.got_dict[ self.symbol_b] == True: for signal in self.signalDict[self.dual_name]: for s_a in self.signalDict[self.symbol_a]: ask_price_a = float(s_a.bar.AskPrice) # 挂卖价 bid_price_a = float(s_a.bar.BidPrice) # 挂买价 for s_b in self.signalDict[self.symbol_b]: ask_price_b = float(s_b.bar.AskPrice) # 挂卖价 bid_price_b = float(s_b.bar.BidPrice) # 挂买价 long_price = ask_price_a - bid_price_b short_price = bid_price_a - ask_price_b bar_dual = VtBarData() bar_dual.vtSymbol = self.dual_name bar_dual.high = short_price bar_dual.low = long_price bar_dual.date = bar.date bar_dual.time = bar.time bar_dual.datetime = bar.date + ' ' + bar.time # 将bar推送给signal signal.onBar(bar_dual, minx) self.result.updateBar(bar_dual) # print(bar_dual.vtSymbol, bar_dual.datetime ) self.got_dict[self.symbol_a] = False self.got_dict[self.symbol_b] = False self.result.updateBar(bar) self.result.updatePos(self.posDict)
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/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 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 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 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处理完毕', '')