speice.io/assets/js/818287cf.fc9a1bb7.js

1 line
28 KiB
JavaScript

"use strict";(self.webpackChunkspeice_io=self.webpackChunkspeice_io||[]).push([["5095"],{32181:function(s,e,n){n.r(e),n.d(e,{assets:function(){return c},contentTitle:function(){return r},default:function(){return d},frontMatter:function(){return l},metadata:function(){return a},toc:function(){return m}});var a=n(41915),t=n(85893),i=n(50065);let l={slug:"2015/11/welcome",title:"Welcome, and an algorithm",date:new Date("2015-11-19T12:00:00.000Z"),last_update:{date:new Date("2015-12-05T12:00:00.000Z")},authors:["bspeice"],tags:[]},r=void 0,c={authorsImageUrls:[void 0]},m=[{value:"Trading Competition Optimization",id:"trading-competition-optimization",level:2},{value:"Calculating the Return",id:"calculating-the-return",level:2},{value:"Calculating the Sharpe ratio",id:"calculating-the-sharpe-ratio",level:2},{value:"Calculating the drawdown",id:"calculating-the-drawdown",level:2}];function h(s){let e={a:"a",annotation:"annotation",code:"code",h1:"h1",h2:"h2",hr:"hr",li:"li",math:"math",mfrac:"mfrac",mi:"mi",mn:"mn",mo:"mo",mover:"mover",mrow:"mrow",mstyle:"mstyle",msub:"msub",mtable:"mtable",mtd:"mtd",mtext:"mtext",mtr:"mtr",p:"p",path:"path",pre:"pre",semantics:"semantics",span:"span",strong:"strong",svg:"svg",table:"table",tbody:"tbody",td:"td",th:"th",thead:"thead",tr:"tr",ul:"ul",...(0,i.a)(),...s.components};return(0,t.jsxs)(t.Fragment,{children:[(0,t.jsx)(e.p,{children:"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!"}),"\n",(0,t.jsx)(e.p,{children:"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."}),"\n",(0,t.jsx)(e.p,{children:"The competition is scored in 3 areas:"}),"\n",(0,t.jsxs)(e.ul,{children:["\n",(0,t.jsx)(e.li,{children:"Total return"}),"\n",(0,t.jsx)(e.li,{children:(0,t.jsx)(e.a,{href:"https://en.wikipedia.org/wiki/Sharpe_ratio",children:"Sharpe ratio"})}),"\n",(0,t.jsx)(e.li,{children:"Maximum drawdown"}),"\n"]}),"\n",(0,t.jsx)(e.p,{children:"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."}),"\n",(0,t.jsx)(e.p,{children:"I'll be updating this post with the results of our algorithm as they come along!"}),"\n",(0,t.jsx)(e.hr,{}),"\n",(0,t.jsxs)(e.p,{children:[(0,t.jsx)(e.strong,{children:"UPDATE 12/5/2015"}),": 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."]}),"\n",(0,t.jsxs)(e.ul,{children:["\n",(0,t.jsx)(e.li,{children:"After week 1: Down .1%"}),"\n",(0,t.jsx)(e.li,{children:"After week 2: Down 1.4%"}),"\n",(0,t.jsx)(e.li,{children:"After week 3: Flat"}),"\n"]}),"\n",(0,t.jsx)(e.p,{children:"And some statistics for all teams participating in the competition:"}),"\n",(0,t.jsxs)(e.table,{children:[(0,t.jsx)(e.thead,{children:(0,t.jsxs)(e.tr,{children:[(0,t.jsx)(e.th,{children:"Statistic"}),(0,t.jsx)(e.th,{children:"Value"})]})}),(0,t.jsxs)(e.tbody,{children:[(0,t.jsxs)(e.tr,{children:[(0,t.jsx)(e.td,{children:"Max Return"}),(0,t.jsx)(e.td,{children:"74.1%"})]}),(0,t.jsxs)(e.tr,{children:[(0,t.jsx)(e.td,{children:"Min Return"}),(0,t.jsx)(e.td,{children:"-97.4%"})]}),(0,t.jsxs)(e.tr,{children:[(0,t.jsx)(e.td,{children:"Average Return"}),(0,t.jsx)(e.td,{children:"-.1%"})]}),(0,t.jsxs)(e.tr,{children:[(0,t.jsx)(e.td,{children:"Std Dev of Returns"}),(0,t.jsx)(e.td,{children:"19.6%"})]})]})]}),"\n",(0,t.jsx)(e.hr,{}),"\n",(0,t.jsx)(e.h2,{id:"trading-competition-optimization",children:"Trading Competition Optimization"}),"\n",(0,t.jsx)(e.p,{children:(0,t.jsx)(e.strong,{children:"Goal: Max return given maximum Sharpe and Drawdown"})}),"\n",(0,t.jsx)(e.pre,{children:(0,t.jsx)(e.code,{className:"language-python",children:"from IPython.display import display\nimport Quandl\nfrom datetime import datetime, timedelta\n\ntickers = ['XOM', 'CVX', 'CLB', 'OXY', 'SLB']\nmarket_ticker = 'GOOG/NYSE_VOO'\nlookback = 30\nd_col = 'Close'\n\ndata = {tick: Quandl.get('YAHOO/{}'.format(tick))[-lookback:] for tick in tickers}\nmarket = Quandl.get(market_ticker)\n"})}),"\n",(0,t.jsx)(e.h2,{id:"calculating-the-return",children:"Calculating the Return"}),"\n",(0,t.jsx)(e.p,{children:"We first want to know how much each ticker returned over the prior period."}),"\n",(0,t.jsx)(e.pre,{children:(0,t.jsx)(e.code,{className:"language-python",children:"returns = {tick: data[tick][d_col].pct_change() for tick in tickers}\n\ndisplay({tick: returns[tick].mean() for tick in tickers})\n"})}),"\n",(0,t.jsx)(e.pre,{children:(0,t.jsx)(e.code,{children:" {'CLB': -0.0016320202164526894,\n 'CVX': 0.0010319531629488911,\n 'OXY': 0.00093418904454400551,\n 'SLB': 0.00098431254720448159,\n 'XOM': 0.00044165797556096868}\n"})}),"\n",(0,t.jsx)(e.h2,{id:"calculating-the-sharpe-ratio",children:"Calculating the Sharpe ratio"}),"\n",(0,t.jsxs)(e.p,{children:["Sharpe: ",(0,t.jsxs)(e.span,{className:"katex",children:[(0,t.jsx)(e.span,{className:"katex-mathml",children:(0,t.jsx)(e.math,{xmlns:"http://www.w3.org/1998/Math/MathML",children:(0,t.jsxs)(e.semantics,{children:[(0,t.jsx)(e.mrow,{children:(0,t.jsxs)(e.mfrac,{children:[(0,t.jsxs)(e.mrow,{children:[(0,t.jsx)(e.mi,{children:"R"}),(0,t.jsx)(e.mo,{children:"\u2212"}),(0,t.jsxs)(e.msub,{children:[(0,t.jsx)(e.mi,{children:"R"}),(0,t.jsx)(e.mi,{children:"M"})]})]}),(0,t.jsx)(e.mi,{children:"\u03C3"})]})}),(0,t.jsx)(e.annotation,{encoding:"application/x-tex",children:"{R - R_M \\over \\sigma}"})]})})}),(0,t.jsx)(e.span,{className:"katex-html","aria-hidden":"true",children:(0,t.jsxs)(e.span,{className:"base",children:[(0,t.jsx)(e.span,{className:"strut",style:{height:"1.2336em",verticalAlign:"-0.345em"}}),(0,t.jsx)(e.span,{className:"mord",children:(0,t.jsxs)(e.span,{className:"mord",children:[(0,t.jsx)(e.span,{className:"mopen nulldelimiter"}),(0,t.jsx)(e.span,{className:"mfrac",children:(0,t.jsxs)(e.span,{className:"vlist-t vlist-t2",children:[(0,t.jsxs)(e.span,{className:"vlist-r",children:[(0,t.jsxs)(e.span,{className:"vlist",style:{height:"0.8886em"},children:[(0,t.jsxs)(e.span,{style:{top:"-2.655em"},children:[(0,t.jsx)(e.span,{className:"pstrut",style:{height:"3em"}}),(0,t.jsx)(e.span,{className:"sizing reset-size6 size3 mtight",children:(0,t.jsx)(e.span,{className:"mord mtight",children:(0,t.jsx)(e.span,{className:"mord mathnormal mtight",style:{marginRight:"0.03588em"},children:"\u03C3"})})})]}),(0,t.jsxs)(e.span,{style:{top:"-3.23em"},children:[(0,t.jsx)(e.span,{className:"pstrut",style:{height:"3em"}}),(0,t.jsx)(e.span,{className:"frac-line",style:{borderBottomWidth:"0.04em"}})]}),(0,t.jsxs)(e.span,{style:{top:"-3.4103em"},children:[(0,t.jsx)(e.span,{className:"pstrut",style:{height:"3em"}}),(0,t.jsx)(e.span,{className:"sizing reset-size6 size3 mtight",children:(0,t.jsxs)(e.span,{className:"mord mtight",children:[(0,t.jsx)(e.span,{className:"mord mathnormal mtight",style:{marginRight:"0.00773em"},children:"R"}),(0,t.jsx)(e.span,{className:"mbin mtight",children:"\u2212"}),(0,t.jsxs)(e.span,{className:"mord mtight",children:[(0,t.jsx)(e.span,{className:"mord mathnormal mtight",style:{marginRight:"0.00773em"},children:"R"}),(0,t.jsx)(e.span,{className:"msupsub",children:(0,t.jsxs)(e.span,{className:"vlist-t vlist-t2",children:[(0,t.jsxs)(e.span,{className:"vlist-r",children:[(0,t.jsx)(e.span,{className:"vlist",style:{height:"0.3448em"},children:(0,t.jsxs)(e.span,{style:{top:"-2.3567em",marginLeft:"-0.0077em",marginRight:"0.0714em"},children:[(0,t.jsx)(e.span,{className:"pstrut",style:{height:"2.5em"}}),(0,t.jsx)(e.span,{className:"sizing reset-size3 size1 mtight",children:(0,t.jsx)(e.span,{className:"mord mathnormal mtight",style:{marginRight:"0.10903em"},children:"M"})})]})}),(0,t.jsx)(e.span,{className:"vlist-s",children:"\u200B"})]}),(0,t.jsx)(e.span,{className:"vlist-r",children:(0,t.jsx)(e.span,{className:"vlist",style:{height:"0.1433em"},children:(0,t.jsx)(e.span,{})})})]})})]})]})})]})]}),(0,t.jsx)(e.span,{className:"vlist-s",children:"\u200B"})]}),(0,t.jsx)(e.span,{className:"vlist-r",children:(0,t.jsx)(e.span,{className:"vlist",style:{height:"0.345em"},children:(0,t.jsx)(e.span,{})})})]})}),(0,t.jsx)(e.span,{className:"mclose nulldelimiter"})]})})]})})]})]}),"\n",(0,t.jsx)(e.p,{children:"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."}),"\n",(0,t.jsx)(e.pre,{children:(0,t.jsx)(e.code,{className:"language-python",children:"market_returns = market.pct_change()\n\nsharpe = lambda ret: (ret.mean() - market_returns[d_col].mean()) / ret.std()\nsharpes = {tick: sharpe(returns[tick]) for tick in tickers}\n\ndisplay(sharpes)\n"})}),"\n",(0,t.jsx)(e.pre,{children:(0,t.jsx)(e.code,{children:" {'CLB': -0.10578734457846127,\n 'CVX': 0.027303529817677398,\n 'OXY': 0.022622210057414487,\n 'SLB': 0.026950946344858676,\n 'XOM': -0.0053519259698605499}\n"})}),"\n",(0,t.jsx)(e.h2,{id:"calculating-the-drawdown",children:"Calculating the drawdown"}),"\n",(0,t.jsx)(e.p,{children:"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?"}),"\n",(0,t.jsx)(e.pre,{children:(0,t.jsx)(e.code,{className:"language-python",children:"drawdown = lambda ret: ret.abs().max()\ndrawdowns = {tick: drawdown(returns[tick]) for tick in tickers}\n\ndisplay(drawdowns)\n"})}),"\n",(0,t.jsx)(e.pre,{children:(0,t.jsx)(e.code,{children:" {'CLB': 0.043551495607375035,\n 'CVX': 0.044894389686214398,\n 'OXY': 0.051424517867144637,\n 'SLB': 0.034774627850375328,\n 'XOM': 0.035851524605672758}\n"})}),"\n",(0,t.jsx)(e.h1,{id:"performing-the-optimization",children:"Performing the optimization"}),"\n",(0,t.jsx)(e.span,{className:"katex-display",children:(0,t.jsxs)(e.span,{className:"katex",children:[(0,t.jsx)(e.span,{className:"katex-mathml",children:(0,t.jsx)(e.math,{xmlns:"http://www.w3.org/1998/Math/MathML",display:"block",children:(0,t.jsxs)(e.semantics,{children:[(0,t.jsxs)(e.mtable,{rowspacing:"0.25em",columnalign:"right left",columnspacing:"0em",children:[(0,t.jsxs)(e.mtr,{children:[(0,t.jsx)(e.mtd,{children:(0,t.jsx)(e.mstyle,{scriptlevel:"0",displaystyle:"true",children:(0,t.jsxs)(e.mrow,{children:[(0,t.jsx)(e.mi,{children:"m"}),(0,t.jsx)(e.mi,{children:"a"}),(0,t.jsx)(e.mi,{children:"x"}),(0,t.jsx)(e.mtext,{children:"\xa0\xa0"})]})})}),(0,t.jsx)(e.mtd,{children:(0,t.jsx)(e.mstyle,{scriptlevel:"0",displaystyle:"true",children:(0,t.jsxs)(e.mrow,{children:[(0,t.jsx)(e.mrow,{}),(0,t.jsx)(e.mi,{children:"\u03BC"}),(0,t.jsx)(e.mo,{children:"\u22C5"}),(0,t.jsx)(e.mi,{children:"\u03C9"})]})})})]}),(0,t.jsxs)(e.mtr,{children:[(0,t.jsx)(e.mtd,{children:(0,t.jsx)(e.mstyle,{scriptlevel:"0",displaystyle:"true",children:(0,t.jsxs)(e.mrow,{children:[(0,t.jsx)(e.mi,{children:"s"}),(0,t.jsx)(e.mi,{mathvariant:"normal",children:"."}),(0,t.jsx)(e.mi,{children:"t"}),(0,t.jsx)(e.mi,{mathvariant:"normal",children:"."}),(0,t.jsx)(e.mtext,{children:"\xa0\xa0"})]})})}),(0,t.jsx)(e.mtd,{children:(0,t.jsx)(e.mstyle,{scriptlevel:"0",displaystyle:"true",children:(0,t.jsxs)(e.mrow,{children:[(0,t.jsx)(e.mrow,{}),(0,t.jsxs)(e.mover,{accent:"true",children:[(0,t.jsx)(e.mn,{children:"1"}),(0,t.jsx)(e.mo,{children:"\u20D7"})]}),(0,t.jsx)(e.mi,{children:"\u03C9"}),(0,t.jsx)(e.mo,{children:"="}),(0,t.jsx)(e.mn,{children:"1"})]})})})]}),(0,t.jsxs)(e.mtr,{children:[(0,t.jsx)(e.mtd,{children:(0,t.jsx)(e.mstyle,{scriptlevel:"0",displaystyle:"true",children:(0,t.jsx)(e.mrow,{})})}),(0,t.jsx)(e.mtd,{children:(0,t.jsx)(e.mstyle,{scriptlevel:"0",displaystyle:"true",children:(0,t.jsxs)(e.mrow,{children:[(0,t.jsx)(e.mrow,{}),(0,t.jsxs)(e.mover,{accent:"true",children:[(0,t.jsx)(e.mi,{children:"S"}),(0,t.jsx)(e.mo,{children:"\u20D7"})]}),(0,t.jsx)(e.mi,{children:"\u03C9"}),(0,t.jsx)(e.mo,{children:"\u2265"}),(0,t.jsx)(e.mi,{children:"s"})]})})})]}),(0,t.jsxs)(e.mtr,{children:[(0,t.jsx)(e.mtd,{children:(0,t.jsx)(e.mstyle,{scriptlevel:"0",displaystyle:"true",children:(0,t.jsx)(e.mrow,{})})}),(0,t.jsx)(e.mtd,{children:(0,t.jsx)(e.mstyle,{scriptlevel:"0",displaystyle:"true",children:(0,t.jsxs)(e.mrow,{children:[(0,t.jsx)(e.mrow,{}),(0,t.jsxs)(e.mover,{accent:"true",children:[(0,t.jsx)(e.mi,{children:"D"}),(0,t.jsx)(e.mo,{children:"\u20D7"})]}),(0,t.jsx)(e.mo,{children:"\u22C5"}),(0,t.jsx)(e.mi,{mathvariant:"normal",children:"\u2223"}),(0,t.jsx)(e.mi,{children:"\u03C9"}),(0,t.jsx)(e.mi,{mathvariant:"normal",children:"\u2223"}),(0,t.jsx)(e.mo,{children:"\u2264"}),(0,t.jsx)(e.mi,{children:"d"})]})})})]}),(0,t.jsxs)(e.mtr,{children:[(0,t.jsx)(e.mtd,{children:(0,t.jsx)(e.mstyle,{scriptlevel:"0",displaystyle:"true",children:(0,t.jsx)(e.mrow,{})})}),(0,t.jsx)(e.mtd,{children:(0,t.jsx)(e.mstyle,{scriptlevel:"0",displaystyle:"true",children:(0,t.jsxs)(e.mrow,{children:[(0,t.jsx)(e.mrow,{}),(0,t.jsxs)(e.mrow,{children:[(0,t.jsx)(e.mo,{fence:"true",children:"\u2223"}),(0,t.jsx)(e.mi,{children:"\u03C9"}),(0,t.jsx)(e.mo,{fence:"true",children:"\u2223"})]}),(0,t.jsx)(e.mo,{children:"\u2264"}),(0,t.jsx)(e.mi,{children:"l"})]})})})]})]}),(0,t.jsx)(e.annotation,{encoding:"application/x-tex",children:"\\begin{align*}\nmax\\ \\ & \\mu \\cdot \\omega\\\\\ns.t.\\ \\ & \\vec{1} \\omega = 1\\\\\n& \\vec{S} \\omega \\ge s\\\\\n& \\vec{D} \\cdot | \\omega | \\le d\\\\\n& \\left|\\omega\\right| \\le l\\\\\n\\end{align*}"})]})})}),(0,t.jsx)(e.span,{className:"katex-html","aria-hidden":"true",children:(0,t.jsxs)(e.span,{className:"base",children:[(0,t.jsx)(e.span,{className:"strut",style:{height:"7.8401em",verticalAlign:"-3.6701em"}}),(0,t.jsx)(e.span,{className:"mord",children:(0,t.jsxs)(e.span,{className:"mtable",children:[(0,t.jsx)(e.span,{className:"col-align-r",children:(0,t.jsxs)(e.span,{className:"vlist-t vlist-t2",children:[(0,t.jsxs)(e.span,{className:"vlist-r",children:[(0,t.jsxs)(e.span,{className:"vlist",style:{height:"4.17em"},children:[(0,t.jsxs)(e.span,{style:{top:"-6.33em"},children:[(0,t.jsx)(e.span,{className:"pstrut",style:{height:"3em"}}),(0,t.jsxs)(e.span,{className:"mord",children:[(0,t.jsx)(e.span,{className:"mord mathnormal",children:"ma"}),(0,t.jsx)(e.span,{className:"mord mathnormal",children:"x"}),(0,t.jsx)(e.span,{className:"mspace",children:"\xa0"}),(0,t.jsx)(e.span,{className:"mspace",children:"\xa0"})]})]}),(0,t.jsxs)(e.span,{style:{top:"-4.7426em"},children:[(0,t.jsx)(e.span,{className:"pstrut",style:{height:"3em"}}),(0,t.jsxs)(e.span,{className:"mord",children:[(0,t.jsx)(e.span,{className:"mord mathnormal",children:"s"}),(0,t.jsx)(e.span,{className:"mord",children:"."}),(0,t.jsx)(e.span,{className:"mord mathnormal",children:"t"}),(0,t.jsx)(e.span,{className:"mord",children:"."}),(0,t.jsx)(e.span,{className:"mspace",children:"\xa0"}),(0,t.jsx)(e.span,{className:"mspace",children:"\xa0"})]})]}),(0,t.jsxs)(e.span,{style:{top:"-3.1163em"},children:[(0,t.jsx)(e.span,{className:"pstrut",style:{height:"3em"}}),(0,t.jsx)(e.span,{className:"mord"})]}),(0,t.jsxs)(e.span,{style:{top:"-1.4899em"},children:[(0,t.jsx)(e.span,{className:"pstrut",style:{height:"3em"}}),(0,t.jsx)(e.span,{className:"mord"})]}),(0,t.jsxs)(e.span,{style:{top:"0.0101em"},children:[(0,t.jsx)(e.span,{className:"pstrut",style:{height:"3em"}}),(0,t.jsx)(e.span,{className:"mord"})]})]}),(0,t.jsx)(e.span,{className:"vlist-s",children:"\u200B"})]}),(0,t.jsx)(e.span,{className:"vlist-r",children:(0,t.jsx)(e.span,{className:"vlist",style:{height:"3.6701em"},children:(0,t.jsx)(e.span,{})})})]})}),(0,t.jsx)(e.span,{className:"col-align-l",children:(0,t.jsxs)(e.span,{className:"vlist-t vlist-t2",children:[(0,t.jsxs)(e.span,{className:"vlist-r",children:[(0,t.jsxs)(e.span,{className:"vlist",style:{height:"4.17em"},children:[(0,t.jsxs)(e.span,{style:{top:"-6.33em"},children:[(0,t.jsx)(e.span,{className:"pstrut",style:{height:"3em"}}),(0,t.jsxs)(e.span,{className:"mord",children:[(0,t.jsx)(e.span,{className:"mord"}),(0,t.jsx)(e.span,{className:"mord mathnormal",children:"\u03BC"}),(0,t.jsx)(e.span,{className:"mspace",style:{marginRight:"0.2222em"}}),(0,t.jsx)(e.span,{className:"mbin",children:"\u22C5"}),(0,t.jsx)(e.span,{className:"mspace",style:{marginRight:"0.2222em"}}),(0,t.jsx)(e.span,{className:"mord mathnormal",style:{marginRight:"0.03588em"},children:"\u03C9"})]})]}),(0,t.jsxs)(e.span,{style:{top:"-4.7426em"},children:[(0,t.jsx)(e.span,{className:"pstrut",style:{height:"3em"}}),(0,t.jsxs)(e.span,{className:"mord",children:[(0,t.jsx)(e.span,{className:"mord"}),(0,t.jsx)(e.span,{className:"mord accent",children:(0,t.jsx)(e.span,{className:"vlist-t",children:(0,t.jsx)(e.span,{className:"vlist-r",children:(0,t.jsxs)(e.span,{className:"vlist",style:{height:"0.9274em"},children:[(0,t.jsxs)(e.span,{style:{top:"-3em"},children:[(0,t.jsx)(e.span,{className:"pstrut",style:{height:"3em"}}),(0,t.jsx)(e.span,{className:"mord",children:"1"})]}),(0,t.jsxs)(e.span,{style:{top:"-3.2134em"},children:[(0,t.jsx)(e.span,{className:"pstrut",style:{height:"3em"}}),(0,t.jsx)(e.span,{className:"accent-body",style:{left:"-0.2355em"},children:(0,t.jsx)(e.span,{className:"overlay",style:{height:"0.714em",width:"0.471em"},children:(0,t.jsx)(e.svg,{xmlns:"http://www.w3.org/2000/svg",width:"0.471em",height:"0.714em",style:{width:"0.471em"},viewBox:"0 0 471 714",preserveAspectRatio:"xMinYMin",children:(0,t.jsx)(e.path,{d:"M377 20c0-5.333 1.833-10 5.5-14S391 0 397 0c4.667 0 8.667 1.667 12 5\n3.333 2.667 6.667 9 10 19 6.667 24.667 20.333 43.667 41 57 7.333 4.667 11\n10.667 11 18 0 6-1 10-3 12s-6.667 5-14 9c-28.667 14.667-53.667 35.667-75 63\n-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\n-7 1c-4.667 0-9.167-1.833-13.5-5.5S337 184 337 178c0-12.667 15.667-32.333 47-59\nH213l-171-1c-8.667-6-13-12.333-13-19 0-4.667 4.333-11.333 13-20h359\nc-16-25.333-24-45-24-59z"})})})})]})]})})})}),(0,t.jsx)(e.span,{className:"mord mathnormal",style:{marginRight:"0.03588em"},children:"\u03C9"}),(0,t.jsx)(e.span,{className:"mspace",style:{marginRight:"0.2778em"}}),(0,t.jsx)(e.span,{className:"mrel",children:"="}),(0,t.jsx)(e.span,{className:"mspace",style:{marginRight:"0.2778em"}}),(0,t.jsx)(e.span,{className:"mord",children:"1"})]})]}),(0,t.jsxs)(e.span,{style:{top:"-3.1163em"},children:[(0,t.jsx)(e.span,{className:"pstrut",style:{height:"3em"}}),(0,t.jsxs)(e.span,{className:"mord",children:[(0,t.jsx)(e.span,{className:"mord"}),(0,t.jsx)(e.span,{className:"mord accent",children:(0,t.jsx)(e.span,{className:"vlist-t",children:(0,t.jsx)(e.span,{className:"vlist-r",children:(0,t.jsxs)(e.span,{className:"vlist",style:{height:"0.9663em"},children:[(0,t.jsxs)(e.span,{style:{top:"-3em"},children:[(0,t.jsx)(e.span,{className:"pstrut",style:{height:"3em"}}),(0,t.jsx)(e.span,{className:"mord mathnormal",style:{marginRight:"0.05764em"},children:"S"})]}),(0,t.jsxs)(e.span,{style:{top:"-3.2523em"},children:[(0,t.jsx)(e.span,{className:"pstrut",style:{height:"3em"}}),(0,t.jsx)(e.span,{className:"accent-body",style:{left:"-0.1522em"},children:(0,t.jsx)(e.span,{className:"overlay",style:{height:"0.714em",width:"0.471em"},children:(0,t.jsx)(e.svg,{xmlns:"http://www.w3.org/2000/svg",width:"0.471em",height:"0.714em",style:{width:"0.471em"},viewBox:"0 0 471 714",preserveAspectRatio:"xMinYMin",children:(0,t.jsx)(e.path,{d:"M377 20c0-5.333 1.833-10 5.5-14S391 0 397 0c4.667 0 8.667 1.667 12 5\n3.333 2.667 6.667 9 10 19 6.667 24.667 20.333 43.667 41 57 7.333 4.667 11\n10.667 11 18 0 6-1 10-3 12s-6.667 5-14 9c-28.667 14.667-53.667 35.667-75 63\n-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\n-7 1c-4.667 0-9.167-1.833-13.5-5.5S337 184 337 178c0-12.667 15.667-32.333 47-59\nH213l-171-1c-8.667-6-13-12.333-13-19 0-4.667 4.333-11.333 13-20h359\nc-16-25.333-24-45-24-59z"})})})})]})]})})})}),(0,t.jsx)(e.span,{className:"mord mathnormal",style:{marginRight:"0.03588em"},children:"\u03C9"}),(0,t.jsx)(e.span,{className:"mspace",style:{marginRight:"0.2778em"}}),(0,t.jsx)(e.span,{className:"mrel",children:"\u2265"}),(0,t.jsx)(e.span,{className:"mspace",style:{marginRight:"0.2778em"}}),(0,t.jsx)(e.span,{className:"mord mathnormal",children:"s"})]})]}),(0,t.jsxs)(e.span,{style:{top:"-1.4899em"},children:[(0,t.jsx)(e.span,{className:"pstrut",style:{height:"3em"}}),(0,t.jsxs)(e.span,{className:"mord",children:[(0,t.jsx)(e.span,{className:"mord"}),(0,t.jsx)(e.span,{className:"mord accent",children:(0,t.jsx)(e.span,{className:"vlist-t",children:(0,t.jsx)(e.span,{className:"vlist-r",children:(0,t.jsxs)(e.span,{className:"vlist",style:{height:"0.9663em"},children:[(0,t.jsxs)(e.span,{style:{top:"-3em"},children:[(0,t.jsx)(e.span,{className:"pstrut",style:{height:"3em"}}),(0,t.jsx)(e.span,{className:"mord mathnormal",style:{marginRight:"0.02778em"},children:"D"})]}),(0,t.jsxs)(e.span,{style:{top:"-3.2523em"},children:[(0,t.jsx)(e.span,{className:"pstrut",style:{height:"3em"}}),(0,t.jsx)(e.span,{className:"accent-body",style:{left:"-0.1799em"},children:(0,t.jsx)(e.span,{className:"overlay",style:{height:"0.714em",width:"0.471em"},children:(0,t.jsx)(e.svg,{xmlns:"http://www.w3.org/2000/svg",width:"0.471em",height:"0.714em",style:{width:"0.471em"},viewBox:"0 0 471 714",preserveAspectRatio:"xMinYMin",children:(0,t.jsx)(e.path,{d:"M377 20c0-5.333 1.833-10 5.5-14S391 0 397 0c4.667 0 8.667 1.667 12 5\n3.333 2.667 6.667 9 10 19 6.667 24.667 20.333 43.667 41 57 7.333 4.667 11\n10.667 11 18 0 6-1 10-3 12s-6.667 5-14 9c-28.667 14.667-53.667 35.667-75 63\n-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\n-7 1c-4.667 0-9.167-1.833-13.5-5.5S337 184 337 178c0-12.667 15.667-32.333 47-59\nH213l-171-1c-8.667-6-13-12.333-13-19 0-4.667 4.333-11.333 13-20h359\nc-16-25.333-24-45-24-59z"})})})})]})]})})})}),(0,t.jsx)(e.span,{className:"mspace",style:{marginRight:"0.2222em"}}),(0,t.jsx)(e.span,{className:"mbin",children:"\u22C5"}),(0,t.jsx)(e.span,{className:"mspace",style:{marginRight:"0.2222em"}}),(0,t.jsx)(e.span,{className:"mord",children:"\u2223"}),(0,t.jsx)(e.span,{className:"mord mathnormal",style:{marginRight:"0.03588em"},children:"\u03C9"}),(0,t.jsx)(e.span,{className:"mord",children:"\u2223"}),(0,t.jsx)(e.span,{className:"mspace",style:{marginRight:"0.2778em"}}),(0,t.jsx)(e.span,{className:"mrel",children:"\u2264"}),(0,t.jsx)(e.span,{className:"mspace",style:{marginRight:"0.2778em"}}),(0,t.jsx)(e.span,{className:"mord mathnormal",children:"d"})]})]}),(0,t.jsxs)(e.span,{style:{top:"0.0101em"},children:[(0,t.jsx)(e.span,{className:"pstrut",style:{height:"3em"}}),(0,t.jsxs)(e.span,{className:"mord",children:[(0,t.jsx)(e.span,{className:"mord"}),(0,t.jsx)(e.span,{className:"mspace",style:{marginRight:"0.1667em"}}),(0,t.jsxs)(e.span,{className:"minner",children:[(0,t.jsx)(e.span,{className:"mopen delimcenter",style:{top:"0em"},children:"\u2223"}),(0,t.jsx)(e.span,{className:"mord mathnormal",style:{marginRight:"0.03588em"},children:"\u03C9"}),(0,t.jsx)(e.span,{className:"mclose delimcenter",style:{top:"0em"},children:"\u2223"})]}),(0,t.jsx)(e.span,{className:"mspace",style:{marginRight:"0.2778em"}}),(0,t.jsx)(e.span,{className:"mrel",children:"\u2264"}),(0,t.jsx)(e.span,{className:"mspace",style:{marginRight:"0.2778em"}}),(0,t.jsx)(e.span,{className:"mord mathnormal",style:{marginRight:"0.01968em"},children:"l"})]})]})]}),(0,t.jsx)(e.span,{className:"vlist-s",children:"\u200B"})]}),(0,t.jsx)(e.span,{className:"vlist-r",children:(0,t.jsx)(e.span,{className:"vlist",style:{height:"3.6701em"},children:(0,t.jsx)(e.span,{})})})]})})]})})]})})]})}),"\n",(0,t.jsx)(e.p,{children:"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."}),"\n",(0,t.jsx)(e.pre,{children:(0,t.jsx)(e.code,{className:"language-python",children:"import numpy as np\nfrom scipy.optimize import minimize\n\n#sharpe_limit = .1\ndrawdown_limit = .05\nleverage = 250\n\n# Use the map so we can guarantee we maintain the correct order\n\n# So we can write as upper-bound\n# sharpe_a = np.array(list(map(lambda tick: sharpes[tick], tickers))) * -1 \ndd_a = np.array(list(map(lambda tick: drawdowns[tick], tickers)))\n\n# Because minimizing\nreturns_a = np.array(list(map(lambda tick: returns[tick].mean(), tickers))) \n\nmeets_sharpe = lambda x: sum(abs(x) * sharpe_a) - sharpe_limit\ndef meets_dd(x):\n portfolio = sum(abs(x))\n if portfolio < .1:\n # If there are no stocks in the portfolio,\n # we can accidentally induce division by 0,\n # or division by something small enough to cause infinity\n return 0\n \n return drawdown_limit - sum(abs(x) * dd_a) / sum(abs(x))\n\nis_portfolio = lambda x: sum(x) - 1\n\ndef within_leverage(x):\n return leverage - sum(abs(x))\n\nobjective = lambda x: sum(x * returns_a) * -1 # Because we're minimizing\nbounds = ((None, None),) * len(tickers)\nx = np.zeros(len(tickers))\n\nconstraints = [\n {\n 'type': 'eq',\n 'fun': is_portfolio\n }, {\n 'type': 'ineq',\n 'fun': within_leverage\n #}, {\n # 'type': 'ineq',\n # 'fun': meets_sharpe\n }, {\n 'type': 'ineq',\n 'fun': meets_dd\n }\n]\n\noptimal = minimize(objective, x, bounds=bounds, constraints=constraints,\n options={'maxiter': 500})\n\n# Optimization time!\ndisplay(optimal.message)\n\ndisplay(\"Holdings: {}\".format(list(zip(tickers, optimal.x))))\n\n# multiply by -100 to scale, and compensate for minimizing\nexpected_return = optimal.fun * -100\ndisplay(\"Expected Return: {:.3f}%\".format(expected_return))\n\nexpected_drawdown = sum(abs(optimal.x) * dd_a) / sum(abs(optimal.x)) * 100\ndisplay(\"Expected Max Drawdown: {0:.2f}%\".format(expected_drawdown))\n\n# TODO: Calculate expected Sharpe\n"})}),"\n",(0,t.jsx)(e.pre,{children:(0,t.jsx)(e.code,{children:" 'Optimization terminated successfully.'\n \"Holdings: [('XOM', 5.8337945679814904),\n ('CVX', 42.935064321851307),\n ('CLB', -124.5),\n ('OXY', 36.790387773552119),\n ('SLB', 39.940753336615096)]\"\n 'Expected Return: 32.375%'\n 'Expected Max Drawdown: 4.34%'\n"})})]})}function d(s={}){let{wrapper:e}={...(0,i.a)(),...s.components};return e?(0,t.jsx)(e,{...s,children:(0,t.jsx)(h,{...s})}):h(s)}},50065:function(s,e,n){n.d(e,{Z:function(){return r},a:function(){return l}});var a=n(67294);let t={},i=a.createContext(t);function l(s){let e=a.useContext(i);return a.useMemo(function(){return"function"==typeof s?s(e):{...e,...s}},[e,s])}function r(s){let e;return e=s.disableParentContext?"function"==typeof s.components?s.components(t):s.components||t:l(s.components),a.createElement(i.Provider,{value:e},s.children)}},41915:function(s){s.exports=JSON.parse('{"permalink":"/2015/11/welcome","source":"@site/blog/2015-11-14-welcome/index.mdx","title":"Welcome, and an algorithm","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!","date":"2015-11-19T12:00:00.000Z","tags":[],"readingTime":4.175,"hasTruncateMarker":true,"authors":[{"name":"Bradlee Speice","socials":{"github":"https://github.com/bspeice"},"key":"bspeice","page":null}],"frontMatter":{"slug":"2015/11/welcome","title":"Welcome, and an algorithm","date":"2015-11-19T12:00:00.000Z","last_update":{"date":"2015-12-05T12:00:00.000Z"},"authors":["bspeice"],"tags":[]},"unlisted":false,"lastUpdatedAt":1449316800000,"prevItem":{"title":"Autocallable Bonds","permalink":"/2015/11/autocallable"}}')}}]);