Пример #1
0
    def processLine(self, line):
        historyData = line.split(',')
        #historyDataLen = len(historyData)
        symbol = historyData[2]
        #print 'processLine, symbol:' + symbol

        #从list转化为tick对象
        historytick = VtTickData()
        historytick._id = historyData[0]
        historytick.gatewayName = 'CTP'
        historytick.symbol = symbol
        historytick.TradingDay = historyData[1]
        historytick.exchange = historyData[3]
        historytick.vtSymbol = historytick.symbol  # '.'.join([tick.symbol, tick.exchange])

        historytick.lastPrice = self.convertFloatZero(historyData[5])
        #lastVolume
        historytick.volume = historyData[12]
        historytick.openInterest = historyData[14]

        UpdateMillisec = int(historyData[20])
        historytick.time = '.'.join([historyData[19], str(UpdateMillisec/ 100)])
        historytick.date = historyData[42]
        historytick.datetime = datetime.strptime(' '.join([historytick.date, historytick.time]), '%Y%m%d %H:%M:%S.%f')

        historytick.openPrice = self.convertFloatZero(historyData[9])
        historytick.highPrice = self.convertFloatZero(historyData[10])
        historytick.lowPrice = self.convertFloatZero(historyData[11])
        historytick.preClosePrice = self.convertFloatZero(historyData[12])

        historytick.ClosePrice = self.convertFloatZero(historyData[15])
        historytick.SettlementPrice = self.convertFloatZero(historyData[16])
        historytick.upperLimit = self.convertFloatZero(historyData[17])
        historytick.lowerLimit = self.convertFloatZero(historyData[18])

        # CTP只有一档行情
        historytick.bidPrice1 = self.convertFloatZero(historyData[21])
        historytick.bidVolume1 = historyData[22]
        historytick.askPrice1 = self.convertFloatZero(historyData[23])
        historytick.askVolume1 = historyData[24]

        historytick.AveragePrice = self.convertFloatZero(historyData[41])

        return historytick
Пример #2
0
def generateVtTick(row, symbol):
    """生成K线"""
    tick = VtTickData()
    tick.symbol = symbol
    tick.vtSymbol = symbol
    
    tick.lastPrice = row['last']
    tick.volume = row['volume']
    tick.openInterest = row['open_interest']
    tick.datetime = row.name
    tick.openPrice = row['open']
    tick.highPrice = row['high']
    tick.lowPrice = row['low']
    tick.preClosePrice = row['prev_close']
    tick.upperLimit = row['limit_up']
    tick.lowerLimit = row['limit_down']
    
    tick.bidPrice1 = row['b1']
    tick.bidPrice2 = row['b2']
    tick.bidPrice3 = row['b3']
    tick.bidPrice4 = row['b4']
    tick.bidPrice5 = row['b5']
    
    tick.bidVolume1 = row['b1_v']
    tick.bidVolume2 = row['b2_v']
    tick.bidVolume3 = row['b3_v']
    tick.bidVolume4 = row['b4_v']
    tick.bidVolume5 = row['b5_v']    
    
    tick.askPrice1 = row['a1']
    tick.askPrice2 = row['a2']
    tick.askPrice3 = row['a3']
    tick.askPrice4 = row['a4']
    tick.askPrice5 = row['a5']
    
    tick.askVolume1 = row['a1_v']
    tick.askVolume2 = row['a2_v']
    tick.askVolume3 = row['a3_v']
    tick.askVolume4 = row['a4_v']
    tick.askVolume5 = row['a5_v']        
    
    return tick
Пример #3
0
def generateVtTick(row, symbol):
    """生成K线"""
    tick = VtTickData()
    tick.symbol = symbol
    tick.vtSymbol = symbol

    tick.lastPrice = row['last']
    tick.volume = row['volume']
    tick.openInterest = row['open_interest']
    tick.datetime = row.name
    tick.openPrice = row['open']
    tick.highPrice = row['high']
    tick.lowPrice = row['low']
    tick.preClosePrice = row['prev_close']
    tick.upperLimit = row['limit_up']
    tick.lowerLimit = row['limit_down']

    tick.bidPrice1 = row['b1']
    tick.bidPrice2 = row['b2']
    tick.bidPrice3 = row['b3']
    tick.bidPrice4 = row['b4']
    tick.bidPrice5 = row['b5']

    tick.bidVolume1 = row['b1_v']
    tick.bidVolume2 = row['b2_v']
    tick.bidVolume3 = row['b3_v']
    tick.bidVolume4 = row['b4_v']
    tick.bidVolume5 = row['b5_v']

    tick.askPrice1 = row['a1']
    tick.askPrice2 = row['a2']
    tick.askPrice3 = row['a3']
    tick.askPrice4 = row['a4']
    tick.askPrice5 = row['a5']

    tick.askVolume1 = row['a1_v']
    tick.askVolume2 = row['a2_v']
    tick.askVolume3 = row['a3_v']
    tick.askVolume4 = row['a4_v']
    tick.askVolume5 = row['a5_v']

    return tick
