mirror of
https://github.com/bspeice/speice.io
synced 2024-11-15 06:28:10 -05:00
47 lines
88 KiB
HTML
47 lines
88 KiB
HTML
|
<!doctype html><html lang=en dir=ltr class="blog-wrapper blog-post-page plugin-blog plugin-id-default" data-has-hydrated=false><meta charset=UTF-8><meta name=generator content="Docusaurus v3.6.0"><title data-rh=true>Welcome, and an algorithm | The Old Speice Guy</title><meta data-rh=true name=viewport content="width=device-width,initial-scale=1.0"><meta data-rh=true name=twitter:card content=summary_large_image><meta data-rh=true property=og:url content=https://speice.io/2015/11/welcome><meta data-rh=true property=og:locale content=en><meta data-rh=true name=docusaurus_locale content=en><meta data-rh=true name=docusaurus_tag content=default><meta data-rh=true name=docsearch:language content=en><meta data-rh=true name=docsearch:docusaurus_tag content=default><meta data-rh=true property=og:title content="Welcome, and an algorithm | The Old Speice Guy"><meta data-rh=true name=description content="Hello! Glad to meet you. I'm currently a student at Columbia University studying Financial Engineering, and want to give an overview of the projects I'm working on!"><meta data-rh=true property=og:description content="Hello! Glad to meet you. I'm currently a student at Columbia University studying Financial Engineering, and want to give an overview of the projects I'm working on!"><meta data-rh=true property=og:type content=article><meta data-rh=true property=article:published_time content=2015-11-19T12:00:00.000Z><link data-rh=true rel=icon href=/img/favicon.ico><link data-rh=true rel=canonical href=https://speice.io/2015/11/welcome><link data-rh=true rel=alternate href=https://speice.io/2015/11/welcome hreflang=en><link data-rh=true rel=alternate href=https://speice.io/2015/11/welcome hreflang=x-default><script data-rh=true type=application/ld+json>{"@context":"https://schema.org","@id":"https://speice.io/2015/11/welcome","@type":"BlogPosting","author":{"@type":"Person","name":"Bradlee Speice"},"dateModified":"2015-12-05T12:00:00.000Z","datePublished":"2015-11-19T12:00:00.000Z","description":"Hello! Glad to meet you. I'm currently a student at Columbia University studying Financial Engineering, and want to give an overview of the projects I'm working on!","headline":"Welcome, and an algorithm","isPartOf":{"@id":"https://speice.io/","@type":"Blog","name":"Blog"},"keywords":[],"mainEntityOfPage":"https://speice.io/2015/11/welcome","name":"Welcome, and an algorithm","url":"https://speice.io/2015/11/welcome"}</script><link rel=alternate type=application/rss+xml href=/rss.xml title="The Old Speice Guy RSS Feed"><link rel=alternate type=application/atom+xml href=/atom.xml title="The Old Speice Guy Atom Feed"><link rel=stylesheet href=https://cdn.jsdelivr.net/npm/katex@0.13.24/dist/katex.min.css integrity=sha384-odtC+0UGzzFL/6PNoE8rX/SPcQDXBJ+uRepguP4QkPCm2LBxH3FA3y+fKSiJ+AmM crossorigin><link rel=stylesheet href=/assets/css/styles.ae6ff4a3.css><script src=/assets/js/runtime~main.751b419d.js defer></script><script src=/assets/js/main.62ce6156.js defer></script><body class=navigation-with-keyboard><script>!function(){var t,e=function(){try{return new URLSearchParams(window.location.search).get("docusaurus-theme")}catch(t){}}()||function(){try{return window.localStorage.getItem("theme")}catch(t){}}();t=null!==e?e:"light",document.documentElement.setAttribute("data-theme",t)}(),function(){try{for(var[t,e]of new URLSearchParams(window.location.search).entries())if(t.startsWith("docusaurus-data-")){var a=t.replace("docusaurus-data-","data-");document.documentElement.setAttribute(a,e)}}catch(t){}}()</script><div id=__docusaurus><div role=region aria-label="Skip to main content"><a class=skipToContent_fXgn href=#__docusaurus_skipToContent_fallback>Skip to main content</a></div><nav aria-label=Main class="navbar navbar--fixed-top"><div class=navbar__inner><div class=navbar__items><button aria-label="Toggle navigation bar" aria-expanded=false class="navbar__toggle clean-btn" type=button><svg width=30 height=30 viewBox="0 0 30 30" aria-hidden=true><path stroke=currentColor stroke-linecap=round stroke-miterlimit=10 stroke-width=2 d="M4 7h22M4 15h
|
|||
|
<p>To start things off, Columbia has been hosting a trading competition that myself and another partner are competing in. I'm including a notebook of the algorithm that we're using, just to give a simple overview of a miniature algorithm.</p>
|
|||
|
<p>The competition is scored in 3 areas:</p>
|
|||
|
<ul>
|
|||
|
<li>Total return</li>
|
|||
|
<li><a href=https://en.wikipedia.org/wiki/Sharpe_ratio target=_blank rel="noopener noreferrer">Sharpe ratio</a></li>
|
|||
|
<li>Maximum drawdown</li>
|
|||
|
</ul>
|
|||
|
<p>Our algorithm uses a basic momentum strategy: in the given list of potential portfolios, pick the stocks that have been performing well in the past 30 days. Then, optimize for return subject to the drawdown being below a specific level. We didn't include the Sharpe ratio as a constraint, mostly because we were a bit late entering the competition.</p>
|
|||
|
<p>I'll be updating this post with the results of our algorithm as they come along!</p>
|
|||
|
<hr>
|
|||
|
<p><strong>UPDATE 12/5/2015</strong>: Now that the competition has ended, I wanted to update how the algorithm performed. Unfortunately, it didn't do very well. I'm planning to make some tweaks over the coming weeks, and do another forward test in January.</p>
|
|||
|
<ul>
|
|||
|
<li>After week 1: Down .1%</li>
|
|||
|
<li>After week 2: Down 1.4%</li>
|
|||
|
<li>After week 3: Flat</li>
|
|||
|
</ul>
|
|||
|
<p>And some statistics for all teams participating in the competition:</p>
|
|||
|
<table><thead><tr><th>Statistic<th>Value<tbody><tr><td>Max Return<td>74.1%<tr><td>Min Return<td>-97.4%<tr><td>Average Return<td>-.1%<tr><td>Std Dev of Returns<td>19.6%</table>
|
|||
|
<hr>
|
|||
|
<h2 class="anchor anchorWithStickyNavbar_LWe7" id=trading-competition-optimization>Trading Competition Optimization<a href=#trading-competition-optimization class=hash-link aria-label="Direct link to Trading Competition Optimization" title="Direct link to Trading Competition Optimization"></a></h2>
|
|||
|
<p><strong>Goal: Max return given maximum Sharpe and Drawdown</strong></p>
|
|||
|
<div class="language-python codeBlockContainer_Ckt0 theme-code-block" style="--prism-background-color:hsl(230, 1%, 98%);--prism-color:hsl(230, 8%, 24%)"><div class=codeBlockContent_biex><pre tabindex=0 class="prism-code language-python codeBlock_bY9V thin-scrollbar" style="background-color:hsl(230, 1%, 98%);color:hsl(230, 8%, 24%)"><code class=codeBlockLines_e6Vv><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token keyword" style="color:hsl(301, 63%, 40%)">from</span><span class="token plain"> IPython</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">.</span><span class="token plain">display </span><span class="token keyword" style="color:hsl(301, 63%, 40%)">import</span><span class="token plain"> display</span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"></span><span class="token keyword" style="color:hsl(301, 63%, 40%)">import</span><span class="token plain"> Quandl</span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"></span><span class="token keyword" style="color:hsl(301, 63%, 40%)">from</span><span class="token plain"> datetime </span><span class="token keyword" style="color:hsl(301, 63%, 40%)">import</span><span class="token plain"> datetime</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> timedelta</span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain" style=display:inline-block></span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain">tickers </span><span class="token operator" style="color:hsl(221, 87%, 60%)">=</span><span class="token plain"> </span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">[</span><span class="token string" style="color:hsl(119, 34%, 47%)">'XOM'</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> </span><span class="token string" style="color:hsl(119, 34%, 47%)">'CVX'</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> </span><span class="token string" style="color:hsl(119, 34%, 47%)">'CLB'</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> </span><span class="token string" style="color:hsl(119, 34%, 47%)">'OXY'</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> </span><span class="token string" style="color:hsl(119, 34%, 47%)">'SLB'</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">]</span><span class="token plain"></span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain">market_ticker </span><span class="token operator" style="color:hsl(221, 87%, 60%)">=</span><span class="token plain"> </span><span class="token string" style="color:hsl(119, 34%, 47%)">'GOOG/NYSE_VOO'</span><span class="token plain"></span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain">lookback </span><span class="token operator" style="color:hsl(221, 87%, 60%)">=</span><span class="token plain"> </span><span class="token number" style="color:hsl(35, 99%, 36%)">30</span><span class="token plain"></span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain">d_col </span><span class="token operator" style="color:hsl(221, 87%, 60%)">=</span><span class="token plain"> </span><span class="token string" style="color:hsl(119, 34%, 47%)">'Close'</span><span class="token plain"></span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain" style=display:inline-block></span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain">data </span><span class="token operator" style="color:hsl(221, 87%, 60%)">=</span><span class="token plain"> </span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">{</span><
|
|||
|
<h2 class="anchor anchorWithStickyNavbar_LWe7" id=calculating-the-return>Calculating the Return<a href=#calculating-the-return class=hash-link aria-label="Direct link to Calculating the Return" title="Direct link to Calculating the Return"></a></h2>
|
|||
|
<p>We first want to know how much each ticker returned over the prior period.</p>
|
|||
|
<div class="language-python codeBlockContainer_Ckt0 theme-code-block" style="--prism-background-color:hsl(230, 1%, 98%);--prism-color:hsl(230, 8%, 24%)"><div class=codeBlockContent_biex><pre tabindex=0 class="prism-code language-python codeBlock_bY9V thin-scrollbar" style="background-color:hsl(230, 1%, 98%);color:hsl(230, 8%, 24%)"><code class=codeBlockLines_e6Vv><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain">returns </span><span class="token operator" style="color:hsl(221, 87%, 60%)">=</span><span class="token plain"> </span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">{</span><span class="token plain">tick</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">:</span><span class="token plain"> data</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">[</span><span class="token plain">tick</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">]</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">[</span><span class="token plain">d_col</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">]</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">.</span><span class="token plain">pct_change</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">(</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">)</span><span class="token plain"> </span><span class="token keyword" style="color:hsl(301, 63%, 40%)">for</span><span class="token plain"> tick </span><span class="token keyword" style="color:hsl(301, 63%, 40%)">in</span><span class="token plain"> tickers</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">}</span><span class="token plain"></span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain" style=display:inline-block></span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain">display</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">(</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">{</span><span class="token plain">tick</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">:</span><span class="token plain"> returns</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">[</span><span class="token plain">tick</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">]</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">.</span><span class="token plain">mean</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">(</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">)</span><span class="token plain"> </span><span class="token keyword" style="color:hsl(301, 63%, 40%)">for</span><span class="token plain"> tick </span><span class="token keyword" style="color:hsl(301, 63%, 40%)">in</span><span class="token plain"> tickers</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">}</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">)</span><br></span></code></pre><div class=buttonGroup__atx><button type=button aria-label="Copy code to clipboard" title=Copy class=clean-btn><span class=copyButtonIcons_eSgA aria-hidden=true><svg viewBox="0 0 24 24" class=copyButtonIcon_y97N><path fill=currentColor d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z"/></svg><svg viewBox="0 0 24 24" class=copyButtonSuccessIcon_LjdS><path fill=currentColor d=M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z /></svg></span></button></div></div></div>
|
|||
|
<div class="codeBlockContainer_Ckt0 theme-code-block" style="--prism-background-color:hsl(230, 1%, 98%);--prism-color:hsl(230, 8%, 24%)"><div class=codeBlockContent_biex><pre tabindex=0 class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="background-color:hsl(230, 1%, 98%);color:hsl(230, 8%, 24%)"><code class=codeBlockLines_e6Vv><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> {'CLB': -0.0016320202164526894,</span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> 'CVX': 0.0010319531629488911,</span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> 'OXY': 0.00093418904454400551,</span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> 'SLB': 0.00098431254720448159,</span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> 'XOM': 0.00044165797556096868}</span><br></span></code></pre><div class=buttonGroup__atx><button type=button aria-label="Copy code to clipboard" title=Copy class=clean-btn><span class=copyButtonIcons_eSgA aria-hidden=true><svg viewBox="0 0 24 24" class=copyButtonIcon_y97N><path fill=currentColor d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z"/></svg><svg viewBox="0 0 24 24" class=copyButtonSuccessIcon_LjdS><path fill=currentColor d=M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z /></svg></span></button></div></div></div>
|
|||
|
<h2 class="anchor anchorWithStickyNavbar_LWe7" id=calculating-the-sharpe-ratio>Calculating the Sharpe ratio<a href=#calculating-the-sharpe-ratio class=hash-link aria-label="Direct link to Calculating the Sharpe ratio" title="Direct link to Calculating the Sharpe ratio"></a></h2>
|
|||
|
<p>Sharpe: <span class=katex><span class=katex-mathml><math><semantics><mrow><mfrac><mrow><mi>R</mi><mo>−</mo><msub><mi>R</mi><mi>M</mi></msub></mrow><mi>σ</mi></mfrac></mrow><annotation encoding=application/x-tex>{R - R_M \over \sigma}</annotation></semantics></math></span><span class=katex-html aria-hidden=true><span class=base><span class=strut style=height:1.2336em;vertical-align:-0.345em></span><span class=mord><span class=mord><span class="mopen nulldelimiter"></span><span class=mfrac><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:0.8886em><span style=top:-2.655em><span class=pstrut style=height:3em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style=margin-right:0.03588em>σ</span></span></span></span><span style=top:-3.23em><span class=pstrut style=height:3em></span><span class=frac-line style=border-bottom-width:0.04em></span></span><span style=top:-3.4103em><span class=pstrut style=height:3em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style=margin-right:0.00773em>R</span><span class="mbin mtight">−</span><span class="mord mtight"><span class="mord mathnormal mtight" style=margin-right:0.00773em>R</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:0.3448em><span style=top:-2.3567em;margin-left:-0.0077em;margin-right:0.0714em><span class=pstrut style=height:2.5em></span><span class="sizing reset-size3 size1 mtight"><span class="mord mathnormal mtight" style=margin-right:0.10903em>M</span></span></span></span><span class=vlist-s></span></span><span class=vlist-r><span class=vlist style=height:0.1433em><span></span></span></span></span></span></span></span></span></span></span><span class=vlist-s></span></span><span class=vlist-r><span class=vlist style=height:0.345em><span></span></span></span></span></span><span class="mclose nulldelimiter"></span></span></span></span></span></span></p>
|
|||
|
<p>We use the average return over the lookback period, minus the market average return, over the ticker standard deviation to calculate the Sharpe. Shorting a stock turns a negative Sharpe positive.</p>
|
|||
|
<div class="language-python codeBlockContainer_Ckt0 theme-code-block" style="--prism-background-color:hsl(230, 1%, 98%);--prism-color:hsl(230, 8%, 24%)"><div class=codeBlockContent_biex><pre tabindex=0 class="prism-code language-python codeBlock_bY9V thin-scrollbar" style="background-color:hsl(230, 1%, 98%);color:hsl(230, 8%, 24%)"><code class=codeBlockLines_e6Vv><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain">market_returns </span><span class="token operator" style="color:hsl(221, 87%, 60%)">=</span><span class="token plain"> market</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">.</span><span class="token plain">pct_change</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">(</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">)</span><span class="token plain"></span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain" style=display:inline-block></span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain">sharpe </span><span class="token operator" style="color:hsl(221, 87%, 60%)">=</span><span class="token plain"> </span><span class="token keyword" style="color:hsl(301, 63%, 40%)">lambda</span><span class="token plain"> ret</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">:</span><span class="token plain"> </span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">(</span><span class="token plain">ret</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">.</span><span class="token plain">mean</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">(</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">)</span><span class="token plain"> </span><span class="token operator" style="color:hsl(221, 87%, 60%)">-</span><span class="token plain"> market_returns</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">[</span><span class="token plain">d_col</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">]</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">.</span><span class="token plain">mean</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">(</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">)</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">)</span><span class="token plain"> </span><span class="token operator" style="color:hsl(221, 87%, 60%)">/</span><span class="token plain"> ret</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">.</span><span class="token plain">std</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">(</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">)</span><span class="token plain"></span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain">sharpes </span><span class="token operator" style="color:hsl(221, 87%, 60%)">=</span><span class="token plain"> </span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">{</span><span class="token plain">tick</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">:</span><span class="token plain"> sharpe</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">(</span><span class="token plain">returns</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">[</span><span class="token plain">tick</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">]</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">)</span><span class="token plain"> </span><span class="token keyword" style="color:hsl(301, 63%, 40%)">for</span><span class="token plain"> tick </span><span class="token keyword" style="color:hsl(301, 63%, 40%)">in</span><span class="token plain"> tickers</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">}</span><span class="token plain"></span><br></span><span class=token-line style="col
|
|||
|
<div class="codeBlockContainer_Ckt0 theme-code-block" style="--prism-background-color:hsl(230, 1%, 98%);--prism-color:hsl(230, 8%, 24%)"><div class=codeBlockContent_biex><pre tabindex=0 class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="background-color:hsl(230, 1%, 98%);color:hsl(230, 8%, 24%)"><code class=codeBlockLines_e6Vv><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> {'CLB': -0.10578734457846127,</span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> 'CVX': 0.027303529817677398,</span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> 'OXY': 0.022622210057414487,</span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> 'SLB': 0.026950946344858676,</span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> 'XOM': -0.0053519259698605499}</span><br></span></code></pre><div class=buttonGroup__atx><button type=button aria-label="Copy code to clipboard" title=Copy class=clean-btn><span class=copyButtonIcons_eSgA aria-hidden=true><svg viewBox="0 0 24 24" class=copyButtonIcon_y97N><path fill=currentColor d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z"/></svg><svg viewBox="0 0 24 24" class=copyButtonSuccessIcon_LjdS><path fill=currentColor d=M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z /></svg></span></button></div></div></div>
|
|||
|
<h2 class="anchor anchorWithStickyNavbar_LWe7" id=calculating-the-drawdown>Calculating the drawdown<a href=#calculating-the-drawdown class=hash-link aria-label="Direct link to Calculating the drawdown" title="Direct link to Calculating the drawdown"></a></h2>
|
|||
|
<p>This one is easy - what is the maximum daily change over the lookback period? That is, because we will allow short positions, we are not concerned strictly with maximum downturn, but in general, what is the largest 1-day change?</p>
|
|||
|
<div class="language-python codeBlockContainer_Ckt0 theme-code-block" style="--prism-background-color:hsl(230, 1%, 98%);--prism-color:hsl(230, 8%, 24%)"><div class=codeBlockContent_biex><pre tabindex=0 class="prism-code language-python codeBlock_bY9V thin-scrollbar" style="background-color:hsl(230, 1%, 98%);color:hsl(230, 8%, 24%)"><code class=codeBlockLines_e6Vv><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain">drawdown </span><span class="token operator" style="color:hsl(221, 87%, 60%)">=</span><span class="token plain"> </span><span class="token keyword" style="color:hsl(301, 63%, 40%)">lambda</span><span class="token plain"> ret</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">:</span><span class="token plain"> ret</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">.</span><span class="token builtin" style="color:hsl(119, 34%, 47%)">abs</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">(</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">)</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">.</span><span class="token builtin" style="color:hsl(119, 34%, 47%)">max</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">(</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">)</span><span class="token plain"></span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain">drawdowns </span><span class="token operator" style="color:hsl(221, 87%, 60%)">=</span><span class="token plain"> </span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">{</span><span class="token plain">tick</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">:</span><span class="token plain"> drawdown</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">(</span><span class="token plain">returns</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">[</span><span class="token plain">tick</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">]</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">)</span><span class="token plain"> </span><span class="token keyword" style="color:hsl(301, 63%, 40%)">for</span><span class="token plain"> tick </span><span class="token keyword" style="color:hsl(301, 63%, 40%)">in</span><span class="token plain"> tickers</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">}</span><span class="token plain"></span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain" style=display:inline-block></span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain">display</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">(</span><span class="token plain">drawdowns</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">)</span><br></span></code></pre><div class=buttonGroup__atx><button type=button aria-label="Copy code to clipboard" title=Copy class=clean-btn><span class=copyButtonIcons_eSgA aria-hidden=true><svg viewBox="0 0 24 24" class=copyButtonIcon_y97N><path fill=currentColor d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z"/></svg><svg viewBox="0 0 24 24" class=copyButtonSuccessIcon_LjdS><path fill=currentColor d=M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z /></svg></span></button></div></div></div>
|
|||
|
<div class="codeBlockContainer_Ckt0 theme-code-block" style="--prism-background-color:hsl(230, 1%, 98%);--prism-color:hsl(230, 8%, 24%)"><div class=codeBlockContent_biex><pre tabindex=0 class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="background-color:hsl(230, 1%, 98%);color:hsl(230, 8%, 24%)"><code class=codeBlockLines_e6Vv><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> {'CLB': 0.043551495607375035,</span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> 'CVX': 0.044894389686214398,</span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> 'OXY': 0.051424517867144637,</span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> 'SLB': 0.034774627850375328,</span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> 'XOM': 0.035851524605672758}</span><br></span></code></pre><div class=buttonGroup__atx><button type=button aria-label="Copy code to clipboard" title=Copy class=clean-btn><span class=copyButtonIcons_eSgA aria-hidden=true><svg viewBox="0 0 24 24" class=copyButtonIcon_y97N><path fill=currentColor d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z"/></svg><svg viewBox="0 0 24 24" class=copyButtonSuccessIcon_LjdS><path fill=currentColor d=M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z /></svg></span></button></div></div></div>
|
|||
|
<h1>Performing the optimization</h1>
|
|||
|
<span class=katex-display><span class=katex><span class=katex-mathml><math display=block><semantics><mtable rowspacing=0.25em columnalign="right left" columnspacing=0em><mtr><mtd><mstyle scriptlevel=0 displaystyle=true><mrow><mi>m</mi><mi>a</mi><mi>x</mi><mtext> </mtext></mrow></mstyle></mtd><mtd><mstyle scriptlevel=0 displaystyle=true><mrow><mrow/><mi>μ</mi><mo>⋅</mo><mi>ω</mi></mrow></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel=0 displaystyle=true><mrow><mi>s</mi><mi mathvariant=normal>.</mi><mi>t</mi><mi mathvariant=normal>.</mi><mtext> </mtext></mrow></mstyle></mtd><mtd><mstyle scriptlevel=0 displaystyle=true><mrow><mrow/><mover accent=true><mn>1</mn><mo>⃗</mo></mover><mi>ω</mi><mo>=</mo><mn>1</mn></mrow></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel=0 displaystyle=true><mrow/></mstyle></mtd><mtd><mstyle scriptlevel=0 displaystyle=true><mrow><mrow/><mover accent=true><mi>S</mi><mo>⃗</mo></mover><mi>ω</mi><mo>≥</mo><mi>s</mi></mrow></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel=0 displaystyle=true><mrow/></mstyle></mtd><mtd><mstyle scriptlevel=0 displaystyle=true><mrow><mrow/><mover accent=true><mi>D</mi><mo>⃗</mo></mover><mo>⋅</mo><mi mathvariant=normal>∣</mi><mi>ω</mi><mi mathvariant=normal>∣</mi><mo>≤</mo><mi>d</mi></mrow></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel=0 displaystyle=true><mrow/></mstyle></mtd><mtd><mstyle scriptlevel=0 displaystyle=true><mrow><mrow/><mrow><mo fence=true>∣</mo><mi>ω</mi><mo fence=true>∣</mo></mrow><mo>≤</mo><mi>l</mi></mrow></mstyle></mtd></mtr></mtable><annotation encoding=application/x-tex>\begin{align*}
|
|||
|
max\ \ & \mu \cdot \omega\\
|
|||
|
s.t.\ \ & \vec{1} \omega = 1\\
|
|||
|
& \vec{S} \omega \ge s\\
|
|||
|
& \vec{D} \cdot | \omega | \le d\\
|
|||
|
& \left|\omega\right| \le l\\
|
|||
|
\end{align*}</annotation></semantics></math></span><span class=katex-html aria-hidden=true><span class=base><span class=strut style=height:7.8401em;vertical-align:-3.6701em></span><span class=mord><span class=mtable><span class=col-align-r><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:4.17em><span style=top:-6.33em><span class=pstrut style=height:3em></span><span class=mord><span class="mord mathnormal">ma</span><span class="mord mathnormal">x</span><span class=mspace> </span><span class=mspace> </span></span></span><span style=top:-4.7426em><span class=pstrut style=height:3em></span><span class=mord><span class="mord mathnormal">s</span><span class=mord>.</span><span class="mord mathnormal">t</span><span class=mord>.</span><span class=mspace> </span><span class=mspace> </span></span></span><span style=top:-3.1163em><span class=pstrut style=height:3em></span><span class=mord></span></span><span style=top:-1.4899em><span class=pstrut style=height:3em></span><span class=mord></span></span><span style=top:0.0101em><span class=pstrut style=height:3em></span><span class=mord></span></span></span><span class=vlist-s></span></span><span class=vlist-r><span class=vlist style=height:3.6701em><span></span></span></span></span></span><span class=col-align-l><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:4.17em><span style=top:-6.33em><span class=pstrut style=height:3em></span><span class=mord><span class=mord></span><span class="mord mathnormal">μ</span><span class=mspace style=margin-right:0.2222em></span><span class=mbin>⋅</span><span class=mspace style=margin-right:0.2222em></span><span class="mord mathnormal" style=margin-right:0.03588em>ω</span></span></span><span style=top:-4.7426em><span class=pstrut style=height:3em></span><span class=mord><span class=mord></span><span class="mord accent"><span class=vlist-t><span class=vlist-r><span class=vlist style=height:0.9274em><span style=top:-3em><span class=pstrut style=height:3em></span><span class=mord>1</span></span><span style=top:-3.2134em><span class=pstrut style=height:3em></span><span class=accent-body style=left:-0.2355em><span class=overlay style=height:0.714em;width:0.471em><svg width=0.471em height=0.714em style=width:0.471em viewBox="0 0 471 714" preserveAspectRatio=xMinYMin><path d="M377 20c0-5.333 1.833-10 5.5-14S391 0 397 0c4.667 0 8.667 1.667 12 5 3.333 2.667 6.667 9 10 19 6.667 24.667 20.333 43.667 41 57 7.333 4.667 11 10.667 11 18 0 6-1 10-3 12s-6.667 5-14 9c-28.667 14.667-53.667 35.667-75 63 -1.333 1.333-3.167 3.5-5.5 6.5s-4 4.833-5 5.5c-1 .667-2.5 1.333-4.5 2s-4.333 1 -7 1c-4.667 0-9.167-1.833-13.5-5.5S337 184 337 178c0-12.667 15.667-32.333 47-59 H213l-171-1c-8.667-6-13-12.333-13-19 0-4.667 4.333-11.333 13-20h359 c-16-25.333-24-45-24-59z"/></svg></span></span></span></span></span></span></span><span class="mord mathnormal" style=margin-right:0.03588em>ω</span><span class=mspace style=margin-right:0.2778em></span><span class=mrel>=</span><span class=mspace style=margin-right:0.2778em></span><span class=mord>1</span></span></span><span style=top:-3.1163em><span class=pstrut style=height:3em></span><span class=mord><span class=mord></span><span class="mord accent"><span class=vlist-t><span class=vlist-r><span class=vlist style=height:0.9663em><span style=top:-3em><span class=pstrut style=height:3em></span><span class="mord mathnormal" style=margin-right:0.05764em>S</span></span><span style=top:-3.2523em><span class=pstrut style=height:3em></span><span class=accent-body style=left:-0.1522em><span class=overlay style=height:0.714em;width:0.471em><svg width=0.471em height=0.714em style=width:0.471em viewBox="0 0 471 714" preserveAspectRatio=xMinYMin><path d="M377 20c0-5.333 1.833-10 5.5-14S391 0 397 0c4.667 0 8.667 1.667 12 5 3.333 2.667 6.667 9 10 19 6.667 24.667 20.333 43.667 41 57 7.333 4.667 11 10.667 11 18 0 6-1 10-3 12s-6.667 5-14 9c-28.667 14.667-53.667 35.667-75 63 -1.333 1.333-3.167 3.5-5.5 6.5s-4 4.833-5 5.5c-1 .667-2.5 1.333-4.5 2s-4.333 1 -7 1c-4.667 0-9.167-1.833-13.5-5.5
|
|||
|
<p>We want to maximize average return subject to having a full portfolio, Sharpe above a specific level, drawdown below a level, and leverage not too high - that is, don't have huge long/short positions.</p>
|
|||
|
<div class="language-python codeBlockContainer_Ckt0 theme-code-block" style="--prism-background-color:hsl(230, 1%, 98%);--prism-color:hsl(230, 8%, 24%)"><div class=codeBlockContent_biex><pre tabindex=0 class="prism-code language-python codeBlock_bY9V thin-scrollbar" style="background-color:hsl(230, 1%, 98%);color:hsl(230, 8%, 24%)"><code class=codeBlockLines_e6Vv><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token keyword" style="color:hsl(301, 63%, 40%)">import</span><span class="token plain"> numpy </span><span class="token keyword" style="color:hsl(301, 63%, 40%)">as</span><span class="token plain"> np</span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"></span><span class="token keyword" style="color:hsl(301, 63%, 40%)">from</span><span class="token plain"> scipy</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">.</span><span class="token plain">optimize </span><span class="token keyword" style="color:hsl(301, 63%, 40%)">import</span><span class="token plain"> minimize</span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain" style=display:inline-block></span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"></span><span class="token comment" style="color:hsl(230, 4%, 64%)">#sharpe_limit = .1</span><span class="token plain"></span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain">drawdown_limit </span><span class="token operator" style="color:hsl(221, 87%, 60%)">=</span><span class="token plain"> </span><span class="token number" style="color:hsl(35, 99%, 36%)">.05</span><span class="token plain"></span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain">leverage </span><span class="token operator" style="color:hsl(221, 87%, 60%)">=</span><span class="token plain"> </span><span class="token number" style="color:hsl(35, 99%, 36%)">250</span><span class="token plain"></span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain" style=display:inline-block></span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"></span><span class="token comment" style="color:hsl(230, 4%, 64%)"># Use the map so we can guarantee we maintain the correct order</span><span class="token plain"></span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain" style=display:inline-block></span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"></span><span class="token comment" style="color:hsl(230, 4%, 64%)"># So we can write as upper-bound</span><span class="token plain"></span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"></span><span class="token comment" style="color:hsl(230, 4%, 64%)"># sharpe_a = np.array(list(map(lambda tick: sharpes[tick], tickers))) * -1 </span><span class="token plain"></span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain">dd_a </span><span class="token operator" style="color:hsl(221, 87%, 60%)">=</span><span class="token plain"> np</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">.</span><span class="token plain">array</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">(</span><span class="token builtin" style="color:hsl(119, 34%, 47%)">list</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">(</span><span class="token builtin" style="color:hsl(119, 34%, 47%)">map</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">(</span><span class="token keyword" style="color:hsl(301, 63%, 40%)">lambda</span><span class="token plain"> tick</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">:</span><span class="token plain"> drawdowns</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">[</span><span class="token plain">tick</s
|
|||
|
<div class="codeBlockContainer_Ckt0 theme-code-block" style="--prism-background-color:hsl(230, 1%, 98%);--prism-color:hsl(230, 8%, 24%)"><div class=codeBlockContent_biex><pre tabindex=0 class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="background-color:hsl(230, 1%, 98%);color:hsl(230, 8%, 24%)"><code class=codeBlockLines_e6Vv><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> 'Optimization terminated successfully.'</span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> "Holdings: [('XOM', 5.8337945679814904),</span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> ('CVX', 42.935064321851307),</span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> ('CLB', -124.5),</span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> ('OXY', 36.790387773552119),</span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> ('SLB', 39.940753336615096)]"</span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> 'Expected Return: 32.375%'</span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> 'Expected Max Drawdown: 4.34%'</span><br></span></code></pre><div class=buttonGroup__atx><button type=button aria-label="Copy code to clipboard" title=Copy class=clean-btn><span class=copyButtonIcons_eSgA aria-hidden=true><svg viewBox="0 0 24 24" class=copyButtonIcon_y97N><path fill=currentColor d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z"/></svg><svg viewBox="0 0 24 24" class=copyButtonSuccessIcon_LjdS><path fill=currentColor d=M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z /></svg></span></button></div></div></div></div></article><nav class="pagination-nav docusaurus-mt-lg" aria-label="Blog post page navigation"><a class="pagination-nav__link pagination-nav__link--next" href=/2015/11/autocallable><div class=pagination-nav__sublabel>Newer post</div><div class=pagination-nav__label>Autocallable Bonds</div></a></nav></main><div class="col col--2"><div class="tableOfContents_bqdL thin-scrollbar"><ul class="table-of-contents table-of-contents__left-border"><li><a href=#trading-competition-optimization class="table-of-contents__link toc-highlight">Trading Competition Optimization</a><li><a href=#calculating-the-return class="table-of-contents__link toc-highlight">Calculating the Return</a><li><a href=#calculating-the-sharpe-ratio class="table-of-contents__link toc-highlight">Calculating the Sharpe ratio</a><li><a href=#calculating-the-drawdown class="table-of-contents__link toc-highlight">Calculating the drawdown</a></ul></div></div></div></div></div><footer class=footer><div class="container container-fluid"><div class="footer__bottom text--center"><div class=footer__copyright>Copyright © 2024 Bradlee Speice</div></div></div></footer></div>
|