Exemplo n.º 1
0
def trade_account_update():
    ids = request.form.getlist('ids[]')
    try:
        trade_accounts = TradeAccount.query({'_id': {'$in': ids}})
        for ta in trade_accounts:
            update_trade_account(ta)
    except Exception as e:
        log.exception(str(e))
        return jsonify(status=500, reason=str(e))

    return jsonify(status=200, reason='')
Exemplo n.º 2
0
def trade_account_update():
    ids = request.form.getlist('ids[]')
    try:
        trade_accounts = TradeAccount.query({'_id': {'$in': ids}})
        for ta in trade_accounts:
            update_trade_account(ta)
    except Exception as e:
        log.exception(str(e))
        return jsonify(status=500, reason=str(e))

    return jsonify(status=200, reason='')
Exemplo n.º 3
0
Arquivo: trader.py Projeto: sopnic/ybk
def accounting(user):
    """ 对账号进行自动记账(如果用户已配置) """
    operated_at = datetime.utcnow() + timedelta(hours=8)

    def add_transaction(type_, exchange, symbol, price, quantity):
        t = Transaction({
            'user': user._id,
            'type_': type_,
            'operated_at': operated_at,
            'exchange': exchange,
            'symbol': symbol,
            'price': price,
            'quantity': quantity,
        })
        t.save()
        if not Position.do_op(t):
            t.remove()

    if user.auto_accounting:
        op = Position.user_position(user._id)
        p2pairs = {}
        for ta in TradeAccount.query({'user': user._id}):
            for p in ta.position or []:
                pair = (ta.exchange, p.symbol)
                if pair not in p2pairs:
                    p2pairs[pair] = p
                else:
                    pp = p2pairs[pair]
                    amount = p.average_price * p.quantity + \
                        pp.average_price * pp.quantity
                    p.quantity += pp.quantity
                    if p.quantity > 0:
                        p.average_price = amount / p.quantity
                    p2pairs[pair] = p
        p1pairs = {(p1['exchange'], p1['symbol']): p1 for p1 in op}
        # 检查更改项
        for p1 in op:
            pair = p1['exchange'], p1['symbol']
            if pair[0] in ['湖南文交所', '海西文交所', '上海邮币卡',
                           '上海文交所', '中俄邮币卡']:
                continue
            if pair in p2pairs:
                p2 = p2pairs[pair]
                quantity = p2.quantity - p1['quantity']
                if quantity != 0:
                    type_ = 'buy' if quantity > 0 else 'sell'
                    if type_ == 'buy':
                        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)
Exemplo n.º 4
0
Arquivo: trader.py Projeto: sopnic/ybk
def trade_account_all():
    """ 更新全部账户 """
    for ta in TradeAccount.query():
        Trader.traders = {}
        update_trade_account(ta)
Exemplo n.º 5
0
def accounting(user):
    """ 对账号进行自动记账(如果用户已配置) """
    operated_at = datetime.utcnow() + timedelta(hours=8)

    def add_transaction(type_, exchange, symbol, price, quantity):
        t = Transaction({
            'user': user._id,
            'type_': type_,
            'operated_at': operated_at,
            'exchange': exchange,
            'symbol': symbol,
            'price': price,
            'quantity': quantity,
        })
        t.save()
        if not Position.do_op(t):
            t.remove()

    if user.auto_accounting:
        op = Position.user_position(user._id)
        p2pairs = {}
        for ta in TradeAccount.query({'user': user._id}):
            for p in ta.position or []:
                pair = (ta.exchange, p.symbol)
                if pair not in p2pairs:
                    p2pairs[pair] = p
                else:
                    pp = p2pairs[pair]
                    amount = p.average_price * p.quantity + \
                        pp.average_price * pp.quantity
                    p.quantity += pp.quantity
                    if p.quantity > 0:
                        p.average_price = amount / p.quantity
                    p2pairs[pair] = p
        p1pairs = {(p1['exchange'], p1['symbol']): p1 for p1 in op}
        # 检查更改项
        for p1 in op:
            pair = p1['exchange'], p1['symbol']
            if pair[0] in ['湖南文交所', '海西文交所', '上海邮币卡', '上海文交所', '中俄邮币卡']:
                continue
            if pair in p2pairs:
                p2 = p2pairs[pair]
                quantity = p2.quantity - p1['quantity']
                if quantity != 0:
                    type_ = 'buy' if quantity > 0 else 'sell'
                    if type_ == 'buy':
                        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)
Exemplo n.º 6
0
def trade_account_all():
    """ 更新全部账户 """
    for ta in TradeAccount.query():
        Trader.traders = {}
        update_trade_account(ta)