def _get_contract_data_without_checking(self, instrument_code, contract_date): result_dict = self._mongo.collection.find_one(dict(instrument_code=instrument_code, contract_date = contract_date)) result_dict.pop(MONGO_ID_KEY) # NOTE: The instrument object inside this contract will be 'bare', with only the instrument code contract_object = futuresContract.create_from_dict(result_dict) return contract_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", hold_rollcycle = "Z", carry_offset = 1)) 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") contract1c = contract1_with_roll_data.carry_contract() self.assertEqual(contract1c.date, "20190300") contract1d = contract1_with_roll_data.next_held_contract() self.assertEqual(contract1d.date, "20191200") contract1e = contract1_with_roll_data.previous_held_contract() self.assertEqual(contract1e.date, "20171200") contract_ident = futuresContract.identGivenCodeAndContractDate("EDOLLAR", "201801") self.assertEqual(contract_ident, "EDOLLAR/20180100")
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, "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 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", hold_rollcycle="Z", carry_offset=1), ) 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") contract1c = contract1_with_roll_data.carry_contract() self.assertEqual(contract1c.date, "20190300") contract1d = contract1_with_roll_data.next_held_contract() self.assertEqual(contract1d.date, "20191200") contract1e = contract1_with_roll_data.previous_held_contract() self.assertEqual(contract1e.date, "20171200") contract_ident = futuresContract.identGivenCodeAndContractDate( "EDOLLAR", "201801") self.assertEqual(contract_ident, "EDOLLAR/20180100")