Zheng-Liang Lu
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
      • Invitee
    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Versions and GitHub Sync Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
Invitee
Publish Note

Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

Your note will be visible on your profile and discoverable by anyone.
Your note is now live.
This note is visible on your profile and discoverable online.
Everyone on the web can find and read all notes of this public team.
See published notes
Unpublish note
Please check the box to agree to the Community Guidelines.
View profile
Engagement control
Commenting
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
  • Everyone
Suggest edit
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
Emoji Reply
Enable
Import from Dropbox Google Drive Gist Clipboard
   owned this note    owned this note      
Published Linked with GitHub
Subscribed
  • Any changes
    Be notified of any changes
  • Mention me
    Be notified of mention me
  • Unsubscribe
Subscribe
--- tags: finance, python --- # Programming Labs for Python Programming in Finance :::warning Welcome to [Python Programming in Finance](https://hackmd.io/@arthurzllu/By0DaDPmu). This course is an inter-disciplinary course in the fields of computer science, finance, and (a lot of) math. These techniques are essential both in **<font color = "red">P</font>** & **<font color = "red">Q</font>** quant. ::: ## Lab 1 Start Your Quant Life In the first lab, I expect you to accomplish the following steps: <img src="https://i.imgur.com/JDTfD1j.png" style="float:right" width="300"/> 1. Data acquisition & preprocessing 2. Data visualization 3. Strategy development 4. Backtesting These 4 steps lead you to a complete quantitative analysis of financial data. Please follow the in-class example and try to beat the buy-and-hold strategy. You may start with the following instructions: - ++**Change the target asset**++ - For example, you may consider any equity in the Taiwan market by collecting its historical adjusted close prices in the package [ffn](https://pmorissette.github.io/ffn/). - 本國證券辨識號碼一覽表:[上市證券](https://isin.twse.com.tw/isin/C_public.jsp?strMode=2) and [上櫃證券](https://isin.twse.com.tw/isin/C_public.jsp?strMode=4) - If you are interested in [興櫃證券](https://isin.twse.com.tw/isin/C_public.jsp?strMode=5), you need to collect the historical data by yourself. - For example, [Taiwan Economic Journal](https://www.finasia.biz/) (TEJ). - Even more, you may even consider various markets like oversea equity markets, foreign exchange markets (for example, [BTC-USD](https://finance.yahoo.com/quote/BTC-USD)) or commodity markets (for example, [Gold](https://finance.yahoo.com/quote/GC=F)). - Try to screen your target: https://www.finviz.com/ <center> ![](https://i.imgur.com/56WKXXe.png =300x) </center> - ++**Develop another promising strategy**++ - You could calculate the buy/sell signals by other technical indicators instead of the crossover between SMA5 and SMA10. - For example, [the Bollinger Bands](https://www.dailyfx.com/education/bollinger-bands/forex-trading.html), [the Moving Average Convergence Divergence (MACD)](https://www.dailyfx.com/education/technical-analysis-tools/macd-indicator.html), [the Stochastic Oscillator (KD)](https://www.dailyfx.com/forex/education/trading_tips/daily_trading_lesson/2014/01/22/How_to_Trade_with_Stochastic_Oscillator.html). - You can find more technical analysis [here](http://www.mrao.cam.ac.uk/~mph/Technical_Analysis.pdf). - In addition to the historical prices, you may consider various analyses below: - the macroeconomical analysis (for example, the fiscal policy and the monetary policy); - the fundamental analysis (for example, monthly revenue YoY); - the chip analysis (for example, institutional investors, margin trading). - ++**Benchmark your performance to convince youself (or your boss)**++ - We have considered the final return and the maximum drawdown (MDD) of the simple crossover strategy in class. - You may consider [the Sharpe ratio](https://en.wikipedia.org/wiki/Sharpe_ratio) or other indicators to evaluate your performance against competitors. :::warning You may start with this notebook [link](https://www.csie.ntu.edu.tw/~d00922011/python/notebooks/pyf_2_lab1_start_your_quant_life.ipynb). ::: :::danger The minimal requirement of Lab 1 is to implement at least one of items listed above and your result should be better than the buy-and-hold strategy (or any baseline). ::: ### Lab 1-1 Diversification :::success Don't put all your eggs in one basket. ::: - To diversify the individual risk, you may consider multiple assets in your portfolio. Note that you may apply different strategies to each asset. - For convenience, modify the program as follows: - Calculate the buy/sell signal mask for each asset. - Sum the profit/loss of each strategy. ### References - 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) - 王冠倫,[Python初階配對交易](https://www.csie.ntu.edu.tw/~d06922002/Course/PTS/) ![](https://i.imgur.com/TkhFbyg.png =150x) - 杜金龍,杜金龍教你技術分析,2005 - Emanuel Derman, [My Life as a Quant: Reflections on Physics and Finance](https://www.amazon.com/My-Life-Quant-Reflections-Physics/dp/0470192739), 2016 ![](https://i.imgur.com/mK9w7ly.png =100x) - AQR Capital Management, LLC, [20 for Twenty: Selected Papers from AQR Capital Management](https://www.aqr.com/insights/research/book/20-for-twenty), 2018 - 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) - Nazário, Silva, Sobreiro, and Kimura, [A literature review of technical analysis on stock markets](https://www.sciencedirect.com/science/article/pii/S1062976917300443), The Quarterly Review of Economics and Finance, 2017 - https://www.quantstart.com/ - http://liang-xin.com/website/w/h?mt=2&mc=3194267&cc=3021364 ## Lab 2 Modern Portfolio Theory Consider one of the classical models shown below and apply the selected model to recent market data. - Harry Markowitz (1952): *Portfolio Selection* [link](https://www.math.ust.hk/~maykwok/courses/ma362/07F/markowitz_JF.pdf) - He won 1990 Nobel Memorial Prize in Economic Sciences [link](https://www.nobelprize.org/uploads/2018/06/markowitz-lecture.pdf) - An interview with Eugen Fama: https://www.youtube.com/watch?v=RVWEhCd819E :::warning You may start with this notebook [link](https://www.csie.ntu.edu.tw/~d00922011/python/notebooks/pyf_4_lab2_mean_variance_analysis.ipynb). This notebook demonstrates the procedure of the mean-variance analysis with the [tech giants](https://en.wikipedia.org/wiki/Big_Tech), for example, FAANG (Facebook, Amazon, Apple, Netflix, and Google). ::: - Sharpe (1964), Lintner (1965), and Mossion (1966): **C**apital **A**sset **P**ricing **M**odel (CAPM) <center> ![](https://i.imgur.com/Bswq7jP.jpg =600x) </center> - Fama and French (2004): *The Capital Asset Pricing Model: Theory and Evidence* [link](http://mba.tuck.dartmouth.edu/bespeneckbo/default/AFA611-Eckbo%20web%20site/AFA611-S6B-FamaFrench-CAPM-JEP04.pdf) :::warning You may start with this notebook [link](https://www.csie.ntu.edu.tw/~d00922011/python/notebooks/pyf_4_lab2_capm.ipynb). ::: - Ross (1976): **A**rbitrage **P**ricing **T**heory (APT) [link](https://www.sciencedirect.com/science/article/pii/0022053176900466) - Roll and Ross (1980): *An Empirical Investigation of the Arbitrage Pricing Theory* [link](https://www.jstor.org/stable/2327087) :::warning You may start with this notebook [link](). ::: - Fama and French (1993): *Common risk factors in the returns on stocks and bonds* [link](https://www.sciencedirect.com/science/article/pii/0304405X93900235?via%3Dihub) - The famous 3-factor model is formulated as $$R_{it} = \alpha_i + \beta_{iM}\,R_{Mt} + \beta_{i\text{SMB}}\,\text{SMB} + \beta_{i\text{HML}}\,\text{HML} + e_{it}, $$ where - SMB refers to the return of a portfolio of small stocks in excess of the return on a portfolio of large stocks, and - HML refers to the return of a portfolio of stocks with a high book-to-market ratio in excess of the return on a portfolio of stocks with a high book-to-market ratio. :::warning You may start with this notebook [link](https://www.csie.ntu.edu.tw/~d00922011/python/notebooks/pyf_4_lab2_fama_french_3_factor_model.ipynb). ::: ### Lab 2-1 Black-Litterman Model - Thomas M. Idzorek (2002): *A Step-by-Step Guide to the Black-Litterman Model* [link](https://faculty.fuqua.duke.edu/~charvey/Teaching/BA453_2006/Idzorek_onBL.pdf) - You may try this web-based [portfolio optimizer](https://www.portfoliovisualizer.com/black-litterman-model). ### Lab 2-2 Barra Risk Factor Analysis - The Barra Risk Factor Analysis is a multi-factor model, created by Barra Inc., used to measure the overall risk associated with a security relative to the market. > The multi-factor risk model uses a number of key fundamental factors that represent the features of an investment. Some of these factors include yield, earnings growth, volatility, liquidity, momentum, size, price-earnings ratio, leverage, and growth; factors which are used to describe the risk or returns of a portfolio or asset by moving from quantitative, but unspecified, factors to readily identifiable fundamental characteristics. - Barr Rosenberg - Dr. Kempthorne (2013): https://ocw.mit.edu/courses/mathematics/18-s096-topics-in-mathematics-with-applications-in-finance-fall-2013/lecture-notes/MIT18_S096F13_lecnote15.pdf - Investments & Wealth Institute (2010): *From Concept to Function: Converting Market Theories into Practical Investment Tools, a Discussion with Barr Rosenberg, PhD* [link](https://papers.ssrn.com/sol3/papers.cfm?abstract_id=1702396) ### Lab 2-3 Application of APT - 應用多因子模型在上市900多檔中找出哪50檔股票可以找出0050 EFT的成分股。 - 致敬 Stephen Ross:从 APT 到多因子模型 [link](http://liang-xin.com/website/w/h?mt=2&mc=3194267&cc=3021364&fp=d&c=40584238) ### References - 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) - Kenneth R. French, [Data Library for Fama-French 3-Factor Model](https://mba.tuck.dartmouth.edu/pages/faculty/ken.french/data_library.html) ![](https://i.imgur.com/DWAF97d.png =400x) - 台灣證券交易所,[產業價值鏈資訊平台](https://ic.tpex.org.tw/) ![](https://i.imgur.com/PBqFJaP.png) - TEJ,[多因子資料庫](https://www.tej.com.tw/webtej/seminar/tv-index.htm) - Frazzini, Kabiller, and Pedersen (2013): *Buffett's Alpha* [link](https://www.aqr.com/Insights/Research/Journal-Article/Buffetts-Alpha) - McLean and Pontiff (2015), *Does Academic Research Destroy Stock Return Predictability?* [link](https://onlinelibrary.wiley.com/doi/full/10.1111/jofi.12365) - Chen and Velikov (2020), *Zeroing in on the Expected Returns of Anomalies* [link](https://www.federalreserve.gov/econres/feds/files/2020039pap.pdf) - Chen and Velikov (2020), *Open Source Cross-Sectional Asset Pricing* [link](https://papers.ssrn.com/sol3/papers.cfm?abstract_id=3604626) ## Lab 3 Time Series Model - Review of time series model - Stationaryness - Unit root test - ADF test - Order of integration - $I(0)$ - $I(1)$: first difference of time series becomes stationary, for example, stock price - Mean equation - **A**uto**r**egressive-**M**oving-**A**verage (ARMA) model - **A**uto**r**egressive-**I**ntegrated-**M**oving-**A**verage (ARIMA) model Now build an ARMA/ARIMA model for financial time series, for example, - equity: stock price, traded volume, 三大法人未平倉量, and so on - 台灣加權指數 (2016 - now):[y9999.xlsx](https://www.csie.ntu.edu.tw/~d00922011/python/data/y9999.xlsx) - 台指期貨、電子期、金融期之外資未平倉量 (2016 - now):[外資未平倉.xlsx](https://www.csie.ntu.edu.tw/~d00922011/python/data/%e5%a4%96%e8%b3%87%e6%9c%aa%e5%b9%b3%e5%80%89.xlsx) - foreign exchange - interest rate: long-term yield rate, short-term yield rate, carry spread - macroeconomical factors: GDP/GNP, PMI, and so on :::warning You may start with this notebook [link](https://www.csie.ntu.edu.tw/~d00922011/python/notebooks/pyf_5_lab3_time_series.ipynb). ::: ### Lab 3-1 Volatility Estimation - GARCH model ### Lab 3-2 Cointegration - A short introduction to cointegration could be found here: https://www.csie.ntu.edu.tw/~d00922011/finmath/pairs_trading.pdf - You may follow the trading algorithm presented in the paper *"Trading in the Presence of Cointegration"* [link](https://www.researchgate.net/publication/228259096_Trading_in_the_Presence_of_Cointegration). - AEX: https://finance.yahoo.com/quote/%5Eaex - CAC 40: https://finance.yahoo.com/quote/%5Efchi/ - FTSE 100: https://finance.yahoo.com/quote/%5EFTSE/ - DAX: https://finance.yahoo.com/quote/%5EGDAXI/ - Apply the algorithm to the following assets: - Futures - 上市上櫃股票 - 台指50成分股 [link](https://www.moneydj.com/ETF/X/Basic/Basic0007a.xdjhtm?etfid=0050.TW) or 寶島50成分股 [link](https://www.twse.com.tw/downloads/zh/products/indices/IndexS01.pdf) ### Lab 3-3 Granger Causality ### References - The Organisation for Economic Cooperation and Development (OECD): https://www.oecd-ilibrary.org/ > OECD iLibrary is the online library of the Organisation for Economic Cooperation and Development (OECD) featuring its books, papers and statistics and is the gateway to OECD's analysis and data. ![](https://i.imgur.com/cw4lYKR.png) ## **Lab 4 Options** <center> ![](https://i.imgur.com/t7Q3x1V.png =600x) <font size = -1 color = "gray"><a href = "https://tw.stock.yahoo.com/future/">https://tw.stock.yahoo.com/future/</a></font> </center> ### Lab 4-1 Brownian Motion - Let $S_t$ be the stock price at time $t$, $\mu$ be the expected return rate, $\sigma$ be the volaility of $S_t$. - Then the return rate of $S_t$ follows a Brownian motion $$\dfrac{dS_t}{S_t} = r dt + \sigma dW_t, $$ where $W_t$ is a Wiener process. - To simulate $S_t$, we apply the Ito's lemma to the aforesaid equation so that we have $$S_t = S_0 e^{(r - \sigma^2 / 2) t + \sigma \sqrt{t} W_t}.$$ - For example, assume that $S_0 = 160, r = 0.1, \sigma = 0.3, T = 1.$ With 100 simulation pathes, the plot below shows the trajectories of the asset price. <center> ![](https://i.imgur.com/dlToLVV.png =500x) </center> ### Lab 4-2 Option Pricing - Write a program to calculate the American puts by using ++BOPM++. For example, the price of the American put is n.nnnn with $S_0 = 160, K = 150, r = 0.1, \sigma = 0.3, T = 1.$ ### Lab 4-3 Calculation of Implied Volatility - The European call price $C$ is $$C = f(S_0, K, T, r, \sigma), $$ where $S_0$ is the spot price and $K$ is the strike price. - Now use the market data from [期交所](https://www.taifex.com.tw/cht/3/optDailyMarketSummary) to calculate the implied volatility by solving the nonlinear equation $$C - f(S_0, K, T, r, \sigma_{\text{imp}}) = 0.$$ - You may apply ++the Newton's method++ or ++the bisection method++ (mentioned in the first lecture) to solve the root-finding problem. <center> ![](https://i.imgur.com/1t1gj87.png =500x) </center> ## Lab 5 Risk Management ### Lab 5-1 Value at Risk (VaR) & Expected Shortfall (ES) ### Lab 5-2 Static Hedging vs. Dynamic Hedging - Write a program to benchmark a static hedging strategy and a dynamic hedging strategy. You may consider the case discussed in class as follows: - As a market maker, we maintain the market liquidity by ==selling a put/call option==. - However, I need to avoid loss by hedging the short position by ==buying the underlying asset==. #### Lab 5-2-1 Sell TXO ### Lab 5-3 Hedging Stock by Futures ## Lab 6 Machine Learning ### Ridge Regression ### LASSO Regression ### Logistic Regression ### Support Vector Machine (SVM) ### K-Means Clustering ### Decision Tree & Random Forest #### AdaBoost ### Principal Component Analysis ### Reinforcement Learning ### Recurrent Net #### Long/Short-Term Memory (LSTM)

