Exemplo n.º 1
0
    def test_repr(self):

        future = Future(2468,
                        notice_date='2014-01-20',
                        expiration_date='2014-02-20')
        rep = future.__repr__()

        self.assertTrue("Future" in rep)
        self.assertTrue("2468" in rep)
        self.assertTrue("notice_date='2014-01-20'" in rep)
        self.assertTrue("expiration_date='2014-02-20'" in rep)
Exemplo n.º 2
0
    def _retrieve_futures_contract(self, sid):
        try:
            return self._future_cache[sid]
        except KeyError:
            pass

        c = self.conn.cursor()
        t = (int(sid), )
        c.row_factory = Row
        c.execute(FUTURE_BY_SID_QUERY, t)
        data = dict(c.fetchone())
        if data:
            if data['start_date']:
                data['start_date'] = pd.Timestamp(data['start_date'], tz='UTC')

            if data['end_date']:
                data['end_date'] = pd.Timestamp(data['end_date'], tz='UTC')

            if data['first_traded']:
                data['first_traded'] = pd.Timestamp(data['first_traded'],
                                                    tz='UTC')

            if data['notice_date']:
                data['notice_date'] = pd.Timestamp(data['notice_date'],
                                                   tz='UTC')

            if data['expiration_date']:
                data['expiration_date'] = pd.Timestamp(data['expiration_date'],
                                                       tz='UTC')

            future = Future(**data)
        else:
            future = None

        self._future_cache[sid] = future
        return future
Exemplo n.º 3
0
    def _spawn_asset(self, identifier, **kwargs):

        # Check if the sid is declared
        try:
            kwargs['sid']
            pass
        except KeyError:
            # If the identifier is not a sid, assign one
            kwargs['sid'] = self._assign_sid(identifier)
            # Update the metadata object with the new sid
            self.insert_metadata(identifier=identifier, sid=kwargs['sid'])

        # If the file_name is in the kwargs, it will be used as the symbol
        try:
            kwargs['symbol'] = kwargs.pop('file_name')
        except KeyError:
            pass

        # If the identifier coming in was a string and there is no defined
        # symbol yet, set the symbol to the incoming identifier
        try:
            kwargs['symbol']
            pass
        except KeyError:
            if isinstance(identifier, string_types):
                kwargs['symbol'] = identifier

        # If the company_name is in the kwargs, it may be the asset_name
        try:
            company_name = kwargs.pop('company_name')
            try:
                kwargs['asset_name']
            except KeyError:
                kwargs['asset_name'] = company_name
        except KeyError:
            pass

        # If dates are given as nanos, pop them
        try:
            kwargs['start_date'] = kwargs.pop('start_date_nano')
        except KeyError:
            pass
        try:
            kwargs['end_date'] = kwargs.pop('end_date_nano')
        except KeyError:
            pass
        try:
            kwargs['notice_date'] = kwargs.pop('notice_date_nano')
        except KeyError:
            pass
        try:
            kwargs['expiration_date'] = kwargs.pop('expiration_date_nano')
        except KeyError:
            pass

        # Process dates to Timestamps
        try:
            kwargs['start_date'] = pd.Timestamp(kwargs['start_date'], tz='UTC')
        except KeyError:
            pass
        try:
            kwargs['end_date'] = pd.Timestamp(kwargs['end_date'], tz='UTC')
        except KeyError:
            pass
        try:
            kwargs['notice_date'] = pd.Timestamp(kwargs['notice_date'],
                                                 tz='UTC')
        except KeyError:
            pass
        try:
            kwargs['expiration_date'] = pd.Timestamp(kwargs['expiration_date'],
                                                     tz='UTC')
        except KeyError:
            pass

        # Build an Asset of the appropriate type, default to Equity
        asset_type = kwargs.pop('asset_type', 'equity')
        if asset_type.lower() == 'equity':
            asset = Equity(**kwargs)
        elif asset_type.lower() == 'future':
            asset = Future(**kwargs)
        else:
            raise InvalidAssetType(asset_type=asset_type)

        return asset
