def test_display_candle(mocker, use_matplotlib):
    mocker.patch.object(target=stocks_helper.gtff,
                        attribute="USE_ION",
                        new=False)
    mocker.patch("matplotlib.pyplot.show")
    mocker.patch("plotly.basedatatypes.BaseFigure.show")

    # LOAD DATA
    ticker = "GME"
    start = datetime.strptime("2020-12-01", "%Y-%m-%d")
    interval = 1440
    end = datetime.strptime("2020-12-08", "%Y-%m-%d")
    prepost = False
    source = "yf"
    df_stock = stocks_helper.load(
        ticker=ticker,
        start=start,
        interval=interval,
        end=end,
        prepost=prepost,
        source=source,
    )

    # PROCESS DATA
    df_stock = stocks_helper.process_candle(df_data=df_stock)

    # DISPLAY CANDLE
    s_ticker = "GME"
    intraday = False
    stocks_helper.display_candle(
        s_ticker=s_ticker,
        df_stock=df_stock,
        use_matplotlib=use_matplotlib,
        intraday=intraday,
    )
def test_display_candle(mocker, use_matplotlib):
    # MOCK VISUALIZE_OUTPUT
    mocker.patch(
        target="gamestonk_terminal.helper_classes.TerminalStyle.visualize_output"
    )

    mocker.patch("plotly.basedatatypes.BaseFigure.show")

    # LOAD DATA
    ticker = "GME"
    start = datetime.strptime("2020-12-01", "%Y-%m-%d")
    interval = 1440
    end = datetime.strptime("2020-12-08", "%Y-%m-%d")
    prepost = False
    source = "yf"
    df_stock = stocks_helper.load(
        ticker=ticker,
        start=start,
        interval=interval,
        end=end,
        prepost=prepost,
        source=source,
    )

    # PROCESS DATA
    df_stock = stocks_helper.process_candle(df=df_stock)

    # DISPLAY CANDLE
    s_ticker = "GME"
    intraday = False
    stocks_helper.display_candle(
        s_ticker=s_ticker,
        df_stock=df_stock,
        use_matplotlib=use_matplotlib,
        intraday=intraday,
    )
    def call_candle(self, other_args: List[str]):
        """Process candle command"""
        parser = argparse.ArgumentParser(
            add_help=False,
            formatter_class=argparse.ArgumentDefaultsHelpFormatter,
            prog="candle",
            description="Shows historic data for a stock",
        )
        parser.add_argument(
            "-m",
            "--matplotlib",
            dest="matplotlib",
            action="store_true",
            default=False,
            help="Flag to show matplotlib instead of interactive plot using plotly.",
        )
        parser.add_argument(
            "--sort",
            choices=[
                "AdjClose",
                "Open",
                "Close",
                "High",
                "Low",
                "Volume",
                "Returns",
                "LogRet",
            ],
            default="",
            type=str,
            dest="sort",
            help="Choose a column to sort by",
        )
        parser.add_argument(
            "-d",
            "--descending",
            action="store_false",
            dest="descending",
            default=True,
            help="Sort selected column descending",
        )
        parser.add_argument(
            "--raw",
            action="store_true",
            dest="raw",
            default=False,
            help="Shows raw data instead of chart",
        )
        parser.add_argument(
            "-n",
            "--num",
            type=check_positive,
            help="Number to show if raw selected",
            dest="num",
            default=20,
        )

        ns_parser = parse_known_args_and_warn(
            parser, other_args, EXPORT_ONLY_RAW_DATA_ALLOWED
        )
        if ns_parser:
            if self.ticker:
                export_data(
                    ns_parser.export,
                    os.path.join(
                        os.path.dirname(os.path.abspath(__file__)), "raw_data"
                    ),
                    f"{self.ticker}",
                    self.stock,
                )

                if ns_parser.raw:
                    qa_view.display_raw(
                        df=self.stock,
                        sort=ns_parser.sort,
                        des=ns_parser.descending,
                        num=ns_parser.num,
                    )

                else:
                    data = stocks_helper.process_candle(self.stock)

                    stocks_helper.display_candle(
                        s_ticker=self.ticker,
                        df_stock=data,
                        use_matplotlib=ns_parser.matplotlib,
                        intraday=self.interval != "1440min",
                    )
            else:
                print("No ticker loaded. First use `load {ticker}`\n")
    def call_candle(self, other_args: List[str]):
        """Process candle command"""
        parser = argparse.ArgumentParser(
            add_help=False,
            formatter_class=argparse.ArgumentDefaultsHelpFormatter,
            prog="candle",
            description="Shows historic data for a stock",
        )
        parser.add_argument(
            "-p",
            "--plotly",
            dest="plotly",
            action="store_false",
            default=True,
            help="Flag to show interactive plotly chart.",
        )
        parser.add_argument(
            "--sort",
            choices=[
                "AdjClose",
                "Open",
                "Close",
                "High",
                "Low",
                "Volume",
                "Returns",
                "LogRet",
            ],
            default="",
            type=str,
            dest="sort",
            help="Choose a column to sort by",
        )
        parser.add_argument(
            "-d",
            "--descending",
            action="store_false",
            dest="descending",
            default=True,
            help="Sort selected column descending",
        )
        parser.add_argument(
            "--raw",
            action="store_true",
            dest="raw",
            default=False,
            help="Shows raw data instead of chart",
        )
        parser.add_argument(
            "-n",
            "--num",
            type=check_positive,
            help="Number to show if raw selected",
            dest="num",
            default=20,
        )
        parser.add_argument(
            "-t",
            "--trend",
            action="store_true",
            default=False,
            help="Flag to add high and low trends to candle.",
            dest="trendlines",
        )
        parser.add_argument(
            "--ma",
            dest="mov_avg",
            type=str,
            help=
            "Add moving average in number of days to plot and separate by a comma. Example: 20,30,50",
            default=None,
        )

        ns_parser = parse_known_args_and_warn(parser, other_args,
                                              EXPORT_ONLY_RAW_DATA_ALLOWED)
        if ns_parser:
            if self.ticker:
                export_data(
                    ns_parser.export,
                    os.path.join(os.path.dirname(os.path.abspath(__file__)),
                                 "raw_data"),
                    f"{self.ticker}",
                    self.stock,
                )

                if ns_parser.raw:
                    qa_view.display_raw(
                        df=self.stock,
                        sort=ns_parser.sort,
                        des=ns_parser.descending,
                        num=ns_parser.num,
                    )

                else:

                    data = stocks_helper.process_candle(self.stock)

                    mov_avgs = []

                    if ns_parser.mov_avg:
                        mov_list = (num
                                    for num in ns_parser.mov_avg.split(","))

                        for num in mov_list:
                            try:
                                mov_avgs.append(int(num))
                            except ValueError:
                                console.print(
                                    f"{num} is not a valid moving average, must be integer"
                                )

                    stocks_helper.display_candle(
                        s_ticker=self.ticker,
                        df_stock=data,
                        use_matplotlib=ns_parser.plotly,
                        intraday=self.interval != "1440min",
                        add_trend=ns_parser.trendlines,
                        ma=mov_avgs,
                    )
            else:
                console.print("No ticker loaded. First use `load {ticker}`\n")
