HSX Arbitrage

[This article was first published on max humber, 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.

TL;DR: I wrote a web scraper to help me make fake money.

I’ve been playing around with an online marketplace called the “Hollywood Stock Exchange” for a couple of months now. The website is basically “fantasy” for actors, directors and upcoming movies but it functions pretty much exactly like a stock market.

In simple terms here’s how the exchange works:

Rogue One (SW16) is currently trading at ~$440 on the HSX. This means that the market thinks that Rogue One will gross $440,000,000 during the first four weeks in theatres. For context, the last Star Wars movie (STAR7) did $936 Million in the same time period. If you think that Rogue One is going to do something in between $440 and $936 Million than you ought to buy the security because it will converge and delist on the actual box office totals.

There’s definitely money* to be made on a Rogue One position, right now. But, I think there’s an arbitrage opportunity in buying the actors and actresses attached to Rogue One instead.

See, actors and actresses, called StarBonds on the HSXm are priced slightly differently. StarBonds are derived from the average total box-office performance for the last five credited films by release date. This means that each time a movie featuring a particular star cashes out and delists, the box office gross is added into the star’s Trailing Average Gross (TAG), and the bond price is adjusted to match.

Mads Mikkelsen (MMIKK) a Rogue One cast member, for instance, is currently priced at $47.10. His actual TAG value, however, is only $43.54. This spread is because of Rogue One. In a couple weeks Rogue One will be added to Mads’ TAG value while at the same time some random indie art film called “A Royal Affair” (ROYAF) will fall off and out of his TAG.

I mean, I could manually do the math for the Mads example and figure out what his TAG will be when Rogue One gets added. But there’s no fun in that! And besides, Mads is already a big deal. The thing about the new Star Wars movies is that a bunch of nobodies get cast and turned it into Super Stars overnight. It means that a lot of random indie art films (think sub ~$5) are about to fall off to make room for Rogue One (~$440). Basically, Rogue One is going to make the TAGs for a lot of people pop.

The punch line:

I’m lazy. And, I didn’t want to manually do the math. So, I built a web scraper to calculate the arbitrage opportunities for me.

(If you don’t care about the code just scroll to the bottom…)

Setup

<span class="c1"># load packages
</span><span class="n">library</span><span class="p">(</span><span class="n">tidyverse</span><span class="p">)</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">rvest</span><span class="p">)</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">purrr</span><span class="p">)</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">stringr</span><span class="p">)</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">knitr</span><span class="p">)</span><span class="w">

