Last active
January 1, 2022 23:13
-
-
Save marcosan93/878993241d6ed90b14cbd518200c0fc4 to your computer and use it in GitHub Desktop.
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 sentimentPositions(val, thresh=0.1, short=True): | |
| """ | |
| Returns position as 1, -1, or 0 for Buy, Sell, | |
| and Do Nothing respectively based on the given | |
| sentiment value and threshold. | |
| """ | |
| if val > thresh: | |
| return 1 | |
| elif val < -thresh and short: | |
| return -1 | |
| else: | |
| return 0 | |
| # Getting sentiment values for the news headlines/titles | |
| news['sentiment'] = news['title'].apply( | |
| lambda x: TextBlob(x.lower()).sentiment[0] | |
| ) | |
| # Grouping together dates and aggregating sentiment scores from the same day | |
| sent_df = news.groupby('date')[['sentiment']].median() | |
| # Applying the position function | |
| sent_df['sentiment_positions'] = sent_df['sentiment'].apply( | |
| lambda x: sentimentPositions(x, thresh=0.1, short=False) | |
| ) | |
| # Filling in missing days with the most recent position value | |
| date_index = [str(i)[:10] for i in pd.date_range(sent_df.index[0], sent_df.index[-1])] | |
| sent_df = sent_df.reindex(date_index).fillna(method='ffill') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment