qmt量化接口查询功能封装源码

查询资产、当前持仓、当日成交、当日委托等:

@api.route('/query/assets', methods=['GET'])
async def query_assets(request):
    '''
    查询总资产
    '''
    asset = trader.xt_trader.query_stock_asset(trader.account)
    return response.json({"总资产": asset.total_asset, "现金": asset.cash, "持仓市值": asset.market_value, "冻结金额": asset.frozen_cash})
 
@api.route('/query/holding', methods=['GET'])
async def query_holding(request):
    '''
    查询当前持仓
    '''
    holding = []
    for p in trader.xt_trader.query_stock_positions(trader.account):
        holding.append([{'股票代码': p.stock_code, '持仓': p.volume, '可用持仓': p.can_use_volume, '成本': p.open_price, '持仓市值': p.market_value}])
    return response.json(holding, ensure_ascii=False)
 
@api.route('/query/trade', methods=['GET'])
async def query_trade(request):
    '''
    查询当日成交
    '''
    trades = trader.xt_trader.query_stock_trades(trader.account)
 
    result = []
    for trade in trades:
        result.append({'股票代码': trade.stock_code, '委托类型': trade.order_type, '成交数量': trade.traded_volume, '成交均价': trade.traded_price, '成交金额': trade.traded_amount, '成交时间': trade.traded_time, "成交编号": trade.traded_id, "柜台合同编号": trade.order_sysid})
    return response.json(result, ensure_ascii=False)
 
@api.route('/query/order', methods=['GET'])
async def query_order(request):
    '''
    查询当日委托
    '''
    order_id = request.args.get('order_id', '')
    if order_id == '':
        orders = trader.xt_trader.query_stock_orders(trader.account)
    else:
        orders = trader.xt_trader.query_stock_order(trader.account, int(order_id))
        # 订单不存在,下单失败
        if orders is None:
            return response.json([], ensure_ascii=False)
        orders = [orders]
    result = []
    for order in orders:
        result.append({'股票代码': order.stock_code, '委托数量': order.order_volume, '成交数量': order.traded_volume, '委托价格': order.price, '委托类型': order.order_type, '委托状态': order_status.get(order.order_status), "订单编号": order.order_id, "柜台合同编号": order.order_sysid, "报单时间": order.order_time})
    return response.json(result, ensure_ascii=False)

你可能感兴趣的:(交易接口,servlet,python,java)