### Install stocksent using pip Source: https://github.com/aryagm/stocksent/blob/master/docs/source/examples/getting_started.md Installs the stocksent library using the pip package manager. This is the first step to using the library's functionalities. ```bash pip install stocksent ``` -------------------------------- ### Get DataFrame of Headlines and Sentiment Source: https://github.com/aryagm/stocksent/blob/master/docs/source/examples/getting_started.md Retrieves a pandas DataFrame containing headlines, source, date, time, and sentiment scores for specified stocks over a given period. This function is useful for detailed analysis of news sentiment. ```python from stocksent import Sentiment stocks = Sentiment(['AAPL','TSLA','AMZN']) sentiment_score = stocks.get_dataframe(days=6) # Get the headlines for the past 6 days. print(sentiment_score) # Returns a DataFrame with headlines, source and sentiment scores. ``` -------------------------------- ### Get Detailed Sentiment DataFrame with Stocksent Source: https://context7.com/aryagm/stocksent/llms.txt Returns a pandas DataFrame containing detailed sentiment data for each news headline, including ticker, date, time, source, headline text, and individual sentiment scores. The 'days' parameter can filter results by date. ```python from stocksent import Sentiment # Get full DataFrame of headlines and sentiment scores stocks = Sentiment(['AAPL', 'TSLA', 'AMZN']) df = stocks.get_dataframe(days=6) print(df.columns.tolist()) print(df.head()) # Filter and analyze specific data positive_headlines = df[df['Overall'] > 0.5] print(f"Found {len(positive_headlines)} highly positive headlines") # Group by ticker and get average sentiment avg_by_ticker = df.groupby('ticker')['Overall'].mean() print(avg_by_ticker) ``` -------------------------------- ### Get Average Sentiment Score with Stocksent Source: https://context7.com/aryagm/stocksent/llms.txt Retrieves the average compound sentiment score for a given stock ticker or multiple tickers. The score ranges from -1 (most negative) to +1 (most positive). An optional 'days' parameter can filter analysis to recent news. ```python from stocksent import Sentiment # Get overall sentiment for Apple stock (all available headlines) stock = Sentiment('AAPL') sentiment_score = stock.get_sentiment() print(f"AAPL Sentiment: {sentiment_score}") # Get sentiment for multiple stocks over the past 4 days stocks = Sentiment(['AAPL', 'TSLA', 'GOOG']) recent_sentiment = stocks.get_sentiment(days=4) print(f"Combined 4-day Sentiment: {recent_sentiment}") # Get sentiment for a single stock over the past week weekly_stock = Sentiment('MSFT') weekly_sentiment = weekly_stock.get_sentiment(days=7) print(f"MSFT 7-day Sentiment: {weekly_sentiment}") ``` -------------------------------- ### Get Sentiment of Single Stock Source: https://github.com/aryagm/stocksent/blob/master/docs/source/examples/getting_started.md Retrieves the sentiment score for a single stock ticker. It initializes the Sentiment object with the stock ticker and calls the get_sentiment method. The output is a float representing the sentiment score. ```python from stocksent import Sentiment stock = Sentiment('AAPL') sentiment_score = stock.get_sentiment() print(sentiment_score) # Returns a float with the sentiment score. ``` -------------------------------- ### Get Sentiment of Multiple Stocks Source: https://github.com/aryagm/stocksent/blob/master/docs/source/examples/getting_started.md Calculates the sentiment score for a list of stock tickers over a specified number of past days. It takes a list of tickers and an optional 'days' parameter. The result is a single float representing the aggregated sentiment. ```python from stocksent import Sentiment stocks = Sentiment(['AAPL','TSLA','GOOG']) sentiment_score = stocks.get_sentiment(days=4) # Get the sentiment for the past 4 days. print(sentiment_score) # Returns a float with the sentiment score. ``` -------------------------------- ### Get Sentiment of Multiple Stocks Source: https://github.com/aryagm/stocksent/blob/master/README.md Fetches the sentiment score for multiple stock tickers simultaneously (e.g., ['AAPL', 'TSLA', 'GOOG']). The `get_sentiment()` method can optionally take a `days` parameter to specify the lookback period for news analysis. ```python from stocksent import Sentiment stocks = Sentiment(['AAPL','TSLA','GOOG']) sentiment_score = stocks.get_sentiment(days=4) # Get the sentiment for the past 4 days. print(sentiment_score) ``` -------------------------------- ### Generate Word Cloud of Headlines Source: https://github.com/aryagm/stocksent/blob/master/docs/source/examples/getting_started.md Creates a word cloud visualization from the headlines of specified stocks over a given number of days. This helps in quickly identifying frequently mentioned terms in the news. ```python from stocksent import Sentiment stocks = Sentiment(['AAPL','AMZN','GOOG','TSLA']) stocks.word_cloud(days=5) #Create a word cloud from news from the past 5 days. ``` -------------------------------- ### Stocksent Exception Handling Source: https://context7.com/aryagm/stocksent/llms.txt Demonstrates how to handle specific exceptions raised by the Stocksent library for invalid inputs, such as missing tickers, invalid ticker symbols, or incorrect data types. Includes a utility function for safe sentiment retrieval. ```python from stocksent import Sentiment from stocksent.get_sentiment_data import ValueNotFound, TickerNotFound, InvalidDataType # Handle invalid ticker symbol try: stock = Sentiment('INVALID_TICKER') sentiment = stock.get_sentiment() except TickerNotFound as e: print(f"Error: {e.message}") # Handle empty input try: stock = Sentiment('') sentiment = stock.get_sentiment() except ValueNotFound as e: print(f"Error: {e.message}") # Handle invalid data type try: stock = Sentiment(12345) # Integer instead of string/list sentiment = stock.get_sentiment() except InvalidDataType as e: print(f"Error: {e.message}") # Safe sentiment retrieval with error handling def get_safe_sentiment(ticker, days=None): try: stock = Sentiment(ticker) return stock.get_sentiment(days=days) except (ValueNotFound, TickerNotFound, InvalidDataType) as e: print(f"Could not get sentiment for {ticker}: {e.message}") return None result = get_safe_sentiment('AAPL', days=7) print(f"Sentiment: {result}") ``` -------------------------------- ### Generate Word Cloud from News Headlines Source: https://context7.com/aryagm/stocksent/llms.txt Generates a word cloud visualization from news headlines for the specified stock ticker(s). This helps in quickly identifying dominant topics and themes in market news. The word cloud can be customized in size and optionally saved to a file. ```python from stocksent import Sentiment # Generate word cloud for multiple stocks over 5 days stocks = Sentiment(['AAPL', 'AMZN', 'GOOG', 'TSLA']) stocks.word_cloud(days=5) # Create a larger word cloud and save it for multiple stocks over 7 days stocks = Sentiment(['MSFT', 'META', 'NVDA']) stocks.word_cloud(days=7, save_figure=True, figsize=(20, 10)) # Generate word cloud for a single stock with default settings single_stock = Sentiment('AAPL') single_stock.word_cloud() ``` -------------------------------- ### Initialize Sentiment Class for Stocksent Source: https://context7.com/aryagm/stocksent/llms.txt Demonstrates how to initialize the main Sentiment class in Stocksent. It can be initialized with a single stock ticker symbol as a string or multiple ticker symbols as a list. Ensure all ticker symbols are valid for NASDAQ, AMEX, or NYSE. ```python from stocksent import Sentiment # Initialize with a single ticker single_stock = Sentiment('AAPL') # Initialize with multiple tickers multiple_stocks = Sentiment(['AAPL', 'TSLA', 'GOOG', 'AMZN']) ``` -------------------------------- ### Plot Sentiment Trends with Stocksent Source: https://context7.com/aryagm/stocksent/llms.txt Generates a bar chart visualization of average daily sentiment scores for specified stock tickers over time. This function helps in comparing sentiment trends across multiple stocks and can save the plot as a PNG file. ```python from stocksent import Sentiment # Create a sentiment plot for multiple stocks stocks = Sentiment(['AAPL', 'TSLA', 'GOOG']) stocks.plot() ``` -------------------------------- ### Sentiment Analysis Methods Source: https://github.com/aryagm/stocksent/blob/master/docs/source/api/sentiment.md Methods for retrieving sentiment data and generating visualizations for stock tickers. ```APIDOC ## GET /sentiment/dataframe ### Description Returns a dataframe containing date, time, headlines, source, and sentiment score for the specified ticker(s). ### Method GET ### Parameters #### Query Parameters - **days** (int) - Optional - Number of days into the past to retrieve data. ### Response #### Success Response (200) - **data** (DataFrame) - A pandas DataFrame containing sentiment analysis results. --- ## GET /sentiment/score ### Description Returns the aggregated sentiment score for the given ticker(s). ### Method GET ### Parameters #### Query Parameters - **days** (int) - Optional - Number of days into the past to analyze. ### Response #### Success Response (200) - **sentiment** (float/dict) - The calculated sentiment score. --- ## GET /sentiment/plot ### Description Generates a visualization of sentiment trends over time. ### Method GET ### Parameters #### Query Parameters - **days** (int) - Optional - Number of days to include in the plot. - **save_figure** (bool) - Optional - Whether to save the output as a PNG file. --- ## GET /sentiment/wordcloud ### Description Generates a word cloud visualization based on news headlines for the ticker(s). ### Method GET ### Parameters #### Query Parameters - **days** (int) - Optional - Number of days to include. - **save_figure** (bool) - Optional - Whether to save the output as a PNG. - **figsize** (tuple) - Optional - Dimensions for the plot. ``` -------------------------------- ### Plot Sentiment Scores Source: https://github.com/aryagm/stocksent/blob/master/docs/source/examples/getting_started.md Generates and displays a plot of sentiment scores for multiple stocks over time. The plot can be saved to a file if the 'save_figure' argument is set to True. ```python from stocksent import Sentiment stocks = Sentiment(['AAPL','TSLA','GOOG']) stocks.plot(save_figure=True) ``` -------------------------------- ### Plot Sentiment with Custom Date Range and Save Source: https://context7.com/aryagm/stocksent/llms.txt Plots the sentiment for specified stocks over a custom date range and optionally saves the plot to a file. This function is useful for visualizing sentiment trends for single or multiple stocks. ```python from stocksent import Sentiment # Plot sentiment for multiple stocks with a 7-day range and save stocks = Sentiment(['AAPL', 'MSFT', 'AMZN']) stocks.plot(days=7, save_figure=True) # Plot sentiment for a single stock with a 14-day range single_stock = Sentiment('NVDA') single_stock.plot(days=14) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.