def _get_prices_at_frequency_for_ibcontract_object_no_checking( self, contract_object_with_ib_broker_config, freq: Frequency, allow_expired: bool = False, ) -> futuresContractPrices: new_log = contract_object_with_ib_broker_config.log(self.log) price_data = self.ib_client.broker_get_historical_futures_data_for_contract( contract_object_with_ib_broker_config, bar_freq=freq, allow_expired=allow_expired, ) if price_data is missing_data: new_log.warn("Something went wrong getting IB price data for %s" % str(contract_object_with_ib_broker_config)) price_data = futuresContractPrices.create_empty() elif len(price_data) == 0: new_log.warn("No IB price data found for %s" % str(contract_object_with_ib_broker_config)) price_data = futuresContractPrices.create_empty() else: price_data = futuresContractPrices(price_data) ## It's important that the data is in local time zone so that this works price_data = price_data.remove_future_data() ## Ignore zeros if no volumes (if volume could be real price eg crude oil) price_data = price_data.remove_zero_prices_if_zero_volumes() return price_data
def _get_prices_at_frequency_for_contract_object_no_checking(self, contract_object: futuresContract, freq: str ) -> futuresContractPrices: """ Get historical prices at a particular frequency We override this method, rather than _get_prices_at_frequency_for_contract_object_no_checking Because the list of dates returned by contracts_with_price_data is likely to not match (expiries) :param contract_object: futuresContract :param freq: str; one of D, H, 15M, 5M, M, 10S, S :return: data """ new_log = contract_object.log(self.log) contract_object_with_ib_broker_config = ( self.futures_contract_data.get_contract_object_with_IB_data( contract_object ) ) if contract_object_with_ib_broker_config is missing_contract: new_log.warn("Can't get data for %s" % str(contract_object)) return futuresContractPrices.create_empty() price_data = self.ib_client.broker_get_historical_futures_data_for_contract( contract_object_with_ib_broker_config, bar_freq=freq) if price_data is missing_data: new_log.warn( "Something went wrong getting IB price data for %s" % str(contract_object)) price_data = futuresContractPrices.create_empty() elif len(price_data) == 0: new_log.warn( "No IB price data found for %s" % str(contract_object)) price_data = futuresContractPrices.create_empty() else: price_data = futuresContractPrices(price_data) ## It's important that the data is in local time zone so that this works price_data = price_data.remove_future_data() ## Some contract data is marked to model, don't want this price_data = price_data.remove_zero_volumes() return price_data
def get_recent_bid_ask_tick_data_for_contract_object( self, contract_object, trade_list_for_multiple_legs=None): """ Get last few price ticks :param contract_object: futuresContract :return: """ new_log = self.log.setup( instrument_code=contract_object.instrument_code, contract_date=contract_object.date_str, ) contract_object_with_ib_data = ( self.futures_contract_data.get_contract_object_with_IB_metadata( contract_object)) if contract_object_with_ib_data is missing_contract: new_log.warn("Can't get data for %s" % str(contract_object)) return futuresContractPrices.create_empty() tick_data = self.ibconnection.ib_get_recent_bid_ask_tick_data( contract_object_with_ib_data, trade_list_for_multiple_legs=trade_list_for_multiple_legs, ) if tick_data is missing_contract: return missing_data tick_data_as_df = from_ib_bid_ask_tick_data_to_dataframe(tick_data) return tick_data_as_df
def cancel_market_data_for_contract_object( self, contract_object, trade_list_for_multiple_legs=None): """ Returns my encapsulation of a ticker object :param contract_object: :param trade_list_for_multiple_legs: :return: """ new_log = self.log.setup( instrument_code=contract_object.instrument_code, contract_date=contract_object.date_str, ) contract_object_with_ib_data = ( self.futures_contract_data.get_contract_object_with_IB_metadata( contract_object)) if contract_object_with_ib_data is missing_contract: new_log.warn("Can't get data for %s" % str(contract_object)) return futuresContractPrices.create_empty() self.ibconnection.cancel_market_data_for_contract_object( contract_object_with_ib_data, trade_list_for_multiple_legs=trade_list_for_multiple_legs, )
def _get_prices_at_frequency_for_ibcontract_object_no_checking( self, contract_object_with_ib_broker_config, freq: Frequency, allow_expired: bool = False, ) -> futuresContractPrices: new_log = contract_object_with_ib_broker_config.log(self.log) price_data = self.ib_client.broker_get_historical_futures_data_for_contract( contract_object_with_ib_broker_config, bar_freq=freq, allow_expired=allow_expired, ) if price_data is missing_data: new_log.warn("Something went wrong getting IB price data for %s" % str(contract_object_with_ib_broker_config)) return failure if len(price_data) == 0: new_log.warn("No IB price data found for %s" % str(contract_object_with_ib_broker_config)) return futuresContractPrices.create_empty() return futuresContractPrices(price_data)
def get_recent_bid_ask_tick_data_for_contract_object( self, contract_object: futuresContract) -> dataFrameOfRecentTicks: """ Get last few price ticks :param contract_object: futuresContract :return: """ new_log = contract_object.log(self.log) contract_object_with_ib_data = ( self.futures_contract_data.get_contract_object_with_IB_data( contract_object)) if contract_object_with_ib_data is missing_contract: new_log.warn("Can't get data for %s" % str(contract_object)) return futuresContractPrices.create_empty() tick_data = self.ib_client.ib_get_recent_bid_ask_tick_data( contract_object_with_ib_data) if tick_data is missing_contract: return missing_data tick_data_as_df = from_ib_bid_ask_tick_data_to_dataframe(tick_data) return tick_data_as_df
def _get_prices_at_frequency_for_contract_object_no_checking( self, contract_object: futuresContract, freq: Frequency, allow_expired=False, ) -> futuresContractPrices: """ Get historical prices at a particular frequency We override this method, rather than _get_prices_at_frequency_for_contract_object_no_checking Because the list of dates returned by contracts_with_price_data is likely to not match (expiries) :param contract_object: futuresContract :param freq: str; one of D, H, 15M, 5M, M, 10S, S :return: data """ new_log = contract_object.log(self.log) contract_object_with_ib_broker_config = ( self.futures_contract_data.get_contract_object_with_IB_data( contract_object, allow_expired=allow_expired)) if contract_object_with_ib_broker_config is missing_contract: new_log.warn("Can't get data for %s" % str(contract_object)) return futuresContractPrices.create_empty() price_data = self._get_prices_at_frequency_for_ibcontract_object_no_checking( contract_object_with_ib_broker_config, freq=freq, allow_expired=allow_expired, ) return price_data
def get_ticker_object_for_contract_object( self, contract_object: futuresContract, trade_list_for_multiple_legs=None) -> ibTickerObject: """ Returns my encapsulation of a ticker object :param contract_object: :param trade_list_for_multiple_legs: :return: """ new_log = contract_object.log(self.log) contract_object_with_ib_data = ( self.futures_contract_data.get_contract_object_with_IB_data(contract_object) ) if contract_object_with_ib_data is missing_contract: new_log.warn("Can't get data for %s" % str(contract_object)) return futuresContractPrices.create_empty() ticker_with_bs = self.ibconnection.get_ticker_object( contract_object_with_ib_data, trade_list_for_multiple_legs=trade_list_for_multiple_legs, ) ticker_object = ibTickerObject(ticker_with_bs, self.ibconnection) return ticker_object
def _get_prices_for_contract_object_no_checking(self, futures_contract_object): """ Read back the prices for a given contract object :param contract_object: futuresContract :return: data """ keyname = self._keyname_given_contract_object(futures_contract_object) filename = self._filename_given_key_name(keyname) date_format = self._input_date_format date_time_column = self._input_date_time_column input_column_mapping = self._input_column_mapping skiprows = self._input_skiprows skipfooter = self._input_skipfooter try: instrpricedata = pd_readcsv( filename, date_index_name=date_time_column, date_format=date_format, input_column_mapping=input_column_mapping, skiprows=skiprows, skipfooter=skipfooter, ) except OSError: self.log.warning("Can't find adjusted price file %s" % filename) return futuresContractPrices.create_empty() instrpricedata = instrpricedata.groupby(level=0).last() instrpricedata = futuresContractPrices(instrpricedata) return instrpricedata
def _get_prices_for_contract_object_no_checking( self, futures_contract_object): """ :param futures_contract_object: futuresContract :return: futuresContractPrices """ self.log.label( instrument_code=futures_contract_object.instrument_code, contract_date=futures_contract_object.date_str, ) try: quandl_contract = _quandlFuturesContract(futures_contract_object) except BaseException: self.log.warning( "Can't parse contract object to find the QUANDL identifier" ) return futuresContractPrices.create_empty() try: contract_data = quandl.get(quandl_contract.quandl_identifier()) except Exception as exception: self.log.warn( "Can't get QUANDL data for %s error %s" % (quandl_contract.quandl_identifier(), exception) ) return futuresContractPrices.create_empty() try: data = quandlFuturesContractPrices(contract_data) except BaseException: self.log.error( "Quandl API error: data fields are not as expected %s" % ",".join(list(contract_data.columns)) ) return futuresContractPrices.create_empty() # apply multiplier factor = quandl_contract.get_dividing_factor() data = data / factor return data
def get_prices_at_frequency_for_contract_object(self, contract_object, freq="D"): """ Get historical prices at a particular frequency We override this method, rather than _get_prices_at_frequency_for_contract_object_no_checking Because the list of dates returned by contracts_with_price_data is likely to not match (expiries) :param contract_object: futuresContract :param freq: str; one of D, H, 15M, 5M, M, 10S, S :return: data """ new_log = self.log.setup( instrument_code=contract_object.instrument_code, contract_date=contract_object.date_str, ) contract_object_with_ib_broker_config = ( self.futures_contract_data.get_contract_object_with_IB_metadata( contract_object)) if contract_object_with_ib_broker_config is missing_contract: new_log.warn("Can't get data for %s" % str(contract_object)) return futuresContractPrices.create_empty() price_data = self.ibconnection.broker_get_historical_futures_data_for_contract( contract_object_with_ib_broker_config, bar_freq=freq) if len(price_data) == 0: new_log.warn("No IB price data found for %s" % str(contract_object)) data = futuresContractPrices.create_empty() else: data = futuresContractPrices(price_data) data = futuresContractPrices( data[data.index < datetime.datetime.now()]) data = data.remove_zero_volumes() return data
def get_prices_for_contract_object(self, contract_object: futuresContract): """ get all prices without worrying about frequency :param contract_object: futuresContract :return: data """ if self.has_data_for_contract(contract_object): return self._get_prices_for_contract_object_no_checking( contract_object) else: return futuresContractPrices.create_empty()
def get_prices_for_contract_object(self, contract_object): """ get some prices :param contract_object: futuresContract :return: data """ if self.has_data_for_contract(contract_object): return self._get_prices_for_contract_object_no_checking( contract_object) else: return futuresContractPrices.create_empty()
def get_prices_at_frequency_for_contract_object(self, contract_object, freq="D"): """ get some prices :param contract_object: futuresContract :param freq: str; one of D, H, 5M, M, 10S, S :return: data """ if self.has_data_for_contract(contract_object): return self._get_prices_at_frequency_for_contract_object_no_checking( contract_object, freq=freq) else: return futuresContractPrices.create_empty()
def cancel_market_data_for_order(self, order: ibBrokerOrder): contract_object = order.futures_contract trade_list_for_multiple_legs = order.trade new_log = order.log_with_attributes(self.log) contract_object_with_ib_data = ( self.futures_contract_data.get_contract_object_with_IB_data( contract_object)) if contract_object_with_ib_data is missing_contract: new_log.warn("Can't get data for %s" % str(contract_object)) return futuresContractPrices.create_empty() self.ib_client.cancel_market_data_for_contract_object( contract_object_with_ib_data, trade_list_for_multiple_legs=trade_list_for_multiple_legs, )
def _get_prices_for_contract_object_no_checking( self, futures_contract_object: futuresContract ) -> futuresContractPrices: """ Read back the prices for a given contract object :param contract_object: futuresContract :return: data """ keyname = self._keyname_given_contract_object(futures_contract_object) filename = self._filename_given_key_name(keyname) config = self.config date_format = config.input_date_format date_time_column = config.input_date_index_name input_column_mapping = config.input_column_mapping skiprows = config.input_skiprows skipfooter = config.input_skipfooter multiplier = config.apply_multiplier inverse = config.apply_inverse try: instrpricedata = pd_readcsv( filename, date_index_name=date_time_column, date_format=date_format, input_column_mapping=input_column_mapping, skiprows=skiprows, skipfooter=skipfooter, ) except OSError: log = futures_contract_object.log(self.log) log.warn("Can't find adjusted price file %s" % filename) return futuresContractPrices.create_empty() instrpricedata = instrpricedata.groupby(level=0).last() for col_name in ["OPEN", "HIGH", "LOW", "FINAL"]: column_series = instrpricedata[col_name] if inverse: column_series = 1 / column_series column_series *= multiplier instrpricedata[col_name] = column_series.round(2) instrpricedata = futuresContractPrices(instrpricedata) return instrpricedata
def get_prices_at_frequency_for_contract_object( self, contract_object: futuresContract, freq: Frequency = DAILY_PRICE_FREQ, return_empty: bool = True): """ get some prices at a given frequency :param contract_object: futuresContract :param freq: str; one of D, H, 5M, M, 10S, S :return: data """ if self.has_data_for_contract(contract_object): return self._get_prices_at_frequency_for_contract_object_no_checking( contract_object, freq=freq) else: if return_empty: return futuresContractPrices.create_empty() else: return failure
def get_ticker_object_for_order(self, order: contractOrder) -> tickerObject: contract_object = order.futures_contract trade_list_for_multiple_legs = order.trade new_log = order.log_with_attributes(self.log) contract_object_with_ib_data = ( self.futures_contract_data.get_contract_object_with_IB_data(contract_object) ) if contract_object_with_ib_data is missing_contract: new_log.warn("Can't get data for %s" % str(contract_object)) return futuresContractPrices.create_empty() ticker_with_bs = self.ib_client.get_ticker_object( contract_object_with_ib_data, trade_list_for_multiple_legs=trade_list_for_multiple_legs, ) ticker_object = ibTickerObject(ticker_with_bs, self.ib_client) return ticker_object