示例#5
0
    def call_candle(self, other_args: List[str]):
        """Process candle command"""
        parser = argparse.ArgumentParser(
            add_help=False,
            formatter_class=argparse.ArgumentDefaultsHelpFormatter,
            prog="candle",
            description="Shows historic data for an ETF",
        )
        parser.add_argument(
            "-p",
            "--plotly",
            dest="plotly",
            action="store_false",
            default=True,
            help="Flag to show interactive plotly chart.",
        )
        parser.add_argument(
            "--sort",
            choices=[
                "AdjClose",
                "Open",
                "Close",
                "High",
                "Low",
                "Volume",
                "Returns",
                "LogRet",
            ],
            default="",
            type=str,
            dest="sort",
            help="Choose a column to sort by",
        )
        parser.add_argument(
            "-d",
            "--descending",
            action="store_false",
            dest="descending",
            default=True,
            help="Sort selected column descending",
        )
        parser.add_argument(
            "--raw",
            action="store_true",
            dest="raw",
            default=False,
            help="Shows raw data instead of chart",
        )
        parser.add_argument(
            "-n",
            "--num",
            type=check_positive,
            help="Number to show if raw selected",
            dest="num",
            default=20,
        )
        parser.add_argument(
            "-t",
            "--trend",
            action="store_true",
            default=False,
            help="Flag to add high and low trends to candle.",
            dest="trendlines",
        )
        parser.add_argument(
            "--ma",
            dest="mov_avg",
            type=str,
            help="Add moving averaged to plot",
            default="",
        )

        ns_parser = parse_known_args_and_warn(parser, other_args,
                                              EXPORT_ONLY_RAW_DATA_ALLOWED)
        if ns_parser:
            if self.etf_name:
                export_data(
                    ns_parser.export,
                    os.path.join(os.path.dirname(os.path.abspath(__file__)),
                                 "raw_data"),
                    f"{self.etf_name}",
                    self.etf_data,
                )

                if ns_parser.raw:
                    qa_view.display_raw(
                        df=self.etf_data,
                        sort=ns_parser.sort,
                        des=ns_parser.descending,
                        num=ns_parser.num,
                    )

                else:

                    data = stocks_helper.process_candle(self.etf_data)
                    mov_avgs = (tuple(
                        int(num) for num in ns_parser.mov_avg.split(","))
                                if ns_parser.mov_avg else None)

                    stocks_helper.display_candle(
                        s_ticker=self.etf_name,
                        df_stock=data,
                        use_matplotlib=ns_parser.plotly,
                        intraday=False,
                        add_trend=ns_parser.trendlines,
                        ma=mov_avgs,
                        asset_type="ETF",
                    )
            else:
                console.print("No ticker loaded. First use `load {ticker}`\n")
