def get_historical_data(self, security, barSize, goBack, endTime=None, whatToShow='', useRTH=1, formatDate=1, waitForFeedbackInSeconds=30, timezoneOfReturn=pytz.timezone('US/Eastern')): """ :param timezoneOfReturn: :param security: Security :param barSize: string barSize can be any of the following values(string) 1 sec, 5 secs,15 secs,30 secs,1 min,2 mins,3 mins,5 mins,15 mins,30 mins,1 hour,1 day :param goBack: string :param endTime: default value is '', IB server deems '' as the current server time. If user wants to supply a value, it must be a datetime with timezone :param whatToShow: string whatToShow: see IB documentation for choices TRADES,MIDPOINT,BID,ASK,BID_ASK,HISTORICAL_VOLATILITY,OPTION_IMPLIED_VOLATILITY :param useRTH: int 1=within regular trading hours, 0=ignoring RTH :param formatDate: int 1= Return datetime 2 = Return epoch seconds :param waitForFeedbackInSeconds: :return: a dataFrame, keyed by a datetime with timezone UTC, columns = ['open', 'high', 'low', 'close', 'volume'] The latest time record at the bottom of the dateFrame. """ self._log.debug(__name__ + '::get_historical_data: security=%s barSize=%s goBack=%s endTime=%s whatToShow=%s useRTH=%s formatDate=%s waitForFeedbackInSeconds=%s' % (security, barSize, goBack, endTime, whatToShow, useRTH, formatDate, waitForFeedbackInSeconds)) # all request datetime MUST be switched to UTC then submit to IB if endTime is not None and endTime != '': if endTime.tzinfo is None: self._log.error(__name__ + '::request_historical_data: EXIT, endTime=%s must have timezone' % (endTime,)) exit() endTime = endTime.astimezone(tz=pytz.utc) endTime = dt.datetime.strftime(endTime, "%Y%m%d %H:%M:%S %Z") # datetime -> string if whatToShow == '': whatToShow = choose_whatToShow(security.secType) orderIdList = self.submit_requests(ReqHistoricalData(security, barSize, goBack, endTime, whatToShow, useRTH, formatDate, waitForFeedbackInSeconds, timezoneOfReturn)) return self._brokerClient.get_submit_requests_result(orderIdList[0]) # return a pandas dataFrame
def _get_hist_from_TD(self, plan): self._log.debug(__name__ + '::_get_hist_from_TD') endTime = plan.endTime.astimezone(pytz.timezone('UTC')) endTime = dt.datetime.strftime(endTime, "%Y%m%d %H:%M:%S %Z") # datetime -> string # the return of request_data is reqId. # To get the content of hist, call brokerService::request_historical_data whatToShow = choose_whatToShow(plan.security.secType) reqIds = self._dataProviderClient.request_data(ReqHistoricalData(plan.security, plan.barSize, plan.goBack, endTime, whatToShow)) # only reqId is returned hist = self._dataProviderClient.get_submit_requests_result(reqIds[0]) # hist.to_csv('%s_%s_%s.csv' % (plan.security, plan.barSize, plan.goBack)) return hist
def provide_hist_from_a_true_dataProvider(self, security, endTime, goBack, barSize, whatToShow, useRTH, formatDate): endTime = plan.endTime.astimezone(pytz.timezone('UTC')) endTime = dt.datetime.strftime( endTime, "%Y%m%d %H:%M:%S %Z") # datetime -> string self._dataProviderClient.add_exchange_to_security(plan.security) self._dataProviderClient.add_primaryExchange_to_security(plan.security) # the return of request_data is reqId. # To get the content of hist, call brokerService::request_historical_data whatToShow = choose_whatToShow(plan.security.secType) reqIds = self._dataProviderClient.request_data( ReqHistoricalData(plan.security, plan.barSize, plan.goBack, endTime, whatToShow)) # only reqId is returned hist = self._dataProviderClient.get_submit_requests_result(reqIds[0]) # hist.to_csv('%s_%s_%s.csv' % (plan.security, plan.barSize, plan.goBack)) return hist
def _get_hist_from_IB(self, plan): self._log.debug(__name__ + '::_get_hist_from_IB: plan=%s' % (plan,)) if plan.fileName is not None: self._log.error(__name__ + '::_get_hist_from_IB: plan=%s should not have fileName. dataProviderName should be LOCAL_FILE.' % (plan,)) exit() endTime = plan.endTime.astimezone(pytz.timezone('UTC')) endTime = dt.datetime.strftime(endTime, "%Y%m%d %H:%M:%S %Z") # datetime -> string self._dataProviderClient.add_exchange_to_security(plan.security) self._dataProviderClient.add_primaryExchange_to_security(plan.security) # the return of request_data is reqId. # To get the content of hist, call brokerService::request_historical_data whatToShow = choose_whatToShow(plan.security.secType) reqIds = self._dataProviderClient.request_data(ReqHistoricalData(plan.security, plan.barSize, plan.goBack, endTime, whatToShow)) # only reqId is returned hist = self._dataProviderClient.get_submit_requests_result(reqIds[0]) # hist.to_csv('%s_%s_%s.csv' % (plan.security, plan.barSize, plan.goBack)) return hist