---
# System prepended metadata

title: TWSIAM
tags: [lab]

---

---
title: 'TWSIAM'
tags: lab
---

# AI and quantitative investing and trading


:::info
交大康明軒&鄧惠文教授研究團隊
- Created on 2021/7/12
- Latest update 2021/8/13
:::
---
#### tags: `AI` `FinTech` `machine learning`
---
## Overview: 8/20 Gathertown 海報報告
:::success
* [海報展演活動位置](https://gather.town/invite?token=Rscb12Av)
* 海報規格: 1920(W) x 1080(H)
* 10:00-12:00 為 poster 時段, 可以自由討論, 以下指定時段請該組同學一定要在自己 poster 前. 

| 時間 | 組別 |
| -------- | -------- | 
| 10:00-10:30 | 量子電腦投資組合最佳化 |
| 10:30-11:00 | 數位訊號與影像處理 |
| 11:00-11:30 | 數學建模與機器學習|
| 11:30-12:00 | AI 與量化投資交易|
:::


### 利用hackMD整理工作成果
:::success
1. [hackMD tutorial book](https://hackmd.io/c/tutorials/%2Fs%2Flink-with-github)
2. 用 powerpoint 做海報，挑個喜歡的模板，改裡面的內容。
:::

---

### 參與成員


| 姓名 | Email | HackMD |  Backup |
| -------- | -------- | -------- | ---|
| 王宣瑋 |[:email:](tiffany.nycu01.sc09@nycu.edu.tw) | [:link:](https://hackmd.io/@tOcOYk2uSKyDq-IaFI8RNQ/HyjVdL3Tu)    | [TWSIAM3](https://hackmd.io/WlNQU0WbRhC04l6gy55iOQ)
| 李國誠 |[:email:](kev776522.mg08@nycu.edu.tw)|[:link:](https://hackmd.io/@Kevin888e213/twsiam)| [TWSIAM4](https://hackmd.io/Q2UinjLxR_GEAzY_lwaK8g)
| 蔡博凱 | [:email:](bradytsai0112@gmail.com)|[:link:](https://hackmd.io/@bradytsai/TWSIAM)| [TWSIAM2](https://hackmd.io/hwpwYWsKRIusC-CLYtxVqA)|
| 戴晨洋 |[:email:](dai.chen.yang.1121@gmail.com)|[:link:](https://hackmd.io/@diamondSheep/HytDPKYTu)| [TWSIAM1](https://hackmd.io/7ecXrjdfT8GYETCQctYcPQ)|



---
### Infrustructure

|Index | Content|
|-----|-----|
| 1|  Facebook messanger |
| 2 | Line|
| 3| Goolge meet|
| 4 | [Gathertown](https://gather.town/app/EtGSMfNb6dx3ZJiY/TengKangLab)|
| 5 | [Google shared folder](https://drive.google.com/drive/u/0/folders/0AKiiT88WqlK9Uk9PVA)|
| 6 | [HackMD](https://hackmd.io/@M795nx6MR4G-KeT3RD4pBQ/SJQmaftp_)| 

---
### Time frame
* 10 am, Monday
* 10 am, Wednesday
* 10 am, Friday 

1. Multichart
2. Fundamenatal analysis
3. Technical analysis
4. Chip analysis
5. Reinforcement learning
6. Final presentation

```mermaid
gantt
    title Weekly plan

    section Target
    w1           :a1, 2021-07-12, 7d
    w2     :after a1  , 7d
    w3     :a3, 2021-07-26, 7d
    w4     :after a3,  7d
    w5     :a4, 2021-08-9, 7d
    w6     :after a4,  7d    
        
```
---

## Week 1. Multichart

--- 

### 目標


#### 1. multichart 

認識multichart以及如何開戶


---

#### 2. Demo如何用multichart做回測交易。

請給一個**具體**的交易策略，請挑三隻，回測時間10年
- 指標: movAvg(均線)，MACD，RSI
- 訊號: L, S, E, X  
--- 
  
### Extension

1. Demo如何用multichart做實際交易 


---
## Week 2. Fundamental Analysis

### 目標

#### 1. Summary of vedios

整理重要財務指標
1. [8 steps on how to analyze a stock](https://www.youtube.com/watch?v=mTqmOQLLl1U)
2. [NYU Professor](https://www.youtube.com/playlist?list=PLUkh9m2BorqnKWu0g5ZUps_CbQ-JGtbI9)

#### 2. Python project

Apply the arbitrage pricing theory to build a linear regression model to explain stock return. Consider three stocks of your interests and apply the [Fama-French three factor model](http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/data_library.html) to the stock. 

##### 2.1 EDA

For returns and factors, list descriptive statistics, pairwise scatterplots, heatmap of correlations. Need to summarize your results in a succinct way
    
Variable descriptions
| Stock/Fund | Descriptions  | 
| -------- | -------- | 
| Text     | Text     | 


Summary statistics
| Stock/Fund | mean | sd  | skewness | kurtosis |min | median | max |  
| -------- | -------- | -------- | -------- | -------- | -------- |-------- | -------- |
| Text     | Text     | Text     |


##### 2.2 In-sample analysis

Show the estimated coefficients and $R^2$. The linear regression model is:
$$(r_t-r_f) = \beta_1 + \beta_2(r_m-r_f)+\beta_3 HML_t +\beta_4 SMB_t + e_t,$$where the error is assumed to be independently normally distributed $e_t\sim N(0,\sigma^2)$. Let $S_t$ the stock price at day $t$. $r_t$ is calculated by the formula of single return:
$$r_t = \frac{S_{t}-S_{t-1}}{S_{t-1}}. $$
Note that all the data is in daily basis. 


To compare various models, we report $R^2$. 

| Stock/Fund | $R^2$ | 
| -------- | -------- |
| Model 1     | Text     | 
| Model 2 | |
##### 2.3 Model diagnostics

Calculate the residual, $\hat{e}_i$:
\begin{align}
\hat{e}_t&=& (r_t-r_f) - \hat{\beta}_1 - \hat{\beta}_{2}(r_m-r_f)-\hat{\beta}_3HML_t-\hat{\beta}_4 SMB_t.
\end{align}
 We will check if residuals follow like iid normal. 

| Type | Plot | What to check |
| -------- | -------- | -------- |
| 1     | time series plot of residuals     | discernible patterns     |
| 2| histogram of residuals| bell-shape|
|3| QQ plot of residuals| a straight line|
|4 | ACF and PACF of residuals | AR-MA effect|
|5 | ACF and PACF of squared-residuals, $\hat{e}_t^2$ | GARCH effect| 

:::warning

*Things to improve*

1. Choose Amazon, Dow Jones index, SP500 index, Nasdaq index.
2. Show the time series plot of 
*accumulative returns*.
3. In return-risk plan, we use *annualized
return (%)*: 
    yaxis: -10% to 35% 
    x-axis: 0 to 35%
4. Use the same period: 
    2018/5/29-2021/5/27.
5. **Because 3-factor model use percentage, we use percentage for the daily return.**
:::


### 延伸閱讀

#### 1. [Quatitative Trading](https://www.amazon.com/Quantitative-Trading-Algorithms-Analytics-Optimization/dp/1498706487) [下載點](https://1lib.net/book/3496321/3d73cc)

#### 2. Pythons examples
    - Beginner of python (lec2) cheatsheat 
    - EDA (lec2) MLFinTech_EDA.ipynb
    - Time series (lec4) lab_PortfolioManagement.ipynb
    - Linear regression (lec6) MLFinTech_Regression.ipynb


---
## Week 3. Technical Analysis

### 目標

#### 1. Summary of a vedio and a paper 

1. [The Only Technical Analysis Video You Will Ever Need... (Full Course: Beginner To Advanced), 3 millions 次, 1:17:34](https://www.youtube.com/watch?v=eynxyoKgpng)
2. [Foundations of technical analysis (2000), *Journal of Finance*](https://www.cis.upenn.edu/~mkearns/teaching/cis700/lo.pdf) 

---    
#### 2. Write you own python codes to test a trading strategy. 

:::info
**Strategy A**: We use TWSE index from 2018/5/29-2021/5/27. 
Indicator: MA (5, 20), 
signal策略: 黃金交叉時做多， 死亡交叉時平倉
k = 30
:::


Assume 50,000 NTD, buy one 1 share each time. 
1. col1: price
2. col2: MA(5)
3. col3: MA(20)
4. ma_sign
5. signal: 1= buy, 0 = no action, -1= sell
6. nShare: number of shares at current time
7. cash: cash at current time
8. MarketValue: 浮動市值 market value of stocks 
9. TotalValue: tatal account value
10. accReturn (compared with TWSE accumulative return)


A simplified example 
| Date | Price | Signal | cash | MarketValue | TotalVAlue | accReturn (%)|
| -------- | -------- | -------- | -------- | -------- | -------- | -------- |
|1  |     16000 |    0| 50000 |     0       |        50000 | 0|
|2   |   15000|       1|     35000|     15000 |50000 | 0|
|3   |   15500|  0 |    35000 |    15500   | 50500  |1|
|4   |   17000| 0 | 35000 | 17000 | 52000 | 4 |


#### 3. Try a different strategy, strategy B. 

自由發揮：可以考慮兩口買賣，或多口買賣。

Plot accumulative return among Strategy A, B, and TWSE. 


#### 4. Question from 國誠

假設手上有5萬塊現金，在大盤2萬放空一張的話，過了五天大盤漲到2萬五。我實際上是虧五千，但market value用當天收盤計價會顯示25000，可是我實際賣掉只會拿回15000

| Date | Price | Signal | cash | MarketValue | TotalVAlue | accReturn (%)|
| -------- | -------- | -------- | -------- | -------- | -------- | -------- |
| 1 | 18000| 0 | 50000| 0| 50000| 0|
| 2 | 20000 | -1 | 70000| -20000 | 50000| 0 |
| 3 | 25000 | 0 | 70000 | -25000 | 45000 | -10 |
| 4 | 25000 | 1 | 45000 | 0 | 45000 | -10| 

#### My solution(by國誠)：
$$\mbox{Teng's total value} = \mbox{cash} + \mbox{market value}.$$

1. 


#### My solution(by國誠)：
$$\mbox{total value} = \mbox{cash} + \mbox{margin} + \mbox{unrealized profit}.$$


| Date  | Price | Signal | Cash  | margin (保證金)   | Market Value | Unrealized Profit | Total Value |
| ----- | ----- | ------ | ----- | --- | ------------ | ----------------- | ----------- |
| Date1 | 18000 | 0      | 50000 | 0    | 0            | 0                 | 50000       |
| Date2 | 20000 | -1     | 30000 | 20000   | 20000        | 0                 | 50000       |
| Date3 | 25000 | 0      | 30000 |  20000   | 15000        | -5000             | 45000       |
| Date4 | 25000 | 1      | 45000 |  0   | 0            | 0                 | 45000       |







### 延伸閱讀

- [Introduction To Technical Analysis for CFD And Forex Trading](https://www.youtube.com/watch?v=aePv-bo18QU)

- [Introduction to Technical Analysis for Beginners, 1,117,082次, 41:34]


---
## Week 4. Chip Analysis and Reinforcement learning

### 目標
1. Summarize 籌碼分析
2. Specify an indicator and propose a strategy and do backtest. 陽明 2019/1/1 - 2021/7/31. 

三大法人: 外資，投信，自營商。

1. 陽明accumulative return
2. shares in hand 
3. 500萬，keep 整數張(一張1000股)。


### 閱讀資料 
1. [籌碼分析，市場先生](https://rich01.com/blog-pos-16/)
2. [8分鐘搞懂籌碼分析：如何搭上千張大戶順風車](https://www.youtube.com/watch?v=WHsaZN-DETs)
3. [8分鐘搞懂籌碼分析：分點資訊，我找到了主力的蹤跡](https://www.youtube.com/watch?v=L4uJTblDM5w)



:::info

input: price $p_t$, volume $v_t$ 

signal: $s(p_t, v_t)$

strategy: $a(s(p_t, v_t), \theta)$: +\theta, -1, 0. 

portfolio value at $t$: $V_t$

Target

$$\max_{ s, a, \theta} \quad V_T $$


:::


:::warning

*** 陽明
*** 台積電
*** 富邦
*** 雄獅


國誠:
signal: 
$$p_{t+h}| max(c_{t-1},c_{t-5}), $$
action: +1, -1, 0 

Technical: $p_t$, $v_t$,
Chip: $c_t$ 
Technical + chip 

MV(3) = (p_{t-1}+p_{t-2}+p_{t-3})/3

|Strategy |Signal| Action|
| -----| -----| -----| 
|1 | MV(3) |$I_{MV(3) > MV(5)}$ | 


博凱:
$$p_{t+h}| I_{c_{(t-1)}>0}  I_{c_{t-3}>0}, $$
晨洋
signal: $c_{t} > threshold$ 

:::








---
## Week 5. Reinforcement learning

### 目標
:::info
1. Explain what is reinforcement learning
2. Reformulate a strategy under the framework of reinforcement learning. And do a backtest. 
:::

### Reference
:::info
1. [Markov Decision Process, Wiki](https://en.wikipedia.org/wiki/Markov_decision_process)
2. [Reinforcement learning, Wiki](https://en.wikipedia.org/wiki/Reinforcement_learning)
3. [李宏毅](https://www.youtube.com/watch?v=kk6DqWreLeU)
4. [Reinforcement Learning for Trading Practical Examples and Lessons Learned by Dr. Tom Starke](https://www.youtube.com/watch?v=c0gpgCyjTM8)
5. [The mathematician who cracked Wall Street | Jim Simons](https://www.youtube.com/watch?v=U5kIdtMJGc8&t=324s)
:::

### Summary
#### 1. Markov chain*

:::info
Let $S$ denote the state space. Let $s_t$ and $a_t$ denote a random variable at time $t$, and let $s$, $s'$, $a$, denote realizations. At each time step, the probability of moving to state $s'$ from state $s$ can be characterized by a transition probability matrix
$$P(s, s') = Pr(s_{t+1}=s'|s_t=s).$$



For example, let $S = \{1,2\}$ to denote a state space of two states. A transition probability matrix is
$$\left[\begin{matrix}0.3 & 0.7 \\ 0.1 & 0.9\end{matrix}\right].$$
:::

#### 2. Markov Decision Process (MDP)
:::info
A MDP is a 4-tuple ${\displaystyle (S,A,P_{a},R_{a})}$, where:
1. $S$: a set of states called the state space,
2. $A$: a set of actions called the action space,
3. $P_{a}(s,s')=\Pr(S_{t+1}=s'\mid s_{t}=s, a_{t}=a)$: the probability that action $a$ in state $s$ at time $t$ will lead to state $s'$ at time $t+1$,
4. $R_{a}(s,s')$: the immediate reward received after transitioning from state $s$ to state $s'$, due to action $a$.
 
For simplicity, we assume the transition probability is independent of the action, 
\begin{align}
P_{a}(s,s')&=\Pr(s_{t+1}=s'\mid s_{t}=s, a_{t}=a)\\
&= \Pr(s_{t+1}=s'\mid s_{t}=s)\\
&= P(s,s').\end{align}

:bear: We need to find a policy $\pi(\cdot)$ that maps $s$ to $a$,  to maximize 
$$E\left[\sum _{t=0}^{\infty }{\gamma ^{t}R_{a_{t}}(s_{t},s_{t+1})}\right],$$
$$s_{t+1}\sim P(s_{t},s_{t+1})$$
where  $\gamma$ is the discount factor satisfying $0\leq \ \gamma\leq 1$. 
:::

:::success
*Toy example 1: (MDP)*
陽明海運，only 1 period, two two state
1. S = {1 = 漲, 2 = 跌}
2. A ={1, -1}, 1 = 買，-1 = 賣. 
3. Transition probability:
$$ Pr(s'|s) =\begin{pmatrix}0.570 & 0.430 \\ 0.516 & 0.484 \end{pmatrix} $$
4. Reward (勝的次數)

| $a$ | R_a(s, s') |
| -------- | -------- |
| 1     | $R_1(1,1) = 1$, $R_1(1,2)=0$ | 
| -1     | $R_{-1}(1,1)=0$, $R_{-1}(1,2) = 1$     | 

5. Policy: 
不管怎樣就是買:
$\pi_1$:
$$\pi_1 = \begin{pmatrix}1 & 0 \\ 1 & 0 \end{pmatrix}$$
$\pi_2$: 不管怎樣都賣
$$\pi_2 = \begin{pmatrix}0 & 1 \\ 0 & 1 \end{pmatrix}$$
$\pi_3$: 看到漲就買，看到跌就賣
$$\pi_3 = \begin{pmatrix}1 & 0 \\ 0 & 1 \end{pmatrix}$$
$\pi_4$: 看到跌就買，看到漲就賣
$$\pi_4 = \begin{pmatrix}0 & 1 \\ 1 & 0 \end{pmatrix}$$


6. Return: Start from $t$ = 2, assume we only trade for consecutive 8 days. Fake stock prices
action = 1, 今天盤後用收盤價買，明天盤後用盤後價賣調。

$\pi_1$
|Time| 1| 2| 3| 4| 5| 6| 7| 8| 9|
|---|-|-|-|-|-|-|-|-|-|
|price| 10| 12| 13| 14| 20| 15| 20| 18| 21|
|State| Na| 1| 1| 1| 1| 2| 1| 2| 1|
|Action| Na | 1 | 1| 1| 1| 1| 1|1|1|
|Expected Reward| Na |  0.57 | 0.57 | 0.57 | 0.57 | 0.516 | 0.57| 0.516 | 0.57|
|Expected Return | Na| 0.57 | 1.14 |  | 
|Realized Reward | Na | 1 | 1 | 1 | 0 | 1 | 0 | 1| Na |
|Realized Return | | 


:heart: Complete these tables for the four strategies ($\pi_1$, $\pi_2$, $\pi_3$, $\pi_4$)
:heart: Complete these four tables using the real four stocks 陽明 (start from day 2 till the last day) 
:star: try a 混和型策略
:star: try a more realistic state space to include the idea of moving average 均線


:::






:::info


*3. Reinforcement learning*
Terminologies: Agent: the investor

1. $S$: state space, environment
2. $A$: action space, 
3. $R$: reward
4. $P(s'|s, a)$: transition probability
5. $\pi(a|s)$: a policy
\begin{align}
&\pi :A\times S\rightarrow [0,1],\\
&\pi (a|s)=\Pr(a_{t}=a\mid s_{t}=s)
\end{align}
6. Return: $U_t = R_t+\gamma R_{t+1}+\gamma^2 R_{t+2}+\cdots = \sum_{i=0}^{\infty}\gamma^{i}R_{t+i}$.
7. Action-value function: $Q_\pi(s_t, a_t) = E[U_t|s_t,a_t].$
8. Optimal action-value function: $Q^*(s_t,a_t) = \max_\pi Q_\pi(s_t,a_t).$
9. State-value function: $V_\pi(s_t) = E_A[Q_\pi(s_t, A)]$.
:::

:::success
*Toy example 2 (reinforcement learning)*
$s_t = 1$ if  $p_t>p_{t-1}$

1. $S = \{1, 2\}$: 1 = 漲， 2= 跌
2. $A = \{1, 2, 3\}$: 1 = 賣一張，2 = 不動，3 =買一張。
3. $R_t = \{1, 0, -1\}$: 1 = 當下為勝，0 = 不動， -1 = 當下為負0
$R_1(1|1) = 1$, $R_1(2|1) =-1$, $R_1(1|2) = 1$, $R_1(2|2) =-1$,
$R_2(1|1) = 0$, $R_2(2|1) = 0$, $R_2(1|2) = 0$, $R_2(2|2) = 0$,
$R_{3}(1|1) = -1$, $R_{3}(2|1) = 1$, $R_{3}(1|2) = -1$, $R_{3}(2|2) = 1$

4. Our policy is indeed a matrix: $$\pi =\begin{pmatrix}\pi_{1,1} & \pi_{1,2} & \pi_{1,3} \\
\pi_{2,1} & \pi_{2,2} & \pi_{2,3}\end{pmatrix}$$
We have constrains: $\pi_{i,j}\geq 0$, $\sum_{j=1}^{3}\pi_{i,j}=1$. 
:::



---
:::success

RL.v2 (2021/8/13)

| No |  Column 2 | Column 3 |
| -------- | -------- | -------- |
| 1     | $S$     | How do you modify your states? 五個(大漲，小漲，平盤，小跌，大跌)。    |
| 2     | $A$     | -1: 賣, 0: 不動, 1: 買 |
| 3     | $R$     | 考慮兩種: 勝率，投資總報酬  | 
| 4     | $P(s,s')$     | |

:::


## Week 6. Final presentation

### 目標

> Multiple choice 單選下面一個路線即可

1. Propose a trading strategy and convince the public that it will work

2. Extensive empirical analysis on an existing strategies. 

Things to write in your hackMD.
:::info
1. State: 
    1. Plot the histogram of the return (or price difference)
    2. Define the 5 states 
    ex. 平盤: -0.2sig <= return <= 0.2sig 
    3. Report the marginal distributions of the five states
    4. Report transision probability matrix 
2. Action: 
3. Reward: 
:::




---

## Appendix

---
### Appendix A. Supervised learning
:::info
Multiple linear regression:
$$y = \beta_0+\beta_1 x_1 + \cdots + \beta_p x_p + \varepsilon = f(\beta, x) + \varepsilon, $$
where $f(\cdot)$ is a linear function. 
::: 

:::info
Machine learning (supervised-learning) generalizes multiple linear regression using a non-linear function $f(\cdot)$. Examples:
1. neural network
2. regression tree
3. support vector machine 
4. boosting
:::

---

### Appendix B. Mathematical formulation of quantitative Trading
:::success
Suppose that, at time $t$, $x_t=(x_{t,1},\ldots,x_{t,p})$ denotes the explanatory variable that can predict the price of an asset $y_t$. Suppose the original value is $V_0$ in NTD. We would like to set up a strategy, $\theta_t$, at time $t$, so that the account value equals the cash value plus the market value: 
$$V_t = (V_0 - \theta_t y_t) + \theta_t y_t $$

|Date | price | Cash account | \theta | Market Account | Total value| 
|1  | $p_1$| $V_0$ | 0 | 0 | 
|2  | $p_2$ | $V_0 - p_2$   | 1 | $p_2$ | 
|3  | $p_3$|   $V_0-p_2$ | 1 | $p_3$ | $V_0-p_2 + p_3$ | 
|4  | $p_4$| $V_0-p_2$


If it is a margin trade, we modify the above setting as follows. Then, the strategy 
$$\theta_t = a(s(p_{t-1}, v_{p-1})). $$
:::
