Exemplo n.º 1
0
def test_display_rsi_strategy(mocker):
    yf_download = stocks_helper.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")

    ticker = "PM"
    start = datetime.strptime("2020-12-01", "%Y-%m-%d")
    end = datetime.strptime("2020-12-02", "%Y-%m-%d")
    df_stock = stocks_helper.load_ticker(ticker=ticker,
                                         start_date=start,
                                         end_date=end)
    bt_view.display_rsi_strategy(
        ticker=ticker,
        df_stock=df_stock,
        periods=2,
        low_rsi=2,
        high_rsi=2,
        spy_bt=True,
        no_bench=False,
        shortable=True,
        export=False,
    )
Exemplo n.º 2
0
def test_display_rsi_strategy(mocker):
    yf_download = stocks_helper.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)

    mocker.patch.object(target=bt_view.gtff, attribute="USE_ION", new=True)
    mocker.patch(
        target="gamestonk_terminal.stocks.backtesting.bt_view.plt.ion")
    mocker.patch(
        target="gamestonk_terminal.stocks.backtesting.bt_view.plt.show")

    ticker = "PM"
    start = datetime.strptime("2020-12-01", "%Y-%m-%d")
    end = datetime.strptime("2020-12-02", "%Y-%m-%d")
    df_stock = stocks_helper.load_ticker(ticker=ticker,
                                         start_date=start,
                                         end_date=end)
    bt_view.display_rsi_strategy(
        ticker=ticker,
        df_stock=df_stock,
        periods=2,
        low_rsi=2,
        high_rsi=2,
        spy_bt=True,
        no_bench=False,
        shortable=True,
        export=False,
    )
Exemplo n.º 3
0
def test_ema_cross_strategy(mocker):
    yf_download = stocks_helper.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)

    ticker = "PM"
    start = datetime.strptime("2020-12-01", "%Y-%m-%d")
    end = datetime.strptime("2020-12-02", "%Y-%m-%d")
    df_stock = stocks_helper.load_ticker(ticker=ticker,
                                         start_date=start,
                                         end_date=end)
    back_test_instance = bt_model.ema_cross_strategy(
        ticker=ticker,
        df_stock=df_stock,
        short_length=2,
        long_length=2,
        spy_bt=True,
        no_bench=False,
        shortable=True,
    )
    assert isinstance(back_test_instance, bt.backtest.Result)
Exemplo n.º 4
0
def test_load_ticker(recorder):
    ticker = "PM"
    start = datetime.strptime("2020-12-01", "%Y-%m-%d")
    end = datetime.strptime("2020-12-02", "%Y-%m-%d")
    result_df = stocks_helper.load_ticker(ticker=ticker,
                                          start_date=start,
                                          end_date=end)
    recorder.capture(result_df)
Exemplo n.º 5
0
def draw_graph(
    ticker: str,
    report_cache_dir: str,
    time_delta: int = 180,
    line_type: str = "candle",
    draw_mas: tuple = (20, 50),
    draw_volume: bool = True,
    high_trend: bool = True,
    low_trend: bool = True,
):
    """Draw Graph is a helper that wraps mplfinance, caching and trendline
    Parameters
    ----------
    ticker : str
        Stock ticker to draw
    report_cache_dir : str
        Ticker data cache directory to use
    time_delta : int, optional
        Graph period in days, by default 180
    line_type : str, optional
        Graph line type to pass to mplfinance, by default "candle"
    draw_mas : tuple, optional
        SMAs to draw, by default (20, 50)
    draw_volume : bool, optional
        Switch to draw trading volume by default True
    high_trend : bool, optional
        Switch to draw high trend line, by default True
    low_trend : bool, optional
        Switch to draw low trend line, by default True
    """
    if report_cache_dir:
        df_stock_cache = pathlib.Path(report_cache_dir,
                                      f"{ticker}_stock_data_{time_delta}.pkl")
        if os.path.isfile(df_stock_cache):
            df_stock = pd.read_pickle(df_stock_cache)
        else:
            df_stock = stocks_helper.load_ticker(
                ticker,
                (datetime.now() -
                 timedelta(days=time_delta)).strftime("%Y-%m-%d"),
            )
            df_stock = stocks_helper.find_trendline(df_stock, "OC_High",
                                                    "high")
            df_stock = stocks_helper.find_trendline(df_stock, "OC_Low", "low")
            df_stock.to_pickle(df_stock_cache)

    else:
        df_stock = stocks_helper.load_ticker(
            ticker,
            (datetime.now() - timedelta(days=time_delta)).strftime("%Y-%m-%d"))
        df_stock = stocks_helper.find_trendline(df_stock, "OC_High", "high")
        df_stock = stocks_helper.find_trendline(df_stock, "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_stock.columns and high_trend:
        ap0.append(mpf.make_addplot(df_stock["OC_High_trend"], color="g"), )

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

    mpf.plot(
        df_stock,
        type=line_type,
        mav=draw_mas,
        volume=draw_volume,
        addplot=ap0,
        xrotation=0,
        style=s,
        figratio=(10, 7),
        figscale=2.00,
        update_width_config=dict(candle_linewidth=1.0,
                                 candle_width=0.8,
                                 volume_linewidth=1.0),
    )