1

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

我在这里找到了页面底部的示例:

使用 IbPy 的基本数据

有用 - 但输出有点令人困惑。如何打印以仅筛选单个合同的当前出价/要价?

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

非常感谢提前!

4

1 回答 1

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
...
于 2017-07-22T08:08:04.817 回答