</span><span class="c1"># knitr options
</span><span class="n">opts_chunk</span><span class="o">$</span><span class="n">set</span><span class="p">(</span><span class="n">cache</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">TRUE</span><span class="p">,</span><span class="w"> </span><span class="n">warning</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">FALSE</span><span class="p">,</span><span class="w"> </span><span class="n">message</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">FALSE</span><span class="p">)</span><span class="w">

</span><span class="c1"># set base URL for HSX.com
</span><span class="n">URL</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="s2">"http://www.hsx.com/security/view/"</span><span class="w">
</span>

Cast Scraper

<span class="n">get_cast</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="k">function</span><span class="p">(</span><span class="n">movie</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w">
    
    </span><span class="n">page</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">read_html</span><span class="p">(</span><span class="n">str_c</span><span class="p">(</span><span class="n">URL</span><span class="p">,</span><span class="w"> </span><span class="n">movie</span><span class="p">))</span><span class="w">
    
    </span><span class="n">name</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">page</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
        </span><span class="n">html_nodes</span><span class="p">(</span><span class="s2">".credit p"</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
        </span><span class="n">html_text</span><span class="p">()</span><span class="w">
    
    </span><span class="n">df</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">tibble</span><span class="p">(</span><span class="n">name</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
        </span><span class="n">separate</span><span class="p">(</span><span class="n">name</span><span class="p">,</span><span class="w"> </span><span class="n">into</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="s2">"name"</span><span class="p">,</span><span class="w"> </span><span class="s2">"symbol"</span><span class="p">),</span><span class="w"> </span><span class="n">sep</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"\\s\\("</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w">
        </span><span class="n">mutate</span><span class="p">(</span><span class="n">symbol</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">str_replace_all</span><span class="p">(</span><span class="n">symbol</span><span class="p">,</span><span class="w"> </span><span class="s2">"\\)"</span><span class="p">,</span><span class="w"> </span><span class="s2">""</span><span class="p">))</span><span class="w">
        
    </span><span class="nf">return</span><span class="p">(</span><span class="n">df</span><span class="p">)</span><span class="w">
</span><span class="p">}</span><span class="w">

</span><span class="n">cast</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">get_cast</span><span class="p">(</span><span class="s2">"SW16"</span><span class="p">)</span><span class="w">
</span>

Credits Scraper

<span class="n">get_credits</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="k">function</span><span class="p">(</span><span class="n">actor</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w">
    
    </span><span class="n">page</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">read_html</span><span class="p">(</span><span class="n">str_c</span><span class="p">(</span><span class="n">URL</span><span class="p">,</span><span class="w"> </span><span class="n">actor</span><span class="p">))</span><span class="w">
    
    </span><span class="n">movie</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">page</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
        </span><span class="n">html_nodes</span><span class="p">(</span><span class="s2">".credit span"</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
        </span><span class="n">html_text</span><span class="p">()</span><span class="w">
    
    </span><span class="n">date</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">page</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
        </span><span class="n">html_nodes</span><span class="p">(</span><span class="s2">"strong"</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
        </span><span class="n">html_text</span><span class="p">()</span><span class="w">
    
    </span><span class="n">l</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="nf">length</span><span class="p">(</span><span class="n">movie</span><span class="p">)</span><span class="w">
    </span><span class="n">date</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">date</span><span class="p">[</span><span class="m">1</span><span class="o">:</span><span class="n">l</span><span class="p">]</span><span class="w">
    
    </span><span class="n">df</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">tibble</span><span class="p">(</span><span class="n">date</span><span class="p">,</span><span class="w"> </span><span class="n">movie</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
        </span><span class="n">mutate</span><span class="p">(</span><span class="n">symbol</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">actor</span><span class="p">)</span><span class="w">
    
    </span><span class="nf">return</span><span class="p">(</span><span class="n">df</span><span class="p">)</span><span class="w">
</span><span class="p">}</span><span class="w">

</span><span class="n">credits</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">map</span><span class="p">(</span><span class="n">cast</span><span class="o">$</span><span class="n">symbol</span><span class="p">,</span><span class="w"> </span><span class="n">get_credits</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="n">bind_rows</span><span class="p">()</span><span class="w"> 
</span>

Clean Credits

<span class="n">clean_credits</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="k">function</span><span class="p">(</span><span class="n">credits</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w">
    
    </span><span class="n">clean</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">credits</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
        </span><span class="n">mutate</span><span class="p">(</span><span class="n">date</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">as.Date</span><span class="p">(</span><span class="n">date</span><span class="p">,</span><span class="w"> </span><span class="n">format</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"%b %d, %Y"</span><span class="p">))</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
        </span><span class="n">mutate</span><span class="p">(</span><span class="n">date</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ifelse</span><span class="p">(</span><span class="o">!</span><span class="nf">is.na</span><span class="p">(</span><span class="n">date</span><span class="p">),</span><span class="w"> 
        	</span><span class="nf">as.character</span><span class="p">(</span><span class="n">date</span><span class="p">),</span><span class="w"> </span><span class="nf">as.character</span><span class="p">(</span><span class="s2">"3000-01-01"</span><span class="p">)))</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
        </span><span class="n">mutate</span><span class="p">(</span><span class="n">date</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">as.Date</span><span class="p">(</span><span class="n">date</span><span class="p">))</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
        </span><span class="n">group_by</span><span class="p">(</span><span class="n">movie</span><span class="p">,</span><span class="w"> </span><span class="n">symbol</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
        </span><span class="n">mutate</span><span class="p">(</span><span class="n">future</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ifelse</span><span class="p">(</span><span class="n">date</span><span class="w"> </span><span class="o">>=</span><span class="w"> </span><span class="n">Sys.Date</span><span class="p">()</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="nf">is.na</span><span class="p">(</span><span class="n">date</span><span class="p">),</span><span class="w"> </span><span class="kc">TRUE</span><span class="p">,</span><span class="w"> </span><span class="kc">FALSE</span><span class="p">))</span><span class="w">
    
    </span><span class="n">five</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">clean</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
        </span><span class="n">filter</span><span class="p">(</span><span class="n">future</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="kc">TRUE</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
        </span><span class="n">arrange</span><span class="p">(</span><span class="n">symbol</span><span class="p">,</span><span class="w"> </span><span class="n">desc</span><span class="p">(</span><span class="n">date</span><span class="p">))</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
        </span><span class="n">group_by</span><span class="p">(</span><span class="n">symbol</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
        </span><span class="n">mutate</span><span class="p">(</span><span class="n">movie_idx</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">row_number</span><span class="p">())</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
        </span><span class="n">filter</span><span class="p">(</span><span class="n">movie_idx</span><span class="w"> </span><span class="o"><=</span><span class="w"> </span><span class="m">5</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
        </span><span class="n">select</span><span class="p">(</span><span class="o">-</span><span class="n">future</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
        </span><span class="n">ungroup</span><span class="p">()</span><span class="w">
    
    </span><span class="nf">return</span><span class="p">(</span><span class="n">five</span><span class="p">)</span><span class="w">
</span><span class="p">}</span><span class="w">

</span><span class="n">tag_credits</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">clean_credits</span><span class="p">(</span><span class="n">credits</span><span class="p">)</span><span class="w">
</span>
<span class="n">movies</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">tag_credits</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
    </span><span class="n">distinct</span><span class="p">(</span><span class="n">movie</span><span class="p">,</span><span class="w"> </span><span class="n">.keep_all</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">FALSE</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
    </span><span class="n">mutate</span><span class="p">(</span><span class="n">search_term</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">str_replace_all</span><span class="p">(</span><span class="n">movie</span><span class="p">,</span><span class="w"> </span><span class="s2">"\\s"</span><span class="p">,</span><span class="w"> </span><span class="s2">"\\+"</span><span class="p">))</span><span class="w">

</span><span class="n">get_meta_m</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="k">function</span><span class="p">(</span><span class="n">movie</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w">

    </span><span class="n">page</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">read_html</span><span class="p">(</span><span class="n">str_c</span><span class="p">(</span><span class="w">
        </span><span class="s2">"http://www.hsx.com/search/?keyword="</span><span class="p">,</span><span class="w"> 
        </span><span class="n">movie</span><span class="p">,</span><span class="w"> 
        </span><span class="s2">"&status=ALL&action=submit_advanced"</span><span class="p">))</span><span class="w">
    
    </span><span class="n">movie</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">page</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
        </span><span class="n">html_nodes</span><span class="p">(</span><span class="s2">"td:nth-child(1)"</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
        </span><span class="n">html_text</span><span class="p">()</span><span class="w">
    
    </span><span class="n">symbol</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">page</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
        </span><span class="n">html_nodes</span><span class="p">(</span><span class="s2">"td:nth-child(2)"</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
        </span><span class="n">html_text</span><span class="p">()</span><span class="w">
    
    </span><span class="n">price</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">page</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
        </span><span class="n">html_nodes</span><span class="p">(</span><span class="s2">"td:nth-child(5)"</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
        </span><span class="n">html_text</span><span class="p">()</span><span class="w">
    
    </span><span class="n">l</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="nf">length</span><span class="p">(</span><span class="n">price</span><span class="p">)</span><span class="w">
    </span><span class="n">movie</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">movie</span><span class="p">[</span><span class="m">1</span><span class="o">:</span><span class="n">l</span><span class="p">]</span><span class="w">
    </span><span class="n">symbol</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">symbol</span><span class="p">[</span><span class="m">1</span><span class="o">:</span><span class="n">l</span><span class="p">]</span><span class="w">
    
    </span><span class="n">df</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">tibble</span><span class="p">(</span><span class="n">movie</span><span class="p">,</span><span class="w"> </span><span class="n">symbol</span><span class="p">,</span><span class="w"> </span><span class="n">price</span><span class="p">)</span><span class="w">
    
    </span><span class="nf">return</span><span class="p">(</span><span class="n">df</span><span class="p">)</span><span class="w">
</span><span class="p">}</span><span class="w">

</span><span class="n">meta_m</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">map</span><span class="p">(</span><span class="n">movies</span><span class="o">$</span><span class="n">search_term</span><span class="p">,</span><span class="w"> </span><span class="n">safely</span><span class="p">(</span><span class="n">get_meta_m</span><span class="p">))</span><span class="w">
</span>

Clean Search Results

<span class="n">clean_meta_m</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="k">function</span><span class="p">(</span><span class="n">meta_m</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w">
    
    </span><span class="n">meta_t</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">meta_m</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="n">transpose</span><span class="p">()</span><span class="w">
    </span><span class="n">is_ok</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">meta_t</span><span class="o">$</span><span class="n">error</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="n">map_lgl</span><span class="p">(</span><span class="n">is_null</span><span class="p">)</span><span class="w">
    </span><span class="n">meta_k</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">meta_t</span><span class="o">$</span><span class="n">result</span><span class="p">[</span><span class="n">is_ok</span><span class="p">]</span><span class="w">
    
    </span><span class="n">df</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">meta_k</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
        </span><span class="n">bind_rows</span><span class="p">()</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
        </span><span class="n">distinct</span><span class="p">(</span><span class="n">.keep_all</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">TRUE</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
        </span><span class="n">filter</span><span class="p">(</span><span class="o">!</span><span class="n">str_detect</span><span class="p">(</span><span class="n">movie</span><span class="p">,</span><span class="w"> </span><span class="s2">"H\\$"</span><span class="p">))</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
        </span><span class="n">filter</span><span class="p">(</span><span class="o">!</span><span class="n">str_detect</span><span class="p">(</span><span class="n">symbol</span><span class="p">,</span><span class="s2">"\\."</span><span class="p">))</span><span class="w">
    
    </span><span class="nf">return</span><span class="p">(</span><span class="n">df</span><span class="p">)</span><span class="w">
</span><span class="p">}</span><span class="w">

</span><span class="n">meta_m</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">clean_meta_m</span><span class="p">(</span><span class="n">meta_m</span><span class="p">)</span><span class="w">
</span>

Join Tickers and Prices

<span class="n">tag_prices</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">tag_credits</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
    </span><span class="n">left_join</span><span class="p">(</span><span class="n">meta_m</span><span class="p">,</span><span class="w"> </span><span class="n">by</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"movie"</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
    </span><span class="n">mutate</span><span class="p">(</span><span class="n">date</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">as.Date</span><span class="p">(</span><span class="n">date</span><span class="p">))</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
    </span><span class="n">mutate</span><span class="p">(</span><span class="n">price</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">as.numeric</span><span class="p">(</span><span class="n">str_replace</span><span class="p">(</span><span class="n">price</span><span class="p">,</span><span class="w"> </span><span class="s2">"H\\$"</span><span class="p">,</span><span class="w"> </span><span class="s2">""</span><span class="p">)))</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
    </span><span class="n">group_by</span><span class="p">(</span><span class="n">movie</span><span class="p">,</span><span class="w"> </span><span class="n">symbol.x</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
    </span><span class="n">slice</span><span class="p">(</span><span class="n">which.max</span><span class="p">(</span><span class="n">price</span><span class="p">))</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
    </span><span class="n">select</span><span class="p">(</span><span class="n">date</span><span class="p">,</span><span class="w"> </span><span class="n">symbol</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">symbol.x</span><span class="p">,</span><span class="w"> </span><span class="n">movie</span><span class="p">,</span><span class="w"> </span><span class="n">price</span><span class="p">,</span><span class="w"> </span><span class="n">movie_idx</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
    </span><span class="c1"># correct minor mistakes for DYEN
</span><span class="w">    </span><span class="n">bind_rows</span><span class="p">(</span><span class="n">tribble</span><span class="p">(</span><span class="w">
        </span><span class="o">~</span><span class="n">date</span><span class="p">,</span><span class="w"> </span><span class="o">~</span><span class="n">symbol</span><span class="p">,</span><span class="w"> </span><span class="o">~</span><span class="n">movie</span><span class="p">,</span><span class="w"> </span><span class="o">~</span><span class="n">price</span><span class="p">,</span><span class="w"> </span><span class="o">~</span><span class="n">movie_idx</span><span class="p">,</span><span class="w"> 
        </span><span class="kc">NA</span><span class="p">,</span><span class="w"> </span><span class="s2">"DYEN"</span><span class="p">,</span><span class="w"> </span><span class="s2">"Legend of the Fist"</span><span class="p">,</span><span class="w"> </span><span class="m">0.05</span><span class="p">,</span><span class="w"> </span><span class="m">4</span><span class="p">,</span><span class="w">
        </span><span class="kc">NA</span><span class="p">,</span><span class="w"> </span><span class="s2">"DYEN"</span><span class="p">,</span><span class="w"> </span><span class="s2">"Ip Man 2"</span><span class="p">,</span><span class="w"> </span><span class="m">0.20</span><span class="p">,</span><span class="w"> </span><span class="m">5</span><span class="p">))</span><span class="w">
</span>

Cast Metadata Scraper

<span class="n">get_meta_a</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="k">function</span><span class="p">(</span><span class="n">actor</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w">

    </span><span class="n">page</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">read_html</span><span class="p">(</span><span class="n">str_c</span><span class="p">(</span><span class="n">URL</span><span class="p">,</span><span class="w"> </span><span class="n">actor</span><span class="p">))</span><span class="w">
    
    </span><span class="n">meta</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">page</span><span class="w"> </span><span class="o">%>%</span><span class="w">
        </span><span class="n">html_nodes</span><span class="p">(</span><span class="s2">".data_column td"</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w">
        </span><span class="n">html_text</span><span class="p">()</span><span class="w">
    
    </span><span class="n">meta_split</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">split</span><span class="p">(</span><span class="n">meta</span><span class="p">,</span><span class="w"> </span><span class="nf">ceiling</span><span class="p">(</span><span class="nf">seq_along</span><span class="p">(</span><span class="n">meta</span><span class="p">)</span><span class="o">/</span><span class="m">2</span><span class="p">))</span><span class="w">
    
    </span><span class="n">meta_df</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">tibble</span><span class="p">(</span><span class="n">meta</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">meta_split</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
        </span><span class="n">mutate</span><span class="p">(</span><span class="n">meta</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">as.character</span><span class="p">(</span><span class="n">meta</span><span class="p">))</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
        </span><span class="n">mutate</span><span class="p">(</span><span class="n">meta</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">str_replace_all</span><span class="p">(</span><span class="n">meta</span><span class="p">,</span><span class="w"> </span><span class="s1">'\\"|\\,|c\\(|\\)'</span><span class="p">,</span><span class="w"> </span><span class="s2">""</span><span class="p">))</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
        </span><span class="n">separate</span><span class="p">(</span><span class="n">meta</span><span class="p">,</span><span class="w"> </span><span class="n">into</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="s2">"meta"</span><span class="p">,</span><span class="w"> </span><span class="s2">"data"</span><span class="p">),</span><span class="w"> </span><span class="n">sep</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"\\:"</span><span class="p">)</span><span class="w">
    
    </span><span class="n">value</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">page</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
        </span><span class="n">html_nodes</span><span class="p">(</span><span class="s2">".value"</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
        </span><span class="n">html_text</span><span class="p">()</span><span class="w">
    
    </span><span class="n">df</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">meta_df</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
        </span><span class="n">spread</span><span class="p">(</span><span class="n">meta</span><span class="p">,</span><span class="w"> </span><span class="n">data</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w">
        </span><span class="n">mutate</span><span class="p">(</span><span class="n">trading_tag</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">value</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
        </span><span class="n">mutate</span><span class="p">(</span><span class="n">TAG</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">as.numeric</span><span class="p">(</span><span class="n">str_replace</span><span class="p">(</span><span class="n">TAG</span><span class="p">,</span><span class="w"> </span><span class="s2">"\\$"</span><span class="p">,</span><span class="w"> </span><span class="s2">""</span><span class="p">)))</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
        </span><span class="n">separate</span><span class="p">(</span><span class="n">trading_tag</span><span class="p">,</span><span class="w"> </span><span class="n">into</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="s2">"junk"</span><span class="p">,</span><span class="w"> </span><span class="s2">"trading_tag"</span><span class="p">),</span><span class="w"> 
        	</span><span class="n">sep</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"H\\$"</span><span class="p">,</span><span class="w"> </span><span class="n">extra</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"drop"</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
        </span><span class="n">mutate</span><span class="p">(</span><span class="n">trading_tag</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">as.numeric</span><span class="p">(</span><span class="n">trading_tag</span><span class="p">))</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
        </span><span class="n">mutate</span><span class="p">(</span><span class="n">actual_tag</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">round</span><span class="p">(</span><span class="nf">as.numeric</span><span class="p">(</span><span class="n">TAG</span><span class="p">)</span><span class="o">/</span><span class="m">1e6</span><span class="p">,</span><span class="m">2</span><span class="p">))</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
        </span><span class="n">select</span><span class="p">(</span><span class="n">symbol</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Symbol</span><span class="p">,</span><span class="w"> </span><span class="n">trading_tag</span><span class="p">,</span><span class="w"> </span><span class="n">actual_tag</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
        </span><span class="n">mutate</span><span class="p">(</span><span class="n">symbol</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">str_replace_all</span><span class="p">(</span><span class="n">symbol</span><span class="p">,</span><span class="w"> </span><span class="s2">"\\s"</span><span class="p">,</span><span class="w"> </span><span class="s2">""</span><span class="p">))</span><span class="w">
    
    </span><span class="nf">return</span><span class="p">(</span><span class="n">df</span><span class="p">)</span><span class="w">
</span><span class="p">}</span><span class="w">

</span><span class="n">meta_a</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">map</span><span class="p">(</span><span class="n">cast</span><span class="o">$</span><span class="n">symbol</span><span class="p">,</span><span class="w"> </span><span class="n">get_meta_a</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="n">bind_rows</span><span class="p">()</span><span class="w">
</span>

TAG Forecasts

<span class="n">tag_new</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">tag_prices</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
    </span><span class="n">filter</span><span class="p">(</span><span class="o">!</span><span class="p">(</span><span class="n">movie_idx</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="m">5</span><span class="p">))</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
    </span><span class="n">group_by</span><span class="p">(</span><span class="n">symbol</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
    </span><span class="n">summarise</span><span class="p">(</span><span class="n">total</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">sum</span><span class="p">(</span><span class="n">price</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="m">440</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
    </span><span class="n">mutate</span><span class="p">(</span><span class="n">tag_forecast</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">total</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="m">5</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
    </span><span class="n">select</span><span class="p">(</span><span class="o">-</span><span class="n">total</span><span class="p">)</span><span class="w">
</span>

Calculate Arbitrage Opportunities

<span class="n">arbitrage</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">meta_a</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
    </span><span class="n">left_join</span><span class="p">(</span><span class="n">tag_new</span><span class="p">,</span><span class="w"> </span><span class="n">by</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"symbol"</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
    </span><span class="n">mutate</span><span class="p">(</span><span class="n">investment</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">20000</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">trading_tag</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
    </span><span class="n">mutate</span><span class="p">(</span><span class="n">return</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ifelse</span><span class="p">(</span><span class="n">tag_forecast</span><span class="w"> </span><span class="o">>=</span><span class="w"> </span><span class="n">trading_tag</span><span class="p">,</span><span class="w"> 
    	</span><span class="p">(</span><span class="m">20000</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">tag_forecast</span><span class="p">),</span><span class="w"> </span><span class="p">(</span><span class="m">20000</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">tag_forecast</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="m">-1</span><span class="p">)))</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
    </span><span class="n">mutate</span><span class="p">(</span><span class="n">roi</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">return</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="n">investment</span><span class="p">)</span><span class="w">
</span>

Punch Line

Ha! I knew it. Arbitrage Galore. Just look at Mads. He’s trading at $43.54 right now. But when Rogue One gets added he’s going to pop to $129.28. But it’s not just Mads. The entire top billed cast of Rogue One is chronincally undervalued right now, with Donnie Yen (DYEN) at the extreme end. Basically an investment in DYEN could yield a return of ~5.5X. Damn, I really wish this wasn’t just fake money…

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

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)