diff --git a/gamestonk_terminal/technical_analysis/ta_controller.py b/gamestonk_terminal/technical_analysis/ta_controller.py index 5c113c1cd63..f53c8ef4e37 100644 --- a/gamestonk_terminal/technical_analysis/ta_controller.py +++ b/gamestonk_terminal/technical_analysis/ta_controller.py @@ -15,6 +15,7 @@ from gamestonk_terminal.technical_analysis import trend_indicators as ta_trend from gamestonk_terminal.technical_analysis import volatility as ta_volatility from gamestonk_terminal.technical_analysis import volume as ta_volume from gamestonk_terminal.technical_analysis import finbrain_view +from gamestonk_terminal.technical_analysis import tradingview_view from prompt_toolkit.completion import NestedCompleter @@ -27,6 +28,7 @@ class TechnicalAnalysisController: "q", "quit", "summary", + "recom", "ema", "sma", "vwap", @@ -78,6 +80,9 @@ class TechnicalAnalysisController: print(" quit quit to abandon program") print("") print(" summary technical summary report [FinBrain API]") + print( + " recom recommendation based on Technical Indicators [Tradingview API]" + ) print("overlap:") print(" ema exponential moving average") print(" sma simple moving average") @@ -130,6 +135,10 @@ class TechnicalAnalysisController: """Process summary command""" finbrain_view.technical_summary_report(other_args, self.ticker) + def call_recom(self, other_args: List[str]): + """Process recom command""" + tradingview_view.print_recommendation(other_args, self.ticker) + # OVERLAP def call_ema(self, other_args: List[str]): """Process ema command""" diff --git a/gamestonk_terminal/technical_analysis/tradingview_view.py b/gamestonk_terminal/technical_analysis/tradingview_view.py new file mode 100644 index 00000000000..90730035af0 --- /dev/null +++ b/gamestonk_terminal/technical_analysis/tradingview_view.py @@ -0,0 +1,153 @@ +import argparse +from typing import List +import requests +import pandas as pd +from gamestonk_terminal import config_terminal as cfg +from tradingview_ta import TA_Handler, Interval, Exchange +from gamestonk_terminal.helper_funcs import ( + parse_known_args_and_warn, +) + +INTERVALS = { + "1m": "1 min", + "5m": "5 min", + "15m": "15 min", + "1h": "1 hour", + "4h": "4 hours", + "1d": "1 day", + "1W": "1 week", + "1M": "1 month", +} + + +def get_tradingview_recommendation( + ticker: str, screener: str, exchange: str, interval: str +) -> str: + """Get tradingview recommendation based on technical indicators + + Parameters + ---------- + ticker : str + Ticker to get the recommendation from tradingview based on technical indicators + screener : str + Screener based on tradingview docs https://python-tradingview-ta.readthedocs.io/en/latest/usage.html + exchange: str + Exchange based on tradingview docs https://python-tradingview-ta.readthedocs.io/en/latest/usage.html + interval: str + Interval time to check technical indicators and correspondent recommendation + + Returns + ------- + str + tradingview recommendation based on technical indicators + """ + + if not exchange: + s_req = f"https://www.alphavantage.co/query?function=OVERVIEW&symbol={ticker}&apikey={cfg.API_KEY_ALPHAVANTAGE}" + result = requests.get(s_req, stream=True) + exchange = result.json()["Exchange"] + + if not interval: + df_recommendation = pd.DataFrame() + index_recommendation = list() + for interv in ["1M", "1W", "1d", "4h", "1h", "15m", "5m", "1m"]: + # If the returned data was successful + if result.status_code == 200: + stock_recommendation = TA_Handler( + symbol=ticker, + screener=screener, + exchange=exchange, + interval=interv, + ) + d_recommendation = stock_recommendation.get_analysis().summary + df_recommendation = df_recommendation.append( + d_recommendation, ignore_index=True + ) + index_recommendation.append(INTERVALS[interv]) + df_recommendation.index = index_recommendation + df_recommendation[["BUY", "NEUTRAL", "SELL"]] = df_recommendation[ + ["BUY", "NEUTRAL", "SELL"] + ].astype(int) + df_recommendation["-----"] = "-----" + df_recommendation.index.name = "Interval" + recommendation = df_recommendation[ + ["RECOMMENDATION", "-----", "BUY", "NEUTRAL", "SELL"] + ].to_string() + + else: + stock_recommendation = TA_Handler( + symbol=ticker, screener=screener, exchange=exchange, interval=interval + ) + d_recommendation = stock_recommendation.get_analysis().summary + recommendation = f"Interval: {INTERVALS[interval]}\n" + recommendation += f"Recommendation: {d_recommendation['RECOMMENDATION']}\n" + recommendation += f"{int(d_recommendation['BUY'])} BUY, {int(d_recommendation['NEUTRAL'])} NEUTRAL" + recommendation += f", {int(d_recommendation['SELL'])} SELL" + + return recommendation + + +def print_recommendation(other_args: List[str], ticker: str): + """Print tradingview recommendation based on technical indicators + + Parameters + ---------- + other_args : List[str] + Command line arguments to be processed with argparse + ticker : str + Ticker to get tradingview recommendation based on technical indicators + """ + parser = argparse.ArgumentParser( + add_help=False, + prog="recom", + description=""" + Print tradingview recommendation based on technical indicators. + [Source: https://pypi.org/project/tradingview-ta/] + """, + ) + parser.add_argument( + "-s", + "--screener", + action="store", + dest="screener", + type=str, + default="america", + choices=["crypto", "forex", "cfd"], + help="Screener. See https://python-tradingview-ta.readthedocs.io/en/latest/usage.html", + ) + parser.add_argument( + "-e", + "--exchange", + action="store", + dest="exchange", + type=str, + default="", + help="""Set exchange. For Forex use: 'FX_IDC', and for crypto use 'TVC'. + See https://python-tradingview-ta.readthedocs.io/en/latest/usage.html. + By default Alpha Vantage tries to get this data from the ticker. """, + ) + parser.add_argument( + "-i", + "--interval", + action="store", + dest="interval", + type=str, + default="", + choices=["1M", "1W", "1d", "4h", "1h", "15m", "5m", "1m"], + help="""Interval, that corresponds to the recommendation given by tradingview based on technical indicators. + See https://python-tradingview-ta.readthedocs.io/en/latest/usage.html""", + ) + + try: + ns_parser = parse_known_args_and_warn(parser, other_args) + if not ns_parser: + return + + recom = get_tradingview_recommendation( + ticker, ns_parser.screener, ns_parser.exchange, ns_parser.interval + ) + print(recom) + print("") + + except Exception as e: + print(e, "\n")