Код: Выделить всё
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
import threading
import time
class TradingApp(EWrapper, EClient):
def __init__(self):
EClient.__init__(self, wrapper=self)
self.nextOrderId = None
self.option_params_received = False
def error(self, reqId, errorCode, errorString, advancedOrderRejectJson=None):
print("Error {} {} {}".format(reqId, errorCode, errorString))
def tickOptionComputation(self, reqId, tickType, impliedVol, delta, optPrice, pvDividend, gamma, vega, theta, undPrice, pvDividend2):
if tickType == 13: # This is the type for option computation
print(f"Greeks for reqId {reqId}: Delta={delta}, Gamma={gamma}, Vega={vega}, Theta={theta}")
def websocket_con():
app.run()
# Create an instance of the TradingApp class and connect to the server.
app = TradingApp()
print("Attempting to connect...")
app.connect("127.0.0.1", 7497, clientId=7)
# Create a thread to connect to the websocket.
con_thread = threading.Thread(target=websocket_con)
con_thread.start()
# Give some lag time for connection
time.sleep(2)
# Create a simple put options contract on AAPL
option_contract = Contract()
option_contract.symbol = "AAPL" # Added symbol for the underlying
option_contract.secType = "OPT"
option_contract.currency = "USD"
option_contract.exchange = "SMART"
option_contract.lastTradeDateOrContractMonth = "20240802"
option_contract.strike = 195
option_contract.tradingClass = 'AAPL'
option_contract.right = "P" # Put option
app.reqMktData(reqId=111,
contract=option_contract,
genericTickList= "101",
snapshot=False,
regulatorySnapshot=False,
mktDataOptions=[])
time.sleep(5)
app.cancelMktData(111)
Подробнее здесь: https://stackoverflow.com/questions/787 ... reqmktdata