Exemple #1
0
def sync_collections():
    from ybk.config import setup_config
    from ybk.models import Collection as C1
    from ybk.models import Quote as Q1
    setup_config()
    for c in C1.query():
        print(c.exchange, c.symbol)
        td = Q1.count({'exchange': c.exchange,
                       'symbol': c.symbol,
                       'type_': '1d'}) + 1
        if td == 1:
            if not c.offers_at:
                # 没录入过, 基本上会挂
                continue

            # 如果K线不存在, 可能是交易行情无法获取, 直接用估算数字
            td = (datetime.utcnow() - c.offers_at).days - 1

        c2 = Collection({
            'exchange': c.exchange,
            'symbol': c.symbol,
            'name': c.name,
            'trade_day': td,
        })
        c2.upsert()
Exemple #2
0
def update_collection(collection):
    """ 更新藏品的账号信息 """
    accounts = list(Account.query({'collections': collection}))
    users = set()
    exchange, symbol = collection.split('_')
    name = ''
    quantity = 0
    buy_price = 0
    for account in accounts:
        users.add(account.user)
    summary = accounts_summary(accounts)
    for p in summary['position']:
        if collection.endswith(p.symbol):
            quantity = p.quantity
            buy_price = p.average_price
            name = p.name
            break

    users = list(users)
    c = Collection.query_one({'_id': collection})
    if not c:
        c = Collection({'_id': collection})
    if exchange:
        c.exchange = exchange
    if symbol:
        c.symbol = symbol
    if name:
        c.name = name
    c.users = users or []
    c.accounts = [a._id for a in accounts] or []
    c.quantity = quantity
    c.buy_price = buy_price
    try:
        c.upsert()
    except:
        pass
Exemple #3
0
def update_collections(collections=None):
    for collection in collections or [c._id for c in Collection.query()]:
        update_collection(collection)