def _get_contract_data_from_key_without_checking( self, key:str) ->futuresContract: result_dict = self.mongo_data.get_result_dict_for_key_without_key_value(key) if result_dict is missing_data: # shouldn't happen... raise Exception("Data for %s gone AWOL" % key) contract_object = futuresContract.create_from_dict(result_dict) return contract_object
def _from_old_style_mongo_record_to_contract_dict(mongo_record_dict): """ :param mongo_record_dict: :return: dict to pass to futuresContract.create_from_dict """ mongo_record_dict.pop("instrument_code") mongo_record_dict.pop("contract_date") contract_object = futuresContract.create_from_dict(mongo_record_dict) return contract_object
def from_mongo_record_to_contract_dict(mongo_record_dict): """ Mongo records contain additional entries: instrument_code, contract_date These are embedded within the nested dicts, so strip out :param mongo_record_dict: :return: dict to pass to futuresContract.create_from_dict """ mongo_record_dict.pop("instrument_code") mongo_record_dict.pop("contract_date") contract_object = futuresContract.create_from_dict(mongo_record_dict) return contract_object
def test_futuresContract(self): contract0 = futuresContract(futuresInstrument.create_empty(), "201801") contract1 = futuresContract.simple("EDOLLAR", "201812") self.assertEqual(contract1.date_str, "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_str, "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_str, "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", hold_rollcycle="Z", carry_offset=1), ) contract1a = contract1_with_roll_data.next_priced_contract() self.assertEqual(contract1a.date_str, "20190300") contract1b = contract1_with_roll_data.previous_priced_contract() self.assertEqual(contract1b.date_str, "20180900") contract1c = contract1_with_roll_data.carry_contract() self.assertEqual(contract1c.date_str, "20190300") contract1d = contract1_with_roll_data.next_held_contract() self.assertEqual(contract1d.date_str, "20191200") contract1e = contract1_with_roll_data.previous_held_contract() self.assertEqual(contract1e.date_str, "20171200") contract_ident = futuresContract.identGivenCodeAndContractDate( "EDOLLAR", "201801") self.assertEqual(contract_ident, "EDOLLAR/20180100")