I want to call contractDetails and feed the values from there ('right', 'lastTradeDateOrContractMonth', and 'strike') into tickPrice to get the option prices.
How do I update the "contract" in the contractDetails function? Right now it tells me "contract is not defined".
Which I understand why.
But I don't know how to fix it.
What does "contract" become? I've tried self.contract too which doesn't work either.
Here is my code and I've bolded the part where I try to update "contract" in the function:
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.common import *
from ibapi.contract import *
import pandas as pd
from threading import Timer
class TestApp(EWrapper, EClient):
def __init__(self):
EWrapper.__init__(self)
EClient.__init__(self, self)
def error(self, reqId, errorCode, errorString):
print("Error: ", reqId, " ", errorCode, " ", errorString)
def nextValidId(self, orderId):
self.start()
def contractDetails(self, reqId, contractDetails):
contract.lastTradeDateOrContractMonth = contractDetails.realExpirationDate
contract.strike = contractDetails.contract.strike
contract.right = contractDetails.contract.right
def tickPrice(self, reqId, tickType, price, attrib):
if tickType == 4 and reqId == 1:
print(price)
def contractDetailsEnd(self, reqId):
print("\ncontractDetails End\n")
self.stop()
def start(self):
contract = Contract()
contract.symbol = 'AAPL'
contract.secType = 'OPT'
contract.currency = 'USD'
contract.exchange = 'SMART'
contract.multiplier = '100'
contract.lastTradeDateOrContractMonth = '20200619'
contract.strike = '180'
contract.right = 'C'
self.reqContractDetails(1, contract)
self.reqMktData(1, contract, '101', False, False, [])
def stop(self):
self.done=True
self.disconnect()
print('disconnect')
def main():
print('start')
app = TestApp()
app.nextOrderId = 0
app.connect('127.0.0.1',7497,108)
#Timer(4, app.stop).start()
app.run()
print('finish')
if __name__ == "__main__":
main()
How do I update the "contract" in the contractDetails function? Right now it tells me "contract is not defined".
Which I understand why.
But I don't know how to fix it.
What does "contract" become? I've tried self.contract too which doesn't work either.
Here is my code and I've bolded the part where I try to update "contract" in the function:
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.common import *
from ibapi.contract import *
import pandas as pd
from threading import Timer
class TestApp(EWrapper, EClient):
def __init__(self):
EWrapper.__init__(self)
EClient.__init__(self, self)
def error(self, reqId, errorCode, errorString):
print("Error: ", reqId, " ", errorCode, " ", errorString)
def nextValidId(self, orderId):
self.start()
def contractDetails(self, reqId, contractDetails):
contract.lastTradeDateOrContractMonth = contractDetails.realExpirationDate
contract.strike = contractDetails.contract.strike
contract.right = contractDetails.contract.right
def tickPrice(self, reqId, tickType, price, attrib):
if tickType == 4 and reqId == 1:
print(price)
def contractDetailsEnd(self, reqId):
print("\ncontractDetails End\n")
self.stop()
def start(self):
contract = Contract()
contract.symbol = 'AAPL'
contract.secType = 'OPT'
contract.currency = 'USD'
contract.exchange = 'SMART'
contract.multiplier = '100'
contract.lastTradeDateOrContractMonth = '20200619'
contract.strike = '180'
contract.right = 'C'
self.reqContractDetails(1, contract)
self.reqMktData(1, contract, '101', False, False, [])
def stop(self):
self.done=True
self.disconnect()
print('disconnect')
def main():
print('start')
app = TestApp()
app.nextOrderId = 0
app.connect('127.0.0.1',7497,108)
#Timer(4, app.stop).start()
app.run()
print('finish')
if __name__ == "__main__":
main()