示例#1
0
    def test_lookup_future_chain(self):
        metadata = {
            # Expires today, so should be valid
            2: {
                'symbol': 'ADN15',
                'root_symbol': 'AD',
                'asset_type': 'future',
                'expiration_date': pd.Timestamp('2015-06-15', tz='UTC'),
                'start_date': pd.Timestamp('2015-01-01', tz='UTC')
            },
            1: {
                'symbol': 'ADV15',
                'root_symbol': 'AD',
                'asset_type': 'future',
                'expiration_date': pd.Timestamp('2015-09-14', tz='UTC'),
                'start_date': pd.Timestamp('2015-01-01', tz='UTC')
            },
            # Starts trading today, so should be valid.
            0: {
                'symbol': 'ADF16',
                'root_symbol': 'AD',
                'asset_type': 'future',
                'expiration_date': pd.Timestamp('2015-12-14', tz='UTC'),
                'start_date': pd.Timestamp('2015-06-15', tz='UTC')
            },
            # Copy of the above future, but starts trading in August,
            # so it isn't valid.
            3: {
                'symbol': 'ADF16',
                'root_symbol': 'AD',
                'asset_type': 'future',
                'expiration_date': pd.Timestamp('2015-12-14', tz='UTC'),
                'start_date': pd.Timestamp('2015-08-01', tz='UTC')
            },

        }

        finder = AssetFinder(metadata=metadata)
        dt = pd.Timestamp('2015-06-15', tz='UTC')
        last_year = pd.Timestamp('2014-01-01', tz='UTC')
        first_day = pd.Timestamp('2015-01-01', tz='UTC')

        # Check that we get the expected number of contracts, in the
        # right order
        ad_contracts = finder.lookup_future_chain('AD', dt, dt)
        self.assertEqual(len(ad_contracts), 3)
        self.assertEqual(ad_contracts[0].sid, 2)
        self.assertEqual(ad_contracts[1].sid, 1)
        self.assertEqual(ad_contracts[2].sid, 0)

        # Check that we get nothing if our knowledge date is last year
        ad_contracts = finder.lookup_future_chain('AD', dt, last_year)
        self.assertEqual(len(ad_contracts), 0)

        # Check that we get things that start on the knowledge date
        ad_contracts = finder.lookup_future_chain('AD', dt, first_day)
        self.assertEqual(len(ad_contracts), 2)
示例#2
0
    def test_lookup_future_chain(self):
        metadata = {
            # Expires today, so should be valid
            2: {
                'symbol': 'ADN15',
                'root_symbol': 'AD',
                'asset_type': 'future',
                'expiration_date': pd.Timestamp('2015-06-15', tz='UTC'),
                'start_date': pd.Timestamp('2015-01-01', tz='UTC')
            },
            1: {
                'symbol': 'ADV15',
                'root_symbol': 'AD',
                'asset_type': 'future',
                'expiration_date': pd.Timestamp('2015-09-14', tz='UTC'),
                'start_date': pd.Timestamp('2015-01-01', tz='UTC')
            },
            # Starts trading today, so should be valid.
            0: {
                'symbol': 'ADF16',
                'root_symbol': 'AD',
                'asset_type': 'future',
                'expiration_date': pd.Timestamp('2015-12-14', tz='UTC'),
                'start_date': pd.Timestamp('2015-06-15', tz='UTC')
            },
            # Copy of the above future, but starts trading in August,
            # so it isn't valid.
            3: {
                'symbol': 'ADF16',
                'root_symbol': 'AD',
                'asset_type': 'future',
                'expiration_date': pd.Timestamp('2015-12-14', tz='UTC'),
                'start_date': pd.Timestamp('2015-08-01', tz='UTC')
            },
        }

        finder = AssetFinder(metadata=metadata)
        dt = pd.Timestamp('2015-06-15', tz='UTC')
        last_year = pd.Timestamp('2014-01-01', tz='UTC')
        first_day = pd.Timestamp('2015-01-01', tz='UTC')

        # Check that we get the expected number of contracts, in the
        # right order
        ad_contracts = finder.lookup_future_chain('AD', dt, dt)
        self.assertEqual(len(ad_contracts), 3)
        self.assertEqual(ad_contracts[0].sid, 2)
        self.assertEqual(ad_contracts[1].sid, 1)
        self.assertEqual(ad_contracts[2].sid, 0)

        # Check that we get nothing if our knowledge date is last year
        ad_contracts = finder.lookup_future_chain('AD', dt, last_year)
        self.assertEqual(len(ad_contracts), 0)

        # Check that we get things that start on the knowledge date
        ad_contracts = finder.lookup_future_chain('AD', dt, first_day)
        self.assertEqual(len(ad_contracts), 2)
