From 6d9bfa6fd3d9b6c9e8f06bbe6329b499c3feb6c6 Mon Sep 17 00:00:00 2001 From: jmaslek Date: Tue, 6 Jul 2021 19:11:24 -0400 Subject: [PATCH] Small bugs (#534) --- gamestonk_terminal/backtesting/bt_helper.py | 28 +++++++++++++++++++-- gamestonk_terminal/backtesting/bt_view.py | 19 ++++++++------ gamestonk_terminal/etf/README.md | 2 +- pyproject.toml | 2 +- 4 files changed, 40 insertions(+), 11 deletions(-) diff --git a/gamestonk_terminal/backtesting/bt_helper.py b/gamestonk_terminal/backtesting/bt_helper.py index 38e835c0c84..702eea77947 100644 --- a/gamestonk_terminal/backtesting/bt_helper.py +++ b/gamestonk_terminal/backtesting/bt_helper.py @@ -7,6 +7,8 @@ from typing import Union from matplotlib import pyplot as plt from pandas.plotting import register_matplotlib_converters import bt +import yfinance as yf +import pandas as pd from gamestonk_terminal.helper_funcs import plot_autoscale from gamestonk_terminal.config_plot import PLOT_DPI from gamestonk_terminal import feature_flags as gtff @@ -14,7 +16,28 @@ from gamestonk_terminal import feature_flags as gtff register_matplotlib_converters() -def buy_and_hold(ticker: str, start: Union[str, datetime], name: str): +def get_data(ticker: str, start_date: Union[str, datetime]): + """ + Function to replace bt.get, Gets Adjusted close of ticker + Parameters + ---------- + ticker: str + Ticker to get data for + start_date: + Start date + + Returns + ------- + prices: pd.DataFrame + Dataframe of Adj Close with columns = [ticker] + """ + prices = yf.download(ticker, start=start_date, progress=False) + prices = pd.DataFrame(prices["Adj Close"]) + prices.columns = [ticker] + return prices + + +def buy_and_hold(ticker: str, start_date: Union[str, datetime], name: str): """ Generates a backtest object for the given ticker Parameters @@ -30,7 +53,8 @@ def buy_and_hold(ticker: str, start: Union[str, datetime], name: str): ------- bt.Backtest object for buy and hold strategy """ - prices = bt.get(ticker, start=start) + # prices = bt.get(ticker, start=start) + prices = get_data(ticker, start_date) bt_strategy = bt.Strategy( name, [ diff --git a/gamestonk_terminal/backtesting/bt_view.py b/gamestonk_terminal/backtesting/bt_view.py index b2cab11fd33..bef2d660247 100644 --- a/gamestonk_terminal/backtesting/bt_view.py +++ b/gamestonk_terminal/backtesting/bt_view.py @@ -3,11 +3,14 @@ __docformat__ = "numpy" import argparse from typing import List, Union from datetime import datetime +import numpy as np import pandas as pd import bt import pandas_ta as ta from gamestonk_terminal.helper_funcs import parse_known_args_and_warn -from gamestonk_terminal.backtesting.bt_helper import buy_and_hold, plot_bt +from gamestonk_terminal.backtesting.bt_helper import buy_and_hold, plot_bt, get_data + +np.seterr(divide="ignore") def simple_ema(ticker: str, start_date: Union[str, datetime], other_args: List[str]): @@ -53,7 +56,9 @@ def simple_ema(ticker: str, start_date: Union[str, datetime], other_args: List[s return ticker = ticker.lower() ema = pd.DataFrame() - prices = bt.get(ticker, start=start_date) + # prices = bt.get(ticker, start=start_date) + # bt.get not working + prices = get_data(ticker, start_date) ema[ticker] = ta.ema(prices[ticker], ns_parser.length) bt_strategy = bt.Strategy( "AboveEMA", @@ -145,7 +150,8 @@ def ema_cross(ticker: str, start_date: Union[str, datetime], other_args: List[st print("Short EMA period is longer than Long EMA period\n") return ticker = ticker.lower() - prices = bt.get(ticker, start=start_date) + # prices = bt.get(ticker, start=start_date) + prices = get_data(ticker, start_date) short_ema = pd.DataFrame(ta.ema(prices[ticker], ns_parser.short)) short_ema.columns = [ticker] long_ema = pd.DataFrame(ta.ema(prices[ticker], ns_parser.long)) @@ -257,8 +263,8 @@ def rsi_strat(ticker: str, start_date: Union[datetime, str], other_args: List[st print("Low RSI value is higher than Low RSI value\n") return ticker = ticker.lower() - prices = bt.get(ticker, start=start_date) - + # prices = bt.get(ticker, start=start_date) + prices = get_data(ticker, start_date) rsi = pd.DataFrame(ta.rsi(prices[ticker], ns_parser.periods)) rsi.columns = [ticker] @@ -294,6 +300,5 @@ def rsi_strat(ticker: str, start_date: Union[datetime, str], other_args: List[st print("") except Exception as e: - print(e) - print("") + print(e, "\n") return diff --git a/gamestonk_terminal/etf/README.md b/gamestonk_terminal/etf/README.md index b1239eead50..602898488ae 100644 --- a/gamestonk_terminal/etf/README.md +++ b/gamestonk_terminal/etf/README.md @@ -81,7 +81,7 @@ usage: compare [-n --names] ``` * -n/--names : ETFs to compare. Input as comma separated (ETF1,ETF2,..) -Campares the overview of different ETFs. Unknown tickers show nan +Campares the overview of different ETFs. Example: diff --git a/pyproject.toml b/pyproject.toml index 07f8c314180..56071d32dcc 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -9,7 +9,7 @@ packages = [ ] [tool.poetry.scripts] -gst = 'terminal:main' +gst = 'terminal:terminal' [tool.poetry.dependencies] python = "^3.6.8"