This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def getData(ticker, window, ma_period): | |
| """ | |
| Grabs price data from a given ticker. Retrieves prices based on the given time window; from now | |
| to N days ago. Sets the moving average period for prediction. Returns a preprocessed DF | |
| formatted for FB Prophet. | |
| """ | |
| # Time periods | |
| now = datetime.now() | |
| # How far back to retrieve tweets |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Initiating options | |
| options = Options() | |
| # Performing without GUI | |
| options.headless = True | |
| options.add_argument("--window-size=1920,1200") | |
| # Accepting downloads without GUI | |
| options.add_experimental_option("prefs", { | |
| "download.default_directory": r"/Users/marcosantos/Downloads", |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def vectBacktest(df, thres=.01, short=True): | |
| """ | |
| Accepts a dataframe of sentiment and log returns. Returns a series of the | |
| portfolio performance from the backtest. | |
| """ | |
| # Getting the initial positions | |
| df['positions'] = df['sentiment'].apply(lambda x: getPositions(x, | |
| thres=thres, | |
| short=short)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def getPositions(val, thres=.01, short=True): | |
| """ | |
| Provided a specific sentiment value, will return 1, 0, or -1 representing | |
| buy, hold, or sell respectively. The threshold will determined how high or low the score | |
| needs to be to trigger a buy or sell, otherwise it will be a hold. | |
| """ | |
| # Sentiment score threshold | |
| thres = .01 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def sentimentAndPrice(ticker, start, end, numtweets=20): | |
| """ | |
| Visually compares sentiment with the closing price of a given stock ticker. | |
| """ | |
| # Creating a DF that contains daily tweets between two dates | |
| df = tweetByDay(start, end, pd.DataFrame(), search="$"+ticker, limit=numtweets) | |
| # Analyzing the sentiment of each tweet | |
| sent_df = getSentiment( | |
| df, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def getStockPrices(ticker, start, end): | |
| """ | |
| Gets the historical daily prices between two dates. Applies the logarithmic function | |
| to return a dataframe of log returns each day. | |
| """ | |
| # Setting the stock | |
| stock = yf.Ticker(ticker) | |
| # Getting historical prices | |
| stock_df = stock.history(start=end, end=start, interval="1d")[['Close']] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def sentimentAndPrice(ticker, start, end, numtweets=20): | |
| """ | |
| Visually compares sentiment with the closing price of a given stock ticker. | |
| """ | |
| # Creating a DF that contains daily tweets between two dates | |
| df = tweetByDay(start, end, pd.DataFrame(), search="$"+ticker, limit=numtweets) | |
| # Analyzing the sentiment of each tweet | |
| sent_df = getSentiment( | |
| df, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def getStockPrices(ticker, start, end): | |
| """ | |
| Gets the historical daily prices between two dates. Scaling the prices based on a | |
| given sentiment dataframe. | |
| """ | |
| # Setting the stock | |
| stock = yf.Ticker(ticker) | |
| # Getting historical prices | |
| stock_df = stock.history(start=end, end=start, interval="1d")[['Close']] |