示例#3
0
    def test_lookup_future_chain(self):
        metadata = {
            2: {
                'symbol': 'ADN15',
                'root_symbol': 'AD',
                'asset_type': 'future',
                'expiration_date': pd.Timestamp('2015-06-15', tz='UTC')
            },
            1: {
                'symbol': 'ADV15',
                'root_symbol': 'AD',
                'asset_type': 'future',
                'expiration_date': pd.Timestamp('2015-09-14', tz='UTC')
            },
            0: {
                'symbol': 'ADF16',
                'root_symbol': 'AD',
                'asset_type': 'future',
                'expiration_date': pd.Timestamp('2015-12-14', tz='UTC')
            },

        }

        finder = AssetFinder(metadata=metadata)
        dt = pd.Timestamp('2015-06-19', tz='UTC')

        # Check that we get the expected number of contract, in the
        # right order
        ad_contracts = finder.lookup_future_chain('AD', dt)
        self.assertEqual(len(ad_contracts), 2)
        self.assertEqual(ad_contracts[0].sid, 1)
        self.assertEqual(ad_contracts[1].sid, 0)
示例#4
0
    def test_lookup_future_chain(self):
        metadata = {
            # Notice day is today, so not valid
            2: {
                'symbol': 'ADN15',
                'root_symbol': 'AD',
                'asset_type': 'future',
                'notice_date': pd.Timestamp('2015-05-14', tz='UTC'),
                'start_date': pd.Timestamp('2015-01-01', tz='UTC')
            },
            1: {
                'symbol': 'ADV15',
                'root_symbol': 'AD',
                'asset_type': 'future',
                'notice_date': pd.Timestamp('2015-08-14', tz='UTC'),
                'start_date': pd.Timestamp('2015-01-01', tz='UTC')
            },
            # Starts trading today, so should be valid.
            0: {
                'symbol': 'ADF16',
                'root_symbol': 'AD',
                'asset_type': 'future',
                'notice_date': pd.Timestamp('2015-11-16', tz='UTC'),
                'start_date': pd.Timestamp('2015-05-14', tz='UTC')
            },
            # Starts trading in August, so not valid.
            3: {
                'symbol': 'ADX16',
                'root_symbol': 'AD',
                'asset_type': 'future',
                'notice_date': pd.Timestamp('2015-11-16', tz='UTC'),
                'start_date': pd.Timestamp('2015-08-01', tz='UTC')
            },
        }
        self.env.write_data(futures_data=metadata)
        finder = AssetFinder(self.env.engine)
        dt = pd.Timestamp('2015-05-14', tz='UTC')
        last_year = pd.Timestamp('2014-01-01', tz='UTC')
        first_day = pd.Timestamp('2015-01-01', tz='UTC')

        # Check that we get the expected number of contracts, in the
        # right order
        ad_contracts = finder.lookup_future_chain('AD', dt, dt)
        self.assertEqual(len(ad_contracts), 2)
        self.assertEqual(ad_contracts[0].sid, 1)
        self.assertEqual(ad_contracts[1].sid, 0)

        # Check that pd.NaT for knowledge_date uses the value of as_of_date
        ad_contracts = finder.lookup_future_chain('AD', dt, pd.NaT)
        self.assertEqual(len(ad_contracts), 2)

        # Check that we get nothing if our knowledge date is last year
        ad_contracts = finder.lookup_future_chain('AD', dt, last_year)
        self.assertEqual(len(ad_contracts), 0)

        # Check that we get things that start on the knowledge date
        ad_contracts = finder.lookup_future_chain('AD', dt, first_day)
        self.assertEqual(len(ad_contracts), 1)

        # Check that pd.NaT for as_of_date gives the whole chain
        ad_contracts = finder.lookup_future_chain('AD', pd.NaT, first_day)
        self.assertEqual(len(ad_contracts), 4)
