def __init__(self, debug=False):
        self._log = logger.getLogger('DLD')
        self._log.debug(
            'Initializing data dwonloader. Pandas version={0}, ibpy version:{1}'
            .format(pandas.__version__, ib.version))

        self.tws = ibConnection()
        self._dataHandler = DataHandler(self.tws)

        if debug:
            self.tws.registerAll(self._debugHandler)
            self.tws.unregister(self._debugHandler, message.HistoricalData)

        self._log.debug('Connecting to tws')
        self.tws.connect()

        self._timeKeeper = TimeKeeper()  # keep track of past requests
        self._reqId = 1  # current request id
Пример #2
0
    def __init__(self,debug=False):
        self._log = logger.getLogger('DLD')        
        self._log.debug('Initializing data dwonloader. Pandas version={0}, ibpy version:{1}'.format(pandas.__version__,ib.version))

        self.tws = ibConnection()
        self._dataHandler = DataHandler(self.tws)
        
        if debug:
            self.tws.registerAll(self._debugHandler)
            self.tws.unregister(self._debugHandler,message.historicalData)
            
        self._log.debug('Connecting to tws')
        self.tws.connect() 
        
        self._timeKeeper = TimeKeeper() # keep track of past requests
        self._reqId = 1 # current request id
class Downloader(object):
    def __init__(self, debug=False):
        self._log = logger.getLogger('DLD')
        self._log.debug(
            'Initializing data dwonloader. Pandas version={0}, ibpy version:{1}'
            .format(pandas.__version__, ib.version))

        self.tws = ibConnection()
        self._dataHandler = DataHandler(self.tws)

        if debug:
            self.tws.registerAll(self._debugHandler)
            self.tws.unregister(self._debugHandler, message.HistoricalData)

        self._log.debug('Connecting to tws')
        self.tws.connect()

        self._timeKeeper = TimeKeeper()  # keep track of past requests
        self._reqId = 1  # current request id

    def _debugHandler(self, msg):
        print '[debug]', msg

    def requestData(self,
                    contract,
                    endDateTime,
                    durationStr='1800 S',
                    barSizeSetting='1 secs',
                    whatToShow='TRADES',
                    useRTH=1,
                    formatDate=1):
        self._log.debug('Requesting data for %s end time %s.' %
                        (contract.m_symbol, endDateTime))

        while self._timeKeeper.nrRequests(timeSpan=600) > 59:
            print 'Too many requests done. Waiting... '
            time.sleep(1)

        self._timeKeeper.addRequest()
        self._dataHandler.reset()
        self.tws.reqHistoricalData(self._reqId, contract, endDateTime,
                                   durationStr, barSizeSetting, whatToShow,
                                   useRTH, formatDate)
        self._reqId += 1

        #wait for data
        startTime = time.time()
        timeout = 3
        while not self._dataHandler.dataReady and (time.time() - startTime <
                                                   timeout):
            sleep(2)

        if not self._dataHandler.dataReady:
            self._log.error('Data timeout')

        print self._dataHandler.data

        return self._dataHandler.data

    def getIntradayData(self, contract, dateTuple):
        ''' get full day data on 1-s interval 
            date: a tuple of (yyyy,mm,dd)
        '''

        openTime = dt.datetime(*dateTuple) + dt.timedelta(hours=16)
        closeTime = dt.datetime(*dateTuple) + dt.timedelta(hours=22)

        timeRange = pandas.date_range(openTime, closeTime, freq='30min')

        datasets = []

        for t in timeRange:
            datasets.append(self.requestData(contract, t.strftime(timeFormat)))

        return pandas.concat(datasets)

    def disconnect(self):
        self.tws.disconnect()
Пример #4
0
class Downloader(object):
    def __init__(self,debug=False):
        self._log = logger.getLogger('DLD')        
        self._log.debug('Initializing data dwonloader. Pandas version={0}, ibpy version:{1}'.format(pandas.__version__,ib.version))

        self.tws = ibConnection()
        self._dataHandler = DataHandler(self.tws)
        
        if debug:
            self.tws.registerAll(self._debugHandler)
            self.tws.unregister(self._debugHandler,message.historicalData)
            
        self._log.debug('Connecting to tws')
        self.tws.connect() 
        
        self._timeKeeper = TimeKeeper() # keep track of past requests
        self._reqId = 1 # current request id
     
        
    def _debugHandler(self,msg):
        print '[debug]', msg
        
    
    def requestData(self,contract,endDateTime,durationStr='1800 S',barSizeSetting='1 secs',whatToShow='TRADES',useRTH=1,formatDate=1):  
        self._log.debug('Requesting data for %s end time %s.' % (contract.m_symbol,endDateTime))
        
        
        while self._timeKeeper.nrRequests(timeSpan=600) > 59:
            print 'Too many requests done. Waiting... '
            time.sleep(1)
        
        self._timeKeeper.addRequest()
        self._dataHandler.reset()
        self.tws.reqHistoricalData(self._reqId,contract,endDateTime,durationStr,barSizeSetting,whatToShow,useRTH,formatDate)
        self._reqId+=1
    
        #wait for data
        startTime = time.time()
        timeout = 3
        while not self._dataHandler.dataReady and (time.time()-startTime < timeout):
            sleep(2)
        
        if not self._dataHandler.dataReady:
            self._log.error('Data timeout')    
         
        print self._dataHandler.data
        
        return self._dataHandler.data  
    
    def getIntradayData(self,contract, dateTuple ):
        ''' get full day data on 1-s interval 
            date: a tuple of (yyyy,mm,dd)
        '''
        
        openTime = dt.datetime(*dateTuple)+dt.timedelta(hours=16)
        closeTime =  dt.datetime(*dateTuple)+dt.timedelta(hours=22)
        
        timeRange = pandas.date_range(openTime,closeTime,freq='30min')
        
        datasets = []
        
        for t in timeRange:
            datasets.append(self.requestData(contract,t.strftime(timeFormat)))
        
        return pandas.concat(datasets)
        
    
    def disconnect(self):
        self.tws.disconnect()