diff --git a/gamestonk_terminal/behavioural_analysis/ba_controller.py b/gamestonk_terminal/behavioural_analysis/ba_controller.py index e8ae8f60aee..725bbdbfa55 100644 --- a/gamestonk_terminal/behavioural_analysis/ba_controller.py +++ b/gamestonk_terminal/behavioural_analysis/ba_controller.py @@ -14,6 +14,7 @@ from gamestonk_terminal.behavioural_analysis import ( reddit_view, stocktwits_view, finbrain_view, + finnhub_view, ) @@ -41,6 +42,7 @@ class BehaviouralAnalysisController: "queries", "rise", "finbrain", + "stats", ] def __init__(self, ticker: str, start: datetime): @@ -63,6 +65,7 @@ class BehaviouralAnalysisController: print(" quit quit to abandon program") print("") print(" finbrain sentiment from 15+ major news headlines") + print(" stats sentiment stats including comparison with sector") print("") print("Reddit:") print( @@ -218,6 +221,10 @@ class BehaviouralAnalysisController: """Process finbrain command""" finbrain_view.sentiment_analysis(other_args, self.ticker) + def call_stats(self, other_args: List[str]): + """Process stats command""" + finnhub_view.sentiment_stats(other_args, self.ticker) + def menu(ticker: str, start: datetime): """Behavioural Analysis Menu""" diff --git a/gamestonk_terminal/behavioural_analysis/finnhub_view.py b/gamestonk_terminal/behavioural_analysis/finnhub_view.py new file mode 100644 index 00000000000..d226f9e7445 --- /dev/null +++ b/gamestonk_terminal/behavioural_analysis/finnhub_view.py @@ -0,0 +1,80 @@ +import argparse +from typing import List, Dict +import requests +import pandas as pd +from gamestonk_terminal import config_terminal as cfg +from gamestonk_terminal.helper_funcs import ( + parse_known_args_and_warn, +) + + +def get_sentiment_stats(ticker: str) -> Dict: + """Get sentiment stats + + Parameters + ---------- + ticker : str + Ticker to get sentiment stats + + Returns + ------- + Dict + Get sentiment stats + """ + response = requests.get( + f"https://finnhub.io/api/v1/news-sentiment?symbol={ticker}&token={cfg.API_FINNHUB_KEY}" + ) + if response.status_code == 200: + return response.json() + + return {} + + +def sentiment_stats(other_args: List[str], ticker: str): + """Sentiment stats + + Parameters + ---------- + other_args : List[str] + Command line arguments to be processed with argparse + ticker : str + Ticker to get sentiment stats + """ + parser = argparse.ArgumentParser( + add_help=False, + prog="stats", + description=""" + Sentiment stats. [Source: https://finnhub.io] + """, + ) + + try: + ns_parser = parse_known_args_and_warn(parser, other_args) + if not ns_parser: + return + + d_stats = get_sentiment_stats(ticker) + + if d_stats: + print(f"Buzz: {round(100*d_stats['buzz']['buzz'],2)} %") + print(f"News Score: {round(100*d_stats['companyNewsScore'],2)} %") + print("") + print(f"Articles Last Week: {d_stats['buzz']['articlesInLastWeek']}") + print(f"Articles Weekly Week: {d_stats['buzz']['weeklyAverage']}") + print("") + print(f"Bullish: {round(100*d_stats['sentiment']['bullishPercent'],2)} %") + print(f"Bearish: {round(100*d_stats['sentiment']['bearishPercent'],2)} %") + print("") + print( + f"Sector Average Bullish: {round(100*d_stats['sectorAverageBullishPercent'],2)} %" + ) + print( + f"Sector Average News Score: {round(100*d_stats['sectorAverageNewsScore'],2)} %" + ) + else: + print("No sentiment stats found.") + + print("") + + except Exception as e: + print(e, "\n")