def __init__(self, instrument_object, contract_date_object, **kwargs): """ futuresContract(futuresInstrument, contractDate) OR futuresContract("instrument_code", "yyyymm") :param instrument_object: str or futuresInstrument :param contract_date_object: contractDate or contractDateWithRollParameters or str """ if isinstance(instrument_object, str): if isinstance(contract_date_object, str): # create a simple object self.instrument = futuresInstrument(instrument_object) self.contract_date = contractDate(contract_date_object) if isinstance(contract_date_object, list): if len(contract_date_object) == 1: self.instrument = futuresInstrument(instrument_object) self.contract_date = contractDate(contract_date_object[0]) else: self.instrument = futuresInstrument(instrument_object) self.contract_date = [ contractDate(contract_date) for contract_date in contract_date_object ] else: self.instrument = instrument_object self.contract_date = contract_date_object self._is_empty = False self.params = kwargs
def get_instrument_object_from_config(instrument_code: str, config: IBconfig=None) ->futuresInstrumentWithIBConfigData: if config is None: config = read_ib_config_from_file() config_row = config[config.Instrument == instrument_code] symbol = config_row.IBSymbol.values[0] exchange = config_row.IBExchange.values[0] currency = value_or_npnan(config_row.IBCurrency.values[0], NOT_REQUIRED_FOR_IB) ib_multiplier = value_or_npnan( config_row.IBMultiplier.values[0], NOT_REQUIRED_FOR_IB) my_multiplier = value_or_npnan( config_row.MyMultiplier.values[0], 1.0) ignore_weekly = config_row.IgnoreWeekly.values[0] # We use the flexibility of futuresInstrument to add additional arguments instrument = futuresInstrument(instrument_code) ib_data = ibInstrumentConfigData(symbol, exchange, currency=currency, ibMultiplier=ib_multiplier, myMultiplier=my_multiplier, ignoreWeekly=ignore_weekly ) futures_instrument_with_ib_data = futuresInstrumentWithIBConfigData(instrument, ib_data) return futures_instrument_with_ib_data
def get_contract_chain(instrument_code, data): diag_contracts = diagContracts(data) diag_prices = diagPrices(data) roll_parameters = diag_contracts.get_roll_parameters(instrument_code) # Get the last contract currently being used multiple_prices = diag_prices.get_multiple_prices(instrument_code) current_contract_dict = multiple_prices.current_contract_dict() current_contract_list = list(current_contract_dict.values()) furthest_out_contract_date = max(current_contract_list) furthest_out_contract = contractDateWithRollParameters( roll_parameters, furthest_out_contract_date) # To give us wiggle room, and ensure we start collecting the new forward a # little in advance final_contract = furthest_out_contract.next_priced_contract() contract_date_chain = ( final_contract.get_unexpired_contracts_from_now_to_contract_date()) # We have a list of contract_date objects, need futureContracts # create a 'bare' instrument object instrument_object = futuresInstrument(instrument_code) contract_object_chain_as_list = [ futuresContract(instrument_object, contract_date_object) for contract_date_object in contract_date_chain ] contract_object_chain = listOfFuturesContracts( contract_object_chain_as_list) return contract_object_chain
def from_two_strings(futuresContract, instrument_code: str, contract_date_str: str): instrument_object = futuresInstrument(instrument_code) contract_date = contractDate(contract_date_str, simple=True) return futuresContract(instrument_object, contract_date, simple=True)
def _resolve_args_where_both_are_str(instrument_object_str, contract_date_object_str): # create a simple object instrument_object = futuresInstrument(instrument_object_str) contract_date_object = contractDate(contract_date_object_str) return instrument_object, contract_date_object
def create_empty(futuresContract): fake_instrument = futuresInstrument("EMPTY") fake_contract_date = contractDate("150001") futures_contract = futuresContract(fake_instrument, fake_contract_date) futures_contract._is_empty = True return futures_contract
def simple(futuresContract, instrument_code, contract_date, **kwargs): DeprecationWarning( "futuresContract.simple(x,y) is deprecated, use futuresContract(x,y) instead" ) return futuresContract( futuresInstrument(instrument_code), contractDate( contract_date, **kwargs))
def __init__(self, strategy_name: str, instrument_code: str, contract_id): """ :param strategy_name: str :param instrument_code: str :param contract_id: a single contract_order_id YYYYMM, or a list of contract IDS YYYYMM for a spread order """ self._contract_date = contractDate(contract_id) self._instrument = futuresInstrument(instrument_code) self._strategy_name = strategy_name
def create_list_of_contracts(instrument_code): instrument_object = futuresInstrument(instrument_code) print(instrument_code) roll_parameters = get_roll_parameters_from_mongo(instrument_code) first_contract_date = get_first_contract_date_from_quandl(instrument_code) list_of_contracts = listOfFuturesContracts.historical_price_contracts( instrument_object, roll_parameters, first_contract_date) return list_of_contracts
def _resolve_args_for_futures_contract(instrument_object, contract_date_object) -> tuple: if isinstance(instrument_object, str): instrument_object = futuresInstrument(instrument_object) if isinstance(contract_date_object, list) or isinstance(contract_date_object, str) \ or isinstance(contract_date_object, dict): contract_date_object = contractDate(contract_date_object) return instrument_object, contract_date_object
def test_futuresInstrument(self): instrument = futuresInstrument("EDOLLAR") self.assertEqual(instrument.instrument_code, "EDOLLAR") instrument_dict = instrument.as_dict() print(instrument_dict) self.assertEqual(instrument_dict["instrument_code"], "EDOLLAR") new_instrument = instrument.create_from_dict(instrument_dict) self.assertEqual(new_instrument.instrument_code, "EDOLLAR")
def _resolve_args_where_instrument_str_and_contract_date_is_list( instrument_object, contract_date_object_list): instrument_object = futuresInstrument(instrument_object) if len(contract_date_object_list) == 1: contract_date_object = contractDate(contract_date_object_list[0]) else: contract_date_object = [ contractDate(contract_date) for contract_date in contract_date_object_list ] return instrument_object, contract_date_object
def get_instrument_with_meta_data_object(all_instrument_data, instrument_code): config_for_this_instrument = all_instrument_data.loc[instrument_code] config_items = all_instrument_data.columns meta_data_dict = get_meta_data_dict_for_instrument( config_for_this_instrument, config_items) instrument = futuresInstrument(instrument_code) meta_data = instrumentMetaData.from_dict(meta_data_dict) instrument_with_meta_data = futuresInstrumentWithMetaData( instrument, meta_data) return instrument_with_meta_data
def _get_instrument_data_without_checking(self, instrument_code): all_instrument_data = self.get_all_instrument_data() config_for_this_instrument = all_instrument_data.loc[instrument_code] config_items = all_instrument_data.columns meta_data = dict([(item_name, getattr(config_for_this_instrument, item_name)) for item_name in config_items]) instrument = futuresInstrument(instrument_code) meta_data = instrumentMetaData.from_dict(meta_data) instrument_with_meta_data = futuresInstrumentWithMetaData( instrument, meta_data) return instrument_with_meta_data
def test_list_of_futures_contracts(self): instrument_object = futuresInstrument("EDOLLAR") roll_parameters = rollParametersTOMOVE( priced_rollcycle="HMUZ", hold_rollcycle="MZ", approx_expiry_offset=15, roll_offset_day=-70, ) flist = listOfFuturesContracts.historical_price_contracts( instrument_object, roll_parameters, "200003", pd.datetime(2001, 1, 1)) self.assertEqual(len(flist), 5) self.assertEqual(flist[0].date_str, "20000300") self.assertEqual(flist[-1].date_str, "20010300")
def create_contract_object_chain_from_contract_date_chain( instrument_code: str, contract_date_chain: list ) -> listOfFuturesContracts: # We have a list of contract_date objects, need futureContracts # create a 'bare' instrument object instrument_object = futuresInstrument(instrument_code) contract_object_chain_as_list = [ futuresContract(instrument_object, contract_date) for contract_date in contract_date_chain ] contract_object_chain = listOfFuturesContracts(contract_object_chain_as_list) return contract_object_chain
def test_futures_instruments(self): data = mongoFuturesInstrumentData(database_name="test") # test db so okay to do this data._mongo.db.drop_collection(data._mongo.collection_name) codes = data.get_list_of_instruments() self.assertEqual(codes, []) instrument_object = data.get_instrument_data("EDOLLAR") self.assertTrue(instrument_object.empty()) instrument_object = futuresInstrument("EDOLLAR", some_data="test") data.add_instrument_data(instrument_object) self.assertEqual(data.get_list_of_instruments(), ["EDOLLAR"]) found_object = data.get_instrument_data("EDOLLAR") self.assertEqual(found_object.instrument_code, "EDOLLAR") found_object = data["EDOLLAR"] self.assertEqual(found_object.instrument_code, "EDOLLAR") self.assertEqual(found_object.meta_data["some_data"], "test") codes = data.get_list_of_instruments() self.assertEqual(codes, ["EDOLLAR"]) data.delete_instrument_data("EDOLLAR", are_you_sure=True) instrument_object = data.get_instrument_data("EDOLLAR") self.assertTrue(instrument_object.empty()) codes = data.get_list_of_instruments() self.assertEqual(codes, [])
def __init__(self, strategy_name: str, instrument_code: str): instrument_object = futuresInstrument(instrument_code) self._instrument = instrument_object self._strategy_name = strategy_name
def __init__(self, position, *args, **kwargs): tradeable_object = futuresInstrument(*args, **kwargs) super().__init__(position, tradeable_object)
def __init__(self, position: int, instrument_code: str): tradeable_object = futuresInstrument(instrument_code) super().__init__(position, tradeable_object)
def position_for_instrument(self, instrument_code): tradeable_object = futuresInstrument(instrument_code) position = self.position_for_object(tradeable_object) return position
def __init__(self, strategy_name, *args, **kwargs): instrument_object = futuresInstrument(*args, **kwargs) self._instrument_object = instrument_object self._strategy_name = strategy_name
def __init__(self, instrument_code: str, position_limit: int): tradeable_object = futuresInstrument(instrument_code) super().__init__(tradeable_object, position_limit)
def update_multiple_prices_on_roll( data: dataBlob, current_multiple_prices: futuresMultiplePrices, instrument_code: str, allow_forward_fill: bool = False) -> futuresMultiplePrices: """ Roll multiple prices Adds rows to multiple prices First row: (optionally) Inferred price and forward prices If there is no (old) forward contract price, one needs to be inferred If there is no (old) price contract price, one needs to be inferred Time index = Last time index + 1 second Second row: Time index: Last time index + 1 second PRICE = last price of the forward contract PRICE_CONTRACT = previous forward contract FORWARD_CONTRACT = the new forward contract FORWARD_PRICE = the new forward price, this can be Nan; it will get filled in CARRY_CONTRACT = the new carry contract CARRY_PRICE = the new carry price: if possible infer from price, this can be Nan :param data: dataBlob :param current_multiple_prices: futuresMultiplePrices :return: new futuresMultiplePrices """ new_multiple_prices = futuresMultiplePrices(copy(current_multiple_prices)) if allow_forward_fill: new_multiple_prices = new_multiple_prices.ffill() # If the last row is all Nans, we can't do this new_multiple_prices = new_multiple_prices.sort_index() new_multiple_prices = new_multiple_prices.drop_trailing_nan() price_column = price_column_names["PRICE"] fwd_column = price_column_names["FORWARD"] current_contract_dict = new_multiple_prices.current_contract_dict() old_forward_contract = current_contract_dict.forward old_priced_contract_last_price, price_inferred = get_or_infer_latest_price( new_multiple_prices, price_col=price_column) old_forward_contract_last_price, forward_inferred = get_or_infer_latest_price( new_multiple_prices, price_col=fwd_column) diag_contracts = dataContracts(data) instrument_object = futuresInstrument(instrument_code) # Old forward contract -> New price contract new_price_contract_date_object = ( diag_contracts.get_contract_date_object_with_roll_parameters( instrument_code, old_forward_contract)) new_forward_contract_date = new_price_contract_date_object.next_held_contract( ) new_carry_contract_date = new_price_contract_date_object.carry_contract() new_price_contract_object = futuresContract( instrument_object, new_price_contract_date_object.contract_date) new_forward_contract_object = futuresContract( instrument_object, new_forward_contract_date.contract_date) new_carry_contract_object = futuresContract( instrument_object, new_carry_contract_date.contract_date) new_price_price = old_forward_contract_last_price new_forward_price = get_final_matched_price_from_contract_object( data, new_forward_contract_object, new_multiple_prices) new_carry_price = get_final_matched_price_from_contract_object( data, new_carry_contract_object, new_multiple_prices) new_price_contractid = new_price_contract_object.date_str new_forward_contractid = new_forward_contract_object.date_str new_carry_contractid = new_carry_contract_object.date_str # If any prices had to be inferred, then add row with both current priced and forward prices # Otherwise adjusted prices will break if price_inferred or forward_inferred: new_single_row = singleRowMultiplePrices( price=old_priced_contract_last_price, forward=old_forward_contract_last_price, ) new_multiple_prices = new_multiple_prices.add_one_row_with_time_delta( new_single_row) # SOME KIND OF WARNING HERE...? # Now we add a row with the new rolled contracts newer_single_row = singleRowMultiplePrices( price=new_price_price, forward=new_forward_price, carry=new_carry_price, price_contract=new_price_contractid, forward_contract=new_forward_contractid, carry_contract=new_carry_contractid, ) newer_multiple_prices = new_multiple_prices.add_one_row_with_time_delta( newer_single_row) return newer_multiple_prices