### Install pytickersymbols Library Source: https://github.com/portfolioplus/pytickersymbols/blob/master/README.md This command installs the pytickersymbols library using pip, making it available for use in Python projects. Ensure pip3 is available on your system. ```shell pip3 install pytickersymbols ``` -------------------------------- ### Get All Countries Source: https://context7.com/portfolioplus/pytickersymbols/llms.txt Retrieve a list of all countries represented in the stock database. This allows filtering stocks by their country of origin. ```APIDOC ## Get All Countries ### Description Retrieve a list of all countries represented in the stock database. This allows filtering stocks by their country of origin. ### Method GET ### Endpoint N/A (Method call on PyTickerSymbols instance) ### Parameters None ### Request Example ```python from pytickersymbols import PyTickerSymbols stock_data = PyTickerSymbols() countries = stock_data.get_all_countries() print("Countries with stocks:", countries) # Output: ['Germany', 'United States', 'United Kingdom', 'France', # 'Netherlands', 'Belgium', 'Spain', 'Finland', 'Sweden', # 'Switzerland', 'Russia', 'Ireland', 'Israel', ...] ``` ### Response #### Success Response (200) - **countries** (list of strings) - A list containing the names of all countries with stocks in the database. ``` -------------------------------- ### Get All Industries Source: https://context7.com/portfolioplus/pytickersymbols/llms.txt Retrieve a list of all industry classifications used in the stock database. This enables filtering stocks by their industry. ```APIDOC ## Get All Industries ### Description Retrieve a list of all industry classifications used in the stock database. This enables filtering stocks by their industry. ### Method GET ### Endpoint N/A (Method call on PyTickerSymbols instance) ### Parameters None ### Request Example ```python from pytickersymbols import PyTickerSymbols stock_data = PyTickerSymbols() industries = stock_data.get_all_industries() print(f"Total industries: {len(industries)}") # Output: Total industries: 50+ print("Sample industries:", industries[:10]) # Output: ['Computer Hardware', 'Gold', 'Banking Services', 'Footwear', # 'Basic Materials', 'Pharmaceuticals', 'Oil & Gas', ...] ``` ### Response #### Success Response (200) - **industries** (list of strings) - A list containing the names of all industry classifications. ``` -------------------------------- ### Get Stocks by Index Source: https://context7.com/portfolioplus/pytickersymbols/llms.txt Retrieve all stocks belonging to a specific index, along with their complete metadata. Stocks can be filtered by index name. ```APIDOC ## Get Stocks by Index ### Description Retrieve all stocks belonging to a specific index, along with their complete metadata. Stocks can be filtered by index name. ### Method GET ### Endpoint N/A (Method call on PyTickerSymbols instance) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```python from pytickersymbols import PyTickerSymbols stock_data = PyTickerSymbols() # Get all DAX stocks dax_stocks = list(stock_data.get_stocks_by_index('DAX')) print(f"DAX has {len(dax_stocks)} stocks") # Output: DAX has 40 stocks # Access detailed stock information for stock in dax_stocks[:2]: print(f"Name: {stock['name']}") print(f"Country: {stock['country']}") print(f"Industries: {stock['industries']}") print(f"Symbols: {stock['symbols']}") print(f"Metadata: {stock.get('metadata', {})}") print("---") # Output example: # Name: adidas AG # Country: Germany # Industries: ['Footwear'] # Symbols: [{'yahoo': 'ADS.F', 'google': 'FRA:ADS'}] # Metadata: {'founded': 1949, 'employees': 57016} # Get stocks from other indices nasdaq_stocks = list(stock_data.get_stocks_by_index('NASDAQ 100')) sp500_stocks = list(stock_data.get_stocks_by_index('S&P 500')) ftse_stocks = list(stock_data.get_stocks_by_index('FTSE 100')) ``` ### Response #### Success Response (200) - **stocks** (list of dictionaries) - A list where each dictionary represents a stock and contains fields like 'name', 'country', 'industries', 'symbols', and 'metadata'. ``` -------------------------------- ### Get All Countries, Indices, and Industries with pytickersymbols Source: https://github.com/portfolioplus/pytickersymbols/blob/master/README.md Demonstrates how to initialize the PyTickerSymbols class and retrieve lists of all available countries, stock indices, and industries supported by the library. This is a foundational step for further data retrieval. ```python from pytickersymbols import PyTickerSymbols stock_data = PyTickerSymbols() countries = stock_data.get_all_countries() indices = stock_data.get_all_indices() industries = stock_data.get_all_industries() ``` -------------------------------- ### Get All Indices Source: https://context7.com/portfolioplus/pytickersymbols/llms.txt Retrieve a list of all available stock market indices supported by the library. This includes major indices from Europe, North America, and Russia. ```APIDOC ## Get All Indices ### Description Retrieve a list of all available stock market indices supported by the library. This includes major indices from Europe, North America, and Russia. ### Method GET ### Endpoint N/A (Method call on PyTickerSymbols instance) ### Parameters None ### Request Example ```python from pytickersymbols import PyTickerSymbols stock_data = PyTickerSymbols() indices = stock_data.get_all_indices() print(f"Total indices: {len(indices)}") # Output: Total indices: 17 print("Available indices:", indices) # Output: ['DAX', 'SDAX', 'MDAX', 'TECDAX', 'DOW JONES', 'NASDAQ 100', # 'S&P 100', 'S&P 500', 'FTSE 100', 'CAC 40', 'AEX', 'BEL 20', # 'IBEX 35', 'OMX Helsinki 25', 'OMX Stockholm 30', 'Switzerland 20', 'MOEX'] ``` ### Response #### Success Response (200) - **indices** (list of strings) - A list containing the names of all available stock market indices. ``` -------------------------------- ### Get All Stocks and Analyze Metadata Source: https://context7.com/portfolioplus/pytickersymbols/llms.txt This snippet shows how to retrieve the entire database of stocks, including their full metadata. It demonstrates iterating through stocks to gather information on countries, industries, and indices, and identifies stocks with multiple trading symbols. It also prints detailed information for a sample stock. ```python from pytickersymbols import PyTickerSymbols stock_data = PyTickerSymbols() # Get all stocks all_stocks = stock_data.get_all_stocks() print(f"Total stocks in database: {len(all_stocks)}") # Output: Total stocks in database: 900+ # Analyze stock data countries = set() industries = set() indices = set() for stock in all_stocks[:100]: # Sample first 100 countries.add(stock['country']) industries.update(stock['industries']) indices.update(stock['indices']) print(f"Countries represented: {len(countries)}") print(f"Industries covered: {len(industries)}") print(f"Indices included: {len(indices)}") # Find stocks with multiple symbols multi_symbol_stocks = [ stock for stock in all_stocks if len(stock['symbols']) > 1 ] print(f"Stocks with multiple symbols: {len(multi_symbol_stocks)}") # Display a sample stock with full details sample = all_stocks[0] print(f"\nSample stock: {sample['name']}") print(f" Symbol: {sample['symbol']}") print(f" Country: {sample['country']}") print(f" Indices: {', '.join(sample['indices'])}") print(f" Industries: {', '.join(sample['industries'])}") print(f" Ticker symbols: {sample['symbols']}") if 'metadata' in sample: print(f" Founded: {sample['metadata'].get('founded', 'N/A')}") print(f" Employees: {sample['metadata'].get('employees', 'N/A')}") ``` -------------------------------- ### Get Stock by Google Symbol using Python Source: https://context7.com/portfolioplus/pytickersymbols/llms.txt Retrieves complete stock information using a Google Finance ticker symbol. Requires the pytickersymbols library. Takes a Google symbol string as input and returns a dictionary containing stock details or None if not found. ```python from pytickersymbols import PyTickerSymbols stock_data = PyTickerSymbols() # Get stock by Google symbol adidas = stock_data.get_stock_by_google_symbol('FRA:ADS') if adidas: print(f"Name: {adidas['name']}") print(f"Country: {adidas['country']}") print(f"All symbols: {adidas['symbols']}") # Output: # Name: adidas AG # Country: Germany # All symbols: [{'yahoo': 'ADS.F', 'google': 'FRA:ADS'}] # Check if symbol exists symbol = 'NASDAQ:GOOGL' stock = stock_data.get_stock_by_google_symbol(symbol) if stock: print(f"{symbol} belongs to {stock['name']}") else: print(f"{symbol} not found in database") # Output: NASDAQ:GOOGL belongs to Alphabet Inc. ``` -------------------------------- ### Get All Countries with Represented Stocks Source: https://context7.com/portfolioplus/pytickersymbols/llms.txt Retrieves a list of all countries that have stocks listed in the library's database. This can be used to filter or categorize stock data based on the country of origin. ```python from pytickersymbols import PyTickerSymbols stock_data = PyTickerSymbols() countries = stock_data.get_all_countries() print("Countries with stocks:", countries) # Output: ['Germany', 'United States', 'United Kingdom', 'France', # 'Netherlands', 'Belgium', 'Spain', 'Finland', 'Sweden', # 'Switzerland', 'Russia', 'Ireland', 'Israel', ...] ``` -------------------------------- ### Get Stocks by Country Source: https://context7.com/portfolioplus/pytickersymbols/llms.txt Filter stocks by their country of origin. This function returns a list of stocks associated with a specified country. ```APIDOC ## Get Stocks by Country ### Description Filter stocks by their country of origin. This function returns a list of stocks associated with a specified country. ### Method GET ### Endpoint N/A (Method call on PyTickerSymbols instance) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```python from pytickersymbols import PyTickerSymbols stock_data = PyTickerSymbols() # Get all German stocks german_stocks = list(stock_data.get_stocks_by_country('Germany')) print(f"German stocks: {len(german_stocks)}") # Get US stocks us_stocks = list(stock_data.get_stocks_by_country('United States')) print(f"US stocks: {len(us_stocks)}") # Access stock details for stock in list(german_stocks)[:3]: print(f"{stock['name']} - {stock['indices']}") # Output example: # adidas AG - ['DAX'] # Allianz SE - ['DAX', 'EURO STOXX 50'] # BASF SE - ['DAX'] ``` ### Response #### Success Response (200) - **stocks** (list of dictionaries) - A list where each dictionary represents a stock and contains fields like 'name', 'country', 'industries', and 'symbols'. ``` -------------------------------- ### Get Stocks by Industry Source: https://context7.com/portfolioplus/pytickersymbols/llms.txt Filter stocks by industry classification. This function returns a list of stocks that belong to a specified industry. ```APIDOC ## Get Stocks by Industry ### Description Filter stocks by industry classification. This function returns a list of stocks that belong to a specified industry. ### Method GET ### Endpoint N/A (Method call on PyTickerSymbols instance) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```python from pytickersymbols import PyTickerSymbols stock_data = PyTickerSymbols() # Get all banking stocks banking_stocks = list(stock_data.get_stocks_by_industry('Banking Services')) print(f"Banking stocks: {len(banking_stocks)}") ``` ### Response #### Success Response (200) - **stocks** (list of dictionaries) - A list where each dictionary represents a stock and contains fields like 'name', 'country', 'industries', and 'symbols'. ``` -------------------------------- ### Get Stocks by Specific Stock Market Index Source: https://context7.com/portfolioplus/pytickersymbols/llms.txt Retrieves a list of all stocks belonging to a specified stock market index. Each returned stock object contains comprehensive metadata including name, country, industries, ticker symbols (Yahoo & Google), and additional company details. ```python from pytickersymbols import PyTickerSymbols stock_data = PyTickerSymbols() # Get all DAX stocks dax_stocks = list(stock_data.get_stocks_by_index('DAX')) print(f"DAX has {len(dax_stocks)} stocks") # Output: DAX has 40 stocks # Access detailed stock information for stock in dax_stocks[:2]: print(f"Name: {stock['name']}") print(f"Country: {stock['country']}") print(f"Industries: {stock['industries']}") print(f"Symbols: {stock['symbols']}") print(f"Metadata: {stock.get('metadata', {})}") print("---") # Output example: # Name: adidas AG # Country: Germany # Industries: ['Footwear'] # Symbols: [{'yahoo': 'ADS.F', 'google': 'FRA:ADS'}] # Metadata: {'founded': 1949, 'employees': 57016} # Get stocks from other indices nasdaq_stocks = list(stock_data.get_stocks_by_index('NASDAQ 100')) sp500_stocks = list(stock_data.get_stocks_by_index('S&P 500')) ftse_stocks = list(stock_data.get_stocks_by_index('FTSE 100')) ``` -------------------------------- ### Get Stocks Filtered by Country of Origin Source: https://context7.com/portfolioplus/pytickersymbols/llms.txt Filters and retrieves a list of stocks based on their country of origin. This allows users to focus on companies from specific geographical regions and access their associated metadata. ```python from pytickersymbols import PyTickerSymbols stock_data = PyTickerSymbols() # Get all German stocks german_stocks = list(stock_data.get_stocks_by_country('Germany')) print(f"German stocks: {len(german_stocks)}") # Get US stocks us_stocks = list(stock_data.get_stocks_by_country('United States')) print(f"US stocks: {len(us_stocks)}") # Access stock details for stock in list(german_stocks)[:3]: print(f"{stock['name']} - {stock['indices']}") # Output example: # adidas AG - ['DAX'] # Allianz SE - ['DAX', 'EURO STOXX 50'] # BASF SE - ['DAX'] ``` -------------------------------- ### Get All Available Industry Classifications Source: https://context7.com/portfolioplus/pytickersymbols/llms.txt Retrieves a list of all industry classifications available in the stock database. This function is useful for analyzing the industry distribution of companies or for filtering stocks by sector. ```python from pytickersymbols import PyTickerSymbols stock_data = PyTickerSymbols() industries = stock_data.get_all_industries() print(f"Total industries: {len(industries)}") # Output: Total industries: 50+ print("Sample industries:", industries[:10]) # Output: ['Computer Hardware', 'Gold', 'Banking Services', 'Footwear', # 'Basic Materials', 'Pharmaceuticals', 'Oil & Gas', ...] ``` -------------------------------- ### Get Google Ticker Symbols by Index (Python) Source: https://context7.com/portfolioplus/pytickersymbols/llms.txt Retrieves Google Finance ticker symbols for a specified stock index. The output includes exchange prefixes (e.g., 'FRA:') for Google's format. This function is useful for accessing stock data compatible with Google Finance. ```python from pytickersymbols import PyTickerSymbols stock_data = PyTickerSymbols() # Get Google tickers for DAX dax_google_tickers = stock_data.get_google_ticker_symbols_by_index('DAX') # Display Google format tickers (include exchange prefix) for tickers in dax_google_tickers[:5]: print(tickers) # Get Google tickers for NASDAQ nasdaq_google = stock_data.get_google_ticker_symbols_by_index('NASDAQ 100') for tickers in list(nasdaq_google)[:3]: print(tickers) ``` -------------------------------- ### Get All Available Stock Market Indices Source: https://context7.com/portfolioplus/pytickersymbols/llms.txt Retrieves a list of all stock market indices supported by the library. This function is useful for understanding the breadth of coverage and for subsequent filtering of stock data by index. ```python from pytickersymbols import PyTickerSymbols stock_data = PyTickerSymbols() indices = stock_data.get_all_indices() print(f"Total indices: {len(indices)}") # Output: Total indices: 17 print("Available indices:", indices) # Output: ['DAX', 'SDAX', 'MDAX', 'TECDAX', 'DOW JONES', 'NASDAQ 100', # 'S&P 100', 'S&P 500', 'FTSE 100', 'CAC 40', 'AEX', 'BEL 20', # 'IBEX 35', 'OMX Helsinki 25', 'OMX Stockholm 30', 'Switzerland 20', 'MOEX'] ``` -------------------------------- ### Get Stocks Filtered by Industry Classification Source: https://context7.com/portfolioplus/pytickersymbols/llms.txt Filters and retrieves a list of stocks based on their industry classification. This enables users to target companies within specific sectors, such as 'Banking Services', and access their details. ```python from pytickersymbols import PyTickerSymbols stock_data = PyTickerSymbols() # Get all banking stocks banking_stocks = list(stock_data.get_stocks_by_industry('Banking Services')) print(f"Banking stocks: {len(banking_stocks)}") ``` -------------------------------- ### Get Stocks by Industry (Python) Source: https://context7.com/portfolioplus/pytickersymbols/llms.txt Retrieves lists of stocks categorized by industry. This function is useful for filtering stocks from specific sectors like 'Computer Hardware' or 'Pharmaceuticals'. It returns a list of stock data dictionaries. ```python from pytickersymbols import PyTickerSymbols stock_data = PyTickerSymbols() # Get technology stocks tech_stocks = list(stock_data.get_stocks_by_industry('Computer Hardware')) # Get pharmaceutical stocks pharma_stocks = list(stock_data.get_stocks_by_industry('Pharmaceuticals')) # Display stocks with their indices for stock in banking_stocks[:5]: symbols = [s['yahoo'] for s in stock['symbols']] print(f"{stock['name']} ({stock['country']}): {symbols}") ``` -------------------------------- ### Get Stock Name by Ticker Symbol using Python Source: https://context7.com/portfolioplus/pytickersymbols/llms.txt Quickly retrieves only the company name associated with a given ticker symbol. Requires the pytickersymbols library. Supports Yahoo and Google symbols as input, returning the company name as a string or None if not found. Can also perform batch lookups. ```python from pytickersymbols import PyTickerSymbols stock_data = PyTickerSymbols() # Get name by Yahoo symbol name_yahoo = stock_data.get_stock_name_by_yahoo_symbol('ADS.F') print(f"Yahoo ADS.F: {name_yahoo}") # Output: Yahoo ADS.F: adidas AG # Get name by Google symbol name_google = stock_data.get_stock_name_by_google_symbol('FRA:ADS') print(f"Google FRA:ADS: {name_google}") # Output: Google FRA:ADS: adidas AG # Batch lookup yahoo_symbols = ['AAPL', 'GOOGL', 'MSFT', 'TSLA'] for symbol in yahoo_symbols: name = stock_data.get_stock_name_by_yahoo_symbol(symbol) print(f"{symbol}: {name or 'Not found'}") # Output: # AAPL: Apple Inc. # GOOGL: Alphabet Inc. # MSFT: Microsoft Corporation # TSLA: Tesla, Inc. # Handle missing symbols unknown = stock_data.get_stock_name_by_yahoo_symbol('INVALID123') print(f"Unknown symbol: {unknown}") # Output: Unknown symbol: None ``` -------------------------------- ### Get Stock Information by Yahoo Symbol (Python) Source: https://context7.com/portfolioplus/pytickersymbols/llms.txt Retrieves detailed information for a specific stock using its Yahoo Finance ticker symbol. The function returns a dictionary containing the stock's name, country, associated indices and industries, and a list of its symbols (Yahoo and Google). It also includes optional metadata if available. ```python from pytickersymbols import PyTickerSymbols stock_data = PyTickerSymbols() # Get stock by Yahoo symbol adidas = stock_data.get_stock_by_yahoo_symbol('ADS.F') if adidas: print(f"Name: {adidas['name']}") print(f"Symbol: {adidas['symbol']}") print(f"Country: {adidas['country']}") print(f"Indices: {adidas['indices']}") print(f"Industries: {adidas['industries']}") print(f"Symbols: {adidas['symbols']}") print(f"Metadata: {adidas.get('metadata', {})}") # Try multiple symbols symbols_to_check = ['AAPL', 'GOOGL', 'MSFT', 'INVALID'] for symbol in symbols_to_check: stock = stock_data.get_stock_by_yahoo_symbol(symbol) if stock: print(f"{symbol}: {stock['name']}") else: print(f"{symbol}: Not found") ``` -------------------------------- ### Get Index Yahoo Symbol using Python Source: https://context7.com/portfolioplus/pytickersymbols/llms.txt Converts common index names into their corresponding Yahoo Finance index symbols. This function is part of the pytickersymbols library. It takes an index name string (e.g., 'DAX', 'S&P 500') and returns the Yahoo symbol string or None. ```python from pytickersymbols import PyTickerSymbols stock_data = PyTickerSymbols() # Get Yahoo symbols for indices dax_symbol = stock_data.index_to_yahoo_symbol('DAX') print(f"DAX index symbol: {dax_symbol}") # Output: DAX index symbol: ^GDAXI sdax_symbol = stock_data.index_to_yahoo_symbol('SDAX') print(f"SDAX index symbol: {sdax_symbol}") # Output: SDAX index symbol: ^SDAXI # Get symbols for major US indices sp500_symbol = stock_data.index_to_yahoo_symbol('S&P 500') dow_symbol = stock_data.index_to_yahoo_symbol('DOW JONES') nasdaq_symbol = stock_data.index_to_yahoo_symbol('NASDAQ 100') print(f"S&P 500: {sp500_symbol}") print(f"DOW JONES: {dow_symbol}") print(f"NASDAQ 100: {nasdaq_symbol}") # Output: # S&P 500: ^GSPC # DOW JONES: ^DJI # NASDAQ 100: ^NDX # Get European indices swiss_symbol = stock_data.index_to_yahoo_symbol('Switzerland 20') print(f"Switzerland 20: {swiss_symbol}") # Output: Switzerland 20: ^SSMI ``` -------------------------------- ### Get Yahoo Ticker Symbols by Index (Python) Source: https://context7.com/portfolioplus/pytickersymbols/llms.txt Fetches Yahoo Finance ticker symbols for a given stock index. The function returns a list of lists, where each inner list contains tickers for a single stock, allowing for multiple symbols per stock. These can be flattened into a single list for easier processing. ```python from pytickersymbols import PyTickerSymbols import itertools stock_data = PyTickerSymbols() # Get Yahoo tickers for DAX dax_yahoo_tickers = stock_data.get_yahoo_ticker_symbols_by_index('DAX') print(f"DAX Yahoo tickers: {len(dax_yahoo_tickers)} stocks") # Each stock can have multiple symbols (returns list of lists) for tickers in dax_yahoo_tickers[:3]: print(tickers) # Flatten to single list all_dax_yahoo = list(itertools.chain.from_iterable(dax_yahoo_tickers)) print(all_dax_yahoo[:10]) # Get tickers for other indices nasdaq_yahoo = stock_data.get_yahoo_ticker_symbols_by_index('NASDAQ 100') sp500_yahoo = stock_data.get_yahoo_ticker_symbols_by_index('S&P 500') ``` -------------------------------- ### PyTickerSymbols Initialization Source: https://context7.com/portfolioplus/pytickersymbols/llms.txt Demonstrates how to create a singleton instance of the PyTickerSymbols class to access stock data. The library uses a singleton pattern to ensure efficient memory usage. ```APIDOC ## PyTickerSymbols Initialization ### Description Create a singleton instance of PyTickerSymbols to access stock data. The library implements a singleton pattern to ensure efficient memory usage and provides both static and dynamically-generated methods for retrieving ticker symbols. ### Method Instantiate the class. ### Endpoint N/A (Class instantiation) ### Parameters None ### Request Example ```python from pytickersymbols import PyTickerSymbols # Create instance (singleton pattern - always returns same instance) stock_data = PyTickerSymbols() # Verify singleton behavior stock_data2 = PyTickerSymbols() assert stock_data is stock_data2 # True - same instance ``` ### Response #### Success Response (200) An instance of the PyTickerSymbols class is returned. #### Response Example `stock_data` (PyTickerSymbols instance) ``` -------------------------------- ### Load and Query Stock Data from Temporary YAML Source: https://context7.com/portfolioplus/pytickersymbols/llms.txt This snippet demonstrates how to write custom YAML data to a temporary file, load it using PyTickerSymbols, and then query for stocks by index. It shows how to access stock details like name, country, and Yahoo symbol. Includes cleanup of the temporary file. ```python import tempfile import os from pytickersymbols import PyTickerSymbols custom_yaml = "..." # Write to temporary YAML file with tempfile.NamedTemporaryFile(mode='w', suffix='.yaml', delete=False) as f: f.write(custom_yaml) temp_path = f.name # Load YAML data stock_data = PyTickerSymbols() stock_data.load_yaml(temp_path) # Query loaded data indices = stock_data.get_all_indices() print(f"Available indices: {indices}") # Output: Available indices: ['TECH 10'] tech_stocks = list(stock_data.get_stocks_by_index('TECH 10')) for stock in tech_stocks: print(f"Name: {stock['name']}") print(f"Country: {stock['country']}") print(f"Yahoo symbol: {stock['symbols'][0]['yahoo']}") # Output: # Name: TechCorp Ltd # Country: United Kingdom # Yahoo symbol: TECH.L # Clean up os.unlink(temp_path) ``` -------------------------------- ### Initialize PyTickerSymbols Singleton Instance Source: https://context7.com/portfolioplus/pytickersymbols/llms.txt Creates a singleton instance of the PyTickerSymbols class. This ensures that only one instance of the class is ever created, optimizing memory usage. Subsequent calls to the constructor will return the same instance. ```python from pytickersymbols import PyTickerSymbols # Create instance (singleton pattern - always returns same instance) stock_data = PyTickerSymbols() # Verify singleton behavior stock_data2 = PyTickerSymbols() assert stock_data is stock_data2 # True - same instance ``` -------------------------------- ### Use Static Index Constants for Stock Queries Source: https://context7.com/portfolioplus/pytickersymbols/llms.txt This snippet demonstrates using predefined static constants from `pytickersymbols.Statics` to specify stock indices, which helps prevent typos and ensures accuracy when querying specific market indices. It retrieves and prints the number of stocks for several major indices. ```python from pytickersymbols import PyTickerSymbols from pytickersymbols import Statics stock_data = PyTickerSymbols() # Use constants instead of strings dax_stocks = list(stock_data.get_stocks_by_index(Statics.Indices.DE_DAX)) sp500_stocks = list(stock_data.get_stocks_by_index(Statics.Indices.US_SP_500)) nasdaq_stocks = list(stock_data.get_stocks_by_index(Statics.Indices.US_NASDAQ)) ftse_stocks = list(stock_data.get_stocks_by_index(Statics.Indices.GB_FTSE)) print(f"DAX: {len(dax_stocks)} stocks") print(f"S&P 500: {len(sp500_stocks)} stocks") print(f"NASDAQ 100: {len(nasdaq_stocks)} stocks") print(f"FTSE 100: {len(ftse_stocks)} stocks") # Available index constants: # Statics.Indices.DE_DAX - 'DAX' # Statics.Indices.DE_MDAX - 'MDAX' # Statics.Indices.DE_SDAX - 'SDAX' # Statics.Indices.DE_TECDAX - 'TECDAX' # Statics.Indices.US_DOW - 'DOW JONES' # Statics.Indices.US_NASDAQ - 'NASDAQ 100' # Statics.Indices.US_SP_100 - 'S&P 100' # Statics.Indices.US_SP_500 - 'S&P 500' # Statics.Indices.GB_FTSE - 'FTSE 100' # Statics.Indices.FR_CAC_40 - 'CAC 40' # Statics.Indices.NL_AEX - 'AEX' # Statics.Indices.BE_20 - 'BEL 20' # Statics.Indices.ES_IBEX_35 - 'IBEX 35' # Statics.Indices.SE_OMX_30 - 'OMX Stockholm 30' # Statics.Indices.FI_OMX_25 - 'OMX Helsinki 25' # Statics.Indices.CH_20 - 'Switzerland 20' # Statics.Indices.RU_MOEX - 'MOEX' ``` -------------------------------- ### Dynamic Ticker Getter Methods (Python) Source: https://context7.com/portfolioplus/pytickersymbols/llms.txt Utilizes dynamically generated methods to fetch ticker symbols. These methods follow a pattern like `get_{index}_{exchange}_{google|yahoo}_tickers` and support various exchanges such as Frankfurt, London, and NYC. The library provides a way to list all available dynamic methods. ```python from pytickersymbols import PyTickerSymbols stock_data = PyTickerSymbols() # DAX stocks from Frankfurt exchange dax_frankfurt_google = stock_data.get_dax_frankfurt_google_tickers() dax_frankfurt_yahoo = stock_data.get_dax_frankfurt_yahoo_tickers() print(f"DAX Google tickers: {len(dax_frankfurt_google)}") print(f"Sample: {dax_frankfurt_google[:3]}") print(f"DAX Yahoo tickers: {len(dax_frankfurt_yahoo)}") print(f"Sample: {dax_frankfurt_yahoo[:3]}") # S&P 500 from NYC sp500_nyc_yahoo = stock_data.get_sp_500_nyc_yahoo_tickers() sp500_nyc_google = stock_data.get_sp_500_nyc_google_tickers() # NASDAQ 100 from NYC nasdaq_nyc_yahoo = stock_data.get_nasdaq_100_nyc_yahoo_tickers() # FTSE 100 from London ftse_london_yahoo = stock_data.get_ftse_100_london_yahoo_tickers() # List all available dynamic methods all_ticker_methods = [ method for method in dir(stock_data) if method.endswith('_google_tickers') or method.endswith('_yahoo_tickers') ] print(f"Total dynamic methods: {len(all_ticker_methods)}") print(f"Sample methods: {all_ticker_methods[:5]}") ``` -------------------------------- ### Load Custom Stock Data from YAML using Python Source: https://context7.com/portfolioplus/pytickersymbols/llms.txt Enables loading custom or updated stock data from an external YAML file into the PyTickerSymbols library. This requires pytickersymbols, yaml, and tempfile. It accepts a file path to a YAML file containing stock data and updates the library's internal data. ```python from pytickersymbols import PyTickerSymbols import yaml import tempfile # Create custom stock data in YAML format custom_yaml = """ indices: - name: "TECH 10" yahoo: "^TECH10" companies: - id: 1 name: "TechCorp Ltd" symbol: "TECH" country: "United Kingdom" indices: - "TECH 10" industries: - "Software" symbols: - yahoo: "TECH.L" google: "LON:TECH" metadata: founded: 2015 employees: 1200 """ # Write custom YAML data to a temporary file with tempfile.NamedTemporaryFile(mode='w', suffix='.yaml', delete=False) as f: f.write(custom_yaml) temp_yaml_path = f.name # Initialize PyTickerSymbols and load custom data from YAML stock_data_yaml = PyTickerSymbols() stock_data_yaml.load_yaml(temp_yaml_path) # Example: Retrieving data loaded from YAML custom_indices = stock_data_yaml.get_all_indices() print(f"Custom Indices from YAML: {custom_indices}") # Expected Output: Custom Indices from YAML: ['TECH 10'] custom_stocks = list(stock_data_yaml.get_stocks_by_index('TECH 10')) if custom_stocks: print(f"Company Name from YAML: {custom_stocks[0]['name']}") # Expected Output: Company Name from YAML: TechCorp Ltd # Clean up the temporary file import os os.unlink(temp_yaml_path) ``` -------------------------------- ### Load Custom Stock Data from JSON using Python Source: https://context7.com/portfolioplus/pytickersymbols/llms.txt Allows loading custom or updated stock data from an external JSON file into the PyTickerSymbols library. Requires pytickersymbols, json, and tempfile. It takes a file path to a JSON file containing stock data in a specific format and updates the internal data structure. ```python from pytickersymbols import PyTickerSymbols import json import tempfile # Create custom stock data custom_data = { "indices": [ { "name": "CUSTOM INDEX", "yahoo": "^CUSTOM" } ], "companies": [ { "id": 1, "name": "Custom Company Inc.", "symbol": "CUST", "country": "United States", "indices": ["CUSTOM INDEX"], "industries": ["Technology"], "symbols": [ { "yahoo": "CUST", "google": "NASDAQ:CUST" } ], "metadata": { "founded": 2020, "employees": 500 } } ] } # Write to temporary file with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f: json.dump(custom_data, f) temp_path = f.name # Load custom data stock_data = PyTickerSymbols() stock_data.load_json(temp_path) # Query custom data indices = stock_data.get_all_indices() print(f"Indices: {indices}") # Output: Indices: ['CUSTOM INDEX'] stocks = list(stock_data.get_stocks_by_index('CUSTOM INDEX')) print(f"Custom stocks: {stocks[0]['name']}") # Output: Custom stocks: Custom Company Inc. # Clean up import os os.unlink(temp_path) ``` -------------------------------- ### Fetch Specific Index Ticker Symbols (Google/Yahoo) with pytickersymbols Source: https://github.com/portfolioplus/pytickersymbols/blob/master/README.md Illustrates fetching ticker symbols for specific indices from either Google or Yahoo, using methods named according to a convention like `get_{index_name}_{exchange_city}_{yahoo or google}_tickers`. It also shows how to dynamically list all available getter methods. ```python from pytickersymbols import PyTickerSymbols stock_data = PyTickerSymbols() # the naming conversation is get_{index_name}_{exchange_city}_{yahoo or google}_tickers dax_google = stock_data.get_dax_frankfurt_google_tickers() dax_yahoo = stock_data.get_dax_frankfurt_yahoo_tickers() sp100_yahoo = stock_data.get_sp_100_nyc_yahoo_tickers() sp500_google = stock_data.get_sp_500_nyc_google_tickers() dow_yahoo = stock_data.get_dow_jones_nyc_yahoo_tickers() # there are too many combination. Here is a complete list of all getters all_ticker_getter_names = list(filter( lambda x: ( x.endswith('_google_tickers') or x.endswith('_yahoo_tickers') ), dir(stock_data), )) print(all_ticker_getter_names) ``` -------------------------------- ### Retrieve Stocks by Index using pytickersymbols Source: https://github.com/portfolioplus/pytickersymbols/blob/master/README.md Shows how to fetch all ticker symbols associated with a specific stock index, such as 'DAX' or 'FTSE 100'. The result is a dictionary-like object containing stock information. The output is then printed as a list. ```python from pytickersymbols import PyTickerSymbols stock_data = PyTickerSymbols() german_stocks = stock_data.get_stocks_by_index('DAX') uk_stocks = stock_data.get_stocks_by_index('FTSE 100') print(list(uk_stocks)) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.