Import from clipboard

Paste your markdown or webpage here...

Advanced permission required

Your current role can only read. Ask the system administrator to acquire write and comment permission.

This team is disabled

Sorry, this team is disabled. You can't edit this note.

This note is locked

Sorry, only owner can edit this note.

Reach the limit

Sorry, you've reached the max length this note can be.
Please reduce the content or divide it to more notes, thank you!

Import from Gist

Import from Snippet

or

Export to Snippet

Are you sure?

Do you really want to delete this note?
All users will lose their connection.

Create a note from template

Create a note from template

Oops...
This template has been removed or transferred.
Upgrade
All
  • All
  • Team
No template.

Create a template

Upgrade

Delete template

Do you really want to delete this template?
Turn this template into a regular note and keep its content, versions, and comments.

This page need refresh

You have an incompatible client version.
Refresh to update.
New version available!
See releases notes here
Refresh to enjoy new features.
Your user state has changed.
Refresh to load new user state.

Sign in

Forgot password

or

By clicking below, you agree to our terms of service.

Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
Wallet ( )
Connect another wallet

New to HackMD? Sign up

Help

  • English
  • 中文
  • Français
  • Deutsch
  • 日本語
  • Español
  • Català
  • Ελληνικά
  • Português
  • italiano
  • Türkçe
  • Русский
  • Nederlands
  • hrvatski jezik
  • język polski
  • Українська
  • हिन्दी
  • svenska
  • Esperanto
  • dansk