示例#6
0
    def call_candle(self, other_args: List[str]):
        """Process candle command"""
        parser = argparse.ArgumentParser(
            add_help=False,
            formatter_class=argparse.ArgumentDefaultsHelpFormatter,
            prog="candle",
            description="Shows historic data for an ETF",
        )
        ns_parser = parse_known_args_and_warn(
            parser, other_args, EXPORT_BOTH_RAW_DATA_AND_FIGURES)
        if ns_parser:
            if self.etf_name:
                # TODO: Should be done in one function
                data = stocks_helper.process_candle(self.etf_data)
                df_etf = stocks_helper.find_trendline(data, "OC_High", "high")
                df_etf = stocks_helper.find_trendline(data, "OC_Low", "low")

                mc = mpf.make_marketcolors(
                    up="green",
                    down="red",
                    edge="black",
                    wick="black",
                    volume="in",
                    ohlc="i",
                )

                s = mpf.make_mpf_style(marketcolors=mc,
                                       gridstyle=":",
                                       y_on_right=True)

                ap0 = []

                if "OC_High_trend" in df_etf.columns:
                    ap0.append(
                        mpf.make_addplot(df_etf["OC_High_trend"], color="g"), )

                if "OC_Low_trend" in df_etf.columns:
                    ap0.append(
                        mpf.make_addplot(df_etf["OC_Low_trend"], color="b"), )

                if gtff.USE_ION:
                    plt.ion()

                mpf.plot(
                    df_etf,
                    type="candle",
                    mav=(20, 50),
                    volume=True,
                    title=f"\nETF: {self.etf_name}",
                    addplot=ap0,
                    xrotation=10,
                    style=s,
                    figratio=(10, 7),
                    figscale=1.10,
                    figsize=(plot_autoscale()),
                    update_width_config=dict(candle_linewidth=1.0,
                                             candle_width=0.8,
                                             volume_linewidth=1.0),
                )
                console.print("")

                export_data(
                    ns_parser.export,
                    os.path.join(os.path.dirname(os.path.abspath(__file__)),
                                 "candle"),
                    f"{self.etf_name}",
                    self.etf_data,
                )

            else:
                console.print("No ticker loaded. First use `load {ticker}`\n")