Skip to content

Instantly share code, notes, and snippets.

@ranaroussi
Last active June 1, 2017 15:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ranaroussi/cc2072e5f2cb2b83514fceaeb4b0ca2e to your computer and use it in GitHub Desktop.
Save ranaroussi/cc2072e5f2cb2b83514fceaeb4b0ca2e to your computer and use it in GitHub Desktop.
ezIBpy: Tick Callback Example (repo: https://github.com/ranaroussi/ezibpy)
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import ezibpy
import time
# ---------------------
# create a callback method to be called on every tick/bid/ask
def ibCallback(caller, msg, **kwargs):
if "tick" in kwargs:
"""
^^ triggers on every new tick
if you want to get quote changes, use:
if "handleTick" in caller:
...
"""
if ibConn.contracts[msg.tickerId].m_secType in ("OPT", "FOP"):
data = ibConn.optionsData[msg.tickerId]
else:
data = ibConn.marketData[msg.tickerId]
print( data ) # data is a single row DataFrame with columns:
# ---------------------
# initialize connection
ibConn = ezibpy.ezIBpy()
ibConn.connect(clientId=1324, host="localhost", port=7497)
# >> introduce callback method <<
ibConn.ibCallback = ibCallback
# create EUR/USD contract
contract = ibConn.createCashContract("EUR", currency="USD")
# ...or create AAPL170407C00143000 CALL Option contract
# contract = ibConn.createOptionContract("AAPL", expiry="20170407", strike=143.0, otype="CALL")
# request market data for the contract
ibConn.requestMarketData()
# ---------------------
# wait until stopped
try:
while True:
time.sleep(1)
except (KeyboardInterrupt, SystemExit):
print('Stopped using Ctrl-c...')
# cancel market data & disconnect
ibConn.cancelMarketData(contract)
ibConn.disconnect()
@ranaroussi
Copy link
Author

ranaroussi commented Apr 1, 2017

Callback Callers are:

  • handleAccount
  • handleConnectionClosed
  • handleConnectionOpened
  • handleContractDetails
  • handleContractDetailsEnd
  • handleError
  • handleHistoricalData
  • handleMarketDepth
  • handleOrders
  • handlePortfolio
  • handlePosition
  • handleTickGeneric
  • handleTickOptionComputation
  • handleTickPrice
  • handleTickSize
  • handleTickSnapshotEnd
  • handleTickString

Market Data for Options

Please note that options data is available in theibConn.optionsData collection and not in the ibConn.marketData collection. I've updated the example code to work with both.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment