--- tags: computer science, finance --- <center> # **Python Programming in Finance** :::info Location: TBA Time: TBA ::: </center> <img src = "https://i.imgur.com/3pCd7Hz.png" height = 200px style="float:left"/> <p style="text-align: right"> "All money is a matter of belief."<br>-- Adam Smith </p> <p style="text-align: right"> "Investing is simple, not easy."<br>-- Warren Buffett </p> <p style="text-align: right"> "In the business world, the rearview mirror<br>is always clearer than the windshield."<br>-- Warren Buffett </p> <p style="text-align: right"> "It's a Marathon, not a sprint."<br>-- Anonymous </p> <p style="text-align: right"> "Money often costs too much."<br>-- Ralph Waldo Emerson </p> ## **Class Information** ### Instructor - Name: 盧政良 (Zheng-Liang Lu, Arthur) - Email address: arthurzllu@gmail.com ### Objectives This course is an inter-disciplinary course in the fields of <b>computer science</b>, <b>finance</b>, and (a lot of) <b>math</b>: - Python programming - Debut of quantative research: data acquisition, visualization, strategy development, and backtesting - Selected math tools - Modern portfolio theory - Financial time series analysis - Pricing theory - Risk management - (Optional) Machine learning These techniques are essential both in <b><font color = "red">P</font></b> & <b><font color = "red">Q</font></b> quant. Just for the record, this course is not to teach you how to get rich in your life but get rich in knowledge (such that you may get rich in the future life). ### Prerequisites - Basic knowledge about [financial markets](https://en.wikipedia.org/wiki/Financial_market) - Basic level of Python programming - You may consider my [Python 101](https://hackmd.io/@arthurzllu/HJNXq84SO) course. ### Working Environment - Google Colab with Python 3.6 <font size = -1>[link](https://colab.research.google.com/)</font> - You need a Google account, of course. :::warning Recording of classroom lectures is <font color="red">**prohibited**</font> unless advance written permission is obtained from the class instructor and any guest presenter(s). ::: ## **Syllabus** - [Course overview](https://www.csie.ntu.edu.tw/~d00922011/python/slides/syllabus.pdf) - [2021 FRM Study Guide](https://www.csie.ntu.edu.tw/~d00922011/python/misc/FRM-StudyGuide-092520_annotated.pdf) - Preliminary knowledge about CS <font size = -1>[link](https://www.csie.ntu.edu.tw/~d00922011/python/slides/cs_preliminary_knowledge.pdf)</font> - (FYR) Kronos Research, [Kronos Webinar Trilogy #1 - Quant Trading 101](https://www.facebook.com/kronosresearch/videos/3280075665338110/), 2020.5.16 ### ++Python Crash Course++ - Variable <font size = -1>==[notebook](https://www.csie.ntu.edu.tw/~d00922011/python/notebooks/pyf_1_python_programming.ipynb)==</font> <font size = -1>==[pdf](https://www.csie.ntu.edu.tw/~d00922011/python/slides/lecture1.pdf)==</font> - Basic data types: int/float, str, bool - Assignment/arithmetic/rational/logical operators - List & slicing - Branching (if-elif-else) - Iterations (for/while loops) - Monte Carlo simulation - Bisection method for root-finding - Function & anonymous expression (lambda) :::success You may refer to my python course [here](https://hackmd.io/@arthurzllu/HJNXq84SO). ::: ### ++Debut of Quantitative Research++ - Data acquisition - Easy-to-use data collector: *ffn* <font size = -1>==[notebook](https://www.csie.ntu.edu.tw/~d00922011/python/notebooks/pyf_2_debut_of_quant.ipynb)==</font> - Another easy-to-use data collector: yfinance <font size = -1>==[notebook](https://www.csie.ntu.edu.tw/~d00922011/python/notebooks/pyf_2_yfinance.ipynb)==</font> - DataFrame of *pandas* <font size = -1>==[notebook](https://www.csie.ntu.edu.tw/~d00922011/python/notebooks/pyf_2_pandas.ipynb)==</font> - Data visualization: *matplotlib* <font size = -1>==[notebook](https://www.csie.ntu.edu.tw/~d00922011/python/notebooks/pyf_2_graphics.ipynb)==</font> - Strategy development - Technical analysis: *ta*/*talib* <font size = -1>==[notebook](https://www.csie.ntu.edu.tw/~d00922011/python/notebooks/pyf_2_technical_analysis.ipynb)==</font> - Rodolfo Toríbio Farias Nazário, Jéssica Lima e Silva, Vinicius Amorim Sobreiro, Herbert Kimura, [A literature review of technical analysis on stock markets](https://www.sciencedirect.com/science/article/pii/S1062976917300443), 2017 - Backtesting: *backtesting* <font size = -1>==[notebook](https://www.csie.ntu.edu.tw/~d00922011/python/notebooks/pyf_2_backtesting.ipynb)==</font> <center> ![](https://i.imgur.com/xuhbN4H.png =500x) <font size = -1>https://finviz.com/</font> </center> ### ++Selected Mathematical Tools++ - Package *numpy* <font size = -1>==[notebook](https://www.csie.ntu.edu.tw/~d00922011/python/notebooks/pyf_3_math_1.ipynb)==</font> - Vectorization - Matrix computation - Inner product - Inverse matrix - Package *scipy* - Interpolation - Linear interpolation - Cubic spline - Optimization - Gradient descent - Essentials of Statistics <font size = -1>==[notebook](https://www.csie.ntu.edu.tw/~d00922011/python/notebooks/pyf_3_math_2.ipynb)==</font> <font size = -1>==[pdf](https://www.csie.ntu.edu.tw/~d00922011/python/slides/statistics.pdf)==</font> - Probability models - You need to know at least the following four distribution: [normal distribution](https://en.wikipedia.org/wiki/Normal_distribution), [$\chi^2$ distribution](https://en.wikipedia.org/wiki/Chi-square_distribution), [Student's t-distribution](https://en.wikipedia.org/wiki/Student%27s_t-distribution), [F distribution](https://en.wikipedia.org/wiki/F-distribution). - Random variable & random number generator - Hypothesis testing - Null hypothesis, significant level $\alpha$ and $p$-value. - Parameter/interval estimation - Simple linear regression <font size = -1>==[pdf](https://www.csie.ntu.edu.tw/~d00922011/python/slides/linear_regression.pdf)==</font> :::success You may refer to my statistics course [here](https://hackmd.io/@arthurzllu/ByC0nhVkr). ::: ### ++Modern Portfolio Theory++ - Mean-variance framework and the efficient frontier <font size = -1>==[notebook](https://www.csie.ntu.edu.tw/~d00922011/python/notebooks/pyf_4_mean_variance_analysis.ipynb)==</font> - Capital asset pricing model (CAPM) <font size = -1>==[notebook](https://www.csie.ntu.edu.tw/~d00922011/python/notebooks/pyf_4_CAPM.ipynb)==</font> - Multi-factor models <font size = -1>==[notebook](https://www.csie.ntu.edu.tw/~d00922011/python/notebooks/pyf_4_factor_models.ipynb)==</font> - Arbitrage pricing theory (APT) - Fama-French 3-factor model - Barra risk factor analysis - AQR: Buffett's Alpha <!-- - Kelly formula for portfolio allocation <font size = -1>==[notebook]()==</font> --> ### ++Financial Time Series Analysis++ - Autocorrelation <font size = -1>==[notebook](https://www.csie.ntu.edu.tw/~d00922011/python/notebooks/pyf_5_time_series.ipynb)==</font> - Stationary process, order of integration, and the mean-reverting property - Autoregressive moving-average (ARMA) model - Generalized autoregressive conditional heteroskedasticity (GARCH) model <font size = -1>==[notebook](https://www.csie.ntu.edu.tw/~d00922011/python/notebooks/pyf_5_garch.ipynb)==</font> - Vector autoregression (VAR) model <font size = -1>==[notebook](https://www.csie.ntu.edu.tw/~d00922011/python/notebooks/pyf_5_var.ipynb)==</font> - Cointegration & vector error correction model (VECM) <font size = -1>==[notebook](https://www.csie.ntu.edu.tw/~d00922011/python/notebooks/pyf_5_cointegration.ipynb)==</font> - Granger causality <font size = -1>==[notebook](https://www.csie.ntu.edu.tw/~d00922011/python/notebooks/pyf_5_granger_causality.ipynb)==</font> - Structural break <font size = -1>==[notebook](https://www.csie.ntu.edu.tw/~d00922011/python/notebooks/pyf_5_structural_break.ipynb)==</font> ### ++Pricing Theory++ - Introduction to futures and options <font size = -1>==[pdf](https://www.csie.ntu.edu.tw/~d00922011/python/slides/options.pdf)==</font> <font size = -1>==[notebook](https://www.csie.ntu.edu.tw/~d00922011/python/notebooks/pyf_6_options.ipynb)==</font> - Arbitrage-free principle - Fundamental theorems of asset pricing - Binomial option pricing model (BOPM) - Risk-neutral valuation - Monte Carlo simulation for option pricing - Black-Scholes formula for European options - (FYR) Case study: Ai Jun Hou, Weining Wang, Cathy Y H Chen, Wolfgang Karl Härdle, [Pricing Cryptocurrency Options](https://academic.oup.com/jfec/article/18/2/250/5837734), 2020 - Model calibration: implied volatility - [CBOE VIX Index](https://www.cboe.com/tradable_products/vix/) - Simulation of financial models: geometric Brownian motion, mean-reverting process, stochastic volatility process, jump-diffusion process <!-- - Case study: Hawkes model & limit-order book (LOB) <font size = -1>==[notebook]()==</font> <font color = "red" size = -1>new</font> - QuantLib <font size = -1>==[notebook]()==</font> <font color = "red" size = -1>new</font> --> ### ++Risk Management++ - Value at risk (VaR) & expected shortfall (ES) <font size = -1>==[pdf](https://www.csie.ntu.edu.tw/~d00922011/python/slides/var.pdf)==</font> <font size = -1>==[notebook](https://www.csie.ntu.edu.tw/~d00922011/python/notebooks/pyf_7_VaR.ipynb)==</font> - Futures Hedging <font size = -1>==[notebook](https://www.csie.ntu.edu.tw/~d00922011/python/notebooks/pyf_7_futures_hedging.ipynb)==</font> <font color = "red" size = -1>new</font> - Sensitivity analysis: Greeks <font size = -1>==[pdf](https://www.csie.ntu.edu.tw/~d00922011/python/slides/greeks.pdf)==</font> <font size = -1>==[notebook](https://www.csie.ntu.edu.tw/~d00922011/python/notebooks/pyf_7_greeks.ipynb)==</font> - Delta hedging ### ++Machine Learning++ <font size = -1>[notebook1](https://www.csie.ntu.edu.tw/~d00922011/python/notebooks/pyf_8_machine_learning_tutorial_1.ipynb)</font> <font size = -1>[notebook2](https://www.csie.ntu.edu.tw/~d00922011/python/notebooks/pyf_8_machine_learning_tutorial_2.ipynb)</font> - Supervised learning - Ridge/LASSO regression <font size = -1>==[notebook]()==</font> - Logistic regression <font size = -1>==[notebook]()==</font> - Support vector machine (SVM) <font size = -1>==[notebook]()==</font> - Decision tree, random forest, AdaBoost <font size = -1>==[notebook]()==</font> - Unsupervised learning - K-means clustering <font size = -1>==[notebook]()==</font> - Principal component analysis (PCA) <font size = -1>==[notebook]()==</font> - Reinforcement learning - Q-learning <font size = -1>==[notebook]()==</font> - Deep learning - Recurrent net (RNN) <font size = -1>==[notebook]()==</font> - Long/Short-Term Memeory Model (LSTM) <font size = -1>==[notebook]()==</font> ## Programming Labs :::warning You can find the list of programming assignments here: https://hackmd.io/@arthurzllu/Hy9ana_iD ::: <center> <iframe src="" width = "700px" height = "200px"></iframe> </center> ## References ### ++Computer Science++ #### Python Programming - [CS41 happy code the python programming language](https://stanfordpython.com/archive/), Stanford University <font size = -1 color = "red">IN PROGRESS</font> - [Python Practice](http://python.berkeley.edu/resources/), University of California, Berkeley <font size = -1 color = "red">IN PROGRESS</font> - [CS 61A: Structure and Interpretation of Computer Programs](https://cs61a.org/), University of California, Berkeley <font size = -1 color = "red">IN PROGRESS</font> - [CMU 15-112: Fundamentals of Programming and Computer Science](https://www.cs.cmu.edu/~112/index.html), Carnegie Mellon University, 2021sp <font size = -1 color = "red">IN PROGRESS</font> #### Quantitative Finance by Python - https://python-advanced.quantecon.org/ #### Machine Learning for Finance - Marcos Lopez de Prado, [Advances in Financial Machine Learning](https://www.amazon.com/Linear-Algebra-5th-Stephen-Friedberg/dp/0134860241/), 2018 ![](https://i.imgur.com/d6H0MJh.png =100x) ### ++Mathematical Foundations++ #### Linear Algebra - Stephen H. Friedberg, Arnold J. Insel, Lawrence E. Spence, [Linear Algebra](https://www.amazon.com/Linear-Algebra-5th-Stephen-Friedberg/dp/0134860241/), 5/e, 2018 ![](https://i.imgur.com/Si0JvQb.png =100x) #### Probability Models - Sheldon Ross, [Introduction to Probability Models](https://www.elsevier.com/books/introduction-to-probability-models/ross/978-0-12-814346-9), 12/e, 2019 ![](https://i.imgur.com/57W4fGF.jpg =100x) #### Statistics - Svetlozar T. Rachev, Markus Hoechstoetter, Frank J. Fabozzi, and Sergio M. Focardi, [Probability and Statistics for Finance](https://www.wiley.com/en-us/Probability+and+Statistics+for+Finance-p-9780470400937), 2010 ![](https://i.imgur.com/UcAVVhr.png =100x) - Robert V. Hogg, Joseph W. McKean, and Allen T. Craig, [Introduction to Mathematical Statistics](https://www.amazon.com/-/zh_TW/dp/0134686993/), 8/e, 2019 ![](https://i.imgur.com/2juN4uq.png =100x) - George Casella and Roger L. Berger, [Statistical Inference](https://www.amazon.com/Statistical-Inference-George-Casella/dp/0534243126), 2/e, 2001 ![](https://i.imgur.com/D9fxPRb.png =100x) - 陳旭昇,[統計學:應用與進階](http://homepage.ntu.edu.tw/~sschen/Book/Book1.html),第三版 ![](https://i.imgur.com/SveUJDN.png =100x) - 陳旭昇,[時間序列分析 - 總體經濟與財務金融之應用](http://homepage.ntu.edu.tw/~sschen/Book/Book2.htm),第二版 ![](https://i.imgur.com/b2Ubehb.png =100x) #### Time Series - Kutner, Nachtsheim, Neter, and Li, [Applied Linear Statistical Models: Applied Linear Regression Models](https://www.amazon.com/Applied-Linear-Statistical-Models-Michael/dp/007310874X), 5/e, 2019 ![](https://i.imgur.com/m6wwNY4.png =100x) #### Numerical Methods - Paul Glasserman, [Monte Carlo Methods in Financial Engineering](https://www.springer.com/gp/book/9780387004518), 2003 ![](https://i.imgur.com/ekBWrHE.png =100x) ### ++Finance++ #### Financial Market - Frank J. Fabozzi, Frank J. Jones, Francesco A. Fabozzi, and Steven V. Mann, [Foundation of Global Financial Markets and Institutions](https://www.amazon.com/Foundations-Global-Financial-Markets-Institutions/dp/0262039540), 5/e, 2019 ![](https://i.imgur.com/4ixEBy1.png =100x) - Stanley Eakins Frederic Mishkin, [Financial Markets & Institutions](https://www.amazon.com/Financial-Markets-Institutions-Stanley-Frederic/dp/1292215003), 9/e, 2018 ![](https://i.imgur.com/GCnUWr7.png =100x) - Frank Fabozzi, [Bond Markets, Analysis, and Strategies](https://www.amazon.com/Bond-Markets-Analysis-Strategies-9th/dp/0133796779), 9/e, 2014 ![](https://i.imgur.com/r5yyYBt.png =100x) #### Investment - Zvi Bodie, Alex Kane, Alan J. Marcus, [Investments](https://www.amazon.com/ISE-Investments/dp/1260571157/), 12/e, 2020 ![](https://i.imgur.com/OJ0I6Zf.png =100x) #### Derivatives - John C. Hull, [Options, Futures, and Other Derivatives](https://www.amazon.com/Options-Futures-Other-Derivatives-10th/dp/013447208X), 10/e, 2017 ![](https://i.imgur.com/hO6Qq5e.png =100x) #### Portfolio Management - Frank J. Fabozzi, Sergio M. Focardi, and Petter N. Kolm, [Financial Modeling of the Equity Market: From CAPM to Cointegration](https://onlinelibrary.wiley.com/doi/book/10.1002/9781119201236), 2012 ![](https://i.imgur.com/rYjNdsx.png =100x) - 石川,[因子投資:方法與實踐](https://www.sanmin.com.tw/Product/index/007867990),2020 ![](https://i.imgur.com/fdoEKVz.png =100x) - Fischer Black and Robert Litterman, [Global Portfolio Optimization](https://www.jstor.org/stable/4479577), 1992: [pdf](http://www.sef.hku.hk/tpg/econ6017/2011/black-litterman-1992.pdf) - Heinz Zimmermann, Wolfgang Drobetz, Peter Oertmann, [Global Asset Allocation: New Methods and Applications](https://www.amazon.com/Global-Asset-Allocation-Methods-Applications/dp/0471264261), 2002 ![](https://i.imgur.com/QRyHY47.png =100x) - Bob Litterman and Quantitative Resources Group, [Modern Investment Management: An Equilibrium Approach](https://www.amazon.com/Modern-Investment-Management-Equilibrium-Approach/dp/0471124109), 2003 ![](https://i.imgur.com/iNHMQQP.png =100x) - Igor Tulchinsky, [Finding Alphas: A Quantitative Approach to Building Trading Strategies](https://www.amazon.com/-/zh_TW/dp/1119571219), 2019 ![](https://i.imgur.com/9OSTQOB.png =100x) #### Trading System - Perry J. Kaufman, [Trading Systems and Methods](https://www.amazon.com/Trading-Systems-Methods-Website-Wiley/dp/1118043561), 5/e, 2013 ![](https://i.imgur.com/fRQBwjE.png =100x) #### Risk Management - John C. Hull, [Risk Management and Financial Institutions](https://www.amazon.com/Management-Financial-Institutions-Wiley-Finance/dp/1119448115/), 5/e, 2018 ![](https://i.imgur.com/wJMVQtp.png =100x) - Emanuel Derman, Michael B. Miller, and David Park, [The Volatility Smile](https://www.amazon.com/Volatility-Smile-Wiley-Finance/dp/1118959167), 2016 ![](https://i.imgur.com/OsigTVf.png =100x) - Colin Bennett, [Trading Volatility: Trading Volatility, Correlation, Term Structure and Skew](https://www.amazon.com/Trading-Volatility-Correlation-Term-Structure/dp/1499206070), 2014 ![](https://i.imgur.com/kFuLglh.png =100x) - Dan Passarelli, [Trading Option Greeks: How Time, Volatility, and Other Pricing Factors Drive Profits](https://onlinelibrary.wiley.com/doi/book/10.1002/9781118531846), 2/e, 2012 ![](https://i.imgur.com/dlGi0qa.png =100x) - Rama Cont, [Frontiers in Quantitative Finance: Volatility and Credit Risk Modeling](https://onlinelibrary.wiley.com/doi/book/10.1002/9781118266915), 2012 ![](https://i.imgur.com/UX7Jw0C.png =100x) - Global Association of Risk Professionals, [Financial Risk Manager (FRM®)](https://www.garp.org/#!/frm) - You may refer to the official study guide: https://www.csie.ntu.edu.tw/~d00922011/python/misc/FRM-StudyGuide-092520_annotated.pdf #### Financial Engineering: Pricing Theory & Financial Mathematics - Tomas Björk, [Arbitrage Theory in Continuous Time](https://www.amazon.com/Arbitrage-Theory-Continuous-Oxford-Finance/dp/019957474X), 3/e, 2009 ![](https://i.imgur.com/883OCn4.png =100x) - Steven Shreve, [Stochastic Calculus for Finance I: The Binomial Asset Pricing Model](https://www.springer.com/gp/book/9780387401003), 2004 ![](https://i.imgur.com/wDR0bLU.png =100x) - Steven Shreve, [Stochastic Calculus for Finance II: Continuous-Time Models](https://www.springer.com/gp/book/9780387401010), 2004 ![](https://i.imgur.com/P64Gat4.png =100x) - Brigo, Damiano, Mercurio, Fabio, [Interest Rate Models Theory and Practice with Smile, Inflation and Credit](https://www.springer.com/it/book/9783540221494), 2/e, 2006 ![](https://i.imgur.com/s2ppFwP.png =100x) ### Additional Reading Materials - Burton G. Malkiel,漫步華爾街:超越股市漲跌的成功投資策略 ![](https://i.imgur.com/5xUY7Vo.png =110x) - James Owen Weatherall,華爾街的物理學 ![](https://i.imgur.com/0CKILZG.png =100x) - Nassim Nicholas Taleb,隨機騙局 ![](https://i.imgur.com/u7wdcRU.png =100x) ### MISC - https://personal.ntu.edu.sg/nprivault/indext.html