% select(created_at, text) # A tibble: 600 x 2 created_at text 1 2021-07-13 03:05:20 "those who attack space\nmaybe don’t realize … 2 2021-07-13 02:39:11 "@Rogozin 👏👏" 3 2021-07-13 02:37:57 "Loki is pretty good. Basically, live-action … 4 2021-07-13 02:33:53 "@dogeofficialceo 🤣" 5 2021-07-13 02:33:26 "@CGDaveMac Maybe if it sees a Shiba Inu, the… 6 2021-07-13 02:30:16 "🤯 https://t.co/Z11qszTY4v" 7 2021-07-12 22:18:34 "@OwenSparks_ @jeremyjudkins Haha Buzz Corp –… 8 2021-07-12 22:07:39 "@ErcXspace @kimpaquette Interesting idea" 9 2021-07-12 21:40:43 "@kimpaquette Not yet, but they will. It’s ne… 10 2021-07-12 21:38:29 "@cleantechnica OPP? https://t.co/muZdxKdUXz" # … with 590 more rows We executed get_timelines() multiple times to get most tweets out of the timeline. To study the price effect of his tweets the binancer package was used to download intraday open-high-low-close (OHCL) data in 1-minute intervals from the Binance crytocurrency exchange. We decided on the Dodge vs. Bitcoin (DOGE/BTC) currency pair to also adjust for the overall market movements and to better see the price effect. The function binance_klines() returns a data.table containing all intraday pricing data: # Install through `remotes::install_github("daroczig/binancer")` library(binancer) binance_klines("DOGEBTC", interval = "1m") %>% select(open_time, open, high, low, close, volume) open_time open high low close volume 1: 2021-07-16 07:51:00 5.78e-06 5.78e-06 5.77e-06 5.78e-06 80201 2: 2021-07-16 07:52:00 5.78e-06 5.78e-06 5.77e-06 5.77e-06 2337 3: 2021-07-16 07:53:00 5.78e-06 5.78e-06 5.77e-06 5.78e-06 6161 4: 2021-07-16 07:54:00 5.77e-06 5.78e-06 5.77e-06 5.78e-06 31250 5: 2021-07-16 07:55:00 5.78e-06 5.78e-06 5.77e-06 5.77e-06 67220 --- 496: 2021-07-16 16:06:00 5.65e-06 5.65e-06 5.63e-06 5.65e-06 145786 497: 2021-07-16 16:07:00 5.65e-06 5.65e-06 5.64e-06 5.64e-06 135197 498: 2021-07-16 16:08:00 5.65e-06 5.65e-06 5.64e-06 5.65e-06 14782 499: 2021-07-16 16:09:00 5.65e-06 5.65e-06 5.64e-06 5.65e-06 254613 500: 2021-07-16 16:10:00 5.65e-06 5.66e-06 5.64e-06 5.66e-06 85440 Twitter Event Study A typical way to study such events on financial markets is to look at the price movements right before and after each tweet happened. Especially the price action around each tweet can give us an indication of its market effect. For this analysis it is critical to correctly join our 2 data sources, containing twitter and price data. We also need to add the relative time (e.g. minutes relative to tweet timestamp) to to the tweet event which can be used as a common scale for plotting. For this task the function find_price_window() was created to return the relative price changes around a specified date: library(lubridate) find_price_window % 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 } Now we can create the data table dogetweets which contains the tweets AND the price action around each tweet by the relative time: library(purrr) dogetweets % filter(grepl("doge", text)) %>% mutate(date = as.Date(created_at)) %>% mutate(price_window = map(created_at, find_price_window)) %>% mutate(event_num = 1:nrow(.)) dogetweets %>% select(created_at, text, price_window) # A tibble: 12 x 3 created_at text price_window 1 2021-07-13 02:33:53 "@dogeofficialceo 🤣" We can finally unnest() the price data from price_window and create a ggplot, containing the price movements around each elonmusk dodge tweet: library(ggplot2) library(tidyr) dogetweets %>% unnest(price_window) %>% select(created_at, date_rounded, time, price, volume) %>% mutate(time = as.numeric(time)) %>% mutate(created_at = format(created_at, "%Y-%m-%d %H:%M:%S")) %>% ggplot(mapping = aes(x = time, y = price)) + geom_line(aes(color = created_at, group = created_at)) + scale_y_continuous(labels = scales::percent) + geom_vline(xintercept = 0) + ylab("") + xlab("Minutes to Tweet Creation") + ggtitle("Price Impact DOGE/BTC around @elonmusk Tweet") + theme_minimal() We can also show a table containing the Top-10 tweets by absolute price movement: dogetweets %>% unnest(price_window) %>% filter(time == 10) %>% arrange(desc(abs(price))) %>% select(created_at, price, text) %>% head(10) # A tibble: 10 x 3 created_at price text 1 2021-05-24 19:49:56 0.0494 "If you’d like to help develop Doge,… 2 2021-05-25 05:37:12 0.0144 "@heydave7 @dogecoin_devs Doge has d… 3 2021-05-20 13:57:18 0.0131 "@thatdogegirl @WhatsupFranks @Tesla… 4 2021-06-01 23:54:20 0.0100 "@dogeofficialceo @SouthPark When I … 5 2021-06-09 20:07:38 0.00757 "@dogeofficialceo @MattWallace888 No… 6 2021-07-13 02:33:53 0.00647 "@dogeofficialceo 🤣" 7 2021-06-25 02:00:20 -0.00508 "@hiddin2urleft @ItsDogeCoin @Invest… 8 2021-07-08 22:34:41 0.00321 "@dogeofficialceo @newscientist Kind… 9 2021-05-22 22:07:12 -0.00221 "@flcnhvy @thatdogegirl @WhatsupFran… 10 2021-06-05 08:21:59 0.00195 "@lexfridman @VitalikButerin @ethere… Results For some tweets we indeed see a slight price effect for the DOGE/BTC quote on the Binance exchange. The most important tweet which triggered an immediate, positive price reaction by almost 5% versus Bitcoin from our sample seems to be this: If you’d like to help develop Doge, please submit ideas on GitHub & https://t.co/liAPQMFaQB @dogecoin_devs — Elon Musk (@elonmusk) May 24, 2021 Reproducing Results, QBit Workspace If you’re interested in a fully reproducible workspace including all data sets for download check the created Workspace HERE In the next post we will investigate how the sentiment of tweets may affect the price direction of specific markets. Happy coding!" />

