Skip to content

Instantly share code, notes, and snippets.

@marcosan93
Created September 13, 2021 00:38
Show Gist options
  • Select an option

  • Save marcosan93/28ac7eac04628d22ed6ba44af1cecf6f to your computer and use it in GitHub Desktop.

Select an option

Save marcosan93/28ac7eac04628d22ed6ba44af1cecf6f to your computer and use it in GitHub Desktop.
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,
measurement='compound'
)
# Getting stock price history
stock_df = getStockPrices(
ticker,
start,
end
)
# Merging the two DF
comb_df = sent_df.merge(stock_df, how='outer', sort=True)
# Shifting the sentiment scores 1 day to compensate for lookahead bias
comb_df['sentiment'] = comb_df['sentiment'].shift(1)
# Filling the nans with zeroes
comb_df = comb_df.fillna(0)
# Setting the index
comb_df.set_index('date', inplace=True)
return comb_df
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment