如何获得合同的当前竞标价格

时间:2017-06-30 17:47:10

标签: ibpy

有人可以帮助我开始用IBPY做一些基本的事情吗?使用IBPY,我只想查询商品的当前竞价价格,例如谷歌单一股票的价格 - 或当前的欧元/美元汇率。

我在页面底部找到了这个例子:

Fundamental Data Using IbPy

很有用 - 但输出有些令人困惑。如何打印以仅显示单个合约的当前买/卖价?

(只是一些生物信息 - 是的我是IBPY和python的新手 - 但我确实有超过20年的C经验)

非常感谢提前!

1 个答案:

答案 0 :(得分:1)

使用您提到的示例,稍作修改:

import signal

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


def price_handler(msg):
    if msg.field == 1:
        print("bid price = %s" % msg.price)
    elif msg.field == 2:
        print("ask price = %s" % msg.price)


def main():
    tws = ibConnection(port=7497)
    tws.register(price_handler, message.tickPrice)
    tws.connect()

    tick_id = 1
    c = Contract()
    c.m_symbol = 'AAPL'
    c.m_secType = 'STK'
    c.m_exchange = "SMART"
    c.m_currency = "USD"
    tws.reqMktData(tick_id, c, '', False)

    signal.pause()


if __name__ == '__main__':
    main()

输出:

bid price = 149.55
ask price = 149.56
bid price = 149.59
ask price = 149.61
...