The Elon Musk Tweet Effect on Dogecoin (DOGE)

[This article was first published on Quantargo Blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

The Elon Musk Tweet Effect on Dogecoin (DOGE)

Unveil the Dogefather

Elon Musk is known for his regular tweets about many different topics—in particular his companies Tesla and SpaceX. With close to 60 million followers he truly is a Twitter celebrity and his opinions have a big impact on technologies and companies. Most recently his tweets also covered Dogecoin, a crypto currency featuring a dog. With a little R-code we checked the effect of his tweets on the Dodgecoin price and discovered significant spikes.

Ingredients

As data sets we use the tweet timeline from @elonmusk. With the rtweet package the timeline can be downloaded as

library(rtweet)

## Visit https://developer.twitter.com/ to get access key
token <- create_token(
  app = "YouAppName",
  consumer_key = "",
  consumer_secret = "",
  access_token = "",
  access_secret = ""
  )

tmls <- get_timelines("elonmusk",
  n = 3200,
  token = token)

Note, that the token creation can be a bit tricky since you first need to register an App at the Twitter developer page https://developer.twitter.com. It’s important to fill-in not only the consumer_key/consumer_secret but also the access_token/access_secret to successfully create the token. After the successful retrieval we get a data frame with (an excerpt) of the tweets from the @elonmusk timeline:

library(dplyr)

tmls %>%
  select(created_at, text)
# A tibble: 600 x 2
   created_at          text                                          
                 <chr>                                         
 1 2021-07-13 03:05:20 "those who attack space\nmaybe don’t realize …
 2 2021-07-13 02:39:11 "@Rogozin 👏👏"                               
 3 2021-07-13 02:37:57 "Loki is pretty good. Basically, live-action …
 4 2021-07-13 02:33:53 "@dogeofficialceo 🤣"                         
 5 2021-07-13 02:33:26 "@CGDaveMac Maybe if it sees a Shiba Inu, the…
 6 2021-07-13 02:30:16 "🤯 https://t.co/Z11qszTY4v"                  
 7 2021-07-12 22:18:34 "@OwenSparks_ @jeremyjudkins Haha Buzz Corp –…
 8 2021-07-12 22:07:39 "@ErcXspace @kimpaquette Interesting idea"    
 9 2021-07-12 21:40:43 "@kimpaquette Not yet, but they will. It’s ne…
10 2021-07-12 21:38:29 "@cleantechnica OPP? https://t.co/muZdxKdUXz" 
# … with 590 more rows

We executed get_timelines() multiple times to get most tweets out of the timeline.

To study the price effect of his tweets the binancer package was used to download intraday open-high-low-close (OHCL) data in 1-minute intervals from the Binance crytocurrency exchange. We decided on the Dodge vs. Bitcoin (DOGE/BTC) currency pair to also adjust for the overall market movements and to better see the price effect. The function binance_klines() returns a data.table containing all intraday pricing data:

# Install through `remotes::install_github("daroczig/binancer")`
library(binancer) 
binance_klines("DOGEBTC", interval = "1m") %>%
  select(open_time, open, high, low, close, volume)
               open_time     open     high      low    close volume
  1: 2021-07-16 07:51:00 5.78e-06 5.78e-06 5.77e-06 5.78e-06  80201
  2: 2021-07-16 07:52:00 5.78e-06 5.78e-06 5.77e-06 5.77e-06   2337
  3: 2021-07-16 07:53:00 5.78e-06 5.78e-06 5.77e-06 5.78e-06   6161
  4: 2021-07-16 07:54:00 5.77e-06 5.78e-06 5.77e-06 5.78e-06  31250
  5: 2021-07-16 07:55:00 5.78e-06 5.78e-06 5.77e-06 5.77e-06  67220
 ---                                                               
496: 2021-07-16 16:06:00 5.65e-06 5.65e-06 5.63e-06 5.65e-06 145786
497: 2021-07-16 16:07:00 5.65e-06 5.65e-06 5.64e-06 5.64e-06 135197
498: 2021-07-16 16:08:00 5.65e-06 5.65e-06 5.64e-06 5.65e-06  14782
499: 2021-07-16 16:09:00 5.65e-06 5.65e-06 5.64e-06 5.65e-06 254613
500: 2021-07-16 16:10:00 5.65e-06 5.66e-06 5.64e-06 5.66e-06  85440

Twitter Event Study

A typical way to study such events on financial markets is to look at the price movements right before and after each tweet happened. Especially the price action around each tweet can give us an indication of its market effect. For this analysis it is critical to correctly join our 2 data sources, containing twitter and price data. We also need to add the relative time (e.g. minutes relative to tweet timestamp) to to the tweet event which can be used as a common scale for plotting. For this task the function find_price_window() was created to return the relative price changes around a specified date:

library(lubridate)

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
}

