Ejemplo n.º 1
0
    def _build_index(self):
        def with_index(df, type, column, new_name="symbol"):
            if not "country" in df:
                df["country"] = "unknown"
            else:
                df["country"] = df["country"].replace({
                    None: "unknown",
                    "": "unknown"
                }).fillna('unknown')

            df.index = pd.MultiIndex.from_tuples([
                (s, type, c)
                for s, c in zip(df[column].to_list(), df["country"].to_list())
            ]).rename([
                new_name if new_name is not None else column, "type", "country"
            ])
            df = df.drop([column, "country"], axis=1)
            df = df.drop(df.index[df.index.duplicated('first')], axis=0)
            return df.loc[df.index.dropna()]

        symbols_df = pd.concat(
            [
                with_index(ip.get_bonds(), "BOND",
                           "name"),  # country	"name"	full_name
                with_index(
                    ip.get_certificates(), "CERT", "symbol"
                ),  # country', 'name', 'full_name', '"symbol"', 'issuer', 'isin', 'asset_class', 'underlying'
                with_index(ip.get_cryptos(), "CRYPTO",
                           "symbol"),  # 'name', '"symbol"', 'currency'
                with_index(
                    ip.get_commodities(), "COMM", "name"
                ),  # 'title', 'country', '"name"', 'full_name', 'currency', 'group'
                with_index(
                    ip.get_etfs(), "ETF", "symbol"
                ),  # 'country', 'name', 'full_name', '"symbol"', 'isin', 'asset_class', 'currency', 'stock_exchange', 'def_stock_exchange'
                # with_index(ip.get_funds(), "FUND", "isin"),             # 'country', 'name', 'symbol', 'issuer', '"isin"', 'asset_class', 'currency', 'underlying'
                with_index(
                    ip.get_indices(), "INDEX", "symbol"
                ),  # 'country', 'name', 'full_name', '"symbol"', 'currency', 'class', 'market'
                with_index(
                    ip.get_stocks(), "STOCK", "symbol"
                ),  # ['country', 'name', 'full_name', 'isin', 'currency', '"symbol"'
                with_index(
                    pd.DataFrame(
                        [f'{c}/USD' for c in ip.get_available_currencies()],
                        columns=['symbol']), "FX", "symbol")
            ],
            axis=0)

        # update the index table
        upsert(self.engine,
               symbols_df,
               DataProvider.symbols_table_name,
               if_row_exists='ignore')
Ejemplo n.º 2
0
def test_investpy_certificates():
    """
    This function checks that certificate data retrieval functions listed in investpy work properly.
    """

    params = [
        {
            'country': 'france',
        },
        {
            'country': None,
        },
    ]

    for param in params:
        investpy.get_certificates(country=param['country'])
        investpy.get_certificates_list(country=param['country'])

    params = [
        {
            'country': None,
            'columns': ['full_name', 'name'],
            'as_json': True
        },
        {
            'country': None,
            'columns': ['full_name', 'name'],
            'as_json': False
        },
        {
            'country': 'france',
            'columns': ['full_name', 'name'],
            'as_json': True
        },
        {
            'country': 'france',
            'columns': ['full_name', 'name'],
            'as_json': False
        },
        {
            'country': 'france',
            'columns': None,
            'as_json': False
        },
    ]

    for param in params:
        investpy.get_certificates_dict(country=param['country'],
                                       columns=param['columns'],
                                       as_json=param['as_json'])

    investpy.get_certificate_countries()

    params = [
        {
            'as_json': True,
            'order': 'ascending',
        },
        {
            'as_json': False,
            'order': 'ascending',
        },
        {
            'as_json': True,
            'order': 'descending',
        },
        {
            'as_json': False,
            'order': 'descending',
        },
    ]

    for param in params:
        investpy.get_certificate_recent_data(certificate='BNP Gold 31Dec99',
                                             country='france',
                                             as_json=param['as_json'],
                                             order=param['order'],
                                             interval='Daily')

        investpy.get_certificate_historical_data(certificate='BNP Gold 31Dec99',
                                                 country='france',
                                                 from_date='01/01/1990',
                                                 to_date='01/01/2019',
                                                 as_json=param['as_json'],
                                                 order=param['order'],
                                                 interval='Daily')

    params = [
        {
            'certificate': 'BNP Gold 31Dec99',
            'country': 'france',
            'as_json': False
        },
        {
            'certificate': 'BNP Gold 31Dec99',
            'country': 'france',
            'as_json': True
        }
    ]

    for param in params:
        investpy.get_certificate_information(certificate=param['certificate'],
                                             country=param['country'],
                                             as_json=param['as_json'])
    
    params = [
        {
            'country': 'france',
            'as_json': True,
            'n_results': 10
        },
        {
            'country': 'france',
            'as_json': False,
            'n_results': 10
        }
    ]

    for param in params:
        investpy.get_certificates_overview(country=param['country'],
                                           as_json=param['as_json'],
                                           n_results=param['n_results'])

    investpy.search_certificates(by='name', value='BNP')