vn.py官网
开源量化社区
Member
avatar
加入于:
帖子: 14
声望: 0

Traceback (most recent call last):
File "C:\vnstudio\lib\site-packages\vnpy\trader\ui\widget.py", line 893, in send_order
self.main_engine.send_order(req, gateway_name)
File "C:\vnstudio\lib\site-packages\vnpy\app\paper_account\engine.py", line 179, in send_order
active_orders.pop(orderid)
UnboundLocalError: local variable 'active_orders' referenced before assignment

Member
avatar
加入于:
帖子: 14
声望: 0

增加对 active_orders=None 也不行,不过从逻辑上讲,这里条件增加新变量,又没有初始化,然后在条件判断中使用,是有风险,大佬,能帮忙看看吗?
def send_order(self, req: OrderRequest, gateway_name: str) -> str:
""""""
contract: ContractData = self.main_engine.get_contract(req.vt_symbol)
if not contract:
self.write_log(f"委托失败,找不到该合约{req.vt_symbol}")
return ""

    self.order_count += 1
    now = datetime.now().strftime("%y%m%d%H%M%S")
    orderid = now + str(self.order_count)
    vt_orderid = f"{GATEWAY_NAME}.{orderid}"

    # Put simulated order update event from gateway
    order = req.create_order_data(orderid, GATEWAY_NAME)
    self.put_event(EVENT_ORDER, copy(order))

    # Check if order is valid
    updated_position = self.check_order_valid(order, contract)

    # Put simulated order update event from exchange
    if order.status != Status.REJECTED:
        order.datetime = datetime.now(LOCAL_TZ)
        order.status = Status.NOTTRADED
        active_orders = self.active_orders.setdefault(order.vt_symbol, {})
        active_orders[orderid] = order

    self.put_event(EVENT_ORDER, copy(order))

    # Update position frozen for close order
    if updated_position:
        self.put_event(EVENT_POSITION, copy(updated_position))

    # Cross order immediately with last tick data
    if self.instant_trade:
        tick = self.ticks.get(order.vt_symbol, None)
        if tick:
            self.cross_order(order, tick)

            if not order.is_active():
                active_orders.pop(orderid)

    return vt_orderid
Member
avatar
加入于:
帖子: 14
声望: 0

原因已找到,先不起用paper服务,就没有问题,应该是ctp和paper仓位冲突的问题

© 2015-2019 上海韦纳软件科技有限公司
备案服务号:沪ICP备18006526号-3

沪公网安备 31011502017034号