作者:量化学习菜鸟 ;来源:维恩的派论坛
def onBar(self, bar):
"""收到Bar推送(必须由用户继承实现)"""
if self.zhouqi == 'm':
dt = bar.datetime.minute
kaiguan = dt % self.zhouqi_val == 0
elif self.zhouqi == 'h':
dt = bar.datetime.hour
kaiguan = dt % self.zhouqi_val == 0 and bar.datetime.minute == 0
if kaiguan:
if self.fiveBar:
fiveBar = self.fiveBar
fiveBar.high = max(fiveBar.high, bar.high)
fiveBar.low = min(fiveBar.low, bar.low)
fiveBar.close = bar.close
# 推送
self.onFiveBar(fiveBar)
# 清空缓存
self.fiveBar = None
else:
fiveBar = VtBarData()
fiveBar.vtSymbol = bar.vtSymbol
fiveBar.symbol = bar.symbol
fiveBar.exchange = bar.exchange
fiveBar.open = bar.open
fiveBar.high = bar.high
fiveBar.low = bar.low
fiveBar.close = bar.close
fiveBar.date = bar.date
fiveBar.time = bar.time
fiveBar.datetime = bar.datetime
#发出去
self.onFiveBar(fiveBar)
#清空
self.fiveBar = None
else:
# 如果没有缓存则新建
#print 'dt zhengchu//0'
if not self.fiveBar:
fiveBar = VtBarData()
fiveBar.vtSymbol = bar.vtSymbol
fiveBar.symbol = bar.symbol
fiveBar.exchange = bar.exchange
fiveBar.open = bar.open
fiveBar.high = bar.high
fiveBar.low = bar.low
fiveBar.close = bar.close
fiveBar.date = bar.date
fiveBar.time = bar.time
fiveBar.datetime = bar.datetime
self.fiveBar = fiveBar
else:
fiveBar = self.fiveBar
fiveBar.high = max(fiveBar.high, bar.high)
fiveBar.low = min(fiveBar.low, bar.low)
fiveBar.close = bar.close