Interactive Brokers TWS API:通过app.reqMktData获取期货价格的问题

huangapple go评论80阅读模式
英文:

Interactive Brokers TWS API: problems with getting futures prices via app.reqMktData

问题

我已经创建了一段能够从IB TWS获取实时期货价格的工作代码,基于这个主题 https://quant.stackexchange.com/questions/55569/interactive-broker-api-error-321

它可以用于期货合约 MNQU3(这是上面示例中的纳斯达克100指数)。然而,当我尝试添加其他期货合约时,它返回错误消息 "200 未找到请求的安全定义"。以下是类似的代码,我正在尝试修复它。关键的代码行是第 31-37 行和第 41-47 行。

我已经购买了期货部分的实时市场数据,我的 TWS 终端显示所有市场数据都已连接。也许有一些关于填写期货信息的已知规则我没有遵守,或者商品期货和指数期货可能有不同的模板?谢谢。

成功示例的截图有问题的示例的截图

from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract

import threading
import time

class IBapi(EWrapper, EClient):
    def __init__(self):
        EClient.__init__(self, self)
    
    def tickPrice(self, reqId, tickType, price, attrib):
        if tickType == 2 and reqId == 1:
            print('当前的卖价为:', price)

def run_loop():
    app.run()

app = IBapi()
app.connect('127.0.0.1', 7497, 123)

# 在线程中启动 socket
api_thread = threading.Thread(target=run_loop, daemon=True)
api_thread.start()

time.sleep(1)  # 休眠一段时间以允许连接到服务器

# 工作示例

# fut_contract = Contract()
# fut_contract.symbol = 'MNQU3'
# fut_contract.secType = 'FUT'
# fut_contract.exchange = 'CME'
# fut_contract.tradingClass = 'MNQ'
# fut_contract.lastTradeDateOrContractMonth = '202309'
# fut_contract.currency = 'USD'

# 不工作示例

fut_contract = Contract()
fut_contract.symbol = 'NGQ3'
fut_contract.secType = 'FUT'
fut_contract.exchange = 'CME'
fut_contract.tradingClass = 'NG'
fut_contract.lastTradeDateOrContractMonth = '202308'
fut_contract.currency = 'USD'

# 请求市场数据
app.reqMktData(1, fut_contract, '', False, False, [])

time.sleep(10)  # 休眠一段时间以等待价格数据的到来
app.disconnect()

还尝试了一些其他期货代码,但似乎除了 MNQU3 之外,都不起作用。

英文:

I've created a working piece of code which gets real time futures prices from IB TWS based on this topic https://quant.stackexchange.com/questions/55569/interactive-broker-api-error-321

It works for futures MNQU3 (this is Nasdaq-100 Index from the example above). However, when I try to put another futures it returns an error "200 No security definition has been found for the request". Below is the code which is created by analogy and which I'm trying to fix. The key lines are 31-37 and 41-47

Real time market data is bought for futures section and my TWS station says all market data farms are connected. May be there are some known rules for filling in information about futures that I break, or may be commodity and index futures have different tepmlates? Thanks

Screenshots of successfull and problematic runs

from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract

import threading
import time



class IBapi(EWrapper, EClient):
    def __init__(self):
        EClient.__init__(self, self)
    def tickPrice(self, reqId, tickType, price, attrib):
        if tickType == 2 and reqId == 1:
            print('The current ask price is: ', price)

def run_loop():
    app.run()

app = IBapi()
app.connect('127.0.0.1', 7497, 123)

#Start the socket in a thread
api_thread = threading.Thread(target=run_loop, daemon=True)
api_thread.start()

time.sleep(1) #Sleep interval to allow time for connection to server

# working example

#fut_contract = Contract()
#fut_contract.symbol = 'MNQU3'
#fut_contract.secType = 'FUT'
#fut_contract.exchange = 'CME'
#fut_contract.tradingClass = 'MNQ'
#fut_contract.lastTradeDateOrContractMonth = '202309'
#fut_contract.currency = 'USD'

# does not work

fut_contract = Contract()
fut_contract.symbol = 'NGQ3'
fut_contract.secType = 'FUT'
fut_contract.exchange = 'CME'
fut_contract.tradingClass = 'NG'
fut_contract.lastTradeDateOrContractMonth = '202308'
fut_contract.currency = 'USD'

#Request Market Data
app.reqMktData(1, fut_contract, '', False, False, [])

time.sleep(10) #Sleep interval to allow time for incoming price data
app.disconnect()

Also tried some other futures codes, but non of them seemed to word, except MNQU3

答案1

得分: 0

错误的交易所,NG在NYMEX而不是CME。
您可以在这里搜索合同https://pennies.interactivebrokers.com/cstools/contract_info/v3.10/index.php

英文:

Wrong exchange, NG is on NYMEX not CME.
You can search contracts here https://pennies.interactivebrokers.com/cstools/contract_info/v3.10/index.php

huangapple
  • 本文由 发表于 2023年6月16日 15:43:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76487986.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定