Last active
June 1, 2017 15:23
-
-
Save ranaroussi/cc2072e5f2cb2b83514fceaeb4b0ca2e to your computer and use it in GitHub Desktop.
ezIBpy: Tick Callback Example (repo: https://github.com/ranaroussi/ezibpy)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Callback Callers are:
Market Data for Options
Please note that options data is available in the
ibConn.optionsData
collection and not in theibConn.marketData
collection. I've updated the example code to work with both.