示例#5
0
    def test_lookup_future_chain(self):
        metadata = {
            # Notice day is today, so should be valid.
            0: {
                "symbol": "ADN15",
                "root_symbol": "AD",
                "notice_date": pd.Timestamp("2015-05-14", tz="UTC"),
                "expiration_date": pd.Timestamp("2015-06-14", tz="UTC"),
                "start_date": pd.Timestamp("2015-01-01", tz="UTC"),
            },
            1: {
                "symbol": "ADV15",
                "root_symbol": "AD",
                "notice_date": pd.Timestamp("2015-08-14", tz="UTC"),
                "expiration_date": pd.Timestamp("2015-09-14", tz="UTC"),
                "start_date": pd.Timestamp("2015-01-01", tz="UTC"),
            },
            # Starts trading today, so should be valid.
            2: {
                "symbol": "ADF16",
                "root_symbol": "AD",
                "notice_date": pd.Timestamp("2015-11-16", tz="UTC"),
                "expiration_date": pd.Timestamp("2015-12-16", tz="UTC"),
                "start_date": pd.Timestamp("2015-05-14", tz="UTC"),
            },
            # Starts trading in August, so not valid.
            3: {
                "symbol": "ADX16",
                "root_symbol": "AD",
                "notice_date": pd.Timestamp("2015-11-16", tz="UTC"),
                "expiration_date": pd.Timestamp("2015-12-16", tz="UTC"),
                "start_date": pd.Timestamp("2015-08-01", tz="UTC"),
            },
            # Notice date comes after expiration
            4: {
                "symbol": "ADZ16",
                "root_symbol": "AD",
                "notice_date": pd.Timestamp("2016-11-25", tz="UTC"),
                "expiration_date": pd.Timestamp("2016-11-16", tz="UTC"),
                "start_date": pd.Timestamp("2015-08-01", tz="UTC"),
            },
            # This contract has no start date and also this contract should be
            # last in all chains
            5: {
                "symbol": "ADZ20",
                "root_symbol": "AD",
                "notice_date": pd.Timestamp("2020-11-25", tz="UTC"),
                "expiration_date": pd.Timestamp("2020-11-16", tz="UTC"),
            },
        }
        self.env.write_data(futures_data=metadata)
        finder = AssetFinder(self.env.engine)
        dt = pd.Timestamp("2015-05-14", tz="UTC")
        dt_2 = pd.Timestamp("2015-10-14", tz="UTC")
        dt_3 = pd.Timestamp("2016-11-17", tz="UTC")

        # Check that we get the expected number of contracts, in the
        # right order
        ad_contracts = finder.lookup_future_chain("AD", dt)
        self.assertEqual(len(ad_contracts), 6)
        self.assertEqual(ad_contracts[0].sid, 0)
        self.assertEqual(ad_contracts[1].sid, 1)
        self.assertEqual(ad_contracts[5].sid, 5)

        # Check that, when some contracts have expired, the chain has advanced
        # properly to the next contracts
        ad_contracts = finder.lookup_future_chain("AD", dt_2)
        self.assertEqual(len(ad_contracts), 4)
        self.assertEqual(ad_contracts[0].sid, 2)
        self.assertEqual(ad_contracts[3].sid, 5)

        # Check that when the expiration_date has passed but the
        # notice_date hasn't, contract is still considered invalid.
        ad_contracts = finder.lookup_future_chain("AD", dt_3)
        self.assertEqual(len(ad_contracts), 1)
        self.assertEqual(ad_contracts[0].sid, 5)

        # Check that pd.NaT for as_of_date gives the whole chain
        ad_contracts = finder.lookup_future_chain("AD", pd.NaT)
        self.assertEqual(len(ad_contracts), 6)
        self.assertEqual(ad_contracts[5].sid, 5)