Now we can create the data table dogetweets which contains the tweets AND the price action around each tweet by the relative time:

library(purrr)

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(.))

dogetweets %>%
  select(created_at, text, price_window)
# A tibble: 12 x 3
   created_at          text                           price_window   
                 <chr>                          <list>         
 1 2021-07-13 02:33:53 "@dogeofficialceo 🤣"          

We can finally unnest() the price data from price_window and create a ggplot, containing the price movements around each elonmusk dodge tweet:

library(ggplot2)
library(tidyr)

dogetweets %>%
  unnest(price_window) %>%
  select(created_at, date_rounded, time, price, volume) %>%
  mutate(time = as.numeric(time)) %>%
  mutate(created_at = format(created_at, "%Y-%m-%d %H:%M:%S")) %>%
  ggplot(mapping = aes(x = time, y = price)) + 
  geom_line(aes(color = created_at, group = created_at)) + 
  scale_y_continuous(labels = scales::percent) + 
  geom_vline(xintercept = 0) + 
  ylab("") + 
  xlab("Minutes to Tweet Creation") + 
  ggtitle("Price Impact DOGE/BTC around @elonmusk Tweet") +
  theme_minimal()

We can also show a table containing the Top-10 tweets by absolute price movement:

dogetweets %>%
  unnest(price_window) %>%
  filter(time == 10) %>%
  arrange(desc(abs(price))) %>%
  select(created_at, price, text) %>%
  head(10)
# A tibble: 10 x 3
   created_at             price text                                 
                    <dbl> <chr>                                
 1 2021-05-24 19:49:56  0.0494  "If you’d like to help develop Doge,…
 2 2021-05-25 05:37:12  0.0144  "@heydave7 @dogecoin_devs Doge has d…
 3 2021-05-20 13:57:18  0.0131  "@thatdogegirl @WhatsupFranks @Tesla…
 4 2021-06-01 23:54:20  0.0100  "@dogeofficialceo @SouthPark When I …
 5 2021-06-09 20:07:38  0.00757 "@dogeofficialceo @MattWallace888 No…
 6 2021-07-13 02:33:53  0.00647 "@dogeofficialceo 🤣"                
 7 2021-06-25 02:00:20 -0.00508 "@hiddin2urleft @ItsDogeCoin @Invest…
 8 2021-07-08 22:34:41  0.00321 "@dogeofficialceo @newscientist Kind…
 9 2021-05-22 22:07:12 -0.00221 "@flcnhvy @thatdogegirl @WhatsupFran…
10 2021-06-05 08:21:59  0.00195 "@lexfridman @VitalikButerin @ethere…

Results

For some tweets we indeed see a slight price effect for the DOGE/BTC quote on the Binance exchange. The most important tweet which triggered an immediate, positive price reaction by almost 5% versus Bitcoin from our sample seems to be this:

Reproducing Results, QBit Workspace

If you’re interested in a fully reproducible workspace including all data sets for download check the created Workspace HERE

In the next post we will investigate how the sentiment of tweets may affect the price direction of specific markets.

Happy coding!

To leave a comment for the author, please follow the link and comment on their blog: Quantargo Blog.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)