Пример #4
0
    def add_gap_ticks(self):
        """
        补充缺失的分时数据
        :return:
        """
        ds = DataSource()
        for vtSymbol in self.renkoDict.keys():
            renkobar_list = self.renkoDict.get(vtSymbol, [])
            cache_bars = None
            cache_start_date = None
            cache_end_date = None
            for renko_bar in renkobar_list:
                # 通过mongo获取最新一个Renko bar的数据日期close时间
                last_renko_dt = self.get_last_datetime(renko_bar.name)

                # 根据日期+vtSymbol,像datasource获取分钟数据,以close价格,转化为tick,推送到renko_bar中
                if last_renko_dt is not None:
                    start_date = last_renko_dt.strftime('%Y-%m-%d')
                else:
                    start_date = (datetime.now() -
                                  timedelta(days=90)).strftime('%Y-%m-%d')

                end_date = (datetime.now() +
                            timedelta(days=5)).strftime('%Y-%m-%d')
                self.writeDrLog(u'从datasource获取{}数据,开始日期:{}'.format(
                    vtSymbol, start_date))
                if cache_bars is None or cache_start_date != start_date or cache_end_date != end_date:
                    fields = [
                        'open', 'close', 'high', 'low', 'volume',
                        'open_interest', 'limit_up', 'limit_down',
                        'trading_date'
                    ]
                    cache_bars = ds.get_price(order_book_id=vtSymbol,
                                              start_date=start_date,
                                              end_date=end_date,
                                              frequency='1m',
                                              fields=fields)
                    cache_start_date = start_date
                    cache_end_date = end_date
                    if cache_bars is not None:
                        total = len(cache_bars)
                        self.writeDrLog(u'一共获取{}条{} 1分钟数据'.format(
                            total, vtSymbol))

                if cache_bars is not None:
                    self.writeDrLog(u'推送分时数据tick:{}到:{}'.format(
                        vtSymbol, renko_bar.name))
                    for idx in cache_bars.index:
                        row = cache_bars.loc[idx]
                        tick = VtTickData()
                        tick.vtSymbol = vtSymbol
                        tick.symbol = vtSymbol
                        last_bar_dt = datetime.strptime(
                            str(idx), '%Y-%m-%d %H:%M:00')
                        tick.datetime = last_bar_dt - timedelta(minutes=1)
                        tick.date = tick.datetime.strftime('%Y-%m-%d')
                        tick.time = tick.datetime.strftime('%H:%M:00')

                        if tick.datetime.hour >= 21:
                            if tick.datetime.isoweekday() == 5:
                                # 星期五=》星期一
                                tick.tradingDay = (
                                    tick.datetime +
                                    timedelta(days=3)).strftime('%Y-%m-%d')
                            else:
                                # 第二天
                                tick.tradingDay = (
                                    tick.datetime +
                                    timedelta(days=1)).strftime('%Y-%m-%d')
                        elif tick.datetime.hour < 8 and tick.datetime.isoweekday(
                        ) == 6:
                            # 星期六=>星期一
                            tick.tradingDay = (
                                tick.datetime +
                                timedelta(days=2)).strftime('%Y-%m-%d')
                        else:
                            tick.tradingDay = tick.date
                        tick.upperLimit = float(row['limit_up'])
                        tick.lowerLimit = float(row['limit_down'])
                        tick.lastPrice = float(row['close'])
                        tick.askPrice1 = float(row['close'])
                        tick.bidPrice1 = float(row['close'])
                        tick.volume = int(row['volume'])
                        tick.askVolume1 = tick.volume
                        tick.bidVolume1 = tick.volume

                        if last_renko_dt is not None and tick.datetime <= last_renko_dt:
                            continue
                        renko_bar.onTick(tick)