def get_companies_per_country_in_sector(
    sector: str, mktcap: str = "", exclude_exchanges: bool = True
):
    """Get number of companies per country in a specific sector (and specific market cap). [Source: Finance Database]

    Parameters
    ----------
    sector: str
        Select sector to get number of companies by each country
    mktcap: str
        Select market cap of companies to consider from Small, Mid and Large
    exclude_exchanges : bool
        Exclude international exchanges

    Returns
    -------
    dict
        Dictionary of countries and number of companies in a specific sector
    """
    companies_per_country = {}
    for country in tqdm(get_countries(sector=sector)):
        if country:
            try:
                companies = fd.select_equities(
                    sector=sector,
                    country=country,
                    exclude_exchanges=exclude_exchanges,
                )
                if mktcap:
                    companies = fd.search_products(
                        companies, query=mktcap + " Cap", search="market_cap"
                    )

                companies_per_country[country] = len(companies)

            except ValueError as e:
                logger.exception(str(e))

    return companies_per_country
Exemple #2
0
def get_companies_per_sector_in_country(country: str,
                                        mktcap: str = "",
                                        exclude_exchanges: bool = True):
    """Get number of companies per sector in a specific country (and specific market cap). [Source: Finance Database]

    Parameters
    ----------
    country: str
        Select country to get number of companies by each sector
    mktcap: str
        Select market cap of companies to consider from Small, Mid and Large
    exclude_exchanges : bool
        Exclude international exchanges

    Returns
    -------
    dict
        Dictionary of sectors and number of companies in a specific country
    """
    companies_per_sector = {}

    for sector in tqdm(get_sectors(country=country)):
        if sector:
            try:
                companies = fd.select_equities(
                    country=country,
                    sector=sector,
                    exclude_exchanges=exclude_exchanges)
                if mktcap:
                    companies = fd.search_products(companies,
                                                   query=mktcap + " Cap",
                                                   search="market_cap")

                companies_per_sector[sector] = len(companies)
            except ValueError:
                pass

    return companies_per_sector
Exemple #3
0
def show_equities(
    country: str,
    sector: str,
    industry: str,
    name: str,
    description: str,
    marketcap: str,
    amount: int,
    include_exchanges: bool,
    options: str,
):
    """
    Display a selection of Equities based on country, sector, industry, name and/or description filtered
    by market cap. If no arguments are given, return equities categorized as Large Cap.
    [Source: Finance Database]

    Parameters
    ----------
    country: str
        Search by country to find stocks matching the criteria.
    sector : str
        Search by sector to find stocks matching the criteria.
    industry : str
        Search by industry to find stocks matching the criteria.
    name : str
        Search by name to find stocks matching the criteria.
    description : str
        Search by description to find stocks matching the criteria.
    marketcap : str
        Select stocks based on the market cap.
    amount : int
        Number of stocks to display, default is 10.
    include_exchanges: bool
        When you wish to include different exchanges use this boolean.
    options : str
        Show the country, sector or industry options.
    """
    if options is not None:
        for option in fd.show_options("equities", options):
            print(option)
        return

    if country is not None:
        country = " ".join(country).title()
    if sector is not None:
        sector = " ".join(sector).title()
    if industry is not None:
        industry = " ".join(industry).title()

    data = fd.select_equities(
        country=country,
        sector=sector,
        industry=industry,
        exclude_exchanges=include_exchanges,
    )

    if name is not None:
        data = fd.search_products(data, query=" ".join(name), search="long_name")
    if description is not None:
        data = fd.search_products(data, query=" ".join(description), search="summary")
    if marketcap is not None:
        data = fd.search_products(
            data, query=f"{''.join(marketcap)} Cap", search="market_cap"
        )

    tabulate_data = pd.DataFrame(data).T[
        [
            "long_name",
            "sector",
            "industry",
            "country",
            "city",
            "website",
            "market_cap",
        ]
    ]

    if gtff.USE_TABULATE_DF:
        print(
            tabulate(
                tabulate_data.iloc[:amount],
                showindex=True,
                headers=[
                    "Name",
                    "Sector",
                    "Industry",
                    "Country",
                    "City",
                    "Website",
                    "Market Cap",
                ],
                floatfmt=".2f",
                tablefmt="fancy_grid",
            ),
            "\n",
        )
    else:
        print(tabulate_data.iloc[:amount].to_string(), "\n")
Exemple #4
0
def filter_stocks(
    country: str,
    sector: str,
    industry: str,
    marketcap: str = "",
    exclude_exchanges: bool = True,
):
    """Filter stocks based on country, sector, industry, market cap and exclude exchanges. [Source: Finance Database]

    Parameters
    ----------
    country: str
        Search by country to find stocks matching the criteria.
    sector : str
        Search by sector to find stocks matching the criteria.
    industry : str
        Search by industry to find stocks matching the criteria.
    marketcap : str
        Select stocks based on the market cap.
    exclude_exchanges: bool
        When you wish to include different exchanges use this boolean.

    Returns
    -------
    list
        List of filtered stocks
    """
    try:
        if country:
            if sector:
                if industry:
                    data = fd.select_equities(
                        country=country,
                        sector=sector,
                        industry=industry,
                        exclude_exchanges=exclude_exchanges,
                    )
                else:  # no industry
                    data = fd.select_equities(
                        country=country,
                        sector=sector,
                        exclude_exchanges=exclude_exchanges,
                    )
            else:  # no sector
                if industry:
                    data = fd.select_equities(
                        country=country,
                        industry=industry,
                        exclude_exchanges=exclude_exchanges,
                    )
                else:  # no industry
                    data = fd.select_equities(
                        country=country,
                        exclude_exchanges=exclude_exchanges,
                    )

        else:  # no country
            if sector:
                if industry:
                    data = fd.select_equities(
                        sector=sector,
                        industry=industry,
                        exclude_exchanges=exclude_exchanges,
                    )
                else:  # no industry
                    data = fd.select_equities(
                        sector=sector,
                        exclude_exchanges=exclude_exchanges,
                    )
            else:  # no sector
                if industry:
                    data = fd.select_equities(
                        industry=industry,
                        exclude_exchanges=exclude_exchanges,
                    )
                else:  # no industry
                    data = {}

        if marketcap:
            data = fd.search_products(data,
                                      query=marketcap,
                                      search="market_cap")

        return list(data.keys())

    except ValueError:
        return list()