示例#6
0
    def test_lookup_future_chain(self):
        metadata = {
            # Notice day is today, so should be valid.
            2: {
                'symbol': 'ADN15',
                'root_symbol': 'AD',
                'asset_type': 'future',
                'notice_date': pd.Timestamp('2015-05-14', tz='UTC'),
                'expiration_date': pd.Timestamp('2015-06-14', tz='UTC'),
                'start_date': pd.Timestamp('2015-01-01', tz='UTC')
            },
            1: {
                'symbol': 'ADV15',
                'root_symbol': 'AD',
                'asset_type': 'future',
                'notice_date': pd.Timestamp('2015-08-14', tz='UTC'),
                'expiration_date': pd.Timestamp('2015-09-14', tz='UTC'),
                'start_date': pd.Timestamp('2015-01-01', tz='UTC')
            },
            # Starts trading today, so should be valid.
            0: {
                'symbol': 'ADF16',
                'root_symbol': 'AD',
                'asset_type': 'future',
                'notice_date': pd.Timestamp('2015-11-16', tz='UTC'),
                'expiration_date': pd.Timestamp('2015-12-16', tz='UTC'),
                'start_date': pd.Timestamp('2015-05-14', tz='UTC')
            },
            # Starts trading in August, so not valid.
            3: {
                'symbol': 'ADX16',
                'root_symbol': 'AD',
                'asset_type': 'future',
                'notice_date': pd.Timestamp('2015-11-16', tz='UTC'),
                'expiration_date': pd.Timestamp('2015-12-16', tz='UTC'),
                'start_date': pd.Timestamp('2015-08-01', tz='UTC')
            },
            # Notice date comes after expiration
            4: {
                'symbol': 'ADZ16',
                'root_symbol': 'AD',
                'asset_type': 'future',
                'notice_date': pd.Timestamp('2015-11-25', tz='UTC'),
                'expiration_date': pd.Timestamp('2016-11-16', tz='UTC'),
                'start_date': pd.Timestamp('2015-08-01', tz='UTC')
            },
        }
        self.env.write_data(futures_data=metadata)
        finder = AssetFinder(self.env.engine)
        dt = pd.Timestamp('2015-05-14', tz='UTC')
        last_year = pd.Timestamp('2014-01-01', tz='UTC')
        first_day = pd.Timestamp('2015-01-01', tz='UTC')
        dt_2 = pd.Timestamp('2016-11-17', tz='UTC')

        # Check that we get the expected number of contracts, in the
        # right order
        ad_contracts = finder.lookup_future_chain('AD', dt, dt)
        self.assertEqual(len(ad_contracts), 3)
        self.assertEqual(ad_contracts[0].sid, 2)
        self.assertEqual(ad_contracts[1].sid, 1)

        # Check that pd.NaT for knowledge_date uses the value of as_of_date
        ad_contracts = finder.lookup_future_chain('AD', dt, pd.NaT)
        self.assertEqual(len(ad_contracts), 3)

        # Check that we get nothing if our knowledge date is last year
        ad_contracts = finder.lookup_future_chain('AD', dt, last_year)
        self.assertEqual(len(ad_contracts), 0)

        # Check that we get things that start on the knowledge date
        ad_contracts = finder.lookup_future_chain('AD', dt, first_day)
        self.assertEqual(len(ad_contracts), 2)

        # Check that pd.NaT for as_of_date gives the whole chain
        ad_contracts = finder.lookup_future_chain('AD', pd.NaT, first_day)
        self.assertEqual(len(ad_contracts), 5)

        # Check that when the expiration_date has past but the
        # notice_date hasn't, contract is still considered invalid.
        ad_contracts = finder.lookup_future_chain('AD', dt_2, dt_2)
        self.assertEqual(len(ad_contracts), 0)
