mirror of
https://github.com/bspeice/bspeice.github.io
synced 2024-10-10 20:21:30 -04:00
Fix notebooks not showing up
This commit is contained in:
parent
3338b5b0ad
commit
fc491198aa
@ -4,20 +4,22 @@
|
||||
<meta charset="utf-8">
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<meta name="description" content="I listen to a lot of Drum and Bass music, because it's beautiful music. And there's a particular site, Bassdrive.com that hosts a lot of great content. Specifically, the archives section of the...">
|
||||
<meta name="description" content="I listen to a lot of Drum and Bass music, because it's beautiful music. And there's a particular site, Bassdrive.com that hosts a lot of great content. Specifically, the archives section of the ...">
|
||||
<meta name="keywords" content="nutone, Rust">
|
||||
<link rel="icon" href="/favicon.ico">
|
||||
<link rel="icon" href="https://bspeice.github.io/favicon.ico">
|
||||
|
||||
<title>A Rustic Re-Podcasting Server (Part 1) - Bradlee Speice</title>
|
||||
|
||||
<!-- Stylesheets -->
|
||||
<link href="/theme/css/bootstrap.min.css" rel="stylesheet">
|
||||
<link href="/theme/css/fonts.css" rel="stylesheet">
|
||||
<link href="/theme/css/nest.css" rel="stylesheet">
|
||||
<link href="/theme/css/pygment.css" rel="stylesheet">
|
||||
<link href="https://bspeice.github.io/theme/css/bootstrap.min.css" rel="stylesheet">
|
||||
<link href="https://bspeice.github.io/theme/css/fonts.css" rel="stylesheet">
|
||||
<link href="https://bspeice.github.io/theme/css/nest.css" rel="stylesheet">
|
||||
<link href="https://bspeice.github.io/theme/css/pygment.css" rel="stylesheet">
|
||||
<!-- /Stylesheets -->
|
||||
|
||||
<!-- RSS Feeds -->
|
||||
<link href="https://bspeice.github.io/feeds/all.atom.xml" type="application/atom+xml" rel="alternate" title="Bradlee Speice Full Atom Feed" />
|
||||
<link href="https://bspeice.github.io/feeds/blog.atom.xml" type="application/atom+xml" rel="alternate" title="Bradlee Speice Categories Atom Feed" />
|
||||
<!-- /RSS Feeds -->
|
||||
|
||||
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
|
||||
@ -26,6 +28,17 @@
|
||||
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
|
||||
<![endif]-->
|
||||
|
||||
<!-- Google Analytics -->
|
||||
<script>
|
||||
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
|
||||
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
|
||||
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
|
||||
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
|
||||
|
||||
ga('create', 'UA-74711362-1', 'auto');
|
||||
ga('send', 'pageview');
|
||||
</script>
|
||||
<!-- /Google Analytics -->
|
||||
|
||||
|
||||
</head>
|
||||
@ -39,7 +52,7 @@
|
||||
<div class="container">
|
||||
<div class="header-nav">
|
||||
<div class="header-logo">
|
||||
<a class="pull-left" href="/"><img class="mr20" src="/images/logo.svg" alt="logo">Bradlee Speice</a>
|
||||
<a class="pull-left" href="https://bspeice.github.io/"><img class="mr20" src="https://bspeice.github.io/images/logo.svg" alt="logo">Bradlee Speice</a>
|
||||
</div>
|
||||
<div class="nav pull-right">
|
||||
</div>
|
||||
@ -54,12 +67,12 @@
|
||||
<div class="col-lg-12">
|
||||
<div class="header-content">
|
||||
<h1 class="header-title">A Rustic Re-Podcasting Server (Part 1)</h1>
|
||||
<p class="header-date"> <a href="/author/bradlee-speice.html">Bradlee Speice</a>, Sat 22 October 2016, <a href="/category/blog.html">Blog</a></p>
|
||||
<p class="header-date"> <a href="https://bspeice.github.io/author/bradlee-speice.html">Bradlee Speice</a>, Sat 22 October 2016, <a href="https://bspeice.github.io/category/blog.html">Blog</a></p>
|
||||
<div class="header-underline"></div>
|
||||
<div class="clearfix"></div>
|
||||
<p class="pull-right header-tags">
|
||||
<span class="glyphicon glyphicon-tags mr5" aria-hidden="true"></span>
|
||||
<a href="/tag/nutone.html">nutone</a>, <a href="/tag/rust.html">Rust</a> </p>
|
||||
<a href="https://bspeice.github.io/tag/nutone.html">nutone</a>, <a href="https://bspeice.github.io/tag/rust.html">Rust</a> </p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@ -115,15 +128,15 @@ on some bad cases, <code>str <-> bytes</code> specifically), but Rust is h
|
||||
should be incredibly simple: All I want is to echo back
|
||||
<code>Didn't find URL: <url></code>. Shouldn't be that hard right? In Python I'd just do
|
||||
something like:</p>
|
||||
<div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">echo_handler</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
|
||||
<div class="highlight"><pre><span class="k">def</span> <span class="nf">echo_handler</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
|
||||
<span class="k">return</span> <span class="s2">"You're visiting: {}"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">request</span><span class="o">.</span><span class="n">uri</span><span class="p">)</span>
|
||||
</pre></div>
|
||||
|
||||
|
||||
<p>And we'd call it a day. Rust isn't so simple. Let's start with the trivial
|
||||
examples people post online:</p>
|
||||
<div class="highlight"><pre><span></span><span class="k">fn</span> <span class="nf">hello_world</span><span class="p">(</span><span class="n">req</span>: <span class="kp">&</span><span class="nc">mut</span><span class="w"> </span><span class="n">Request</span><span class="p">)</span><span class="w"> </span>-> <span class="nc">IronResult</span><span class="o"><</span><span class="n">Response</span><span class="o">></span><span class="w"> </span><span class="p">{</span><span class="w"></span>
|
||||
<span class="w"> </span><span class="nb">Ok</span><span class="p">(</span><span class="n">Response</span>::<span class="n">with</span><span class="p">((</span><span class="n">status</span>::<span class="nb">Ok</span><span class="p">,</span><span class="w"> </span><span class="s">"You found the server!"</span><span class="p">)))</span><span class="w"></span>
|
||||
<div class="highlight"><pre><span class="k">fn</span><span class="w"> </span><span class="n">hello_world</span><span class="p">(</span><span class="n">req</span><span class="o">:</span><span class="w"> </span><span class="o">&</span><span class="k">mut</span><span class="w"> </span><span class="n">Request</span><span class="p">)</span><span class="w"> </span><span class="o">-></span><span class="w"> </span><span class="n">IronResult</span><span class="o"><</span><span class="n">Response</span><span class="o">></span><span class="w"> </span><span class="p">{</span><span class="w"></span>
|
||||
<span class="w"> </span><span class="nb">Ok</span><span class="p">(</span><span class="n">Response</span><span class="o">::</span><span class="n">with</span><span class="p">((</span><span class="n">status</span><span class="o">::</span><span class="nb">Ok</span><span class="p">,</span><span class="w"> </span><span class="s">"You found the server!"</span><span class="p">)))</span><span class="w"></span>
|
||||
<span class="p">}</span><span class="w"></span>
|
||||
</pre></div>
|
||||
|
||||
@ -132,22 +145,22 @@ examples people post online:</p>
|
||||
version! All we need to do is just send back a string of some form. So, we
|
||||
look up the documentation for <a href="http://ironframework.io/doc/iron/request/struct.Request.html"><code>Request</code></a> and see a <code>url</code> field that will contain
|
||||
what we want. Let's try the first iteration:</p>
|
||||
<div class="highlight"><pre><span></span><span class="k">fn</span> <span class="nf">hello_world</span><span class="p">(</span><span class="n">req</span>: <span class="kp">&</span><span class="nc">mut</span><span class="w"> </span><span class="n">Request</span><span class="p">)</span><span class="w"> </span>-> <span class="nc">IronResult</span><span class="o"><</span><span class="n">Response</span><span class="o">></span><span class="w"> </span><span class="p">{</span><span class="w"></span>
|
||||
<span class="w"> </span><span class="nb">Ok</span><span class="p">(</span><span class="n">Response</span>::<span class="n">with</span><span class="p">((</span><span class="n">status</span>::<span class="nb">Ok</span><span class="p">,</span><span class="w"> </span><span class="s">"You found the URL: "</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">req</span><span class="p">.</span><span class="n">url</span><span class="p">)))</span><span class="w"></span>
|
||||
<div class="highlight"><pre><span class="k">fn</span><span class="w"> </span><span class="n">hello_world</span><span class="p">(</span><span class="n">req</span><span class="o">:</span><span class="w"> </span><span class="o">&</span><span class="k">mut</span><span class="w"> </span><span class="n">Request</span><span class="p">)</span><span class="w"> </span><span class="o">-></span><span class="w"> </span><span class="n">IronResult</span><span class="o"><</span><span class="n">Response</span><span class="o">></span><span class="w"> </span><span class="p">{</span><span class="w"></span>
|
||||
<span class="w"> </span><span class="nb">Ok</span><span class="p">(</span><span class="n">Response</span><span class="o">::</span><span class="n">with</span><span class="p">((</span><span class="n">status</span><span class="o">::</span><span class="nb">Ok</span><span class="p">,</span><span class="w"> </span><span class="s">"You found the URL: "</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">req</span><span class="p">.</span><span class="n">url</span><span class="p">)))</span><span class="w"></span>
|
||||
<span class="p">}</span><span class="w"></span>
|
||||
</pre></div>
|
||||
|
||||
|
||||
<p>Which yields the error:</p>
|
||||
<div class="highlight"><pre><span></span>error[E0369]: binary operation `+` cannot be applied to type `&'static str`
|
||||
<div class="highlight"><pre>error[E0369]: binary operation `+` cannot be applied to type `&'static str`
|
||||
</pre></div>
|
||||
|
||||
|
||||
<p>OK, what's going on here? Time to start Googling for <a href="https://www.google.com/#q=concatenate+strings+in+rust">"concatenate strings in Rust"</a>. That's what we
|
||||
want to do right? Concatenate a static string and the URL.</p>
|
||||
<p>After Googling, we come across a helpful <a href="https://doc.rust-lang.org/std/macro.concat!.html"><code>concat!</code></a> macro that looks really nice! Let's try that one:</p>
|
||||
<div class="highlight"><pre><span></span><span class="k">fn</span> <span class="nf">hello_world</span><span class="p">(</span><span class="n">req</span>: <span class="kp">&</span><span class="nc">mut</span><span class="w"> </span><span class="n">Request</span><span class="p">)</span><span class="w"> </span>-> <span class="nc">IronResult</span><span class="o"><</span><span class="n">Response</span><span class="o">></span><span class="w"> </span><span class="p">{</span><span class="w"></span>
|
||||
<span class="w"> </span><span class="nb">Ok</span><span class="p">(</span><span class="n">Response</span>::<span class="n">with</span><span class="p">((</span><span class="n">status</span>::<span class="nb">Ok</span><span class="p">,</span><span class="w"> </span><span class="n">concat</span><span class="o">!</span><span class="p">(</span><span class="s">"You found the URL: "</span><span class="p">,</span><span class="w"> </span><span class="n">req</span><span class="p">.</span><span class="n">url</span><span class="p">))))</span><span class="w"></span>
|
||||
<div class="highlight"><pre><span class="k">fn</span><span class="w"> </span><span class="n">hello_world</span><span class="p">(</span><span class="n">req</span><span class="o">:</span><span class="w"> </span><span class="o">&</span><span class="k">mut</span><span class="w"> </span><span class="n">Request</span><span class="p">)</span><span class="w"> </span><span class="o">-></span><span class="w"> </span><span class="n">IronResult</span><span class="o"><</span><span class="n">Response</span><span class="o">></span><span class="w"> </span><span class="p">{</span><span class="w"></span>
|
||||
<span class="w"> </span><span class="nb">Ok</span><span class="p">(</span><span class="n">Response</span><span class="o">::</span><span class="n">with</span><span class="p">((</span><span class="n">status</span><span class="o">::</span><span class="nb">Ok</span><span class="p">,</span><span class="w"> </span><span class="n">concat</span><span class="o">!</span><span class="p">(</span><span class="s">"You found the URL: "</span><span class="p">,</span><span class="w"> </span><span class="n">req</span><span class="p">.</span><span class="n">url</span><span class="p">))))</span><span class="w"></span>
|
||||
<span class="p">}</span><span class="w"></span>
|
||||
</pre></div>
|
||||
|
||||
@ -159,8 +172,8 @@ at compile time what <code>req.url</code> is. Which, in my outsider opinion, is
|
||||
strange. <code>println!</code> and <code>format!</code>, etc., all handle values they don't know at
|
||||
compile time. Why can't <code>concat!</code>? By any means, we need a new plan of attack.
|
||||
How about we try formatting strings?</p>
|
||||
<div class="highlight"><pre><span></span><span class="k">fn</span> <span class="nf">hello_world</span><span class="p">(</span><span class="n">req</span>: <span class="kp">&</span><span class="nc">mut</span><span class="w"> </span><span class="n">Request</span><span class="p">)</span><span class="w"> </span>-> <span class="nc">IronResult</span><span class="o"><</span><span class="n">Response</span><span class="o">></span><span class="w"> </span><span class="p">{</span><span class="w"></span>
|
||||
<span class="w"> </span><span class="nb">Ok</span><span class="p">(</span><span class="n">Response</span>::<span class="n">with</span><span class="p">((</span><span class="n">status</span>::<span class="nb">Ok</span><span class="p">,</span><span class="w"> </span><span class="n">format</span><span class="o">!</span><span class="p">(</span><span class="s">"You found the URL: {}"</span><span class="p">,</span><span class="w"> </span><span class="n">req</span><span class="p">.</span><span class="n">url</span><span class="p">))))</span><span class="w"></span>
|
||||
<div class="highlight"><pre><span class="k">fn</span><span class="w"> </span><span class="n">hello_world</span><span class="p">(</span><span class="n">req</span><span class="o">:</span><span class="w"> </span><span class="o">&</span><span class="k">mut</span><span class="w"> </span><span class="n">Request</span><span class="p">)</span><span class="w"> </span><span class="o">-></span><span class="w"> </span><span class="n">IronResult</span><span class="o"><</span><span class="n">Response</span><span class="o">></span><span class="w"> </span><span class="p">{</span><span class="w"></span>
|
||||
<span class="w"> </span><span class="nb">Ok</span><span class="p">(</span><span class="n">Response</span><span class="o">::</span><span class="n">with</span><span class="p">((</span><span class="n">status</span><span class="o">::</span><span class="nb">Ok</span><span class="p">,</span><span class="w"> </span><span class="n">format</span><span class="o">!</span><span class="p">(</span><span class="s">"You found the URL: {}"</span><span class="p">,</span><span class="w"> </span><span class="n">req</span><span class="p">.</span><span class="n">url</span><span class="p">))))</span><span class="w"></span>
|
||||
<span class="p">}</span><span class="w"></span>
|
||||
</pre></div>
|
||||
|
||||
@ -191,9 +204,9 @@ working on things that are a bit more complex?</p>
|
||||
<p>We're going to cover that here. Our first try: creating a function which returns
|
||||
other functions. This is a principle called <a href="http://stackoverflow.com/a/36321/1454178">currying</a>. We set up a function that allows us to keep some data in scope
|
||||
for another function to come later.</p>
|
||||
<div class="highlight"><pre><span></span><span class="k">fn</span> <span class="nf">build_handler</span><span class="p">(</span><span class="n">message</span>: <span class="nb">String</span><span class="p">)</span><span class="w"> </span>-> <span class="nb">Fn</span><span class="p">(</span><span class="o">&</span><span class="k">mut</span><span class="w"> </span><span class="n">Request</span><span class="p">)</span><span class="w"> </span>-> <span class="nc">IronResult</span><span class="o"><</span><span class="n">Response</span><span class="o">></span><span class="w"> </span><span class="p">{</span><span class="w"></span>
|
||||
<span class="w"> </span><span class="k">move</span><span class="w"> </span><span class="o">|</span><span class="n">_</span>: <span class="kp">&</span><span class="nc">mut</span><span class="w"> </span><span class="n">Request</span><span class="o">|</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
|
||||
<span class="w"> </span><span class="nb">Ok</span><span class="p">(</span><span class="n">Response</span>::<span class="n">with</span><span class="p">((</span><span class="n">status</span>::<span class="nb">Ok</span><span class="p">,</span><span class="w"> </span><span class="n">message</span><span class="p">)))</span><span class="w"></span>
|
||||
<div class="highlight"><pre><span class="k">fn</span><span class="w"> </span><span class="n">build_handler</span><span class="p">(</span><span class="n">message</span><span class="o">:</span><span class="w"> </span><span class="nb">String</span><span class="p">)</span><span class="w"> </span><span class="o">-></span><span class="w"> </span><span class="nb">Fn</span><span class="p">(</span><span class="o">&</span><span class="k">mut</span><span class="w"> </span><span class="n">Request</span><span class="p">)</span><span class="w"> </span><span class="o">-></span><span class="w"> </span><span class="n">IronResult</span><span class="o"><</span><span class="n">Response</span><span class="o">></span><span class="w"> </span><span class="p">{</span><span class="w"></span>
|
||||
<span class="w"> </span><span class="n">move</span><span class="w"> </span><span class="o">|</span><span class="n">_</span><span class="o">:</span><span class="w"> </span><span class="o">&</span><span class="k">mut</span><span class="w"> </span><span class="n">Request</span><span class="o">|</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
|
||||
<span class="w"> </span><span class="nb">Ok</span><span class="p">(</span><span class="n">Response</span><span class="o">::</span><span class="n">with</span><span class="p">((</span><span class="n">status</span><span class="o">::</span><span class="nb">Ok</span><span class="p">,</span><span class="w"> </span><span class="n">message</span><span class="p">)))</span><span class="w"></span>
|
||||
<span class="w"> </span><span class="p">}</span><span class="w"></span>
|
||||
<span class="p">}</span><span class="w"></span>
|
||||
</pre></div>
|
||||
@ -202,7 +215,7 @@ for another function to come later.</p>
|
||||
<p>We've simply set up a function that returns another anonymous function with the
|
||||
<code>message</code> parameter scoped in. If you compile this, you get not 1, not 2, but 5
|
||||
new errors. 4 of them are the same though:</p>
|
||||
<div class="highlight"><pre><span></span>error[E0277]: the trait bound `for<'r, 'r, 'r> std::ops::Fn(&'r mut iron::Request<'r, 'r>) -> std::result::Result<iron::Response, iron::IronError> + 'static: std::marker::Sized` is not satisfied
|
||||
<div class="highlight"><pre>error[E0277]: the trait bound `for<'r, 'r, 'r> std::ops::Fn(&'r mut iron::Request<'r, 'r>) -> std::result::Result<iron::Response, iron::IronError> + 'static: std::marker::Sized` is not satisfied
|
||||
</pre></div>
|
||||
|
||||
|
||||
@ -230,19 +243,19 @@ we've been working with so far.</p>
|
||||
<p>The principle is that we need to define a new <code>struct</code> to hold our data, then
|
||||
implement that <code>handle()</code> method to return the result. Something that looks
|
||||
like this might do:</p>
|
||||
<div class="highlight"><pre><span></span><span class="k">struct</span> <span class="nc">EchoHandler</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
|
||||
<span class="w"> </span><span class="n">message</span>: <span class="nb">String</span>
|
||||
<div class="highlight"><pre><span class="k">struct</span><span class="w"> </span><span class="n">EchoHandler</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
|
||||
<span class="w"> </span><span class="n">message</span><span class="o">:</span><span class="w"> </span><span class="nb">String</span><span class="w"></span>
|
||||
<span class="p">}</span><span class="w"></span>
|
||||
|
||||
<span class="k">impl</span><span class="w"> </span><span class="n">Handler</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">EchoHandler</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
|
||||
<span class="w"> </span><span class="k">fn</span> <span class="nf">handle</span><span class="p">(</span><span class="o">&</span><span class="bp">self</span><span class="p">,</span><span class="w"> </span><span class="n">_</span>: <span class="kp">&</span><span class="nc">mut</span><span class="w"> </span><span class="n">Request</span><span class="p">)</span><span class="w"> </span>-> <span class="nc">IronResult</span><span class="o"><</span><span class="n">Response</span><span class="o">></span><span class="w"> </span><span class="p">{</span><span class="w"></span>
|
||||
<span class="w"> </span><span class="nb">Ok</span><span class="p">(</span><span class="n">Response</span>::<span class="n">with</span><span class="p">((</span><span class="n">status</span>::<span class="nb">Ok</span><span class="p">,</span><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="n">message</span><span class="p">)))</span><span class="w"></span>
|
||||
<span class="w"> </span><span class="k">fn</span><span class="w"> </span><span class="n">handle</span><span class="p">(</span><span class="o">&</span><span class="bp">self</span><span class="p">,</span><span class="w"> </span><span class="n">_</span><span class="o">:</span><span class="w"> </span><span class="o">&</span><span class="k">mut</span><span class="w"> </span><span class="n">Request</span><span class="p">)</span><span class="w"> </span><span class="o">-></span><span class="w"> </span><span class="n">IronResult</span><span class="o"><</span><span class="n">Response</span><span class="o">></span><span class="w"> </span><span class="p">{</span><span class="w"></span>
|
||||
<span class="w"> </span><span class="nb">Ok</span><span class="p">(</span><span class="n">Response</span><span class="o">::</span><span class="n">with</span><span class="p">((</span><span class="n">status</span><span class="o">::</span><span class="nb">Ok</span><span class="p">,</span><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="n">message</span><span class="p">)))</span><span class="w"></span>
|
||||
<span class="w"> </span><span class="p">}</span><span class="w"></span>
|
||||
<span class="p">}</span><span class="w"></span>
|
||||
|
||||
<span class="c1">// Later in the code when we set up the router...</span>
|
||||
<span class="kd">let</span><span class="w"> </span><span class="n">echo</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">EchoHandler</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
|
||||
<span class="w"> </span><span class="n">message</span>: <span class="s">"Is it working yet?"</span><span class="w"></span>
|
||||
<span class="w"> </span><span class="n">message</span><span class="o">:</span><span class="w"> </span><span class="s">"Is it working yet?"</span><span class="w"></span>
|
||||
<span class="p">}</span><span class="w"></span>
|
||||
<span class="n">router</span><span class="p">.</span><span class="n">get</span><span class="p">(</span><span class="s">"/"</span><span class="p">,</span><span class="w"> </span><span class="n">echo</span><span class="p">.</span><span class="n">handle</span><span class="p">,</span><span class="w"> </span><span class="s">"index"</span><span class="p">);</span><span class="w"></span>
|
||||
</pre></div>
|
||||
@ -251,38 +264,38 @@ like this might do:</p>
|
||||
<p>We attempt to build a struct, and give its <code>handle</code> method off to the router
|
||||
so the router knows what to do.</p>
|
||||
<p>You guessed it, more errors:</p>
|
||||
<div class="highlight"><pre><span></span><span class="n">error</span><span class="o">:</span> <span class="n">attempted</span> <span class="n">to</span> <span class="n">take</span> <span class="n">value</span> <span class="n">of</span> <span class="n">method</span> <span class="err">`</span><span class="n">handle</span><span class="err">`</span> <span class="n">on</span> <span class="n">type</span> <span class="err">`</span><span class="n">EchoHandler</span><span class="err">`</span>
|
||||
<div class="highlight"><pre><span class="n">error</span><span class="o">:</span> <span class="n">attempted</span> <span class="n">to</span> <span class="n">take</span> <span class="n">value</span> <span class="n">of</span> <span class="n">method</span> <span class="err">`</span><span class="n">handle</span><span class="err">`</span> <span class="n">on</span> <span class="n">type</span> <span class="err">`</span><span class="n">EchoHandler</span><span class="err">`</span>
|
||||
</pre></div>
|
||||
|
||||
|
||||
<p>Now, the Rust compiler is actually a really nice fellow, and offers us help:</p>
|
||||
<div class="highlight"><pre><span></span><span class="n">help</span><span class="o">:</span> <span class="n">maybe</span> <span class="n">a</span> <span class="err">`</span><span class="o">()</span><span class="err">`</span> <span class="n">to</span> <span class="n">call</span> <span class="n">it</span> <span class="k">is</span> <span class="n">missing</span><span class="o">?</span> <span class="n">If</span> <span class="n">not</span><span class="o">,</span> <span class="k">try</span> <span class="n">an</span> <span class="n">anonymous</span> <span class="kd">function</span>
|
||||
<div class="highlight"><pre><span class="n">help</span><span class="o">:</span> <span class="n">maybe</span> <span class="n">a</span> <span class="err">`</span><span class="o">()</span><span class="err">`</span> <span class="n">to</span> <span class="n">call</span> <span class="n">it</span> <span class="k">is</span> <span class="n">missing</span><span class="o">?</span> <span class="n">If</span> <span class="n">not</span><span class="o">,</span> <span class="k">try</span> <span class="n">an</span> <span class="n">anonymous</span> <span class="kd">function</span>
|
||||
</pre></div>
|
||||
|
||||
|
||||
<p>We definitely don't want to call that function, so maybe try an anonymous
|
||||
function as it recommends?</p>
|
||||
<div class="highlight"><pre><span></span><span class="n">router</span><span class="p">.</span><span class="n">get</span><span class="p">(</span><span class="s">"/"</span><span class="p">,</span><span class="w"> </span><span class="o">|</span><span class="n">req</span>: <span class="kp">&</span><span class="nc">mut</span><span class="w"> </span><span class="n">Request</span><span class="o">|</span><span class="w"> </span><span class="n">echo</span><span class="p">.</span><span class="n">handle</span><span class="p">(</span><span class="n">req</span><span class="p">),</span><span class="w"> </span><span class="s">"index"</span><span class="p">);</span><span class="w"></span>
|
||||
<div class="highlight"><pre><span class="n">router</span><span class="p">.</span><span class="n">get</span><span class="p">(</span><span class="s">"/"</span><span class="p">,</span><span class="w"> </span><span class="o">|</span><span class="n">req</span><span class="o">:</span><span class="w"> </span><span class="o">&</span><span class="k">mut</span><span class="w"> </span><span class="n">Request</span><span class="o">|</span><span class="w"> </span><span class="n">echo</span><span class="p">.</span><span class="n">handle</span><span class="p">(</span><span class="n">req</span><span class="p">),</span><span class="w"> </span><span class="s">"index"</span><span class="p">);</span><span class="w"></span>
|
||||
</pre></div>
|
||||
|
||||
|
||||
<p>Another error:</p>
|
||||
<div class="highlight"><pre><span></span>error[E0373]: closure may outlive the current function, but it borrows `echo`, which is owned by the current function
|
||||
<div class="highlight"><pre>error[E0373]: closure may outlive the current function, but it borrows `echo`, which is owned by the current function
|
||||
</pre></div>
|
||||
|
||||
|
||||
<p>Another helpful message:</p>
|
||||
<div class="highlight"><pre><span></span><span class="n">help</span><span class="o">:</span> <span class="n">to</span> <span class="n">force</span> <span class="n">the</span> <span class="n">closure</span> <span class="n">to</span> <span class="n">take</span> <span class="n">ownership</span> <span class="n">of</span> <span class="err">`</span><span class="n">echo</span><span class="err">`</span> <span class="o">(</span><span class="n">and</span> <span class="n">any</span> <span class="n">other</span> <span class="n">referenced</span> <span class="n">variables</span><span class="o">),</span> <span class="n">use</span> <span class="n">the</span> <span class="err">`</span><span class="n">move</span><span class="err">`</span> <span class="n">keyword</span>
|
||||
<div class="highlight"><pre><span class="n">help</span><span class="o">:</span> <span class="n">to</span> <span class="n">force</span> <span class="n">the</span> <span class="n">closure</span> <span class="n">to</span> <span class="n">take</span> <span class="n">ownership</span> <span class="n">of</span> <span class="err">`</span><span class="n">echo</span><span class="err">`</span> <span class="o">(</span><span class="n">and</span> <span class="n">any</span> <span class="n">other</span> <span class="n">referenced</span> <span class="n">variables</span><span class="o">),</span> <span class="n">use</span> <span class="n">the</span> <span class="err">`</span><span class="n">move</span><span class="err">`</span> <span class="n">keyword</span>
|
||||
</pre></div>
|
||||
|
||||
|
||||
<p>We're getting closer though! Let's implement this change:</p>
|
||||
<div class="highlight"><pre><span></span><span class="n">router</span><span class="p">.</span><span class="n">get</span><span class="p">(</span><span class="s">"/"</span><span class="p">,</span><span class="w"> </span><span class="k">move</span><span class="w"> </span><span class="o">|</span><span class="n">req</span>: <span class="kp">&</span><span class="nc">mut</span><span class="w"> </span><span class="n">Request</span><span class="o">|</span><span class="w"> </span><span class="n">echo</span><span class="p">.</span><span class="n">handle</span><span class="p">(</span><span class="n">req</span><span class="p">),</span><span class="w"> </span><span class="s">"index"</span><span class="p">);</span><span class="w"></span>
|
||||
<div class="highlight"><pre><span class="n">router</span><span class="p">.</span><span class="n">get</span><span class="p">(</span><span class="s">"/"</span><span class="p">,</span><span class="w"> </span><span class="n">move</span><span class="w"> </span><span class="o">|</span><span class="n">req</span><span class="o">:</span><span class="w"> </span><span class="o">&</span><span class="k">mut</span><span class="w"> </span><span class="n">Request</span><span class="o">|</span><span class="w"> </span><span class="n">echo</span><span class="p">.</span><span class="n">handle</span><span class="p">(</span><span class="n">req</span><span class="p">),</span><span class="w"> </span><span class="s">"index"</span><span class="p">);</span><span class="w"></span>
|
||||
</pre></div>
|
||||
|
||||
|
||||
<p>And here's where things get strange:</p>
|
||||
<div class="highlight"><pre><span></span>error[E0507]: cannot move out of borrowed content
|
||||
<div class="highlight"><pre>error[E0507]: cannot move out of borrowed content
|
||||
--> src/main.rs:18:40
|
||||
|
|
||||
18 | Ok(Response::with((status::Ok, self.message)))
|
||||
@ -307,7 +320,7 @@ instead of transferring ownership</li>
|
||||
audience out. Because <code>iron</code> won't accept a reference, we are forced into the
|
||||
second option: making a copy. To do so, we just need to change the function
|
||||
to look like this:</p>
|
||||
<div class="highlight"><pre><span></span><span class="nb">Ok</span><span class="p">(</span><span class="n">Response</span>::<span class="n">with</span><span class="p">((</span><span class="n">status</span>::<span class="nb">Ok</span><span class="p">,</span><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="n">message</span><span class="p">.</span><span class="n">clone</span><span class="p">())))</span><span class="w"></span>
|
||||
<div class="highlight"><pre><span class="nb">Ok</span><span class="p">(</span><span class="n">Response</span><span class="o">::</span><span class="n">with</span><span class="p">((</span><span class="n">status</span><span class="o">::</span><span class="nb">Ok</span><span class="p">,</span><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="n">message</span><span class="p">.</span><span class="n">clone</span><span class="p">())))</span><span class="w"></span>
|
||||
</pre></div>
|
||||
|
||||
|
||||
@ -343,6 +356,20 @@ incredibly precise about how I use it.</p>
|
||||
going to take me a lot longer to do this than I originally thought.</p>
|
||||
|
||||
|
||||
<div class="comments">
|
||||
<div id="disqus_thread"></div>
|
||||
<script type="text/javascript">
|
||||
var disqus_shortname = 'bradleespeice';
|
||||
var disqus_identifier = 'a-rustic-re-podcasting-server-part-1.html';
|
||||
var disqus_url = 'https://bspeice.github.io/a-rustic-re-podcasting-server-part-1.html';
|
||||
(function() {
|
||||
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
|
||||
dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
|
||||
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
|
||||
})();
|
||||
</script>
|
||||
<noscript>Please enable JavaScript to view the comments.</noscript>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
<!-- /Content -->
|
||||
@ -354,6 +381,7 @@ going to take me a lot longer to do this than I originally thought.</p>
|
||||
<div class="col-xs-4 col-sm-3 col-md-3 col-lg-3">
|
||||
<div class="footer-title"></div>
|
||||
<ul class="list-unstyled">
|
||||
<li><a href="https://bspeice.github.io/feeds/all.atom.xml" type="application/atom+xml" rel="alternate"></a></li>
|
||||
</ul>
|
||||
</div>
|
||||
<div class="col-xs-4 col-sm-3 col-md-3 col-lg-3">
|
||||
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load Diff
@ -4,20 +4,22 @@
|
||||
<meta charset="utf-8">
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<meta name="description" content="{% notebook 2016-2-3-guaranteed-money-maker.ipynb %} MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$'], ['\(','\)']]}});">
|
||||
<meta name="description" content="If you can see into the future, that is.¶My previous class in Stochastic Calculus covered a lot of interesting topics, and the important one for today is the Gambler's Ruin problem. If you're ...">
|
||||
<meta name="keywords" content="martingale, strategy">
|
||||
<link rel="icon" href="/favicon.ico">
|
||||
<link rel="icon" href="https://bspeice.github.io/favicon.ico">
|
||||
|
||||
<title>Guaranteed Money Maker - Bradlee Speice</title>
|
||||
|
||||
<!-- Stylesheets -->
|
||||
<link href="/theme/css/bootstrap.min.css" rel="stylesheet">
|
||||
<link href="/theme/css/fonts.css" rel="stylesheet">
|
||||
<link href="/theme/css/nest.css" rel="stylesheet">
|
||||
<link href="/theme/css/pygment.css" rel="stylesheet">
|
||||
<link href="https://bspeice.github.io/theme/css/bootstrap.min.css" rel="stylesheet">
|
||||
<link href="https://bspeice.github.io/theme/css/fonts.css" rel="stylesheet">
|
||||
<link href="https://bspeice.github.io/theme/css/nest.css" rel="stylesheet">
|
||||
<link href="https://bspeice.github.io/theme/css/pygment.css" rel="stylesheet">
|
||||
<!-- /Stylesheets -->
|
||||
|
||||
<!-- RSS Feeds -->
|
||||
<link href="https://bspeice.github.io/feeds/all.atom.xml" type="application/atom+xml" rel="alternate" title="Bradlee Speice Full Atom Feed" />
|
||||
<link href="https://bspeice.github.io/feeds/blog.atom.xml" type="application/atom+xml" rel="alternate" title="Bradlee Speice Categories Atom Feed" />
|
||||
<!-- /RSS Feeds -->
|
||||
|
||||
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
|
||||
@ -26,6 +28,17 @@
|
||||
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
|
||||
<![endif]-->
|
||||
|
||||
<!-- Google Analytics -->
|
||||
<script>
|
||||
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
|
||||
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
|
||||
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
|
||||
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
|
||||
|
||||
ga('create', 'UA-74711362-1', 'auto');
|
||||
ga('send', 'pageview');
|
||||
</script>
|
||||
<!-- /Google Analytics -->
|
||||
|
||||
|
||||
</head>
|
||||
@ -39,7 +52,7 @@
|
||||
<div class="container">
|
||||
<div class="header-nav">
|
||||
<div class="header-logo">
|
||||
<a class="pull-left" href="/"><img class="mr20" src="/images/logo.svg" alt="logo">Bradlee Speice</a>
|
||||
<a class="pull-left" href="https://bspeice.github.io/"><img class="mr20" src="https://bspeice.github.io/images/logo.svg" alt="logo">Bradlee Speice</a>
|
||||
</div>
|
||||
<div class="nav pull-right">
|
||||
</div>
|
||||
@ -54,12 +67,12 @@
|
||||
<div class="col-lg-12">
|
||||
<div class="header-content">
|
||||
<h1 class="header-title">Guaranteed Money Maker</h1>
|
||||
<p class="header-date"> <a href="/author/bradlee-speice.html">Bradlee Speice</a>, Wed 03 February 2016, <a href="/category/blog.html">Blog</a></p>
|
||||
<p class="header-date"> <a href="https://bspeice.github.io/author/bradlee-speice.html">Bradlee Speice</a>, Wed 03 February 2016, <a href="https://bspeice.github.io/category/blog.html">Blog</a></p>
|
||||
<div class="header-underline"></div>
|
||||
<div class="clearfix"></div>
|
||||
<p class="pull-right header-tags">
|
||||
<span class="glyphicon glyphicon-tags mr5" aria-hidden="true"></span>
|
||||
<a href="/tag/martingale.html">martingale</a>, <a href="/tag/strategy.html">strategy</a> </p>
|
||||
<a href="https://bspeice.github.io/tag/martingale.html">martingale</a>, <a href="https://bspeice.github.io/tag/strategy.html">strategy</a> </p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@ -73,7 +86,265 @@
|
||||
|
||||
<!-- Content -->
|
||||
<div class="container content">
|
||||
<p>{% notebook 2016-2-3-guaranteed-money-maker.ipynb %}</p>
|
||||
<p>
|
||||
<div class="cell border-box-sizing text_cell rendered">
|
||||
<div class="prompt input_prompt">
|
||||
</div>
|
||||
<div class="inner_cell">
|
||||
<div class="text_cell_render border-box-sizing rendered_html">
|
||||
<h3 id="If-you-can-see-into-the-future,-that-is.">If you can see into the future, that is.<a class="anchor-link" href="#If-you-can-see-into-the-future,-that-is.">¶</a></h3><p>My previous class in Stochastic Calculus covered a lot of interesting topics, and the important one for today
|
||||
is the <a href="https://en.wikipedia.org/wiki/Gambler's_ruin">Gambler's Ruin</a> problem. If you're interested in some of the theory behind it, also make sure to check out
|
||||
<a href="https://en.wikipedia.org/wiki/Random_walk">random walks</a>. The important bit is that we studied the <a href="https://en.wikipedia.org/wiki/Martingale_%28betting_system%29">Martingale Betting Strategy</a>, which describes for us
|
||||
a <strong>guaranteed way</strong> to <span style='font-size: x-small'>eventually</span> make money.</p>
|
||||
<p>The strategy goes like this: You are going to toss a fair coin with a friend. If you guess heads or tails correctly, you get back double the money you bet. If you guess incorrectly, you lose money. How should you bet?</p>
|
||||
<p>The correct answer is that you should double your bet each time you lose. Then when you finally win, you'll be guaranteed to make back everything you lost and then $1 extra! Consider the scenario:</p>
|
||||
<ol>
|
||||
<li>You bet $1, and guess incorrectly. You're 1 dollar in the hole.</li>
|
||||
<li>You bet $2, and guess incorrectly. You're 3 dollars in the hole now.</li>
|
||||
<li>You bet $4, and guess incorrectly. You're 7 dollars in the hole.</li>
|
||||
<li>You bet $8, and guess correctly! You now get back those 8 dollars you bet, plus 8 extra for winning, for a <strong>total profit of one dollar</strong>!</li>
|
||||
</ol>
|
||||
<p>Mathematically, we can prove that as long as you have unlimited money to bet, you are guaranteed to make money.</p>
|
||||
<h1 id="Applying-the-Martingale-Strategy">Applying the Martingale Strategy<a class="anchor-link" href="#Applying-the-Martingale-Strategy">¶</a></h1><p>But we're all realistic people, and once you start talking about "unlimited money" eyebrows should be raised. Even still, this is an interesting strategy to investigate, and I want to apply it to the stock market. As long as we can guarantee there's a single day in which the stock goes up, we should be able to make money right? The question is just how much we have to invest to guarantee this.</p>
|
||||
<p>Now it's time for the math. We'll use the following definitions:</p>
|
||||
<ul>
|
||||
<li>$o_i$ = the share price at the opening of day $i$</li>
|
||||
<li>$c_i$ = the share price at the close of day $i$</li>
|
||||
<li>$d_i$ = the amount of money we want to invest at the beginning of day $i$</li>
|
||||
</ul>
|
||||
<p>With those definitions in place, I'd like to present the formula that is <strong>guaranteed to make you money</strong>. I call it <em>Bradlee's Investment Formula</em>:</p>
|
||||
<p>$c_n \sum_{i=1}^n \frac{d_i}{o_i} > \sum_{i=1}^{n} d_i$</p>
|
||||
<p>It might not look like much, but if you can manage to make it so that this formula holds true, you will be guaranteed to make money. The intuition behind the formula is this: The closing share price times the number of shares you have purchased ends up greater than the amount of money you invested.</p>
|
||||
<p>That is, on day $n$, <span style='font-size: x-small'>if you know what the closing price will be</span> you can set up the amount of money you invest that day to <strong>guarantee you make money</strong>. I'll even teach you to figure out how much money that is! Take a look:</p>
|
||||
<p>$
|
||||
\begin{align}
|
||||
c_n \sum_{i=1}^{n-1} \frac{d_i}{o_i} + \frac{c_nd_n}{o_n} &> \sum_{i=1}^{n-1}d_i + d_n\\
|
||||
\frac{c_nd_n}{o_n} - d_n &> \sum_{i=1}^{n-1}(d_i - \frac{c_nd_i}{o_i})\\
|
||||
d_n (\frac{c_n - o_n}{o_n}) &> \sum_{i=1}^{n-1} d_i(1 - \frac{c_n}{o_i})\\
|
||||
d_n &> \frac{o_n}{c_n - o_n} \sum_{i=1}^{n-1} d_i(1 - \frac{1}{o_i})
|
||||
\end{align}$</p>
|
||||
<p>If you invest exactly $d_n$ that day, you'll break even. But if you can make sure the money you invest is greater than that quantity on the right <span style='font-size: x-small'>(which requires that you have a crystal ball tell you the stock's closing price)</span> you are <strong>guaranteed to make money!</strong></p>
|
||||
<h1 id="Interesting-Implications">Interesting Implications<a class="anchor-link" href="#Interesting-Implications">¶</a></h1><p>On a more serious note though, the formula above tells us a couple of interesting things:</p>
|
||||
<ol>
|
||||
<li>It's impossible to make money without the closing price at some point being greater than the opening price (or vice-versa if you are short selling) - there is no amount of money you can invest that will turn things in your favor.</li>
|
||||
<li>Close prices of the past aren't important if you're concerned about the bottom line. While chart technicians use price history to make judgment calls, in the end, the closing price on anything other than the last day is irrelevant.</li>
|
||||
<li>It's possible to make money as long as there is a single day where the closing price is greater than the opening price! You might have to invest a lot to do so, but it's possible.</li>
|
||||
<li>You must make a prediction about where the stock will close at if you want to know how much to invest. That is, we can set up our investment for the day to make money if the stock goes up 1%, but if it only goes up .5% we'll still lose money.</li>
|
||||
<li>It's possible the winning move is to scale back your position. Consider the scenario:<ul>
|
||||
<li>You invest money and the stock closes down the day .5%</li>
|
||||
<li>You invest tomorrow expecting the stock to go up 1%</li>
|
||||
<li>The winning investment to break even (assuming a 1% increase) is to scale back the position, since the shares you purchased at the beginning would then be profitable</li>
|
||||
</ul>
|
||||
</li>
|
||||
</ol>
|
||||
<h1 id="Running-the-simulation">Running the simulation<a class="anchor-link" href="#Running-the-simulation">¶</a></h1><p>So now that we've defined our investment formula,we need to tweak a couple things in order to make an investment strategy we can actually work with. There are two issues we need to address:</p>
|
||||
<ol>
|
||||
<li>The formula only tells us how much to invest if we want to break even ($d_n$). If we actually want to turn a profit, we need to invest more than that, which we will refer to as the <strong>bias</strong>.</li>
|
||||
<li>The formula assumes we know what the closing price will be on any given day. If we don't know this, we can still invest assuming the stock price will close at a level we choose. If the price doesn't meet this objective, we try again tomorrow! This predetermined closing price will be referred to as the <strong>expectation</strong>.</li>
|
||||
</ol>
|
||||
<p>Now that we've defined our <em>bias</em> and <em>expectation</em>, we can actually build a strategy we can simulate. Much like the martingale strategy told you to bet twice your previous bet in order to make money, we've designed a system that tells us how much to bet in order to make money as well.</p>
|
||||
<p>Now, let's get to the code!</p>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="cell border-box-sizing code_cell rendered">
|
||||
<div class="input">
|
||||
<div class="prompt input_prompt">In [1]:</div>
|
||||
<div class="inner_cell">
|
||||
<div class="input_area">
|
||||
<div class=" highlight hl-julia"><pre><span class="k">using</span> <span class="n">Quandl</span>
|
||||
<span class="n">api_key</span> <span class="o">=</span> <span class="s">""</span>
|
||||
<span class="n">daily_investment</span> <span class="o">=</span> <span class="n">function</span><span class="p">(</span><span class="n">current_open</span><span class="p">,</span> <span class="n">current_close</span><span class="p">,</span> <span class="n">purchase_history</span><span class="p">,</span> <span class="n">open_history</span><span class="p">)</span>
|
||||
<span class="c"># We're not going to safeguard against divide by 0 - that's the user's responsibility</span>
|
||||
<span class="n">t1</span> <span class="o">=</span> <span class="n">current_close</span> <span class="o">/</span> <span class="n">current_open</span> <span class="o">-</span> <span class="mi">1</span>
|
||||
<span class="n">t2</span> <span class="o">=</span> <span class="n">sum</span><span class="p">(</span><span class="n">purchase_history</span> <span class="o">-</span> <span class="n">purchase_history</span><span class="o">*</span><span class="n">current_close</span> <span class="o">./</span> <span class="n">open_history</span><span class="p">)</span>
|
||||
<span class="k">return</span> <span class="n">t2</span> <span class="o">/</span> <span class="n">t1</span>
|
||||
<span class="k">end</span><span class="p">;</span>
|
||||
</pre></div>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
<div class="cell border-box-sizing text_cell rendered">
|
||||
<div class="prompt input_prompt">
|
||||
</div>
|
||||
<div class="inner_cell">
|
||||
<div class="text_cell_render border-box-sizing rendered_html">
|
||||
<p>And let's code a way to run simulations quickly:</p>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="cell border-box-sizing code_cell rendered">
|
||||
<div class="input">
|
||||
<div class="prompt input_prompt">In [2]:</div>
|
||||
<div class="inner_cell">
|
||||
<div class="input_area">
|
||||
<div class=" highlight hl-julia"><pre><span class="n">is_profitable</span> <span class="o">=</span> <span class="n">function</span><span class="p">(</span><span class="n">current_price</span><span class="p">,</span> <span class="n">purchase_history</span><span class="p">,</span> <span class="n">open_history</span><span class="p">)</span>
|
||||
<span class="n">shares</span> <span class="o">=</span> <span class="n">sum</span><span class="p">(</span><span class="n">purchase_history</span> <span class="o">./</span> <span class="n">open_history</span><span class="p">)</span>
|
||||
<span class="k">return</span> <span class="n">current_price</span><span class="o">*</span><span class="n">shares</span> <span class="o">></span> <span class="n">sum</span><span class="p">(</span><span class="n">purchase_history</span><span class="p">)</span>
|
||||
<span class="k">end</span>
|
||||
|
||||
<span class="n">simulate</span> <span class="o">=</span> <span class="n">function</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">start</span><span class="p">,</span> <span class="n">init</span><span class="p">,</span> <span class="n">expected</span><span class="p">,</span> <span class="n">bias</span><span class="p">)</span>
|
||||
<span class="n">ticker_info</span> <span class="o">=</span> <span class="n">quandlget</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">from</span><span class="o">=</span><span class="n">start</span><span class="p">,</span> <span class="n">api_key</span><span class="o">=</span><span class="n">api_key</span><span class="p">)</span>
|
||||
<span class="n">open_vals</span> <span class="o">=</span> <span class="n">ticker_info</span><span class="p">[</span><span class="s">"Open"</span><span class="p">]</span><span class="o">.</span><span class="n">values</span>
|
||||
<span class="n">close_vals</span> <span class="o">=</span> <span class="n">ticker_info</span><span class="p">[</span><span class="s">"Close"</span><span class="p">]</span><span class="o">.</span><span class="n">values</span>
|
||||
<span class="n">invested</span> <span class="o">=</span> <span class="p">[</span><span class="n">init</span><span class="p">]</span>
|
||||
|
||||
<span class="c"># The simulation stops once we've made a profit</span>
|
||||
<span class="n">day</span> <span class="o">=</span> <span class="mi">1</span>
|
||||
<span class="n">profitable</span> <span class="o">=</span> <span class="n">is_profitable</span><span class="p">(</span><span class="n">close_vals</span><span class="p">[</span><span class="n">day</span><span class="p">],</span> <span class="n">invested</span><span class="p">,</span> <span class="n">open_vals</span><span class="p">[</span><span class="mi">1</span><span class="p">:</span><span class="n">length</span><span class="p">(</span><span class="n">invested</span><span class="p">)])</span> <span class="o">||</span>
|
||||
<span class="n">is_profitable</span><span class="p">(</span><span class="n">open_vals</span><span class="p">[</span><span class="n">day</span><span class="o">+</span><span class="mi">1</span><span class="p">],</span> <span class="n">invested</span><span class="p">,</span> <span class="n">open_vals</span><span class="p">[</span><span class="mi">1</span><span class="p">:</span><span class="n">length</span><span class="p">(</span><span class="n">invested</span><span class="p">)])</span>
|
||||
<span class="k">while</span> <span class="o">!</span><span class="n">profitable</span>
|
||||
<span class="n">expected_close</span> <span class="o">=</span> <span class="n">open_vals</span><span class="p">[</span><span class="n">day</span><span class="o">+</span><span class="mi">1</span><span class="p">]</span> <span class="o">*</span> <span class="n">expected</span>
|
||||
<span class="n">todays_purchase</span> <span class="o">=</span> <span class="n">daily_investment</span><span class="p">(</span><span class="n">open_vals</span><span class="p">[</span><span class="n">day</span><span class="o">+</span><span class="mi">1</span><span class="p">],</span> <span class="n">expected_close</span><span class="p">,</span> <span class="n">invested</span><span class="p">,</span> <span class="n">open_vals</span><span class="p">[</span><span class="mi">1</span><span class="p">:</span><span class="n">day</span><span class="p">])</span>
|
||||
<span class="n">invested</span> <span class="o">=</span> <span class="p">[</span><span class="n">invested</span><span class="p">;</span> <span class="n">todays_purchase</span> <span class="o">+</span> <span class="n">bias</span><span class="p">]</span>
|
||||
<span class="c"># expected_profit = expected_close * sum(invested ./ open_vals[1:length(invested)]) - sum(invested)</span>
|
||||
<span class="n">day</span> <span class="o">+=</span> <span class="mi">1</span>
|
||||
<span class="n">profitable</span> <span class="o">=</span> <span class="n">is_profitable</span><span class="p">(</span><span class="n">close_vals</span><span class="p">[</span><span class="n">day</span><span class="p">],</span> <span class="n">invested</span><span class="p">,</span> <span class="n">open_vals</span><span class="p">[</span><span class="mi">1</span><span class="p">:</span><span class="n">length</span><span class="p">(</span><span class="n">invested</span><span class="p">)])</span> <span class="o">||</span>
|
||||
<span class="n">is_profitable</span><span class="p">(</span><span class="n">open_vals</span><span class="p">[</span><span class="n">day</span><span class="o">+</span><span class="mi">1</span><span class="p">],</span> <span class="n">invested</span><span class="p">,</span> <span class="n">open_vals</span><span class="p">[</span><span class="mi">1</span><span class="p">:</span><span class="n">length</span><span class="p">(</span><span class="n">invested</span><span class="p">)])</span>
|
||||
<span class="k">end</span>
|
||||
|
||||
<span class="n">shares</span> <span class="o">=</span> <span class="n">sum</span><span class="p">(</span><span class="n">invested</span> <span class="o">./</span> <span class="n">open_vals</span><span class="p">[</span><span class="mi">1</span><span class="p">:</span><span class="n">length</span><span class="p">(</span><span class="n">invested</span><span class="p">)])</span>
|
||||
<span class="n">max_profit</span> <span class="o">=</span> <span class="n">max</span><span class="p">(</span><span class="n">close_vals</span><span class="p">[</span><span class="n">day</span><span class="p">],</span> <span class="n">open_vals</span><span class="p">[</span><span class="n">day</span><span class="o">+</span><span class="mi">1</span><span class="p">])</span>
|
||||
<span class="n">profit</span> <span class="o">=</span> <span class="n">shares</span> <span class="o">*</span> <span class="n">max_profit</span> <span class="o">-</span> <span class="n">sum</span><span class="p">(</span><span class="n">invested</span><span class="p">)</span>
|
||||
<span class="k">return</span> <span class="p">(</span><span class="n">invested</span><span class="p">,</span> <span class="n">profit</span><span class="p">)</span>
|
||||
<span class="k">end</span>
|
||||
|
||||
<span class="n">sim_summary</span> <span class="o">=</span> <span class="n">function</span><span class="p">(</span><span class="n">investments</span><span class="p">,</span> <span class="n">profit</span><span class="p">)</span>
|
||||
<span class="n">leverages</span> <span class="o">=</span> <span class="p">[</span><span class="n">sum</span><span class="p">(</span><span class="n">investments</span><span class="p">[</span><span class="mi">1</span><span class="p">:</span><span class="n">i</span><span class="p">])</span> <span class="k">for</span> <span class="n">i</span><span class="o">=</span><span class="mi">1</span><span class="p">:</span><span class="n">length</span><span class="p">(</span><span class="n">investments</span><span class="p">)]</span>
|
||||
<span class="n">max_leverage</span> <span class="o">=</span> <span class="n">maximum</span><span class="p">(</span><span class="n">leverages</span><span class="p">)</span> <span class="o">/</span> <span class="n">investments</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
|
||||
<span class="n">println</span><span class="p">(</span><span class="s">"Max leverage: </span><span class="si">$(max_leverage)</span><span class="s">"</span><span class="p">)</span>
|
||||
<span class="n">println</span><span class="p">(</span><span class="s">"Days invested: </span><span class="si">$</span><span class="s">(length(investments))"</span><span class="p">)</span>
|
||||
<span class="n">println</span><span class="p">(</span><span class="s">"Profit: </span><span class="si">$</span><span class="s">profit"</span><span class="p">)</span>
|
||||
<span class="k">end</span><span class="p">;</span>
|
||||
</pre></div>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
<div class="cell border-box-sizing text_cell rendered">
|
||||
<div class="prompt input_prompt">
|
||||
</div>
|
||||
<div class="inner_cell">
|
||||
<div class="text_cell_render border-box-sizing rendered_html">
|
||||
<p>Now, let's get some data and run a simulation! Our first test:</p>
|
||||
<ul>
|
||||
<li>We'll invest 100 dollars in LMT, and expect that the stock will close up 1% every day. We'll invest $d_n$ + 10 dollars every day that we haven't turned a profit, and end the simulation once we've made a profit.</li>
|
||||
</ul>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="cell border-box-sizing code_cell rendered">
|
||||
<div class="input">
|
||||
<div class="prompt input_prompt">In [3]:</div>
|
||||
<div class="inner_cell">
|
||||
<div class="input_area">
|
||||
<div class=" highlight hl-julia"><pre><span class="n">investments</span><span class="p">,</span> <span class="n">profit</span> <span class="o">=</span> <span class="n">simulate</span><span class="p">(</span><span class="s">"YAHOO/LMT"</span><span class="p">,</span> <span class="n">Date</span><span class="p">(</span><span class="mi">2015</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="mi">29</span><span class="p">),</span> <span class="mi">100</span><span class="p">,</span> <span class="mf">1.01</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span>
|
||||
<span class="n">sim_summary</span><span class="p">(</span><span class="n">investments</span><span class="p">,</span> <span class="n">profit</span><span class="p">)</span>
|
||||
</pre></div>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="output_wrapper">
|
||||
<div class="output">
|
||||
|
||||
|
||||
<div class="output_area"><div class="prompt"></div>
|
||||
<div class="output_subarea output_stream output_stdout output_text">
|
||||
<pre>Max leverage: 5.590373200042106
|
||||
Days invested: 5
|
||||
Profit: 0.6894803101560001
|
||||
</pre>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
<div class="cell border-box-sizing text_cell rendered">
|
||||
<div class="prompt input_prompt">
|
||||
</div>
|
||||
<div class="inner_cell">
|
||||
<div class="text_cell_render border-box-sizing rendered_html">
|
||||
<p>The result: We need to invest 5.6x our initial position over a period of 5 days to make approximately .69¢</p>
|
||||
<ul>
|
||||
<li>Now let's try the same thing, but we'll assume the stock closes up 2% instead.</li>
|
||||
</ul>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="cell border-box-sizing code_cell rendered">
|
||||
<div class="input">
|
||||
<div class="prompt input_prompt">In [4]:</div>
|
||||
<div class="inner_cell">
|
||||
<div class="input_area">
|
||||
<div class=" highlight hl-julia"><pre><span class="n">investments</span><span class="p">,</span> <span class="n">profit</span> <span class="o">=</span> <span class="n">simulate</span><span class="p">(</span><span class="s">"YAHOO/LMT"</span><span class="p">,</span> <span class="n">Date</span><span class="p">(</span><span class="mi">2015</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="mi">29</span><span class="p">),</span> <span class="mi">100</span><span class="p">,</span> <span class="mf">1.02</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span>
|
||||
<span class="n">sim_summary</span><span class="p">(</span><span class="n">investments</span><span class="p">,</span> <span class="n">profit</span><span class="p">)</span>
|
||||
</pre></div>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="output_wrapper">
|
||||
<div class="output">
|
||||
|
||||
|
||||
<div class="output_area"><div class="prompt"></div>
|
||||
<div class="output_subarea output_stream output_stdout output_text">
|
||||
<pre>Max leverage: 1.854949900247809
|
||||
Days invested: 25
|
||||
Profit: 0.08304813163696423
|
||||
</pre>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
<div class="cell border-box-sizing text_cell rendered">
|
||||
<div class="prompt input_prompt">
|
||||
</div>
|
||||
<div class="inner_cell">
|
||||
<div class="text_cell_render border-box-sizing rendered_html">
|
||||
<p>In this example, we only get up to a 1.85x leveraged position, but it takes 25 days to turn a profit of 8¢</p>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="cell border-box-sizing text_cell rendered">
|
||||
<div class="prompt input_prompt">
|
||||
</div>
|
||||
<div class="inner_cell">
|
||||
<div class="text_cell_render border-box-sizing rendered_html">
|
||||
<h1 id="Summary">Summary<a class="anchor-link" href="#Summary">¶</a></h1><p>We've defined an investment strategy that can tell us how much to invest when we know what the closing position of a stock will be. We can tweak the strategy to actually make money, but plenty of work needs to be done so that we can optimize the money invested.</p>
|
||||
<p>In the next post I'm going to post more information about some backtests and strategy tests on this strategy (unless of course this experiment actually produces a significant profit potential, and then I'm keeping it for myself).</p>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="cell border-box-sizing text_cell rendered">
|
||||
<div class="prompt input_prompt">
|
||||
</div>
|
||||
<div class="inner_cell">
|
||||
<div class="text_cell_render border-box-sizing rendered_html">
|
||||
<h1 id="Side-note-and-disclaimer">Side note and disclaimer<a class="anchor-link" href="#Side-note-and-disclaimer">¶</a></h1><p>The claims made in this presentation about being able to guarantee making money are intended as a joke and do not constitute investment advice of any sort.</p>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div></p>
|
||||
<script type="text/x-mathjax-config">
|
||||
MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$'], ['\(','\)']]}});
|
||||
</script>
|
||||
@ -81,6 +352,20 @@ MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$'], ['\(','\)']]}});
|
||||
<script async src='https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_CHTML'></script>
|
||||
|
||||
|
||||
<div class="comments">
|
||||
<div id="disqus_thread"></div>
|
||||
<script type="text/javascript">
|
||||
var disqus_shortname = 'bradleespeice';
|
||||
var disqus_identifier = 'guaranteed-money-maker.html';
|
||||
var disqus_url = 'https://bspeice.github.io/guaranteed-money-maker.html';
|
||||
(function() {
|
||||
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
|
||||
dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
|
||||
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
|
||||
})();
|
||||
</script>
|
||||
<noscript>Please enable JavaScript to view the comments.</noscript>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
<!-- /Content -->
|
||||
@ -92,6 +377,7 @@ MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$'], ['\(','\)']]}});
|
||||
<div class="col-xs-4 col-sm-3 col-md-3 col-lg-3">
|
||||
<div class="footer-title"></div>
|
||||
<ul class="list-unstyled">
|
||||
<li><a href="https://bspeice.github.io/feeds/all.atom.xml" type="application/atom+xml" rel="alternate"></a></li>
|
||||
</ul>
|
||||
</div>
|
||||
<div class="col-xs-4 col-sm-3 col-md-3 col-lg-3">
|
||||
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -4,20 +4,22 @@
|
||||
<meta charset="utf-8">
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<meta name="description" content="{% notebook 2015-12-26-testing_cramer.ipynb %} MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$'], ['\(','\)']]}});">
|
||||
<meta name="description" content="In [1]: import requests import pandas as pd import numpy as np from dateutil import parser as dtparser from dateutil.relativedelta import relativedelta from datetime import datetime from ...">
|
||||
<meta name="keywords" content="data science, futures">
|
||||
<link rel="icon" href="/favicon.ico">
|
||||
<link rel="icon" href="https://bspeice.github.io/favicon.ico">
|
||||
|
||||
<title>Testing Cramer - Bradlee Speice</title>
|
||||
|
||||
<!-- Stylesheets -->
|
||||
<link href="/theme/css/bootstrap.min.css" rel="stylesheet">
|
||||
<link href="/theme/css/fonts.css" rel="stylesheet">
|
||||
<link href="/theme/css/nest.css" rel="stylesheet">
|
||||
<link href="/theme/css/pygment.css" rel="stylesheet">
|
||||
<link href="https://bspeice.github.io/theme/css/bootstrap.min.css" rel="stylesheet">
|
||||
<link href="https://bspeice.github.io/theme/css/fonts.css" rel="stylesheet">
|
||||
<link href="https://bspeice.github.io/theme/css/nest.css" rel="stylesheet">
|
||||
<link href="https://bspeice.github.io/theme/css/pygment.css" rel="stylesheet">
|
||||
<!-- /Stylesheets -->
|
||||
|
||||
<!-- RSS Feeds -->
|
||||
<link href="https://bspeice.github.io/feeds/all.atom.xml" type="application/atom+xml" rel="alternate" title="Bradlee Speice Full Atom Feed" />
|
||||
<link href="https://bspeice.github.io/feeds/blog.atom.xml" type="application/atom+xml" rel="alternate" title="Bradlee Speice Categories Atom Feed" />
|
||||
<!-- /RSS Feeds -->
|
||||
|
||||
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
|
||||
@ -26,6 +28,17 @@
|
||||
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
|
||||
<![endif]-->
|
||||
|
||||
<!-- Google Analytics -->
|
||||
<script>
|
||||
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
|
||||
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
|
||||
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
|
||||
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
|
||||
|
||||
ga('create', 'UA-74711362-1', 'auto');
|
||||
ga('send', 'pageview');
|
||||
</script>
|
||||
<!-- /Google Analytics -->
|
||||
|
||||
|
||||
</head>
|
||||
@ -39,7 +52,7 @@
|
||||
<div class="container">
|
||||
<div class="header-nav">
|
||||
<div class="header-logo">
|
||||
<a class="pull-left" href="/"><img class="mr20" src="/images/logo.svg" alt="logo">Bradlee Speice</a>
|
||||
<a class="pull-left" href="https://bspeice.github.io/"><img class="mr20" src="https://bspeice.github.io/images/logo.svg" alt="logo">Bradlee Speice</a>
|
||||
</div>
|
||||
<div class="nav pull-right">
|
||||
</div>
|
||||
@ -54,12 +67,12 @@
|
||||
<div class="col-lg-12">
|
||||
<div class="header-content">
|
||||
<h1 class="header-title">Testing Cramer</h1>
|
||||
<p class="header-date"> <a href="/author/bradlee-speice.html">Bradlee Speice</a>, Sat 26 December 2015, <a href="/category/blog.html">Blog</a></p>
|
||||
<p class="header-date"> <a href="https://bspeice.github.io/author/bradlee-speice.html">Bradlee Speice</a>, Sat 26 December 2015, <a href="https://bspeice.github.io/category/blog.html">Blog</a></p>
|
||||
<div class="header-underline"></div>
|
||||
<div class="clearfix"></div>
|
||||
<p class="pull-right header-tags">
|
||||
<span class="glyphicon glyphicon-tags mr5" aria-hidden="true"></span>
|
||||
<a href="/tag/data-science.html">data science</a>, <a href="/tag/futures.html">futures</a> </p>
|
||||
<a href="https://bspeice.github.io/tag/data-science.html">data science</a>, <a href="https://bspeice.github.io/tag/futures.html">futures</a> </p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@ -73,7 +86,425 @@
|
||||
|
||||
<!-- Content -->
|
||||
<div class="container content">
|
||||
<p>{% notebook 2015-12-26-testing_cramer.ipynb %}</p>
|
||||
<p>
|
||||
<div class="cell border-box-sizing code_cell rendered">
|
||||
<div class="input">
|
||||
<div class="prompt input_prompt">In [1]:</div>
|
||||
<div class="inner_cell">
|
||||
<div class="input_area">
|
||||
<div class=" highlight hl-ipython3"><pre><span class="kn">import</span> <span class="nn">requests</span>
|
||||
<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
|
||||
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
|
||||
<span class="kn">from</span> <span class="nn">dateutil</span> <span class="k">import</span> <span class="n">parser</span> <span class="k">as</span> <span class="n">dtparser</span>
|
||||
<span class="kn">from</span> <span class="nn">dateutil.relativedelta</span> <span class="k">import</span> <span class="n">relativedelta</span>
|
||||
<span class="kn">from</span> <span class="nn">datetime</span> <span class="k">import</span> <span class="n">datetime</span>
|
||||
<span class="kn">from</span> <span class="nn">html.parser</span> <span class="k">import</span> <span class="n">HTMLParser</span>
|
||||
<span class="kn">from</span> <span class="nn">copy</span> <span class="k">import</span> <span class="n">copy</span>
|
||||
<span class="kn">import</span> <span class="nn">Quandl</span>
|
||||
</pre></div>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
<div class="cell border-box-sizing text_cell rendered">
|
||||
<div class="prompt input_prompt">
|
||||
</div>
|
||||
<div class="inner_cell">
|
||||
<div class="text_cell_render border-box-sizing rendered_html">
|
||||
<h1 id="Testing-Cramer">Testing Cramer<a class="anchor-link" href="#Testing-Cramer">¶</a></h1><p>Pursuant to attending a graduate school studying Financial Engineering, I've been a fan of the <a href="http://seekingalpha.com/author/wall-street-breakfast?s=wall-street-breakfast">Mad Money</a> TV show featuring the bombastic Jim Cramer. One of the things that he's said is that you shouldn't use the futures to predict where the stock market is going to go. But he says it often enough, I've begun to wonder - who is he trying to convince?</p>
|
||||
<p>It makes sense that because futures on things like the S&P 500 are traded continuously, they would price in market information before the stock market opens. So is Cramer right to be convinced that strategies based on the futures are a poor idea? I wanted to test it out.</p>
|
||||
<p>The first question is where to get the future's data. I've been part of <a href="http://seekingalpha.com/">Seeking Alpha</a> for a bit, and they publish the <a href="http://seekingalpha.com/author/wall-street-breakfast?s=wall-street-breakfast">Wall Street Breakfast</a> newsletter which contains daily future's returns as of 6:20 AM EST. I'd be interested in using that data to see if we can actually make some money.</p>
|
||||
<p>First though, let's get the data:</p>
|
||||
<h1 id="Downloading-Futures-data-from-Seeking-Alpha">Downloading Futures data from Seeking Alpha<a class="anchor-link" href="#Downloading-Futures-data-from-Seeking-Alpha">¶</a></h1><p>We're going to define two HTML parsing classes - one to get the article URL's from a page, and one to get the actual data from each article.</p>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="cell border-box-sizing code_cell rendered">
|
||||
<div class="input">
|
||||
<div class="prompt input_prompt">In [2]:</div>
|
||||
<div class="inner_cell">
|
||||
<div class="input_area">
|
||||
<div class=" highlight hl-ipython3"><pre><span class="k">class</span> <span class="nc">ArticleListParser</span><span class="p">(</span><span class="n">HTMLParser</span><span class="p">):</span>
|
||||
<span class="sd">"""Given a web page with articles on it, parse out the article links"""</span>
|
||||
|
||||
<span class="n">articles</span> <span class="o">=</span> <span class="p">[]</span>
|
||||
|
||||
<span class="k">def</span> <span class="nf">handle_starttag</span><span class="p">(</span><span class= |