How can i avoid to get OSError: [Errno 9] Bad file descriptor using ibapi?

假如想象 提交于 2020-01-16 01:48:07

问题


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

回答1:


The ibapi package disconnects from the socket but the reader is still trying to read from the socket. The source code has been modified to just trap the error but it hasn't made it into a release yet. You can modify your source code like is suggested here. https://groups.io/g/twsapi/message/42580 .



来源:https://stackoverflow.com/questions/58619155/how-can-i-avoid-to-get-oserror-errno-9-bad-file-descriptor-using-ibapi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!