IbPy:我无法获取市场数据 [英] IbPy: I Can't Get Market Data

查看:155
本文介绍了IbPy:我无法获取市场数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里查看了所有对我有帮助的内容,但是它似乎没有用,我在编程方面相对较新,并且任何答复都将不胜感激. 我需要能够将Apple的股票价格下载到一个变量中并进行打印.我正在使用盈透证券的交易平台的演示版.

I have looked at everything in here that my help me, but it doesn't seem to work, I am relatively new at programming, and any responses will be greatly appreciated. I need to be able to download the stock price of Apple into a variable and print it. I am using the demo version of Interactive Brokers' TWS.

from ib.ext.Contract import Contract
from ib.opt import ibConnection, message
from time import sleep

# print all messages from TWS
def watcher(msg):
print msg

# show Bid and Ask quotes
def my_BidAsk(msg):
if msg.field == 1:
    print ('%s:%s: bid: %s' % (contractTuple[0],
                   contractTuple[6], msg.price))
elif msg.field == 2:
    print ('%s:%s: ask: %s' % (contractTuple[0], contractTuple[6], msg.price))

def makeStkContract(contractTuple):
newContract = Contract()
newContract.m_symbol = contractTuple[0]
newContract.m_secType = contractTuple[1]
newContract.m_exchange = contractTuple[2]
newContract.m_currency = contractTuple[3]
newContract.m_expiry = contractTuple[4]
newContract.m_strike = contractTuple[5]
newContract.m_right = contractTuple[6]
print ('Contract Values:%s,%s,%s,%s,%s,%s,%s:' % contractTuple)
return newContract

if __name__ == '__main__':
con = ibConnection()
con.registerAll(watcher)
showBidAskOnly = False  # set False to see the raw messages
if showBidAskOnly:
    con.unregister(watcher, message.tickSize, message.tickPrice,
                   message.tickString, message.tickOptionComputation)
    con.register(my_BidAsk, message.tickPrice)
con.connect()
sleep(1)
tickId = 59

# Note: Option quotes will give an error if they aren't shown in TWS
contractTuple = ('AAPL', 'STK', 'SMART', 'USD', '', 0.0, '')
#contractTuple = ('QQQQ', 'OPT', 'SMART', 'USD', '20070921', 47.0, 'CALL')
#contractTuple = ('ES', 'FUT', 'GLOBEX', 'USD', '200709', 0.0, '')
#contractTuple = ('ES', 'FOP', 'GLOBEX', 'USD', '20070920', 1460.0, 'CALL')
#contractTuple = ('EUR', 'CASH', 'IDEALPRO', 'USD', '', 0.0, '')
stkContract = makeStkContract(contractTuple)
print ('* * * * REQUESTING MARKET DATA * * * *')
con.reqMktData(tickId, stkContract, 'AAPL', False)
sleep(15)
print ('* * * * CANCELING MARKET DATA * * * *')
con.cancelMktData(tickId)
sleep(1)
con.disconnect()
sleep(1)

这是我从IbPy获得的代码.

This is the code I have from IbPy.

推荐答案

我假设您只是在粘贴代码时弄乱了格式.否则它将永远无法正常工作.

I'm assuming you just messed up the formatting when pasting the code. It would never work otherwise.

如果您收到错误回调,则可能会看到类似无效的通用刻度"的信息.您将"AAPL"放置在您指定想要的报价类型的位置.只需将其保留为空白即可.

If you were getting error callbacks you may have seen something like "invalid generic ticks". You put 'AAPL' in the position where you specify which type of ticks you want. Just leave this empty for normal ticks.

con.reqMktData(tickId, stkContract, '', False)

我不确定演示将使用哪个端口和ID,但您可以在此处指定是否不是7496、0(默认值).

I'm not sure what port and id the demo uses but you can specify here if not 7496, 0 (the defaults).

例如con = ibConnection(port = 7497, clientId = 123)

这篇关于IbPy:我无法获取市场数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