Esempio n. 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()
Esempio n. 2
0
File: cli.py Progetto: maocis/ybk
def do_parse(parser, args):
    setup_config(args)
    if args.all:
        parse_all()
    elif args.sites:
        for site in args.sites:
            parse(_get_site_by_abbr(site))
    else:
        parser.print_help()
Esempio n. 3
0
File: cli.py Progetto: maocis/ybk
def do_quote(parser, args):
    setup_config(args)
    if args.qsubparser == 'realtime':
        parser = parser._actions[-1].choices['realtime']
        if args.sites:
            for site in args.sites:
                realtime(site)
        elif args.all:
            realtime_all()
        else:
            parser.print_help()
    elif args.qsubparser == 'history':
        parser = parser._actions[-1].choices['history']
        if args.sites:
            for site in args.sites:
                history(site, args.force)
        elif args.all:
            history_all()
        else:
            parser.print_help()
    else:
        parser.print_help()
Esempio n. 4
0
File: cli.py Progetto: maocis/ybk
def do_cron(parser, args):
    setup_config(args)
    lockfile = '/tmp/ybk.cron.lock'
    path = pathlib.Path(lockfile)

    class doing(object):

        def __enter__(self):
            path.open('w').write('')

        def __exit__(self, type, value, traceback):
            if value:
                crawl_log.exception('出错啦')
            path.unlink()
            return True

    if not path.exists():
        with doing():
            crawl_all()

        now = datetime.utcnow() + timedelta(hours=8)
        with doing():
            if 9 <= now.hour <= 20:
                realtime_all()

        with doing():
            if now.hour == 6 and now.minute < 5:
                history_all()

        with doing():
            if 9 <= now.hour <= 20:
                # 生成所有人的历史收益记录
                ProfitLog.ensure_all_profits()

        # 更新所有交易账号的状态
        if now.hour == 22 and 30 <= now.minute <= 35:
            trade_account_all()
    else:
        crawl_log.info('已有cron在跑, 直接退出')
Esempio n. 5
0
File: cli.py Progetto: maocis/ybk
def do_serve(parser, args):
    from ybk.app import create_app
    conf = setup_config(args)
    app = create_app(conf)
    if args.debug:
        args.loglevel = 'DEBUG'
    app.config['SECRET_KEY'] = conf['secret_key']
    if args.debug:
        app.run(host='0.0.0.0', port=conf['port'], debug=True)
    elif args.production:
        app.run(host='0.0.0.0',
                port=conf['port'],
                threaded=True)
    else:
        parser.print_help()
Esempio n. 6
0
File: trader.py Progetto: sopnic/ybk
                        price = (p2.average_price * p2.quantity -
                                 p1['avg_buy_price'] * p1['quantity']) / quantity
                    else:
                        price = p2.price
                    if price < 0.01:
                        price = Quote.latest_price(p1['exchange'], p1['symbol'])
                    price = int(price * 100) / 100.
                    add_transaction(
                        type_, pair[0], pair[1], price, abs(quantity))
            else:
                # 按现价计算已卖出
                if p1['quantity'] > 0:
                    price = Quote.latest_price(p1['exchange'], p1['symbol'])
                    price = int(price * 100) / 100.
                    add_transaction(
                        'sell', pair[0], pair[1], price, p1['quantity'])
        # 检查新增项
        for pair, p2 in p2pairs.items():
            if pair[0] in ['湖南文交所', '海西文交所', '上海邮币卡',
                           '上海文交所', '中俄邮币卡']:
                continue
            if pair not in p1pairs and p2.quantity > 0:
                price = int(p2.average_price * 100) / 100.
                add_transaction('buy', pair[0], pair[1], price, p2.quantity)


if __name__ == '__main__':
    from ybk.config import setup_config
    setup_config() 
    trade_account_all()
Esempio n. 7
0
                            p2.average_price * p2.quantity -
                            p1['avg_buy_price'] * p1['quantity']) / quantity
                    else:
                        price = p2.price
                    if price < 0.01:
                        price = Quote.latest_price(p1['exchange'],
                                                   p1['symbol'])
                    price = int(price * 100) / 100.
                    add_transaction(type_, pair[0], pair[1], price,
                                    abs(quantity))
            else:
                # 按现价计算已卖出
                if p1['quantity'] > 0:
                    price = Quote.latest_price(p1['exchange'], p1['symbol'])
                    price = int(price * 100) / 100.
                    add_transaction('sell', pair[0], pair[1], price,
                                    p1['quantity'])
        # 检查新增项
        for pair, p2 in p2pairs.items():
            if pair[0] in ['湖南文交所', '海西文交所', '上海邮币卡', '上海文交所', '中俄邮币卡']:
                continue
            if pair not in p1pairs and p2.quantity > 0:
                price = int(p2.average_price * 100) / 100.
                add_transaction('buy', pair[0], pair[1], price, p2.quantity)


if __name__ == '__main__':
    from ybk.config import setup_config
    setup_config()
    trade_account_all()