Here i am going to pull recent FINANCE sentiments on stock counters based on recent news as at 2017-11-01
Let me explain how i get the symbols. Type GOOGLE FINANCE in browser, from there, search stock counter name and then look for the symbols to download.
company <- c("OCBC", "SPH", "SMRT", "SBST", "ComfortDelgro")
symbol <- c("O39", "T39", "S53", "S61", "C52")
download_articles <- function(symbol) {
WebCorpus(GoogleFinanceSource(paste0("SGX:", symbol)))
}
stock_articles <- data_frame(company = company,symbol = symbol) %>% mutate(corpus = map(symbol, download_articles))
stock_tokens <- stock_articles %>%
unnest(map(corpus, tidy)) %>%
unnest_tokens(word, text) %>%
select(company, datetimestamp, word, id, heading)
stock_tf_idf <- stock_tokens %>%
count(company, word) %>%
filter(!str_detect(word, "\\d+")) %>%
bind_tf_idf(word, company, n) %>%
arrange(-tf_idf)
stock_sentiment_count <- stock_tokens %>%
inner_join(get_sentiments("loughran"), by = "word") %>%
count(sentiment, company) %>%
spread(sentiment, n, fill = 0)
stock_sentiment_count %>%
mutate(score = (positive - negative) / (positive + negative)) %>%
mutate(company = reorder(company, score)) %>%
ggplot(aes(company, score, fill = score > 0)) +
geom_col(show.legend = FALSE) +
coord_flip() +
labs(x = "Company", y = "Positivity score among 20 recent news articles")
