Reputation: 49
It seems that the TWS API request reqFundamentalData
is no longer working. I'm using TWS version 10.19.2. It appears that the server is functioning, but the response is empty; I'm not getting any feedback.
# Initializing
class IBapi(EWrapper, EClient):
def __init__(self):
EClient.__init__(self, self)
# Error handling function
def error(self, reqId, errorCode, errorString):
print("error: ", reqId, " ", errorCode, " ", errorString)
if errorCode == 504: # Not connected error
print("Attempting to reconnect...")
self.reconnect()
# Inherit and overwrite fundamentalData() function in EWrapper
def fundamentalData(self, reqId: int, data: str):
super().fundamentalData(reqId, data)
self.fundamental_data = data # Store fundamental data in instance variable
print("FundamentalData Returned. ReqId: {}, Symbol: {}, XML Data: {}".format(
reqId, self.contract.symbol, data))
self.display_fundamental_data() # Call function to display fundamental data
def display_fundamental_data(self):
if hasattr(self, 'fundamental_data'):
print("Fundamental Data:", self.fundamental_data)
else:
print("No fundamental data available.")
def reconnect(self):
self.disconnect()
t.sleep(5) # Wait for 5 seconds before reconnecting (renamed to t)
self.connect("127.0.0.1", port=7497, clientId=990)
tws = IBapi()
tws.connect("127.0.0.1", port=7497, clientId=990)
t.sleep(5) # Renamed to t
print("Connected to IB server.")
c = Contract()
c.symbol = 'AAPL' # Changed to c.symbol
c.secType = 'STK' # Changed to c.secType
c.exchange = "SMART" # Changed to c.exchange
c.currency = "USD" # Changed to c.currency
tws.reqFundamentalData(1, c, 'ReportsFinStatements', [])
print("Requested fundamental data for symbol AAPL.")
t.sleep(10) # Renamed to t
# Implement other requests
tws.disconnect()
print("Disconnected from IB server.")
Here is the result.
Connected to IB server.
Requested fundamental data for symbol AAPL.
Disconnected from IB server.
Upvotes: 0
Views: 448
Reputation: 10989
I thought the code looked ok but it didn't run. I reorganized it a bit and realized you just forgot to call run().
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
import time as t
class IBapi(EWrapper, EClient):
def __init__(self):
EClient.__init__(self, self)
def error(self, reqId, errorCode, errorString, reject):
print("error: ", reqId, " ", errorCode, " ", errorString)
def nextValidId(self, orderId:int):
c = Contract()
c.symbol = 'AAPL' # Changed to c.symbol
c.secType = 'STK' # Changed to c.secType
c.exchange = "SMART" # Changed to c.exchange
c.currency = "USD" # Changed to c.currency
self.contract = c
self.reqFundamentalData(1, c, 'ReportsFinStatements', [])
print("Requested fundamental data for symbol AAPL.")
# Inherit and overwrite fundamentalData() function in EWrapper
def fundamentalData(self, reqId: int, data: str):
super().fundamentalData(reqId, data)
self.fundamental_data = data # Store fundamental data in instance variable
print("FundamentalData Returned. ReqId: {}, Symbol: {}, XML Data: {}".format(
reqId, self.contract.symbol, data))
self.display_fundamental_data() # Call function to display fundamental data
self.disconnect()
print("Disconnected from IB server.")
def display_fundamental_data(self):
if hasattr(self, 'fundamental_data'):
print("Fundamental Data:", self.fundamental_data)
else:
print("No fundamental data available.")
tws = IBapi()
tws.connect("127.0.0.1", port=7497, clientId=990)
tws.run() # pretty sure you just forgot this line
Upvotes: 1