Python For Finance

Transcription

SECOND EDITIONPython for FinanceMastering Data-Driven FinanceYves HilpischBeijingBoston Farnham SebastopolTokyo

Python for Financeby Yves HilpischCopyright 2019 Yves Hilpisch. All rights reserved.Printed in Canada.Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions arealso available for most titles (http://oreilly.com/safari). For more information, contact our corporate/insti‐tutional sales department: 800-998-9938 or corporate@oreilly.com.Editors: Susan Conant and Jeff BleielProduction Editor: Kristen BrownCopyeditor: Rachel HeadProofreader: Kim CoferDecember 2014:December 2018:Indexer: Judith McConvilleInterior Designer: David FutatoCover Designer: Karen MontgomeryIllustrator: Rebecca DemarestFirst EditionSecond EditionRevision History for the Second Edition2018-11-29:First ReleaseSee http://oreilly.com/catalog/errata.csp?isbn 9781492024330 for release details.The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. Python for Finance, the cover image,and related trade dress are trademarks of O’Reilly Media, Inc.The views expressed in this work are those of the author, and do not represent the publisher’s views.While the publisher and the author have used good faith efforts to ensure that the information andinstructions contained in this work are accurate, the publisher and the author disclaim all responsibilityfor errors or omissions, including without limitation responsibility for damages resulting from the use ofor reliance on this work. Use of the information and instructions contained in this work is at your ownrisk. If any code samples or other technology this work contains or describes is subject to open sourcelicenses or the intellectual property rights of others, it is your responsibility to ensure that your usethereof complies with such licenses and/or rights.978-1-492-02433-0[MBP]

Table of ContentsPreface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiiiPart I.Python and Finance1. Why Python for Finance. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3The Python Programming LanguageA Brief History of PythonThe Python EcosystemThe Python User SpectrumThe Scientific StackTechnology in FinanceTechnology SpendingTechnology as EnablerTechnology and Talent as Barriers to EntryEver-Increasing Speeds, Frequencies, and Data VolumesThe Rise of Real-Time AnalyticsPython for FinanceFinance and Python SyntaxEfficiency and Productivity Through PythonFrom Prototyping to ProductionData-Driven and AI-First FinanceData-Driven FinanceAI-First FinanceConclusionFurther Resources356789910111113141418232424283131iii

2. Python Infrastructure. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33conda as a Package ManagerInstalling MinicondaBasic Operations with condaconda as a Virtual Environment ManagerUsing Docker ContainersDocker Images and ContainersBuilding an Ubuntu and Python Docker ImageUsing Cloud InstancesRSA Public and Private KeysJupyter Notebook Configuration FileInstallation Script for Python and Jupyter NotebookScript to Orchestrate the Droplet SetupConclusionFurther Resources3535374145454650515253555657Part II. Mastering the Basics3. Data Types and Structures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61Basic Data TypesIntegersFloatsBooleansStringsExcursion: Printing and String ReplacementsExcursion: Regular ExpressionsBasic Data StructuresTuplesListsExcursion: Control StructuresExcursion: Functional ProgrammingDictsSetsConclusionFurther Resources626263666971747575767880818284844. Numerical Computing with NumPy. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85Arrays of DataArrays with Python ListsThe Python array ClassRegular NumPy Arraysiv Table of Contents86868890

The BasicsMultiple DimensionsMetainformationReshaping and ResizingBoolean ArraysSpeed ComparisonStructured NumPy ArraysVectorization of CodeBasic VectorizationMemory LayoutConclusionFurther Resources909497981011031051061071101121125. Data Analysis with pandas. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113The DataFrame ClassFirst Steps with the DataFrame ClassSecond Steps with the DataFrame ClassBasic AnalyticsBasic VisualizationThe Series ClassGroupBy OperationsComplex SelectionConcatenation, Joining, and MergingConcatenationJoiningMergingPerformance AspectsConclusionFurther 436. Object-Oriented Programming. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145A Look at Python ObjectsintlistndarrayDataFrameBasics of Python ClassesPython Data ModelThe Vector ClassConclusionFurther Resources149149150151152154159163164164Table of Contents v

Part III. Financial Data Science7. Data Visualization. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167Static 2D PlottingOne-Dimensional Data SetsTwo-Dimensional Data SetsOther Plot StylesStatic 3D PlottingInteractive 2D PlottingBasic PlotsFinancial PlotsConclusionFurther Resources1681691761831911951951992032048. Financial Time Series. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205Financial DataData ImportSummary StatisticsChanges over TimeResamplingRolling StatisticsAn OverviewA Technical Analysis ExampleCorrelation AnalysisThe DataLogarithmic ReturnsOLS RegressionCorrelationHigh-Frequency DataConclusionFurther 82302309. Input/Output Operations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231Basic I/O with PythonWriting Objects to DiskReading and Writing Text FilesWorking with SQL DatabasesWriting and Reading NumPy ArraysI/O with pandasWorking with SQL DatabasesFrom SQL to pandasWorking with CSV Filesvi Table of Contents232232236239242244245247250

Working with Excel FilesI/O with PyTablesWorking with TablesWorking with Compressed TablesWorking with ArraysOut-of-Memory ComputationsI/O with TsTablesSample DataData StorageData RetrievalConclusionFurther Resources25125225326026226426726726927027227310. Performance Python. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275LoopsPythonNumPyNumbaCythonAlgorithmsPrime NumbersFibonacci NumbersThe Number PiBinomial TreesPythonNumPyNumbaCythonMonte Carlo ursive pandas AlgorithmPythonNumbaCythonConclusionFurther 7299300301302302303304305307307308309Table of Contents vii

11. Mathematical Tools. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311ApproximationRegressionInterpolationConvex OptimizationGlobal OptimizationLocal OptimizationConstrained OptimizationIntegrationNumerical IntegrationIntegration by SimulationSymbolic ComputationBasicsEquationsIntegration and DifferentiationDifferentiationConclusionFurther 034134334312. Stochastics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345Random NumbersSimulationRandom VariablesStochastic ProcessesVariance ReductionValuationEuropean OptionsAmerican OptionsRisk MeasuresValue-at-RiskCredit Valuation AdjustmentsPython ScriptConclusionFurther 513. Statistics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397Normality TestsBenchmark CaseReal-World DataPortfolio OptimizationThe DataThe Basic Theoryviii Table of Contents398399409415416417

Optimal PortfoliosEfficient FrontierCapital Market LineBayesian StatisticsBayes’ FormulaBayesian RegressionTwo Financial InstrumentsUpdating Estimates over TimeMachine LearningUnsupervised LearningSupervised LearningConclusionFurther ResourcesPart mic Trading14. The FXCM Trading Platform. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 467Getting StartedRetrieving DataRetrieving Tick DataRetrieving Candles DataWorking with the APIRetrieving Historical DataRetrieving Streaming DataPlacing OrdersAccount InformationConclusionFurther Resources46946947047247447547747848048048115. Trading Strategies. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483Simple Moving AveragesData ImportTrading StrategyVectorized BacktestingOptimizationRandom Walk HypothesisLinear OLS RegressionThe DataRegressionClusteringFrequency Approach484485485487489491494495497499501Table of Contents ix

ClassificationTwo Binary FeaturesFive Binary FeaturesFive Digitized FeaturesSequential Train-Test SplitRandomized Train-Test SplitDeep Neural NetworksDNNs with scikit-learnDNNs with TensorFlowConclusionFurther Resources50450450650850951151251251551951916. Automated Trading. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 521Capital ManagementThe Kelly Criterion in a Binomial SettingThe Kelly Criterion for Stocks and IndicesML-Based Trading StrategyVectorized BacktestingOptimal LeverageRisk AnalysisPersisting the Model ObjectOnline AlgorithmInfrastructure and DeploymentLogging and MonitoringConclusionPython ScriptsAutomated Trading StrategyStrategy MonitoringFurther ResourcesPart Derivatives Analytics17. Valuation Framework. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557Fundamental Theorem of Asset PricingA Simple ExampleThe General ResultsRisk-Neutral DiscountingModeling and Handling DatesConstant Short RateMarket EnvironmentsConclusionx Table of Contents558558559560561563565568

Further Resources56918. Simulation of Financial Models. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 571Random Number GenerationGeneric Simulation ClassGeometric Brownian MotionThe Simulation ClassA Use CaseJump DiffusionThe Simulation ClassA Use CaseSquare-Root DiffusionThe Simulation ClassA Use CaseConclusionFurther . Derivatives Valuation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 595Generic Valuation ClassEuropean ExerciseThe Valuation ClassA Use CaseAmerican ExerciseLeast-Squares Monte CarloThe Valuation ClassA Use CaseConclusionFurther Resources59660060060260760860961161461620. Portfolio Valuation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 617Derivatives PositionsThe ClassA Use CaseDerivatives PortfoliosThe ClassA Use CaseConclusionFurther Resources61861962062262262663463521. Market-Based Valuation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 637Options DataModel Calibration638641Table of Contents xi

Relevant Market DataOption ModelingCalibration ProcedurePortfolio ValuationModeling Option PositionsThe Options PortfolioPython CodeConclusionFurther Resources641643646651652653654656657A. Dates and Times. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 659B. BSM Option Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 673Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 679xii Table of Contents

PrefaceThese days, Python is undoubtedly one of the major strategic technology platforms inthe financial industry. When I started writing the first edition of this book in 2013, Istill had many conversations and presentations in which I argued relentlessly forPython’s competitive advantages in finance over other languages and platforms.Toward the end of 2018, this is not a question anymore: financial institutions aroundthe world now simply try to make the best use of Python and its powerful ecosystemof data analysis, visualization, and machine learning packages.Beyond the realm of finance, Python is also often the language of choice in introduc‐tory programming courses, such as in computer science programs. Beyond its reada‐ble syntax and multiparadigm approach, a major reason for this is that Python hasalso become a first class citizen in the areas of artificial intelligence (AI), machinelearning (ML), and deep learning (DL). Many of the most popular packages andlibraries in these areas are either written directly in Python (such as scikit-learn forML) or have Python wrappers available (such as TensorFlow for DL).Finance itself is entering a new era, and two major forces are driving this evolution.The first is the programmatic access to basically all the financial data available—ingeneral, this happens in real time and is what leads to data-driven finance. Decadesago, most trading or investment decisions were driven by what traders and portfoliomanagers could read in the newspaper or learn through personal conversations. Thencame terminals that brought financial data in real time to the traders’ and portfoliomanagers’ desks via computers and electronic communication. Today, individuals (orteams) can no longer keep up with the vast amounts of financial data generated ineven a single minute. Only machines, with their ever-increasing processing speedsand computational power, can keep up with the volume and velocity of financial data.This means, among other things, that most of today’s global equities trading volumeis driven by algorithms and computers rather than by human traders.The second major force is the increasing importance of AI in finance. More and morefinancial institutions try to capitalize on ML and DL algorithms to improve opera‐xiii

tions and their trading and investment performances. At the beginning of 2018, thefirst dedicated book on “financial machine learning” was published, which under‐scores this trend. Without a doubt, there are more to come. This leads to what mightbe called AI-first finance, where flexible, parameterizable ML and DL algorithmsreplace traditional financial theory—theory that might be elegant but no longer veryuseful in the new era of data-driven, AI-first finance.Python is the right programming language and ecosystem to tackle the challenges ofthis era of finance. Although this book covers basic ML algorithms for unsupervisedand supervised learning (as well as deep neural networks, for instance), the focus ison Python’s data processing and analysis capabilities. To fully account for the impor‐tance of AI in finance—now and in the future—another book-length treatment isnecessary. However, most of the AI, ML, and DL techniques require such largeamounts of data that mastering data-driven finance should come first anyway.This second edition of Python for Finance is more of an upgrade than an update. Forexample, it adds a complete part (Part IV) about algorithmic trading. This topic hasrecently become quite important in the financial industry, and is also quite popularwith retail traders. It also adds a more introductory part (Part II) where fundamentalPython programming and data analysis topics are presented before they are appliedin later parts of the book. On the other hand, some chapters from the first editionhave been deleted completely. For instance, the chapter on web techniques and pack‐ages (such as Flask) was dropped because there are more dedicated and focusedbooks about such topics available today.For the second edition, I tried to cover even more finance-related topics and to focuson Python techniques that are particularly useful for financial data science, algorith‐mic trading, and computational finance. As in the first edition, the approach is apractical one, in that implementation and illustration come before theoretical detailsand I generally focus on the big picture rather than the most arcane parameterizationoptions of a certain class, method, or function.Having described the basic approach for the second edition, it is worth emphasizingthat this book is neither an introduction to Python programming nor to finance ingeneral. A vast number of excellent resources are available for both. This book is loca‐ted at the intersection of these two exciting fields, and assumes that the reader hassome background in programming (not necessarily Python) as well as in finance.Such readers learn how to apply Python and its ecosystem to the financial domain.The Jupyter Notebooks and codes accompanying this book can be accessed and exe‐cuted via our Quant Platform. You can sign up for free at http://py4fi.pqp.io.My company (The Python Quants) and myself provide many more resources to mas‐ter Python for financial data science, artificial intelligence, algorithmic trading, andcomputational finance. You can start by visiting the following sites:xiv Preface

Our company website My private website Our Python books website Our online training website The Certificate Program websiteFrom all the offerings that we have created over the last few years, I am most proud ofour Certificate Program in Python for Algorithmic Trading. It provides over 150 hoursof live and recorded instruction, over 1,200 pages of documentation, over 5,000 linesof Python code, and over 50 Jupyter Notebooks. The program is offered multipletimes per year and we update and improve it with every cohort. The online programis the first of its kind, in that successful delegates obtain an official university certifi‐cate in cooperation with htw saar University of Applied Sciences.In addition, I recently started The AI Machine, a new project and company to stand‐ardize the deployment of automated, algorithmic trading strategies. With this project,we want to implement in a systematic and scalable fashion what we have been teach‐ing over the years in the field, in order to capitalize on the many opportunities in thealgorithmic trading field. Thanks to Python—and data-driven and AI-first finance—this project is possible these days even for a smaller team like ours.I closed the preface for the first edition with the following words:I am really excited that Python has established itself as an important technology in thefinancial industry. I am also sure that it will play an even more important role there inthe future, in fields like derivatives and risk analytics or high performance computing.My hope is that this book will help professionals, researchers, and students alike makethe most of Python when facing the challenges of this fascinating field.When I wrote these lines in 2014, I couldn’t have predicted how important Pythonwould become in finance. In 2018, I am even happier that my expectations and hopeshave been so greatly surpassed. Maybe the first edition of the book played a small partin this. In any case, a big thank you is in order to all the relentless open source devel‐opers out there, without whom the success story of Python couldn’t have beenwritten.Conventions Used in This BookThe following typographical conventions are used in this book:ItalicIndicates new terms, URLs, and email addresses.Preface xv

Constant widthUsed for program listings, as well as within paragraphs to refer to software pack‐ages, programming languages, file extensions, filenames, program elements suchas variable or function names, databases, data types, environment variables, state‐ments, and keywords.Constant width italicShows text that should be replaced with user-supplied values or by values deter‐mined by context.This element signifies a tip or suggestion.This element signifies a general note.This element indicates a warning or caution.Using Code ExamplesSupplemental material (in particular, Jupyter Notebooks and Python scripts/modules) is available for usage and download at http://py4fi.pqp.io.This book is here to help you get your job done. In general, if example code is offeredwith this book, you may use it in your programs and documentation. You do notneed to contact us for permission unless you’re reproducing a significant portion ofthe code. For example, writing a program that uses several chunks of code from thisbook does not require permission. Selling or distributing a CD-ROM of examplesfrom O’Reilly books does require permission. Answering a question by citing thisbook and quoting example code does not require permission. Incorporating a signifi‐cant amount of example code from this book into your product’s documentation doesrequire permission.We appreciate, but do not require, attribution. An attribution usually includes thetitle, author, publisher, and ISBN. For example: “Python for Finance, 2nd Edition, byYves Hilpisch (O’Reilly). Copyright 2019 Yves Hilpisch, 978-1-492-02433-0.”xvi Preface

If you feel your use of code examples falls outside fair use or the permission givenabove, feel free to contact us at permissions@oreilly.com.O’Reilly SafariSafari (formerly Safari Books Online) is a membership-basedtraining and reference platform for enterprise, government,educators, and individuals.Members have access to thousands of books, training videos, Learning Paths, interac‐tive tutorials, and curated playlists from over 250 publishers, including O’ReillyMedia, Harvard Business Review, Prentice Hall Professional, Addison-Wesley Profes‐sional, Microsoft Press, Sams, Que, Peachpit Press, Adobe, Focal Press, Cisco Press,John Wiley & Sons, Syngress, Morgan Kaufmann, IBM Redbooks, Packt, AdobePress, FT Press, Apress, Manning, New Riders, McGraw-Hill, Jones & Bartlett, andCourse Technology, among others.For more information, please visit http://oreilly.com/safari.How to Contact UsPlease address comments and questions concerning this book to the publisher:O’Reilly Media, Inc.1005 Gravenstein Highway NorthSebastopol, CA 95472800-998-9938 (in the United States or Canada)707-829-0515 (international or local)707-829-0104 (fax)We have a web page for this book, where we list errata, examples, and any additionalinformation. You can access this page at http://bit.ly/python-finance-2e.To comment or ask technical questions about this book, send email to bookques‐tions@oreilly.com.For more information about our books, courses, conferences, and news, see our web‐site at http://www.oreilly.com.Find us on Facebook: http://facebook.com/oreillyFollow us on Twitter: http://twitter.com/oreillymediaWatch us on YouTube: http://www.youtube.com/oreillymediaPreface xvii

AcknowledgmentsI want to thank all those who helped to make this book a reality—in particular, theteam at O’Reilly, who really improved my manuscript in many ways. I would like tothank the tech reviewers, Hugh Brown and Jake VanderPlas. The book benefited fromtheir valuable feedback and their many suggestions. Any remaining errors, of course,are mine.Michael Schwed, with whom I have been working closely for more than ten years,deserves a special thank you. Over the years, I have benefited in innumerable waysfrom his work, support, and Python know-how.I also want to thank Jason Ramchandani and Jorge Santos of Refinitiv (formerlyThomson Reuters) for their continued support not only of my work but also of theopen source community in general.As with the first edition, the second edition of this book has tremendously benefitedfrom the dozens of “Python for finance” talks I have given over the years, as well asthe hundreds of hours of “Python for finance” trainings. In many cases the feedbackfrom participants helped to improve my training materials, which often ended up aschapters or sections in this book.Writing the first edition took me about a year. Overall, writing and upgrading the sec‐ond edition also took about a year, which was quite a bit longer than I expected. Thisis mainly because the topic itself keeps me very busy travel- and business-wise, whichI am very grateful for.Writing books requires many hours in solitude and such hours cannot be spent withthe family. Therefore, thank you to Sandra, Lilli, Henry, Adolf, Petra, and Heinz forall your understanding and support—not only with regard to writing this book.I dedicate the second edition of this book, as the first one, to my lovely, strong, andcompassionate wife Sandra. She has given new meaning over the years to what familyis really about. Thank you.— YvesSaarland, November 2018xviii Preface

PART IPython and FinanceThis part introduces Python for finance. It consists of two chapters: Chapter 1 briefly discusses Python in general and argues in some detail whyPython is well suited to addressing the technological challenges in the financialindustry as well as in financial data analytics. Chapter 2 is about Python infrastructure; it provides a concise overview ofimportant aspects of managing a Python environment to get you started withinteractive financial analytics and financial application development in Python.

CHAPTER 1Why Python for FinanceBanks are essentially technology firms.—Hugo BanzigerThe Python Programming LanguagePython is a high-level, multipurpose programming language that is used in a widerange of domains and technical fields. On the Python website you find the followingexecutive summary:Python is an interpreted, object-oriented, high-level programming language withdynamic semantics. Its high-level built in data structures, combined with dynamic typ‐ing and dynamic binding, make it very attractive for Rapid Application Development,as well as for use as a scripting or glue language to connect existing componentstogether. Python’s simple, easy to learn syntax emphasizes readability and thereforereduces the cost of program maintenance. Python supports modules and packages,which encourages program modularity and code reuse. The Python interpreter and theextensive standard library are available in source or binary form without charge for allmajor platforms, and can be freely distributed.This pretty well describes why Python has evolved into one of the major program‐ming languages today. Nowadays, Python is used by the beginner programmer as wellas by the highly skilled expert developer, at schools, in universities, at web companies,in large corporations and financial institutions, as well as in any scientific field.Among other features, Python is:Open sourcePython and the majority of supporting libraries and tools available are opensource and generally come with quite flexible and open licenses.3

InterpretedThe reference CPython implementation is an interpreter of the language thattranslates Python code at runtime to executable byte code.MultiparadigmPython supports different programming and implementation paradigms, such asobject orientation and imperative, functional, or procedural programming.MultipurposePython can be used for rapid, interactive code development as well as for build‐ing large applications; it can be used for low-level systems operations as well asfor high-level analytics tasks.Cross-platformPython is available for the most important operating systems, such as Windows,Linux, and macOS. It is used to build desktop as well as web applications, and itcan be used on the largest clusters and most powerful servers as well as on suchsmall devices as the Raspberry Pi.Dynamically typedTypes in Python are in general inferred at runtime and not statically declared asin most compiled languages.Indentation awareIn contrast to the majority of other programming languages, Python uses inden‐tation for marking code blocks instead of parentheses, brackets, or semicolons.Garbage collectingPython has automated garbage collection, avoiding the need for the programmerto manage memory.When it comes to Python syntax and what Python is all about, Python EnhancementProposal 20—i.e., the so-called “Zen of Python”—provides the major guidelines. Itcan be accessed from every interactive shell with the command import this:In [1]: import thisThe Zen of Python, by Tim PetersBeautiful is better than ugly.Explicit is better than implicit.Simple is better than complex.Complex is better than complicated.Flat is better than nested.Sparse is better than dense.Readability counts.Special cases aren't special enough to break the rules.Although practicality beats purity.Errors should never pass silently.Unless explicitly silenced.4 Chapter 1: Why Python for Finance

In the face of ambiguity, refuse the temptation to guess.There should be one-- and preferably only one --obvious way to do it.Although that way may not be obvious at first unless you're Dutch.Now is better than never.Although never is often better than *right* now.If the implementation is hard to explain, it's a bad idea.If the implementation is easy to explain, it may be a good idea.Namespaces are one honking great idea -- let's do more of those!A Brief History of PythonAlthough Python might still have the appeal of something new to some people, it hasbeen around for quite a long time. In fact, development efforts began in the 1980s byGuido van Rossum from the Netherlands. He is still active in Python developmentand has been awarded the title of Benevolent Dictator for Life by the Python commu‐nity. In July 2018, van Rossum stepped down from this position after decades of beingan active driver of the Python core development efforts. The following can b

SECOND EDITION Beijing Boston Farnham Sebastopol Tokyo. 978-1-492-02433-0 [MBP] Python for Finance . Risk Analysis 539 Persisting the Model Object 543 . most trading or investment