def get_peers(ticker_symbol, page=None): """ Gets the list of Top Peers for a stock as listed on the "Premium Research: Industry Analysis" section :param ticker_symbol: The ticker symbol of the interested stock (e.g., "AAPL", "GOOG", "MSFT") :param page: html tree structure based on the html markup of the scraped website :return: a list of the Top Peers as listed on a stock's "Premium Research: Industry Analysis" section on it's respective zacks page """ if page is None: page = scrape_page(BASE_URL+ ticker_symbol) peers = page.xpath(PEERS_XPATH) if peers: try: peers.remove(ticker_symbol.upper()) except: pass if peers: return peers else: return None else: return None
def get_bearish_sentiment(ticker_symbol, page=None): """ Gets the bearish sentiment of the target ticker symbol :param ticker_symbol: The ticker symbol of the interested stock (e.g., "AAPL", "GOOG", "MSFT") :param page: html tree structure based on the html markup of the scraped website :return: a string of the percentage of bearish sentiment as listed on a stock's StockTwits page """ if page is None: page = scrape_page(BASE_URL + ticker_symbol) sentiment = page.xpath(BEARISH_SENTIMENT_XPATH) if not sentiment: return None else: return sentiment[0].replace("\n", "") + " Bearish"
def get_industry_rank(ticker_symbol, page=None): """ Gets the Zacks' Industry Rank of the target ticker symbol. :param ticker_symbol: The ticker symbol of the interested stock (e.g., "AAPL", "GOOG", "MSFT") :param page: html tree structure based on the html markup of the scraped website :return: a string of the industry rank """ if page is None: page = scrape_page(BASE_URL + ticker_symbol) industry_rank = page.xpath(INDUSTRY_RANK_XPATH) if not industry_rank: return None else: return industry_rank[0]
def get_rating(ticker_symbol, page=None): """ Gets the Zack's Rank Rating of the target ticker symbol :param ticker_symbol: The ticker symbol of the interested stock (e.g., "AAPL", "GOOG", "MSFT") :param page: html tree structure based on the html markup of the scraped website :return: String of Zack's Rank Rating as listed on a stock's Zacks page """ if page is None: page = scrape_page(BASE_URL + ticker_symbol) rating = page.xpath(RATING_XPATH) if not rating: return None else: return rating[0]
def get_sentiment(ticker_symbol, page=None): """ Gets both the bullish and bearish sentiment of the target ticker symbol :param ticker_symbol: The ticker symbol of the interested stock (e.g., "AAPL", "GOOG", "MSFT") :param page: html tree structure based on the html markup of the scraped website :return: a tuple of strings containing both the bullish and bearish sentiment as listed on a stock's StockTwits page """ if page is None: page = scrape_page(BASE_URL + ticker_symbol) bullish_sentiment = get_bullish_sentiment(ticker_symbol, page) if bullish_sentiment: return bullish_sentiment, get_bearish_sentiment(ticker_symbol, page) else: return None
def get_style_scores(ticker_symbol, page=None): """ Gets the Zacks' Style Scores of the target ticker symbol. There are 4 score categories Value, Growth, Momentum and VGM. Each category is given a grade from A - F. :param ticker_symbol: The ticker symbol of the interested stock (e.g., "AAPL", "GOOG", "MSFT") :param page: html tree structure based on the html markup of the scraped website :return: a dictionary containing all the Style Scores """ if page is None: page = scrape_page(BASE_URL + ticker_symbol) scores = page.xpath(STYLE_SCORES_XPATH) if not scores: return None else: return dict(zip(STYLES, scores))
def get_all_statistics(ticker_symbol, page=None): """ This function will get all the associated financial statistics from the correspoding finviz page given the ticker symbol :param ticker_symbol: The ticker symbol of the interested stock (e.g., "AAPL", "GGOG", "MSFT") :param page: HTML tree structure based on the html markup of the scraped page. If one is not passed in the function will scrape the page :return: a dictionary of all the financial statistics listed on a stock's finviz page, otherwise None """ if page is None: page = scrape_page(BASE_URL + ticker_symbol) table = get_statistics_table(page) if table: return table else: return None
def get_statistic(ticker_symbol, stat_name, page=None): """ This function will get the associated financial statistic from the corresponding finviz page given the statistic's name and the ticker symbol :param ticker_symbol: The ticker symbol of the interested stock (e.g., "AAPL", "GOOG", "MSFT") :param stat_name: The name of the interested financial statistic (e.g., "P/E", "Price", "Volume"). An exhaustive list of available financial statistics can be found on a stock's finviz page :param page: HTML tree structure based on the html markup of the scraped web page. If one is not passed in the function will scrape the page :return: the value of the interested financial statistic if it exists, otherwise None """ if page is None: page = scrape_page(BASE_URL + ticker_symbol) table = get_statistics_table(page) if stat_name in table.keys() and table[stat_name]: return table[stat_name] else: return None