Beispiel #1
0
 def json(self):
     data = self.dict()
     if data['time']:
         data['time'] = timestamp_to_str(datetime_to_timestamp(data['time']))
     if data['sys_time']:
         data['sys_time'] = timestamp_to_str(datetime_to_timestamp(data['sys_time']))
     return json.dumps(data)
Beispiel #2
0
    def threadDataFanout(self):
        """运行插入线程"""
        while self.active:
            try:
                # print 'current tick queue size:', self.queue.qsize()
                # dbName, collectionName, d = self.queue.get(block=True, timeout=1)
                tick = self.queue.get(block=True, timeout=1)
                symbol = tick.vtSymbol

                dt = datetime.datetime.strptime(
                    ' '.join([tick.date, tick.time]), '%Y%m%d %H:%M:%S.%f')
                tick.ts = datetime_to_timestamp(dt)  # 合约生成时间
                tick.ts_host = int(time.time())  # 主机系统时间

                jsondata = json.dumps(tick.__dict__)
                self.dataFanout('switch0', jsondata, symbol=symbol)

                # -- cache current tick into redis ---
                key_name = XtpMarketSymbolTickFormat.format(
                    symbol=tick.vtSymbol)
                redis = instance.datasourceManager.get('redis').conn
                redis.hmset(key_name, tick.__dict__)

                print 'Tick:', tick.__dict__
                self.ticks_counter += 1
                if len(self.ticks_samples) > 5:
                    del self.ticks_samples[0]
                self.ticks_samples.append(tick.__dict__)

            except Exception as e:
                # self.logger.error( str(e) )
                pass
Beispiel #3
0
def get_message_on_ctp_bars(message, ctx):
    # chan.props.get('bar')

    bar = json.loads(message)
    bar['datetime'] = datetime.datetime.strptime(
        ' '.join([bar.get('date'), bar.get('time')]), '%Y%m%d %H:%M:%S.%f')
    bar['ts'] = datetime_to_timestamp(bar['datetime'])
    tablename = bar.get('_table_')
    if not tablename:
        tablename = bar.get('vtSymbol')
    conn = instance.datasourceManager.get('mongodb').conn
    scale = bar.get('scale', '')
    dbname = ctx.get('channel').cfgs.get('extra',
                                         {}).get('db_name', CTP_BAR_DB_NAME)
    dbname = dbname.format(scale=scale)
    db = conn[dbname]
    table = db[tablename]
    table.insert_one(bar)
Beispiel #4
0
def get_device_share_info():
    """查询本人创建的共享设备信息
        支持 共享设备编号查询 或 设备编号查询
        如果共享记录未创建则创建,并返回
    """
    user = g.user
    share_id = request.values.get('share_id')
    # device_id = request.values.get('device_id')

    link = None
    # if share_id:
    #     link = model.SharedDeviceLink.get(user_id=user.id,_id=ObjectId(share_id))
    # device = model.DeviceUserRelation.get(user_id=user.id, device_id=device_id)
    # if not device:
    #     return ErrorReturn(ErrorDefs.ObjectNotExist,u'设备对象不存在').response

    # link = model.SharedDeviceLink.get(user_id=user.id,device_id=device_id)
    link = model.SharedDeviceLink.get(_id=ObjectId(share_id))

    result = link.dict()
    result['share_id'] = link.id
    result['create_time_str'] = timestamp_to_str(result['create_time'])

    expire_time = parse(link.expire_time) + relativedelta(days=+1)
    expire_time = datetime_to_timestamp(expire_time)
    # 共享设备是否过期
    if expire_time and expire_time < timestamp_current():
        result['is_expired'] = True

    # 访问上限
    if link.user_limit:
        num = model.DeviceUserRelation.collection().find({
            'share_device_link':
            link.id
        }).count()
        if num >= link.user_limit:
            result['is_limited'] = True

    if link.password:
        result['is_password_null'] = False
    else:
        result['is_password_null'] = True

    return CR(result=result).response
