def test_roll_with_date(self):
        roll_data = rollParameters(priced_rollcycle="HMUZ", hold_rollcycle="Z",  approx_expiry_offset=15)
        rollwithdate = contractDateWithRollParameters(roll_data, "201801")

        self.assertRaises(Exception, rollwithdate.next_priced_contract)

        roll_data_no_price_cycle = rollParameters(hold_rollcycle="F", approx_expiry_offset=15)
        rollwithdate = contractDateWithRollParameters(roll_data, "201801")

        self.assertRaises(Exception, rollwithdate.next_priced_contract)

        roll_data = rollParameters(priced_rollcycle="HMUZ", hold_rollcycle="MZ",  approx_expiry_offset=15)
        rollwithdate = contractDateWithRollParameters(roll_data, "201806")

        self.assertEqual(rollwithdate.contract_date, "20180600")
        self.assertEqual(rollwithdate.expiry_date, datetime.datetime(2018,6,16))
        self.assertEqual(rollwithdate.check_if_expiry_after_date(datetime.datetime(2018,1,1)), True)

        next_held = rollwithdate.next_held_contract()
        prior_held = rollwithdate.previous_held_contract()

        next_priced = rollwithdate.next_priced_contract()
        prior_priced = rollwithdate.previous_priced_contract()

        self.assertEqual(next_held.contract_date, "20181200")
        self.assertEqual(prior_held.contract_date, "20171200")
        self.assertEqual(next_priced.contract_date, "20180900")
        self.assertEqual(prior_priced.contract_date, "20180300")
    def test_rolldata(self):
        roll_data_blank = rollParameters()

        self.assertRaises(Exception, roll_data_blank.check_for_price_cycle)
        self.assertRaises(Exception, roll_data_blank.check_for_hold_cycle)

        roll_data_empty = rollParameters.create_empty()
        self.assertEqual(roll_data_empty.empty(), True)

        roll_data = rollParameters(priced_rollcycle="HMUZ",
                                   hold_rollcycle="Z",
                                   contract_offset=1,
                                   approx_expiry_offset=15)

        contract_date = roll_data.approx_first_priced_contractDate_after_date(
            datetime.datetime(2008, 1, 1))
        self.assertEqual(contract_date.contract_date, "20080300")
        self.assertEqual(contract_date.expiry_date,
                         datetime.datetime(2008, 3, 16))

        contract_date_held = roll_data.approx_first_held_contractDate_after_date(
            datetime.datetime(2008, 1, 1))
        self.assertEqual(contract_date_held.contract_date, "20091200")
        self.assertEqual(contract_date_held.expiry_date,
                         datetime.datetime(2009, 12, 16))
Ejemplo n.º 3
0
    def test_roll_parameters(self):
        data = mongoRollParametersData(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, [])

        roll_object = data.get_roll_parameters("EDOLLAR")

        self.assertTrue(roll_object.empty())

        roll_object = rollParameters(hold_rollcycle ="HMUZ", priced_rollcycle="HM")
        data.add_roll_parameters(roll_object, "EDOLLAR")

        self.assertEqual(data.get_list_of_instruments(), ['EDOLLAR'])

        found_object = data.get_roll_parameters("EDOLLAR")
        self.assertEqual(found_object.hold_rollcycle.cyclestring, "HMUZ")

        found_object = data['EDOLLAR']
        self.assertEqual(found_object.priced_rollcycle.cyclestring, "HM")

        codes = data.get_list_of_instruments()
        self.assertEqual(codes, ['EDOLLAR'])

        data.delete_roll_parameters("EDOLLAR", are_you_sure=True)

        found_object = data.get_roll_parameters("EDOLLAR")

        self.assertTrue(found_object.empty())
        codes = data.get_list_of_instruments()
        self.assertEqual(codes, [])
Ejemplo n.º 4
0
    def _get_roll_parameters_without_checking(self, instrument_code):
        config_for_this_instrument = self._get_config_information().loc[instrument_code]
        roll_parameters_object = rollParameters(hold_rollcycle= config_for_this_instrument.HoldRollCycle,
                                                  roll_offset_day = config_for_this_instrument.RollOffsetDays,
                                                  carry_offset = config_for_this_instrument.CarryOffset,
                                                  priced_rollcycle = config_for_this_instrument.PricedRollCycle,
                                                  approx_expiry_offset=config_for_this_instrument.ExpiryOffset)

        return roll_parameters_object
    def test_rolldata(self):
        roll_data_blank = rollParameters()

        self.assertRaises(Exception, roll_data_blank.check_for_price_cycle)
        self.assertRaises(Exception, roll_data_blank.check_for_hold_cycle)

        roll_data_empty = rollParameters.create_empty()
        self.assertEqual(roll_data_empty.empty(), True)

        roll_data = rollParameters(priced_rollcycle="HMUZ", hold_rollcycle="Z",  approx_expiry_offset=15)

        contract_date = roll_data.approx_first_held_contractDate_at_date(datetime.datetime(2008,1,1))
        self.assertEqual(contract_date.contract_date, "20081200")
        self.assertEqual(contract_date.expiry_date, datetime.datetime(2008,12,16))

        roll_data = rollParameters(priced_rollcycle="HMUZ", hold_rollcycle="HMUZ",   roll_offset_day=-365)

        contract_date_held = roll_data.approx_first_held_contractDate_at_date(datetime.datetime(2008,1,1))
        self.assertEqual(contract_date_held.contract_date, "20090300")
        self.assertEqual(contract_date_held.expiry_date, datetime.datetime(2009,3,1))
    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_roll_with_date(self):
        roll_data = rollParameters(priced_rollcycle="HMUZ",
                                   hold_rollcycle="Z",
                                   contract_offset=1,
                                   approx_expiry_offset=15)
        rollwithdate = contractDateWithRollParameters(roll_data, "201801")

        self.assertRaises(Exception, rollwithdate.next_priced_contract)

        roll_data_no_price_cycle = rollParameters(hold_rollcycle="F",
                                                  contract_offset=1,
                                                  approx_expiry_offset=15)
        rollwithdate = contractDateWithRollParameters(roll_data, "201801")

        self.assertRaises(Exception, rollwithdate.next_priced_contract)

        roll_data = rollParameters(priced_rollcycle="HMUZ",
                                   hold_rollcycle="MZ",
                                   contract_offset=1,
                                   approx_expiry_offset=15)
        rollwithdate = contractDateWithRollParameters(roll_data, "201806")

        self.assertEqual(rollwithdate.contract_date, "20180600")
        self.assertEqual(rollwithdate.expiry_date,
                         datetime.datetime(2018, 6, 16))
        self.assertEqual(
            rollwithdate.check_if_expiry_after_date(
                datetime.datetime(2018, 1, 1)), True)

        next_held = rollwithdate.next_held_contract()
        prior_held = rollwithdate.previous_held_contract()

        next_priced = rollwithdate.next_priced_contract()
        prior_priced = rollwithdate.previous_priced_contract()

        self.assertEqual(next_held.contract_date, "20181200")
        self.assertEqual(prior_held.contract_date, "20171200")
        self.assertEqual(next_priced.contract_date, "20180900")
        self.assertEqual(prior_priced.contract_date, "20180300")
Ejemplo n.º 8
0
    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_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")