VeighNa量化社区
你的开源社区量化交易平台
Member
avatar
加入于:
帖子: 187
声望: 59

作者:量化学习菜鸟 ;来源:维恩的派论坛
 
 

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
Member
avatar
加入于:
帖子: 5
声望: 0

如果生成4h周期的k线,参数
self.zhouqi_val = 4
self.zhouqi = ‘h’

生成5分中k线
self.zhouqi_val = 5
self.zhouqi = ‘m’

Member
加入于:
帖子: 12
声望: 1

日线怎么生成呢?

Member
avatar
加入于:
帖子: 5
声望: 0
        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 or (bar.datetime.hour == 11 and bar.datetime.minute == 30)
        elif self.zhouqi =='d':
            dt = bar.datetime.day
            kaiguan = dt % self.zhouqi_val == 0  and bar.datetime.hour == 15 and bar.datetime.minute == 00

股票版,带日线。

© 2015-2022 上海韦纳软件科技有限公司
备案服务号:沪ICP备18006526号

沪公网安备 31011502017034号

【用户协议】
【隐私政策】
【免责条款】