Beispiel #5
0
def quoteTickRecv(message, ctx):
    """Tick进入,推送到不同的k线周期处理对象"""
    # print '== current thread: ',threading.current_thread().ident

    data = message
    data = json.loads(data)

    # tick = TickData()
    # object_assign(tick, data, add_new=True)
    # 这个DateTime 是交易日YMD+ 当日HMS
    # 恶心的问题,在于非交易时间接收的tick的HMS居然也是错误的,shit,所以过滤是否有效tick要判断SaveTime字段了

    data['DateTime'] = datetime.datetime.strptime(data['DateTime'],
                                                  '%Y%m%d %H:%M:%S.%f')
    # data['SaveTime'] = datetime.datetime.strptime(data['SaveTime'], '%Y%m%d %H:%M:%S.%f')
    data['SaveTime'] = datetime.datetime.now()

    # 剔除 savetime 与 datetime 相距1分钟的tick
    a = data['DateTime'].replace(2019, 1, 1)
    b = data['SaveTime'].replace(2019, 1, 1)
    if abs(datetime_to_timestamp(a) - datetime_to_timestamp(b)) > 60:
        return

    a = data['DateTime']
    b = datetime.datetime.now()
    if a > b and (a - b).seconds > 120:
        # return
        pass

    symbol = data['InstrumentID']  # 合约名称
    prd_name = TickFix.get_product_code(symbol).upper()

    # where TradeData='2019-10-9'  order by SaveTime asc
    p = TickFix.Products.get(prd_name)
    if p:
        if TickFix.is_in_trading_time(
                data['DateTime'],
                p):  # and TickFix.is_in_trading_time( data['SaveTime'], p):
            rt = data['DateTime']
            # 夜盘时间
            if rt.time() >= datetime.time(
                    hour=21) and rt.time() <= datetime.time(
                        hour=23, minute=59, second=59,
                        microsecond=1000000 - 1):
                # 郑州以晚上21点开盘为交易日,其他交易所以隔日为交易日
                # 为保证夜盘时间在前,将非郑州的行情记录时间(21-00)算为交易日前一天晚间的记录,交易日跳跃多日的情况,夜间开盘日期会不准
                if p['exchange'] != 'ZCE':
                    rt = rt - datetime.timedelta(
                        days=1)  # 夜盘期间的tick日期记录为交易日的前一天

            data['RealTime'] = rt
            day = data['TradingDay']  # 20190917
            trade_date = datetime.datetime.strptime(day, '%Y%m%d')
            data['TradeDate'] = trade_date

            time = '20001010 ' + data['UpdateTime'] + '.' + str(
                data['UpdateMillisec'])
            trade_time = datetime.datetime.strptime(time, '%Y%m%d %H:%M:%S.%f')
            data['TradeTime'] = trade_time
        else:
            return
    else:
        return

    tick = TickData()
    object_assign(tick, data, add_new=True)
    tick.datetime = data['RealTime']

    # print 'symbol:',tick.InstrumentID, ' datetime:',tick.DateTime

    symbol = tick.InstrumentID
    ws = workerset_list.get(symbol)
    if not ws:
        ws = worker.BarWorkerSet(symbol, onBar).init()
        workerset_list[symbol] = ws

    # if TickFilter().filter(tick):  # 是否有效时间段的tick
    ws.onTick(tick)
Beispiel #6
0
def take_share_device():
    """用户接收共享设备,添加成为自己的设备
        如果设置了密码,且用户未上传密码,则提示密码输入
        @:return 返回新设备id
    """
    user = g.user
    auth = g.auth

    share_id = request.values.get('share_id')
    password = request.values.get('password')
    link = model.SharedDeviceLink.get(_id=ObjectId(share_id))
    if not link:
        return ErrorReturn(ErrorDefs.ObjectNotExist, u'共享资源对象不存在').response

    # 共享设备是否是自己的设备(自己注册的或者之前分享来自他人的)
    rel = model.DeviceUserRelation.get(user_id=user.id,
                                       device_id=link.device_id)
    if rel:
        return ErrorReturn(ErrorDefs.ObjectHasExist, u'分享设备已存在').response

    expire_time = parse(link.expire_time) + relativedelta(days=+1)
    expire_time = datetime_to_timestamp(expire_time)
    # 共享设备是否过期
    if expire_time and expire_time < timestamp_current():
        return ErrorReturn(ErrorDefs.ResExpired, u'设备分享已过期').response

    # 访问上限
    if link.user_limit:
        num = model.DeviceUserRelation.collection().find({
            'share_device_link':
            link.id
        }).count()
        if num >= link.user_limit:
            return ErrorReturn(ErrorDefs.ReachLimit, u'分享已达到人数上限').response

    # 检测当前用户是否被阻止
    follower = model.ShareDeviceFollower.get(share_id=share_id,
                                             user_id=user.id)
    if follower and follower.denied:
        return ErrorReturn(ErrorDefs.AccessDenied, u'当前用户访问共享资源受限').response

    #是否要提供密码
    if link.password:
        if password != link.password:
            return ErrorReturn(ErrorDefs.NeedPassword, u'需要提供分享访问密码').response
    #创建设备
    device = model.DeviceUserRelation()
    device.user_id = user.id
    device.device_id = link.device_id
    device.device_name = link.name
    device.update_time = timestamp_current()
    device.is_share_device = True
    device.share_user_id = link.user_id
    device.share_device_link = link.id
    device.save()
    #添加到分享者列表
    if not follower:
        follower = model.ShareDeviceFollower()
        follower.user_id = user.id
        follower.open_id = auth.open_id
        follower.share_id = link.id
        follower.device_id = device.id
        follower.create_time = timestamp_current()
        wxuser = model.WxUser.get(open_id=auth.open_id)
        if wxuser:
            follower.avatar_url = wxuser.avatarUrl
            follower.nick_name = wxuser.nickName
        follower.save()

    return CR(result=str(device.id)).response