示例#7
0
    def test_lookup_future_chain(self):
        metadata = {
            # Notice day is today, so not valid
            2: {
                'symbol': 'ADN15',
                'root_symbol': 'AD',
                'asset_type': 'future',
                'notice_date': pd.Timestamp('2015-05-14', tz='UTC'),
                'start_date': pd.Timestamp('2015-01-01', tz='UTC')
            },
            1: {
                'symbol': 'ADV15',
                'root_symbol': 'AD',
                'asset_type': 'future',
                'notice_date': pd.Timestamp('2015-08-14', tz='UTC'),
                'start_date': pd.Timestamp('2015-01-01', tz='UTC')
            },
            # Starts trading today, so should be valid.
            0: {
                'symbol': 'ADF16',
                'root_symbol': 'AD',
                'asset_type': 'future',
                'notice_date': pd.Timestamp('2015-11-16', tz='UTC'),
                'start_date': pd.Timestamp('2015-05-14', tz='UTC')
            },
            # Copy of the above future, but starts trading in August,
            # so it isn't valid.
            3: {
                'symbol': 'ADF16',
                'root_symbol': 'AD',
                'asset_type': 'future',
                'notice_date': pd.Timestamp('2015-11-16', tz='UTC'),
                'start_date': pd.Timestamp('2015-08-01', tz='UTC')
            },

        }

        finder = AssetFinder(metadata=metadata)
        dt = pd.Timestamp('2015-05-14', tz='UTC')
        last_year = pd.Timestamp('2014-01-01', tz='UTC')
        first_day = pd.Timestamp('2015-01-01', tz='UTC')

        # Check that we get the expected number of contracts, in the
        # right order
        ad_contracts = finder.lookup_future_chain('AD', dt, dt)
        self.assertEqual(len(ad_contracts), 2)
        self.assertEqual(ad_contracts[0].sid, 1)
        self.assertEqual(ad_contracts[1].sid, 0)

        # Check that pd.NaT for knowledge_date uses the value of as_of_date
        ad_contracts = finder.lookup_future_chain('AD', dt, pd.NaT)
        self.assertEqual(len(ad_contracts), 2)

        # Check that we get nothing if our knowledge date is last year
        ad_contracts = finder.lookup_future_chain('AD', dt, last_year)
        self.assertEqual(len(ad_contracts), 0)

        # Check that we get things that start on the knowledge date
        ad_contracts = finder.lookup_future_chain('AD', dt, first_day)
        self.assertEqual(len(ad_contracts), 1)

        # Check that pd.NaT for as_of_date gives the whole chain
        ad_contracts = finder.lookup_future_chain('AD', pd.NaT, first_day)
        self.assertEqual(len(ad_contracts), 4)
示例#8
0
    def test_lookup_future_chain(self):
        metadata = {
            # Notice day is today, so should be valid.
            0: {
                'symbol': 'ADN15',
                'root_symbol': 'AD',
                'asset_type': 'future',
                'notice_date': pd.Timestamp('2015-05-14', tz='UTC'),
                'expiration_date': pd.Timestamp('2015-06-14', tz='UTC'),
                'start_date': pd.Timestamp('2015-01-01', tz='UTC')
            },
            1: {
                'symbol': 'ADV15',
                'root_symbol': 'AD',
                'asset_type': 'future',
                'notice_date': pd.Timestamp('2015-08-14', tz='UTC'),
                'expiration_date': pd.Timestamp('2015-09-14', tz='UTC'),
                'start_date': pd.Timestamp('2015-01-01', tz='UTC')
            },
            # Starts trading today, so should be valid.
            2: {
                'symbol': 'ADF16',
                'root_symbol': 'AD',
                'asset_type': 'future',
                'notice_date': pd.Timestamp('2015-11-16', tz='UTC'),
                'expiration_date': pd.Timestamp('2015-12-16', tz='UTC'),
                'start_date': pd.Timestamp('2015-05-14', tz='UTC')
            },
            # Starts trading in August, so not valid.
            3: {
                'symbol': 'ADX16',
                'root_symbol': 'AD',
                'asset_type': 'future',
                'notice_date': pd.Timestamp('2015-11-16', tz='UTC'),
                'expiration_date': pd.Timestamp('2015-12-16', tz='UTC'),
                'start_date': pd.Timestamp('2015-08-01', tz='UTC')
            },
            # Notice date comes after expiration
            4: {
                'symbol': 'ADZ16',
                'root_symbol': 'AD',
                'asset_type': 'future',
                'notice_date': pd.Timestamp('2016-11-25', tz='UTC'),
                'expiration_date': pd.Timestamp('2016-11-16', tz='UTC'),
                'start_date': pd.Timestamp('2015-08-01', tz='UTC')
            },
            # This contract has no start date and also this contract should be
            # last in all chains
            5: {
                'symbol': 'ADZ20',
                'root_symbol': 'AD',
                'asset_type': 'future',
                'notice_date': pd.Timestamp('2020-11-25', tz='UTC'),
                'expiration_date': pd.Timestamp('2020-11-16', tz='UTC')
            },
        }
        self.env.write_data(futures_data=metadata)
        finder = AssetFinder(self.env.engine)
        dt = pd.Timestamp('2015-05-14', tz='UTC')
        dt_2 = pd.Timestamp('2015-10-14', tz='UTC')
        dt_3 = pd.Timestamp('2016-11-17', tz='UTC')

        # Check that we get the expected number of contracts, in the
        # right order
        ad_contracts = finder.lookup_future_chain('AD', dt)
        self.assertEqual(len(ad_contracts), 6)
        self.assertEqual(ad_contracts[0].sid, 0)
        self.assertEqual(ad_contracts[1].sid, 1)
        self.assertEqual(ad_contracts[5].sid, 5)

        # Check that, when some contracts have expired, the chain has advanced
        # properly to the next contracts
        ad_contracts = finder.lookup_future_chain('AD', dt_2)
        self.assertEqual(len(ad_contracts), 4)
        self.assertEqual(ad_contracts[0].sid, 2)
        self.assertEqual(ad_contracts[3].sid, 5)

        # Check that when the expiration_date has passed but the
        # notice_date hasn't, contract is still considered invalid.
        ad_contracts = finder.lookup_future_chain('AD', dt_3)
        self.assertEqual(len(ad_contracts), 1)
        self.assertEqual(ad_contracts[0].sid, 5)

        # Check that pd.NaT for as_of_date gives the whole chain
        ad_contracts = finder.lookup_future_chain('AD', pd.NaT)
        self.assertEqual(len(ad_contracts), 6)
        self.assertEqual(ad_contracts[5].sid, 5)
