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 Dogecoin price and discovered significant spikes.
Ingredients
library(rtweet) ## Visit https://developer.twitter.com/ to get access key token <- create_token( app = "YouAppName", consumer_key = "<CONSUMER-KEY>", consumer_secret = "<CONSUMER-SECRET>", access_token = "<ACCESS-TOKEN>", access_secret = "<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 × 2 created_at text <dttm> <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 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: 2022-03-11 07:08:00 2.99e-06 2.99e-06 2.98e-06 2.98e-06 15045 2: 2022-03-11 07:09:00 2.99e-06 3.00e-06 2.98e-06 3.00e-06 85814 3: 2022-03-11 07:10:00 2.99e-06 3.00e-06 2.99e-06 2.99e-06 29702 4: 2022-03-11 07:11:00 2.99e-06 2.99e-06 2.98e-06 2.99e-06 224728 5: 2022-03-11 07:12:00 2.99e-06 3.00e-06 2.99e-06 2.99e-06 12573 --- 496: 2022-03-11 15:23:00 2.97e-06 2.97e-06 2.96e-06 2.96e-06 2828 497: 2022-03-11 15:24:00 2.96e-06 2.96e-06 2.96e-06 2.96e-06 301 498: 2022-03-11 15:25:00 2.97e-06 2.97e-06 2.96e-06 2.96e-06 13263 499: 2022-03-11 15:26:00 2.97e-06 2.97e-06 2.96e-06 2.96e-06 1784 500: 2022-03-11 15:27:00 2.97e-06 2.97e-06 2.96e-06 2.96e-06 1034
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 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 × 3 created_at text price_window <dttm> <chr> <list> 1 2021-07-13 02:33:53 "@dogeofficialceo 🤣" <data.table> 2 2021-07-08 22:34:41 "@dogeofficialceo @newscientist … <data.table> 3 2021-07-08 22:32:19 "@dogeofficialceo @newscientist … <data.table> 4 2021-07-01 01:25:05 "@dogeofficialceo @torybruno @Ch… <data.table> 5 2021-06-25 02:00:20 "@hiddin2urleft @ItsDogeCoin @In… <data.table> 6 2021-06-09 20:07:38 "@dogeofficialceo @MattWallace88… <data.table> 7 2021-06-05 08:21:59 "@lexfridman @VitalikButerin @et… <data.table> 8 2021-06-01 23:54:20 "@dogeofficialceo @SouthPark Whe… <data.table> 9 2021-05-25 05:37:12 "@heydave7 @dogecoin_devs Doge h… <data.table> 10 2021-05-24 19:49:56 "If you’d like to help develop D… <data.table> 11 2021-05-22 22:07:12 "@flcnhvy @thatdogegirl @Whatsup… <data.table> 12 2021-05-20 13:57:18 "@thatdogegirl @WhatsupFranks @T… <data.table>
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 × 3 created_at price text <dttm> <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:
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!