示例#1
0
    def call_historical(self, other_args: List[str]):
        """Process historical command"""
        parser = argparse.ArgumentParser(
            add_help=False,
            formatter_class=argparse.ArgumentDefaultsHelpFormatter,
            prog="historical",
            description=
            """Historical price comparison between similar companies.""",
        )
        parser.add_argument(
            "-t",
            "--type",
            action="store",
            dest="type_candle",
            type=str,
            choices=["o", "h", "l", "c", "a"],
            default="a",  # in case it's adjusted close
            help=
            "Candle data to use: o-open, h-high, l-low, c-close, a-adjusted close.",
        )
        parser.add_argument(
            "-n",
            "--no-scale",
            action="store_false",
            dest="no_scale",
            default=False,
            help="Flag to not put all prices on same 0-1 scale",
        )
        parser.add_argument(
            "-s",
            "--start",
            type=valid_date,
            default=(datetime.now() -
                     timedelta(days=366)).strftime("%Y-%m-%d"),
            dest="start",
            help="The starting date (format YYYY-MM-DD) of the stock",
        )
        if other_args and "-" not in other_args[0][0]:
            other_args.insert(0, "-t")
        ns_parser = parse_known_args_and_warn(parser, other_args,
                                              EXPORT_ONLY_RAW_DATA_ALLOWED)
        if ns_parser:
            if self.similar and len(self.similar) > 1:
                yahoo_finance_view.display_historical(
                    similar_tickers=self.similar,
                    start=ns_parser.start.strftime("%Y-%m-%d"),
                    candle_type=ns_parser.type_candle,
                    normalize=not ns_parser.no_scale,
                    export=ns_parser.export,
                )

            else:
                console.print(
                    "Please make sure there are more than 1 similar tickers selected. \n"
                )
def test_display_historical(mocker):
    # FORCE SINGLE THREADING
    yf_download = yahoo_finance_view.yahoo_finance_model.yf.download

    def mock_yf_download(*args, **kwargs):
        kwargs["threads"] = False
        return yf_download(*args, **kwargs)

    mocker.patch("yfinance.download", side_effect=mock_yf_download)

    # MOCK VISUALIZE_OUTPUT
    mocker.patch(
        target="gamestonk_terminal.helper_classes.TerminalStyle.visualize_output"
    )

    yahoo_finance_view.display_historical(
        similar_tickers=["TSLA", "GM"],
        start=datetime.strptime("2020-12-21", "%Y-%m-%d"),
        candle_type="o",
        normalize=True,
        export="",
    )
def test_display_historical(mocker):
    # FORCE SINGLE THREADING
    yf_download = yahoo_finance_view.yahoo_finance_model.yf.download

    def mock_yf_download(*args, **kwargs):
        kwargs["threads"] = False
        return yf_download(*args, **kwargs)

    mocker.patch("yfinance.download", side_effect=mock_yf_download)

    mock_show = mocker.Mock()
    mocker.patch("matplotlib.pyplot.show", new=mock_show)

    yahoo_finance_view.display_historical(
        similar_tickers=["TSLA", "GM"],
        start=datetime.strptime("2020-12-21", "%Y-%m-%d"),
        candle_type="o",
        normalize=True,
        export="",
    )

    mock_show.assert_called_once()
    def call_historical(self, other_args: List[str]):
        """Process historical command"""
        parser = argparse.ArgumentParser(
            add_help=False,
            formatter_class=argparse.ArgumentDefaultsHelpFormatter,
            prog="historical",
            description="""Historical price comparison between similar companies.
            """,
        )
        parser.add_argument(
            "-t",
            "--type",
            action="store",
            dest="type_candle",
            type=str,
            choices=["o", "h", "l", "c", "a"],
            default="a",  # in case it's adjusted close
            help=
            "Candle data to use: o-open, h-high, l-low, c-close, a-adjusted close.",
        )
        parser.add_argument(
            "-s",
            "--no-scale",
            action="store_false",
            dest="no_scale",
            default=True,
            help="Flag to not put all prices on same 0-1 scale",
        )
        parser.add_argument(
            "--export",
            choices=["csv", "json", "xlsx"],
            default="",
            type=str,
            dest="export",
            help="Export dataframe data to csv,json,xlsx file",
        )

        try:
            if self.interval != "1440min":
                print(
                    "Intraday historical data analysis comparison is not yet available."
                )
                # Alpha Vantage only supports 5 calls per minute, we need another API to get intraday data
            else:
                ns_parser = parse_known_args_and_warn(parser, other_args)
                if not ns_parser:
                    return

                if not self.similar or not self.ticker:
                    print(
                        "Please make sure there are both a loaded ticker and similar tickers selected. \n"
                    )
                    return

                yahoo_finance_view.display_historical(
                    ticker=self.ticker,
                    similar_tickers=self.similar,
                    start=self.start,
                    candle_type=ns_parser.type_candle,
                    normalize=ns_parser.no_scale,
                    export=ns_parser.export,
                )

        except Exception as e:
            print(e, "\n")