Mention of 'Doge' by Elon Musk on Twitter

Example Qbit showing mentions of the word 'doge' on Elon Musk's Twitter account.



library(rtweet)
library(tidytext)
library(dplyr)
library(tidyquant)
library(tidyr)
library(ggplot2)
library(binancer)
library(lubridate)
library(plotly)
library(purrr)

## Visit https://developer.twitter.com/ to get access key
# token <- create_token(
#   app = "QuantargoBot",
#   consumer_key = "<CONSUMER-KEY>",
#   consumer_secret = "<CONSUMER-SECRET>",
#   access_token = "<ACCESS-TOKEN>",
#   access_secret = "<ACCESS-SECRET>"
#   )

find_price_window <- function(date, sym = "DOGEBTC", window_length = 20) {
  date_rounded <- floor_date(date, unit = "minute")
  start_time <- date_rounded - window_length * 60
  end_time <- date_rounded + window_length * 60
  dodgebtc <- binance_klines(sym, interval = '1m', start_time = start_time, end_time = end_time)
  dodgebtc$close_time <- ceiling_date(dodgebtc$close_time, unit = "minute")
  close_zero <- dodgebtc$close[dodgebtc$close_time == date_rounded]
  out <- dodgebtc %>%
    mutate(timediff = difftime(close_time, date_rounded, units = "mins")) %>%
    mutate(price_rel = close/close_zero - 1) %>%
    mutate(date_rounded = date_rounded) %>%
    select(date_rounded, time = timediff, price = price_rel, volume = taker_buy_base_asset_volume) %>%
    arrange(time)
  out
}

# tmls <- get_timelines("elonmusk",
#  n = 3200,
#  token = token)
#
#saveRDS(tmls, file = "tmls.rds")
tmls <- readRDS("tmls.rds")

## preview tweets data
dogetweets <- tmls %>%
  filter(grepl("doge", text)) %>%
  mutate(date = as.Date(created_at)) %>% 
  mutate(price_window = map(created_at, find_price_window))  %>%
  mutate(event_num = 1:nrow(.))

### Plot twitter event
dogetweets %>%
  unnest(price_window) %>%
  select(created_at, date_rounded, time, price, volume) %>%
  mutate(time = as.numeric(time)) %>%
  ggplot(mapping = aes(x = time, y = price)) + 
  geom_line(aes(color = created_at, group = created_at)) + 
  geom_smooth(color = "orange") + 
  scale_y_continuous(labels = scales::percent) + 
  ylab("") + 
  xlab("Minutes to Tweet Creation") + 
  ggtitle("Price Impact DOGE/BTC around @elonmusk Tweet") + 
  theme_minimal()

# Top-10 Tweets by Absolute Price Change
dogetweets %>%
  unnest(price_window) %>%
  filter(time == 10) %>%
  arrange(desc(abs(price))) %>%
  select(created_at, price, volume, retweet_count, favorite_count, text) %>%
  head(10)

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.