Example #1
0
def save_quotes(q, c, first_quote=False):
    if first_quote:
        # 看一下需不需要补齐申购数据
        if not c.name.endswith('指数'):
            if not c.offers_at or abs((c.offers_at - q['quote_at']).days) >= 7:
                c.offers_at = q['quote_at'] - timedelta(days=2)
                c.offer_price = q['high'] / 1.3
                if 'quantity' in q:
                    # 假定申购是全部数量的1/10
                    c.offer_quantity = q['quantity'] // 10
                log.info('补齐数据:{}_{}, 申购价:{}, 申购量:{}, 时间:{}'
                         ''.format(c.exchange, c.symbol, c.offer_price,
                                   c.offer_quantity, c.offers_at))
                # 然而实际上不补齐
                # c.upsert()

    Quote(q).upsert()
Example #2
0
def realtime(site):
    conf = get_conf(site)
    exchange = conf['abbr']
    url = conf['quote']['realtime']['url']
    type_ = conf['quote']['realtime']['type']
    today = datetime.utcnow().replace(
        minute=0, second=0, microsecond=0) + timedelta(hours=8)

    if not url:
        log.warning('{}尚未配置实时行情url'.format(exchange))
        return
    if today.hour < 9 or today.hour > 22:
        log.warning('不在9点到22点之间, 不做解析')
        return

    today = today.replace(hour=0)
    text = session.get(url, timeout=(3, 7)).text
    quotes = parse_quotes(type_, text)
    saved = 0
    for q in quotes:
        Collection.update_one({'exchange': exchange,
                               'symbol': q['symbol'].strip()},
                              {'$set': {'name': q['name']}},
                              upsert=True)
        q['exchange'] = exchange
        q['quote_type'] = '1d'
        q['quote_at'] = today
        if q['open_'] in ['—', '-', None, '']:
            continue
        else:
            # 找到上一个交易日的数据, 如果和lclose不符则舍弃
            # 需要保证数据每天更新/不足时需要把日线补足才能正常显示
            lq = Quote.query_one({'exchange': exchange,
                                  'symbol': q['symbol'].strip(),
                                  'quote_type': '1d',
                                  'quote_at': {'$lt': today}},
                                 sort=[('quote_at', -1)])
            if not lq or abs(lq.close - q['lclose']) < 0.01:
                Quote(q).upsert()
                saved += 1

    log.info('{} 导入 {}/{} 条实时交易记录'.format(exchange, saved, len(quotes)))