vn.py量化社区
By Traders, For Traders.
Member
avatar
加入于:
帖子: 5
声望: 2

本文要解决的问题有两个

  1. 在vnpy中通过IB下单,订单状态一直显示“提交中”或“未成交”。
  2. 在vnpy中订阅/下单,api提示ambiguous symbol。

其实这两个问题都跟交易所有关系。

首先来搞问题 1

问题描述:

在vnpy中,下单的时候经常会见到这样的情况

description

订单的状态一直就是“未成交”或者是“提交中”,但是IB的trader workstation却提示我说订单已经交易完成。

原因

这是因为vnpy里面自带的交易所列表不全的原因。在vnpy > trader > constant 的 Exchange类中我们可以看到,vnpy自带的交易所就只有这么一些

description

而这是远远不够的,我们可以在IB的trader workstation中看到还有其他的交易所

description

而出问题的,就是这些在别的交易所成交的股票。

解决办法

所以,我们只需要把vnpy的交易所列表补全就好了,在IB的Exchange Listings里面, 我们可以把这些交易所手动添加进去vnpy里面

class Exchange(Enum):
    """
    Exchange.
    """
    # Chinese
    CFFEX = "CFFEX"         # China Financial Futures Exchange
    SHFE = "SHFE"           # Shanghai Futures Exchange
    CZCE = "CZCE"           # Zhengzhou Commodity Exchange
    DCE = "DCE"             # Dalian Commodity Exchange
    INE = "INE"             # Shanghai International Energy Exchange
    SSE = "SSE"             # Shanghai Stock Exchange
    SZSE = "SZSE"           # Shenzhen Stock Exchange
    SGE = "SGE"             # Shanghai Gold Exchange
    WXE = "WXE"             # Wuxi Steel Exchange
    CFETS = "CFETS"         # China Foreign Exchange Trade System

    # Global/Amaerica
    SMART = "SMART"         # Smart Router for US stocks
    NYSE = "NYSE"           # New York Stock Exchnage
    NASDAQ = "NASDAQ"       # Nasdaq Exchange
    NYMEX = "NYMEX"         # New York Mercantile Exchange
    COMEX = "COMEX"         # a division of theNew York Mercantile Exchange
    GLOBEX = "GLOBEX"       # Globex of CME
    IDEALPRO = "IDEALPRO"   # Forex ECN of Interactive Brokers
    CME = "CME"             # Chicago Mercantile Exchange
    ICE = "ICE"             # Intercontinental Exchange
    SEHK = "SEHK"           # Stock Exchange of Hong Kong
    HKFE = "HKFE"           # Hong Kong Futures Exchange
    HKSE = "HKSE"           # Hong Kong Stock Exchange
    SGX = "SGX"             # Singapore Global Exchange
    CBOT = "CBT"            # Chicago Board of Trade
    CBOE = "CBOE"           # Chicago Board Options Exchange
    CFE = "CFE"             # CBOE Futures Exchange
    DME = "DME"             # Dubai Mercantile Exchange
    EUREX = "EUX"           # Eurex Exchange
    APEX = "APEX"           # Asia Pacific Exchange
    LME = "LME"             # London Metal Exchange
    BMD = "BMD"             # Bursa Malaysia Derivatives
    TOCOM = "TOCOM"         # Tokyo Commodity Exchange
    EUNX = "EUNX"           # Euronext Exchange
    KRX = "KRX"             # Korean Exchange

    OANDA = "OANDA"         # oanda.com

    ARCAEDGE = "ARCAEDGE"   # ArcaEdge (ARCAEDGE)
    BYX = "BYX"             # Bats BYX (BYX)
    BATS = "BATS"           # Bats Global Markets (BATS)
    CHX = "CHX"             # Chicago Stock Exchange (CHX)
    DRCTEDGE = "DRCTEDGE"   # Direct Edge (DRCTEDGE)
    EDGEA = "EDGEA"         # Direct Edge (EDGEA)
    VMAP = "VMAP"           # IB VWAP Dealing Network (VWAP)
    IEX = "IEX"             # IEX
    BEX  = "BEX"            # NASDAQ OMX BX (BEX)
    PSX  = "PSX"            # NASDAQ OMX PSX (PSX)
    ISLAND = "ISLAND"       # NASDAQ/Island
    ARCA = "ARCA"           # NYSE Arca (ARCA)
    PINK = "PINK"           # OTC Markets Pink (PINK)

    IBKRATS = "IBKRATS"     # IB's paper trading exchange

    # Canada
    ALPHA = "ALPHA"         # Alpha ATS (ALPHA)
    OMEGA = "OMEGA"         # Omega ATS (OMEGA)
    TSE = "TSE"             # Toronto Stock Exchange (TSE)      
    VENTURE = "VENTURE"     # TSX Venture (VENTURE)

    # Mexico
    MEXI = "MEXI"           # Mexican Stock Exchange (MEXI)

    # CryptoCurrency
    BITMEX = "BITMEX"
    OKEX = "OKEX"
    HUOBI = "HUOBI"
    BITFINEX = "BITFINEX"
    BINANCE = "BINANCE"
    BYBIT = "BYBIT"         # bybit.com
    COINBASE = "COINBASE"
    DERIBIT = "DERIBIT"
    GATEIO = "GATEIO"
    BITSTAMP = "BITSTAMP"

    # Special Function
    LOCAL = "LOCAL"         # For local generated data

注意就是,玩paper trading的朋友一定要把 IBKRATS 这个交易所添上,在paper trading中,有一些订单是在这里交易的。

问题 2

问题描述

这个问题只会在少数的几只股票上发生,例如在订阅MSFT的时候会发现

description

原因

这个问题其实在IB的API手册中有提到过

description

这是因为vnpy发出的合约信息对应了多个可能的合约(大概应该是因为一只股票有在几个交易所都有上市,IB不知道你这个合约是指哪一个吧?)。

解决办法

所以针对MSFT和CSCO这两只股票(应该还有其他的,不过IB的官网也没说,我也不知道。。。),我们给他们加上primary exchange就好了。

description

Administrator
avatar
加入于:
帖子: 4188
声望: 235

精细的分析,给你加个精华。

第二个情况,请问能否试试直接选NASDAQ交易所(不用SMART),看看是不是就解决了?

Member
avatar
加入于:
帖子: 5
声望: 2

过了几天回来看。突然发现漏了点东西没有说。^_^|||

关于问题1

除了要再Exchange类里加入交易所之外,在 vnpy > gateway > ib > ib_gateway.py 里要把 EXCHANGE_VT2IB 也要改一改。

description

关于问题2

用Python的交易员 wrote:

精细的分析,给你加个精华。

第二个情况,请问能否试试直接选NASDAQ交易所(不用SMART),看看是不是就解决了?

直接选择NASDAQ交易所的话不知道为什么都会出现 No security definition has been found for the request 的情况(我也不知道这是为什么?)所以我就干脆就都用SMART了。

description

description

倒是选NYSE的话就会像你所说的那样没有问题!

description

Member
加入于:
帖子: 7
声望: 2

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

https://contract.ibkr.info/v3.10/index.php
大家关于合约的信息可以到这里查,也可以改代码做一个从这个获取主用exchage的

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