So I have written the following program:
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
import threading
import time
class IBapi(EWrapper, EClient):
def __init__(self):
EClient.__init__(self, self)
def tickPrice(self, reqId, tickType, price, attrib):
if tickType == 2 and reqId == 1:
print('The current ask price is: ', price)
def run_loop():
app.run()
app = IBapi()
app.connect('127.0.0.1', 7497, 123)
#Start the socket in a thread
api_thread = threading.Thread(target=run_loop, daemon=True)
api_thread.start()
time.sleep(1) #Sleep interval to allow time for connection to server
#Create contract object
contract = Contract()
contract.symbol = 'TSLA'
contract.secType = 'OPT'
contract.exchange = 'SMART'
contract.lastTradeDateOrContractMonth = '2023120'
contract.strike = 100
contract.right = 'C'
contract.multiplier = '100'
#Request Market Data
app.reqMktData(1, contract, '', False, False, [])
time.sleep(9) #Sleep interval to allow time for incoming price data
app.disconnect()
The output I get is
ERROR -1 2104 Market data farm connection is OK:usfarm.nj
ERROR -1 2104 Market data farm connection is OK:cashfarm
ERROR -1 2104 Market data farm connection is OK:usfarm
ERROR -1 2106 HMDS data farm connection is OK:ushmds
ERROR -1 2158 Sec-def data farm connection is OK:secdefnj
As you can see there is no price of the option, I looked at the interactive brokers API guide I changed the code a bit but still no price came
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
import threading
import time
class IBapi(EWrapper, EClient):
def __init__(self):
EClient.__init__(self, self)
def tickOptionComputation(self, reqId, tickType, tickAttrib, impliedVol, delta, optPrice, pvDividend, gamma, vega, theta, undPrice):
super().tickOptionComputation(reqId, tickType, tickAttrib, impliedVol, delta,
optPrice, pvDividend, gamma, vega, theta, undPrice)
print("TickOptionComputation. TickerId:", reqId, "TickType:", tickType, "TickAttrib:", (tickAttrib), "ImpliedVolatility:", (impliedVol), "Delta:", (delta), "OptionPrice:", (optPrice), "pvDividend:", (pvDividend), "Gamma: ", (gamma), "Vega:", (vega), "Theta:", (theta), "UnderlyingPrice:", (undPrice))
def run_loop():
app.run()
app = IBapi()
app.connect('127.0.0.1', 7497, 123)
#Start the socket in a thread
api_thread = threading.Thread(target=run_loop, daemon=True)
api_thread.start()
time.sleep(1) #Sleep interval to allow time for connection to server
#Create contract object
contract = Contract()
contract.symbol = 'TSLA'
contract.secType = 'OPT'
contract.exchange = 'SMART'
contract.lastTradeDateOrContractMonth = '2023120'
contract.strike = 100
contract.right = 'C'
contract.multiplier = '100'
#Request Market Data
app.reqMktData(1, contract, '', False, False, [])
time.sleep(9) #Sleep interval to allow time for incoming price data
app.disconnect()
Output
ERROR -1 2104 Market data farm connection is OK:usfarm.nj
ERROR -1 2104 Market data farm connection is OK:cashfarm
ERROR -1 2104 Market data farm connection is OK:usfarm
ERROR -1 2106 HMDS data farm connection is OK:ushmds
ERROR -1 2158 Sec-def data farm connection is OK:secdefnj
I really don't know what I am getting wrong but it is probably in my calculation of the option price I have followed the API calculation guide ( https://interactivebrokers.github.io/tws-api/option_computations.html#opt_calculations) in the second edition of the code, So I still don't know.
I would appreciate it if someone could help me.
Thank you for your time!
I wanted to download options data using python from IBKR it didn't work
There are some cases that use
self.nextValidOrderId
variable and before callapp.run
setapp.nextorderId = None
that should be defined by:now after initialize
api_thread
it will print:
By this way i see that is used
Order
instances andapp.placeOrder
method, and maybe it is not what you desire. This script wrapstickOptionComputation
and use multithreading to create a infinite loop to connect to the api, the difference to the above code is that you need to modify it to manage awhile
loop to print a connection status otherwise you also should check on Settings section for any improperly stablished feature that generates connection issues.additional info:
Warning messages code (2104, 2106, 2158)
Tick Types