1

Can someone help me get started with doing some basic things with IBPY? Using IBPY, I just want to be able to enquire the current bidding price for a commodity such as the price of a single share in Google - or the current Eur/dollar exchange rate.

I found the example at the bottom of the page here:

Fundamental Data Using IbPy

useful - but the output is somewhat confusing. How do I print to screen just the current bid/asking price of a single contract?

(Just some bio info - yes I am new to IBPY and python - but I do have over 20 years experience with C)

Many kind thanks in advance!

  • Do the same thing as that example but register a callback for tickPrice and implement a handler. After that just `reqMktData` for a contract and you should set `isShanshot` to true if you just want one quote. Try this and make some code, leave me a comment when you're done and I'll have a look. – brian Jul 02 '17 at 22:44

1 Answers1

1

Using the example you referred to, with slightly changes:

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()

Output:

bid price = 149.55
ask price = 149.56
bid price = 149.59
ask price = 149.61
...
Eran Friedman
  • 578
  • 6
  • 9