Documents

Help & Tutorial

How to use Book mode

Slide Example

API Docs

Edit in VSCode

Install browser extension

Contacts

Feedback

Discord

Send us email

Resources

Releases

Pricing

Blog

Policy

Terms

Privacy

Cheatsheet

Syntax Example Reference
# Header Header 基本排版
- Unordered List
  • Unordered List
1. Ordered List
  1. Ordered List
- [ ] Todo List
  • Todo List
> Blockquote
Blockquote
**Bold font** Bold font
*Italics font* Italics font
~~Strikethrough~~ Strikethrough
19^th^ 19th
H~2~O H2O
++Inserted text++ Inserted text
==Marked text== Marked text
[link text](https:// "title") Link
![image alt](https:// "title") Image
`Code` Code 在筆記中貼入程式碼
```javascript
var i = 0;
```
var i = 0;
:smile: :smile: Emoji list
{%youtube youtube_id %} Externals
$L^aT_eX$ LaTeX
:::info
This is a alert area.
:::

This is a alert area.

Versions and GitHub Sync
Get Full History Access

  • Edit version name
  • Delete

revision author avatar     named on  

More Less

Note content is identical to the latest version.
Compare
    Choose a version
    No search result
    Version not found
Sign in to link this note to GitHub
Learn more
This note is not linked with GitHub
 

Feedback

Submission failed, please try again

Thanks for your support.

On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

Please give us some advice and help us improve HackMD.

 

Thanks for your feedback

Remove version name

Do you want to remove this version name and description?

Transfer ownership

Transfer to
    Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

      Link with GitHub

      Please authorize HackMD on GitHub
      • Please sign in to GitHub and install the HackMD app on your GitHub repo.
      • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
      Learn more  Sign in to GitHub

      Push the note to GitHub Push to GitHub Pull a file from GitHub

        Authorize again
       

      Choose which file to push to

      Select repo
      Refresh Authorize more repos
      Select branch
      Select file
      Select branch
      Choose version(s) to push
      • Save a new version and push
      • Choose from existing versions
      Include title and tags
      Available push count

      Pull from GitHub

       
      File from GitHub
      File from HackMD

      GitHub Link Settings

      File linked

      Linked by
      File path
      Last synced branch
      Available push count

      Danger Zone

      Unlink
      You will no longer receive notification when GitHub file changes after unlink.

      Syncing

      Push failed

      Push successfully