Exemplo n.º 4
0
    def _insert_metadata(self, identifier, **kwargs):
        """
        Inserts the given metadata kwargs to the entry for the given
        identifier. Matching fields in the existing entry will be overwritten.
        :param identifier: The identifier for which to insert metadata
        :param kwargs: The keyed metadata to insert
        """
        if identifier in self.metadata_cache:
            # Multiple pass insertion no longer supported.
            # This could and probably should raise an Exception, but is
            # currently just a short-circuit for compatibility with existing
            # testing structure in the test_algorithm module which creates
            # multiple sources which all insert redundant metadata.
            return

        entry = {}

        for key, value in kwargs.items():
            # Do not accept invalid fields
            if key not in ASSET_FIELDS:
                continue
            # Do not accept Nones
            if value is None:
                continue
            # Do not accept empty strings
            if value == '':
                continue
            # Do not accept nans from dataframes
            if isinstance(value, float) and np.isnan(value):
                continue
            entry[key] = value

        # Check if the sid is declared
        try:
            entry['sid']
        except KeyError:
            # If the identifier is not a sid, assign one
            if hasattr(identifier, '__int__'):
                entry['sid'] = identifier.__int__()
            else:
                if self.allow_sid_assignment:
                    # Assign the sid the value of its insertion order.
                    # This assumes that we are assigning values to all assets.
                    entry['sid'] = len(self.metadata_cache)
                else:
                    raise SidAssignmentError(identifier=identifier)

        # If the file_name is in the kwargs, it will be used as the symbol
        try:
            entry['symbol'] = entry.pop('file_name')
        except KeyError:
            pass

        # If the identifier coming in was a string and there is no defined
        # symbol yet, set the symbol to the incoming identifier
        try:
            entry['symbol']
            pass
        except KeyError:
            if isinstance(identifier, string_types):
                entry['symbol'] = identifier

        # If the company_name is in the kwargs, it may be the asset_name
        try:
            company_name = entry.pop('company_name')
            try:
                entry['asset_name']
            except KeyError:
                entry['asset_name'] = company_name
        except KeyError:
            pass

        # If dates are given as nanos, pop them
        try:
            entry['start_date'] = entry.pop('start_date_nano')
        except KeyError:
            pass
        try:
            entry['end_date'] = entry.pop('end_date_nano')
        except KeyError:
            pass
        try:
            entry['notice_date'] = entry.pop('notice_date_nano')
        except KeyError:
            pass
        try:
            entry['expiration_date'] = entry.pop('expiration_date_nano')
        except KeyError:
            pass

        # Process dates to Timestamps
        try:
            entry['start_date'] = pd.Timestamp(entry['start_date'], tz='UTC')
        except KeyError:
            # Set a default start_date of the EPOCH, so that all date queries
            # work when a start date is not provided.
            entry['start_date'] = pd.Timestamp(0, tz='UTC')
        try:
            # Set a default end_date of 'now', so that all date queries
            # work when a end date is not provided.
            entry['end_date'] = pd.Timestamp(entry['end_date'], tz='UTC')
        except KeyError:
            entry['end_date'] = self.end_date_to_assign
        try:
            entry['notice_date'] = pd.Timestamp(entry['notice_date'], tz='UTC')
        except KeyError:
            pass
        try:
            entry['expiration_date'] = pd.Timestamp(entry['expiration_date'],
                                                    tz='UTC')
        except KeyError:
            pass

        # Build an Asset of the appropriate type, default to Equity
        asset_type = entry.pop('asset_type', 'equity')
        if asset_type.lower() == 'equity':
            try:
                fuzzy = entry['symbol'].replace(self.fuzzy_char, '') \
                    if self.fuzzy_char else None
            except KeyError:
                fuzzy = None
            asset = Equity(**entry)
            c = self.conn.cursor()
            t = (asset.sid, asset.symbol, asset.asset_name,
                 asset.start_date.value if asset.start_date else None,
                 asset.end_date.value if asset.end_date else None,
                 asset.first_traded.value if asset.first_traded else None,
                 asset.exchange, fuzzy)
            c.execute(
                """INSERT INTO equities(
            sid,
            symbol,
            asset_name,
            start_date,
            end_date,
            first_traded,
            exchange,
            fuzzy)
            VALUES(?, ?, ?, ?, ?, ?, ?, ?)""", t)

            t = (asset.sid, 'equity')
            c.execute(
                """INSERT INTO asset_router(sid, asset_type)
            VALUES(?, ?)""", t)

        elif asset_type.lower() == 'future':
            asset = Future(**entry)
            c = self.conn.cursor()
            t = (asset.sid, asset.symbol, asset.asset_name,
                 asset.start_date.value if asset.start_date else None,
                 asset.end_date.value if asset.end_date else None,
                 asset.first_traded.value if asset.first_traded else None,
                 asset.exchange, asset.root_symbol,
                 asset.notice_date.value if asset.notice_date else None,
                 asset.expiration_date.value
                 if asset.expiration_date else None, asset.contract_multiplier)
            c.execute(
                """INSERT INTO futures(
            sid,
            symbol,
            asset_name,
            start_date,
            end_date,
            first_traded,
            exchange,
            root_symbol,
            notice_date,
            expiration_date,
            contract_multiplier)
            VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)""", t)

            t = (asset.sid, 'future')
            c.execute(
                """INSERT INTO asset_router(sid, asset_type)
            VALUES(?, ?)""", t)
        else:
            raise InvalidAssetType(asset_type=asset_type)

        self.metadata_cache[identifier] = entry