Exemple #1
0
def test_get_similar_companies(compare_list, recorder):
    result_tuple = finviz_compare_model.get_similar_companies(
        ticker="TSLA",
        compare_list=compare_list,
    )

    recorder.capture(result_tuple)
    def call_getfinviz(self, other_args: List[str]):
        """Process getfinviz command"""
        parser = argparse.ArgumentParser(
            add_help=False,
            formatter_class=argparse.ArgumentDefaultsHelpFormatter,
            prog="getfinviz",
            description="""Get similar companies from finviz to compare with.""",
        )
        parser.add_argument(
            "-n",
            "--nocountry",
            action="store_true",
            default=False,
            dest="b_no_country",
            help="Similar stocks from finviz using only Industry and Sector.",
        )
        parser.add_argument(
            "-l",
            "--limit",
            default=10,
            dest="limit",
            type=check_positive,
            help="Limit of stocks to retrieve.",
        )
        if other_args and "-" not in other_args[0][0]:
            other_args.insert(0, "-l")
        ns_parser = parse_known_args_and_warn(parser, other_args)
        if ns_parser:
            if self.ticker:
                if ns_parser.b_no_country:
                    compare_list = ["Sector", "Industry"]
                else:
                    compare_list = ["Sector", "Industry", "Country"]

                self.similar, self.user = finviz_compare_model.get_similar_companies(
                    self.ticker, compare_list
                )

                if self.ticker.upper() in self.similar:
                    self.similar.remove(self.ticker.upper())

                if len(self.similar) > ns_parser.limit:
                    random.shuffle(self.similar)
                    self.similar = sorted(self.similar[: ns_parser.limit])
                    console.print(
                        f"The limit of stocks to compare are {ns_parser.limit}. The subsample will occur randomly.\n",
                    )

                if self.similar:
                    self.similar = [self.ticker] + self.similar

                    console.print(
                        f"[{self.user}] Similar Companies: {', '.join(self.similar)}",
                        "\n",
                    )
            else:
                console.print(
                    "You need to 'set' a ticker to get similar companies from first!"
                )
    def call_getfinviz(self, other_args: List[str]):
        """Process getfinviz command"""
        parser = argparse.ArgumentParser(
            add_help=False,
            formatter_class=argparse.ArgumentDefaultsHelpFormatter,
            prog="getfinviz",
            description=
            """Get similar companies from finviz to compare with.""",
        )
        parser.add_argument(
            "--nocountry",
            action="store_true",
            default=False,
            dest="b_no_country",
            help="Similar stocks from finviz using only Industry and Sector.",
        )
        try:
            ns_parser = parse_known_args_and_warn(parser, other_args)
            if not ns_parser:
                return
            if ns_parser.b_no_country:
                compare_list = ["Sector", "Industry"]
            else:
                compare_list = ["Sector", "Industry", "Country"]

            self.similar, self.user = finviz_compare_model.get_similar_companies(
                self.ticker, compare_list)

            if self.ticker.upper() in self.similar:
                self.similar.remove(self.ticker.upper())

            if len(self.similar) > 10:
                random.shuffle(self.similar)
                self.similar = sorted(self.similar[:10])
                print(
                    "The limit of stocks to compare with are 10. Hence, 10 random similar stocks will be displayed.\n",
                )

            if self.similar:
                print(
                    f"[{self.user}] Similar Companies: {', '.join(self.similar)}",
                    "\n")
        except Exception as e:
            print(e, "\n")