예제 #1
0
def sync_trade_data(id='', bond_code=''):
    bond = None
    if id != '':
        if 'new_grid_trade_data.html' in str(request.url_rule):
            bond = db.session.query(TradeHistory).filter(
                TradeHistory.id == id).first()
            # 先关闭session, 在修改model, 否则会触发update
            db.session.close()
            bond.id = ''
        else:
            bond = db.session.query(HoldBond).filter(HoldBond.id == id).first()
    elif bond_code != '':
        bond = db.session.query(ChangedBond).filter(
            ChangedBond.bond_code == bond_code).first()
        # 先关闭session, 在修改model, 否则会触发update
        db.session.close()
        bond.id = ''

    options = get_strategy_options_html(None if bond is None else (
        bond.strategy_type if hasattr(bond, 'strategy_type') else None))

    return render_template("sync_trade_data.html",
                           bond=bond,
                           navbar=build_personal_nav_html(request.url_rule),
                           strategy_options=options)
예제 #2
0
def edit_changed_bond_select(bond_code=''):
    bond = None
    if bond_code != '':
        bond = db.session.query(ChangedBondSelect).filter(ChangedBondSelect.bond_code == bond_code).first()

    type = None
    if bond is not None:
        type = bond.strategy_type
    options = get_strategy_options_html(type)

    navbar = build_select_nav_html('/edit_changed_bond_select.html')

    return render_template("edit_changed_bond_select.html", bond=bond, strategy_options=options, navbar=navbar)
예제 #3
0
def save_changed_bond_select():
    id = request.form['id']
    changed_bond_select = None
    if id is None or id.strip(' ') == '':
        changed_bond_select = ChangedBondSelect()
    else:
        changed_bond_select = db.session.query(ChangedBondSelect).filter(
            ChangedBondSelect.id == id).first()

    bond_code = request.form['bond_code']
    if bond_code is None or bond_code.strip(' ') == '':
        raise Exception('转债代码不能为空')

    changed_bond_select.bond_code = bond_code

    cb_name_id = request.form['cb_name_id']
    if cb_name_id is None or cb_name_id.strip(' ') == '':
        raise Exception('转债名称不能为空')

    changed_bond_select.cb_name_id = cb_name_id
    changed_bond_select.pinyin = request.form['pinyin']

    strategy_type = request.form['strategy_type']
    if strategy_type is not None and strategy_type.strip(' ') != '':
        changed_bond_select.strategy_type = strategy_type

    memo = request.form['memo']
    # if memo is not None and memo.strip(' ') != '':
    changed_bond_select.memo = memo

    changed_bond_select.modify_date = datetime.now()
    if id is None or id.strip(' ') == '':
        changed_bond_select.create_date = datetime.now()
        db.session.add(changed_bond_select)
    db.session.commit()

    options = get_strategy_options_html(None)

    navbar = build_select_nav_html('/edit_changed_bond_select.html')

    return render_template("edit_changed_bond_select.html",
                           result='save is successful',
                           strategy_options=options,
                           navbar=navbar)
예제 #4
0
def save_trade_data():
    id = request.form['id']
    hold_bond = None
    is_new = id is None or id.strip(' ') == ''
    if is_new:
        # 新增操作
        hold_bond = HoldBond()
    else:
        # 更新操作
        hold_bond = db.session.query(HoldBond).filter(HoldBond.id == id).first()

    bond_code = request.form['bond_code']
    if bond_code is None or bond_code.strip(' ') == '':
        raise Exception('转债代码不能为空')

    hold_bond.bond_code = bond_code

    is_sh_market = bond_code.startswith('11')

    if is_sh_market:
        hold_bond.hold_unit = 10
    else:
        hold_bond.hold_unit = 1

    cb_name_id = request.form['bond_name']
    if cb_name_id is None or cb_name_id.strip(' ') == '':
        raise Exception('转债名称不能为空')

    hold_bond.cb_name_id = cb_name_id

    trade_amount = request.form['trade_amount']
    if trade_amount is None or trade_amount.strip(' ') == '':
        raise Exception('成交量不能为空')

    if int(trade_amount) < 0:
        raise Exception("成交量必须大于0")

    trade_price = request.form['trade_price']
    if trade_price is None or trade_price.strip(' ') == '':
        raise Exception('成交价不能为空')

    direction = request.form['direction']
    if direction is None or direction.strip(' ') == '':
        raise Exception('必须指定买卖方向')

    is_sell = direction == 'sell'
    if is_sell:
        if int(trade_amount) > hold_bond.hold_amount:
            raise Exception("成交量(" + trade_amount + ")不能超过持有量(" + str(hold_bond.hold_amount) + ")")

    account = request.form['account']
    if account is None or account.strip(' ') == '':
        raise Exception("必须指定交易账户")

    hold_bond.account = account

    # 计算持仓成本
    fee = trade_utils.calc_hold_price(hold_bond, direction, trade_amount, trade_price)

    strategy_type = request.form['strategy_type']
    if strategy_type is None or strategy_type.strip(' ') == '':
        raise Exception('必须指定策略类型')

    hold_bond.strategy_type = strategy_type
    hold_bond.pinyin = request.form['pinyin']

    hold_bond.modify_date = datetime.now()
    if is_new:
        # 增加开始时间
        hold_bond.start_date = datetime.now()
        db.session.add(hold_bond)
        # 获取id, 强刷
        db.session.flush()

    # 保存成交记录
    trade_history = TradeHistory()
    trade_history.bond_code = bond_code
    trade_history.fee = fee
    user_id = session.get('_user_id')
    trade_history.owner_id = user_id
    trade_history.cb_name_id = cb_name_id
    trade_history.account = account
    trade_history.strategy_type = strategy_type
    trade_history.price = trade_price
    trade_history.amount = -int(trade_amount) if is_sell else trade_amount
    trade_history.hold_id = hold_bond.id
    db.session.add(trade_history)

    db.session.commit()

    options = get_strategy_options_html(None)


    return render_template("sync_trade_data.html", bond=None, navbar=build_personal_nav_html(request.url_rule), result='operation is successful', strategy_options=options)