In the following code i am collecting data to pandas dataframe called ohlcv as a function and running the app throw to the ib server:
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
from ibapi.ticktype import TickTypeEnum
from ibapi.common import * #for TickerId type
import pandas as pd
from socket import error as SocketError
import errno
def read_ohlcv(reqId, symbol, sec_type, exch, prim_exch, curr, durationStr, barSizeSetting):
contract = Contract()
contract.symbol = symbol
contract.secType = sec_type
contract.exchange = exch
contract.primaryExchange = prim_exch
contract.currency = curr
class TestApp(EWrapper, EClient):
def __init__(self):
EClient.__init__(self,self)
self.historicaldata = pd.DataFrame([], columns = ['Open', 'High', 'Low', 'Close', 'Volume'])
def error(self, reqId:TickerId, errorCode:int, errorString:str):
if reqId > -1:
print("Error. Id: " , reqId, " Code: " , errorCode , " Msg: " , errorString)
def historicalData(self,reqId, bar):
self.historicaldata.index.name = 'Date'
self.historicaldata.loc[bar.date] = bar.open, bar.high, bar.low, bar.close, bar.volume
def historicalDataEnd(self, reqId: int, start: str, end: str):
super().historicalDataEnd(reqId, start, end)
print("HistoricalDataEnd. ReqId:", reqId, "from", start, "to", end)
self.disconnect()
app = TestApp()
app.connect('127.0.0.1', 7497, 0)
app.reqHistoricalData(reqId = reqId,
contract = contract,
endDateTime = '',
durationStr = durationStr,
barSizeSetting = barSizeSetting,
whatToShow = 'TRADES',
useRTH = 1, # =1 for RTH data
formatDate = 1,
keepUpToDate = False,
chartOptions = [])
ohlcv = app.historicaldata
app.run()
sleep(5)
return ohlcv
When i call the function, the code is working well and I collect the data as pandas dataframe. However i get the following error which i would like to understand and find a way to avoid it:
unhandled exception in EReader thread
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/ibapi/reader.py", line 34, in run
data = self.conn.recvMsg()
File "/usr/local/lib/python3.6/dist-packages/ibapi/connection.py", line 99, in recvMsg
buf = self._recvAllMsg()
File "/usr/local/lib/python3.6/dist-packages/ibapi/connection.py", line 119, in _recvAllMsg
buf = self.socket.recv(4096)
OSError: [Errno 9] Bad file descriptor