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 type(instrument_object) is str: if type(contract_date_object) is str: # create a simple object self.instrument = futuresInstrument(instrument_object) self.contract_date = contractDate(contract_date_object) if type(contract_date_object) is 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_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 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 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 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 _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_object = futuresInstrument(instrument_code, **meta_data) print(instrument_object) return instrument_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 test_list_of_futures_contracts(self): instrument_object = futuresInstrument("EDOLLAR") roll_parameters = rollParameters(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, "20000300") self.assertEqual(flist[-1].date, "20010300")
def test_list_of_futures_contracts(self): instrument_object = futuresInstrument("EDOLLAR") roll_parameters = rollParameters(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, "20000300") self.assertEqual(flist[-1].date, "20010300")
def _get_instrument_data_without_checking(self, instrument_code): config_for_this_instrument = self._get_config_information( ).loc[instrument_code] instrument_object = futuresInstrument( instrument_code, description=config_for_this_instrument.Description, exchange=config_for_this_instrument.Exchange, point_size=config_for_this_instrument.Pointsize, currency=config_for_this_instrument.Currency, asset_class=config_for_this_instrument.Assetclass) print(instrument_object) return instrument_object
def get_instrument_object_from_config( instrument_code, config = None): if config is None: config = get_ib_config() 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) ib_multiplier = value_or_npnan(config_row.IBMultiplier.values[0], NOT_REQUIRED) my_multiplier = value_or_npnan(config_row.MyMultiplier.values[0], NOT_REQUIRED) ignore_weekly = config_row.IgnoreWeekly.values[0] ## We use the flexibility of futuresInstrument to add additional arguments instrument_config = futuresInstrument(instrument_code, symbol=symbol, exchange=exchange, currency=currency, ibMultiplier=ib_multiplier, myMultiplier=my_multiplier, ignoreWeekly=ignore_weekly) return instrument_config
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, position, *args, **kwargs): tradeable_object = futuresInstrument(*args, **kwargs) super().__init__(position, tradeable_object)
def test_futuresContract(self): contract0 = futuresContract(futuresInstrument.create_empty(), "201801") contract1 = futuresContract.simple("EDOLLAR", "201812") self.assertEqual(contract1.date, "20181200") self.assertEqual(contract1.instrument_code, "EDOLLAR") self.assertTrue(contract1.expiry_date, datetime.datetime(2018, 12, 1)) # dictionaries contract1_as_dict = contract1.as_dict() self.assertEqual( contract1_as_dict, dict(instrument_code="EDOLLAR", expiry_date=(2018, 12, 1), contract_date="201812", approx_expiry_offset=0)) contract1_fromdict = futuresContract.create_from_dict( contract1_as_dict) self.assertEqual(contract1_fromdict.instrument_code, "EDOLLAR") self.assertEqual(contract1_fromdict.expiry_date, datetime.datetime(2018, 12, 1)) self.assertEqual(contract1_fromdict.date, "20181200") contract2 = futuresContract.simple("EDOLLAR", "20181215", expiry_date=(2018, 12, 15)) self.assertEqual(contract2.expiry_date, datetime.datetime(2018, 12, 15)) self.assertEqual(contract2.date, "20181215") contract3 = futuresContract.simple("EDOLLAR", "20181215", approx_expiry_offset=4) self.assertEqual(contract3.expiry_date, datetime.datetime(2018, 12, 19)) # rolling contract1_with_roll_data = futuresContract.create_from_dict_with_rolldata( dict(instrument_code="EDOLLAR", contract_date="201812"), dict(priced_rollcycle="HMUZ")) contract1a = contract1_with_roll_data.next_priced_contract() self.assertEqual(contract1a.date, "20190300") contract1b = contract1_with_roll_data.previous_priced_contract() self.assertEqual(contract1b.date, "20180900") contract3 = futuresContract.approx_first_priced_futuresContract_after_date( futuresInstrument("EDOLLAR"), rollParameters(priced_rollcycle="HMUZ"), datetime.datetime(1970, 12, 1)) self.assertEqual(contract3.date, "19710300") list_of_contracts = listOfFuturesContracts.series_of_price_contracts_within_daterange( futuresInstrument("EDOLLAR"), rollParameters(priced_rollcycle="HMUZ"), datetime.datetime(2016, 1, 1), datetime.datetime(2018, 1, 1)) self.assertEqual(list_of_contracts[0].date, "20160300") self.assertEqual(list_of_contracts[-1].date, "20180300") contract_ident = futuresContract.identGivenCodeAndContractDate( "EDOLLAR", "201801") self.assertEqual(contract_ident, "EDOLLAR/20180100")
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 simple(futuresContract, instrument_code, contract_date, **kwargs): return futuresContract(futuresInstrument(instrument_code), contractDate(contract_date, **kwargs))
def update_multiple_prices_on_roll(data, current_multiple_prices, instrument_code): """ 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 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[fwd_column] 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 = diagContracts(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_price_contract_object = futuresContract( instrument_object, new_price_contract_date_object) new_forward_contract_object = new_price_contract_object.next_held_contract( ) new_carry_contract_object = new_price_contract_object.carry_contract() new_price_price = get_final_matched_price_from_contract_object( data, new_price_contract_object, new_multiple_prices) 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 new_forward_contractid = new_forward_contract_object.date new_carry_contractid = new_carry_contract_object.date # 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_multiple_prices = new_multiple_prices.add_one_row_with_time_delta( dict(price=old_priced_contract_last_price, forward=old_forward_contract_last_price)) ## SOME KIND OF WARNING HERE...? # Now we add a row with the new rolled contracts new_multiple_prices = new_multiple_prices.add_one_row_with_time_delta( dict(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)) return new_multiple_prices
def __init__(self, strategy_name, *args, **kwargs): instrument_object = futuresInstrument(*args, **kwargs) self._instrument_object = instrument_object self._strategy_name = strategy_name