示例#9
0
    def test_lookup_future_chain(self):
        metadata = {
            # Notice day is today, so not valid
            2: {
                "symbol": "ADN15",
                "root_symbol": "AD",
                "asset_type": "future",
                "notice_date": pd.Timestamp("2015-05-14", tz="UTC"),
                "start_date": pd.Timestamp("2015-01-01", tz="UTC"),
            },
            1: {
                "symbol": "ADV15",
                "root_symbol": "AD",
                "asset_type": "future",
                "notice_date": pd.Timestamp("2015-08-14", tz="UTC"),
                "start_date": pd.Timestamp("2015-01-01", tz="UTC"),
            },
            # Starts trading today, so should be valid.
            0: {
                "symbol": "ADF16",
                "root_symbol": "AD",
                "asset_type": "future",
                "notice_date": pd.Timestamp("2015-11-16", tz="UTC"),
                "start_date": pd.Timestamp("2015-05-14", tz="UTC"),
            },
            # Copy of the above future, but starts trading in August,
            # so it isn't valid.
            3: {
                "symbol": "ADF16",
                "root_symbol": "AD",
                "asset_type": "future",
                "notice_date": pd.Timestamp("2015-11-16", tz="UTC"),
                "start_date": pd.Timestamp("2015-08-01", tz="UTC"),
            },
        }
        self.env.write_data(futures_data=metadata)
        finder = AssetFinder(self.env.engine)
        dt = pd.Timestamp("2015-05-14", tz="UTC")
        last_year = pd.Timestamp("2014-01-01", tz="UTC")
        first_day = pd.Timestamp("2015-01-01", tz="UTC")

        # Check that we get the expected number of contracts, in the
        # right order
        ad_contracts = finder.lookup_future_chain("AD", dt, dt)
        self.assertEqual(len(ad_contracts), 2)
        self.assertEqual(ad_contracts[0].sid, 1)
        self.assertEqual(ad_contracts[1].sid, 0)

        # Check that pd.NaT for knowledge_date uses the value of as_of_date
        ad_contracts = finder.lookup_future_chain("AD", dt, pd.NaT)
        self.assertEqual(len(ad_contracts), 2)

        # Check that we get nothing if our knowledge date is last year
        ad_contracts = finder.lookup_future_chain("AD", dt, last_year)
        self.assertEqual(len(ad_contracts), 0)

        # Check that we get things that start on the knowledge date
        ad_contracts = finder.lookup_future_chain("AD", dt, first_day)
        self.assertEqual(len(ad_contracts), 1)

        # Check that pd.NaT for as_of_date gives the whole chain
        ad_contracts = finder.lookup_future_chain("AD", pd.NaT, first_day)
        self.assertEqual(len(ad_contracts), 4)