(cid:2) TrimSize:6inx9in Chan ffirs.tex V2-12/11/2016 12:44A.M. Pagei M T ACHINE RADING (cid:2) (cid:2) (cid:2) (cid:2) TrimSize:6inx9in Chan ffirs.tex V2-12/11/2016 12:44A.M. Pageii Founded in 1807, John Wiley & Sons is the oldest independent pub- lishing company in the United States. With offices in North America, Europe,Australia,andAsia,Wileyisgloballycommittedtodeveloping and marketing print and electronic products and services for our customers’professionalandpersonalknowledgeandunderstanding. The Wiley Trading series features books by traders who have sur- vivedthemarket’sever-changingtemperamentandhaveprospered— (cid:2) (cid:2) some by reinventing systems, others by getting back to basics. Whether a novice trader, professional, or somewhere in between, these books will provide the advice and strategies needed to prosper todayandwellintothefuture. Formoreonthisseries,visitourwebsiteatwww.WileyTrading.com. (cid:2) (cid:2) TrimSize:6inx9in Chan ffirs.tex V2-12/11/2016 12:44A.M. Pageiii M ACHINE T RADING Deploying Computer Algorithms to Conquer the Markets (cid:2) (cid:2) Ernest P. Chan (cid:2) (cid:2) TrimSize:6inx9in Chan ffirs.tex V2-12/11/2016 12:44A.M. Pageiv Copyright©2017byErnestP.Chan.Allrightsreserved. PublishedbyJohnWiley&Sons,Inc.,Hoboken,NewJersey. PublishedsimultaneouslyinCanada. Nopartofthispublicationmaybereproduced,storedinaretrievalsystem,ortransmittedinany formorbyanymeans,electronic,mechanical,photocopying,recording,scanning,orotherwise, exceptaspermittedunderSection107or108ofthe1976UnitedStatesCopyrightAct,without eitherthepriorwrittenpermissionofthePublisher,orauthorizationthroughpaymentofthe appropriateper-copyfeetotheCopyrightClearanceCenter,Inc.,222RosewoodDrive,Danvers, MA01923,(978)750-8400,fax(978)646-8600,orontheWebatwww.copyright.com.Requests tothePublisherforpermissionshouldbeaddressedtothePermissionsDepartment,JohnWiley& Sons,Inc.,111RiverStreet,Hoboken,NJ07030,(201)748-6011,fax(201)748-6008,oronline athttp://www.wiley.com/go/permissions. LimitofLiability/DisclaimerofWarranty:Whilethepublisherandauthorhaveusedtheirbest effortsinpreparingthisbook,theymakenorepresentationsorwarrantieswithrespecttothe accuracyorcompletenessofthecontentsofthisbookandspecificallydisclaimanyimplied warrantiesofmerchantabilityorfitnessforaparticularpurpose.Nowarrantymaybecreatedor extendedbysalesrepresentativesorwrittensalesmaterials.Theadviceandstrategiescontained hereinmaynotbesuitableforyoursituation.Youshouldconsultwithaprofessionalwhere appropriate.Neitherthepublishernorauthorshallbeliableforanylossofprofitoranyother commercialdamages,includingbutnotlimitedtospecial,incidental,consequential,orother (cid:2) (cid:2) damages. Forgeneralinformationonourotherproductsandservicesorfortechnicalsupport,pleasecontact ourCustomerCareDepartmentwithintheUnitedStatesat(800)762-2974,outsidetheUnited Statesat(317)572-3993,orfax(317)572-4002. Wileypublishesinavarietyofprintandelectronicformatsandbyprint-on-demand.Somematerial includedwithstandardprintversionsofthisbookmaynotbeincludedine-booksorin print-on-demand.IfthisbookreferstomediasuchasaCDorDVDthatisnotincludedinthe versionyoupurchased,youmaydownloadthismaterialathttp://booksupport.wiley.com.For moreinformationaboutWileyproducts,visitwww.wiley.com. LibraryofCongressCataloging-in-PublicationDataisavailable: ISBN978-1-119-21960-6(Hardcover) ISBN978-1-119-21967-5(ePDF) ISBN978-1-119-21965-1(ePub) CoverDesign:Wiley CoverImages:Wave©kdrkara90/Shutterstock;Abstractbackground©Marina Koven/Shutterstock;FractalRealms©agsandrew/Shutterstock PrintedintheUnitedStatesofAmerica 10 9 8 7 6 5 4 3 2 1 (cid:2) (cid:2) TrimSize:6inx9in Chan ffirs.tex V2-12/11/2016 12:44A.M. Pagev To my mom, Ching, my spouse, Ben, and to the memory of my beloved father, Hung Yip. (cid:2) (cid:2) (cid:2) (cid:2) TrimSize:6inx9in Chan ftoc.tex V1-12/11/2016 12:44A.M. Pagevii CONTENTS Preface ix CHAPTER1 TheBasicsofAlgorithmicTrading 1 vii (cid:2) CHAPTER2 FactorModels 27 (cid:2) CHAPTER3 Time-SeriesAnalysis 59 CHAPTER4 ArtificialIntelligenceTechniques 83 CHAPTER5 OptionsStrategies 119 CHAPTER6 IntradayTradingandMarket Microstructure 159 CHAPTER7 Bitcoins 201 CHAPTER8 AlgorithmicTradingIsGoodforBody andSoul 215 Bibliography 227 AbouttheAuthor 235 Index 237 (cid:2) (cid:2) TrimSize:6inx9in Chan fpref.tex V2-12/11/2016 12:44A.M. Pageix PREFACE Thebestwaytolearnsomethingreallywellistoteachittosomeoneelse (BarghandSchul,1980).SoIconfessthatonemajormotivationformy writingthisbook,thethirdandthemostadvancedtodateinaseries,isto forcemyselftostudyinmoredepththefollowingtopics: ix (cid:2) (cid:2) ■ The latest backtesting and trading platforms and the best and most cost-effectivevendorsforallmannersofdata(Chapter1); ■ Howtopickthebestbrokerforalgorithmicexecutionsandwhatprecau- tionsweshouldtake(Chapter1); ■ Thesimplestwaytooptimizeallocationstodifferentassetsandstrategies (Chapter1); ■ Factormodelsinalltheirglory,includingthosederivedfromtheoptions market,andwhytheycanbeusefultoshort-termtraders(Chapter2); ■ Timeseriestechniques:ARIMA,VAR,andstatespacemodels(withhid- denvariables)asappliedtopracticaltrading(Chapter3); ■ Artificialintelligence/machinelearningtechniques:particularlymethods thatwillreduceoverfitting(Chapter4); ■ Options and volatility trading strategies, including those that involve portfoliosofoptions(Chapter5); (cid:2) (cid:2) TrimSize:6inx9in Chan fpref.tex V2-12/11/2016 12:44A.M. Pagex ■ Intraday and higher frequency trading: market microstructure, order types and routing optimization, dark pools, adverse selection, order flow,andhowtobacktestintradaystrategieswithtickdata(Chapter6); ■ Bitcoins: bringing some of the techniques we covered to this new asset class(Chapter7); ■ Howtokeepupwiththelatestknowledge(Chapter8); ■ Transitioning from a proprietary trader to an investment advisor (Chapter8). I don’t know if these topics will excite you or bring you profits, but my study of them has certainly improved my own money management skills. Besides,sharingknowledgeandideasisfunandultimatelyconducivetocre- ativityandprofits. You will find most of the materials quite accessible to anyone who has some experience in a quantitative field, be it computer science, engineer- ing,orphysics.Notmuchpriorknowledgeoftradingandfinanceisassumed (except for the chapter on options, where we do assume basic familiar- ity).However,ifyouarecompletelynewtotrading,youmayfindmymore basictreatmentsinQuantitativeTrading(Chan,2009)andAlgorithmicTrading x (cid:2) (cid:2) (Chan,2013)easiertounderstand.Thisbookcanbetreatedasacontinua- tionofmyfirsttwobooks,withcoverageontopicsthatIhavenotdiscussed E before,butitcanalsobereadindependently. C A F Although many prototype trading strategies have been included as E R P examples,oneshoulddefinitelynottreatthemasshrink-wrappedproducts ready to deploy in live trading. As I have emphasized in my previous books, nobody should trade someone else’s strategies without a thorough, independentbacktest,removingalllikelysourcesofbiasesanddataerrors, and adding various variations for improvement. Most, if not all, the strategies I describe contain hidden biases in one way or another, waiting foryoutounearthandeliminate. I use MATLAB for all of my research in trading. I find it extremely user-friendly, with constantly improving and new features, and with an increasingnumberofspecializedtoolboxesthatIcandrawon.Forexample, without the Statistics and Machine Learning Toolbox, it would take much longer to explore using AI/ML techniques for trading. (See why Google scientistandmachinelearningexpertKevinMurphyprefersMATLABtoR forAI/MLresearchinMurphy,2015.)Inthepast,readershavecomplained aboutthehighpriceofaMATLABlicense.Butnow,itcostsonly$150fora (cid:2) (cid:2) TrimSize:6inx9in Chan fpref.tex V2-12/11/2016 12:44A.M. Pagexi ‘‘Home’’license,witheachadditionaltoolboxcostingonly$45.Noserious traders should compromise their productivity because of this small cost. IamalsofamiliarwithR,whichisacloserelativetoMATLAB.Butfrankly, itisnomatchforMATLABintermsofperformanceanduser-friendliness. AdetailedcomparisonoftheselanguagescanbefoundinChapters1and6. Ifyoudon’talreadyknowMATLAB,itisveryeasytorequestaone-month trial license from mathworks.com and use its many free online tutorials to learn the language. One great advantage of MATLAB over R or other open-source languages is that there is excellent customer support: If you haveaquestion,justemailorcallthestaffatMathworks.(Often,someone withaPhDwillansweryourquestions.) Ihavetaughtmanyofthesetopicstobothretailandinstitutionaltraders atmybiannualworkshopsinLondon,aswellasonline(www.epchan.com). Inordertofacilitatelecturerswhowouldliketousethisasatextbookfora specialtopicscourseonAlgorithmicTrading,Ihaveincludedmanyexercises at the end of most chapters. Some of these exercises should be treated as suggestionsforopen-endedprojects;therearenoready-madeanswers. Readers will also find all of the software and some data used in the examples on epchan.com/book3. The userid and password are embedded in Box 1.1. But unlike my previous books, some of the data involved in xi (cid:2) (cid:2) the example strategies are under strict licensing restrictions and therefore are unavailable for free download from my website. Readers are invited P to purchase or rent them from their original sources, all of which are R E describedinChapter1. FA C I have benefited from tips, ideas, and help from many people in putting E thecontenttogether.Anincompletelistwouldinclude: ■ StephenAikin,arenownedauthor(Aikin,2012)andlecturer,whohelped me understand implied quotes due to calendar spreads in the futures markets(Chapter6). ■ DavidDonandJosephSignorelliofLimeBrokerage,whocorrectedsome ofmymisunderstandingofthemarketmicrostructure(Chapter6). ■ Jonathan Shore, infinitely knowledgeable about bitcoins, who helped compile some order book data in that market and shared that with me (Chapter7). ■ Dr. Roger Hunter, CTO at our firm, QTS Capital Management, who reviewed my manuscript and who never failed to find software bugs in mycodes. (cid:2) (cid:2) TrimSize:6inx9in Chan fpref.tex V2-12/11/2016 12:44A.M. Pagexii ■ The team at Interactive Brokers (especially Joanne, Ragini, Mike, Greg, Ian,andRalph)whoseinfinitepatiencewithmyquestionsaboutallissues relatedtotradingaremuchappreciated. I would like to thank Professor Thomas Miller of Northwestern Uni- versity for hiring me to teach the Risk Analytics course at the Master of Science in Predictive Analytics program. In the same vein, I would also like to thank Matthew Clements and Jim Biss at Global Markets Training for organizing the London workshops for me over the years. Quite a few nuggets of knowledge in this book come out of materials or discussions fromthesecoursesandworkshops. Tradingandresearchhavebeenmadealotmoreinterestingandenjoyable becauseIwasabletoworkcloselywithourteamatQTS,whocontributed toresearch,ideas,andgeneralknowledge,someofwhichfindtheirwayinto thisbook.Amongthem,Roger,ofcourse,withoutwhomtherewouldn’tbe QTS,butalsoYang,Marcin,Sam,andlastbutnotleast,Ray. Of course, none of my books would come into existence without the supportofWiley,especiallymylong-timeeditorBillFalloon,development editorJulieKerr,productioneditorCarolineMaria,andcopyeditorCheryl Ferguson(fromwhomnomissing‘‘end’’toa‘‘for’’-loopcanescape).Itwas xii trulyagreatpleasuretoworkwiththem,andtheirenthusiasmandprofes- (cid:2) (cid:2) sionalismaregreatlyappreciated. E C A F E R P (cid:2)