Market strategy and formula to derive the rate of return Python code

First, the premise of the model and assumptions

The total number of days set policy for the \ (the n-\) , the first \ (t \) day market closing price of \ (P_T \) , the first \ (t \) one-day return day rate \ (r_t \) , \ ( n-\) accumulated earnings day rate \ (r_ {cum} \)

Suppose policy sale only market index, the \ (T \) position days under section \ ((t-1) \ ) closing price out \ (S_ {T-1} \) , so the first day of yield \ (r_ {1} = 0 \)

Important Note: To avoid future function, can not use the \ (s_ {t} \) Calculation \ (T \) positions day.

Second, the single-day market rate of return

1. Discrete

\[ r_t=\frac{P_t}{P_{t-1}}-1 \]

Python code corresponding to:

df['market_dis'] = df['close']/df['close'].shift()-1
或
df['market_dis'] = df['close'].pct_change()

2. Continuous

\[ r_t=ln\frac{P_t}{P_{t-1}} \]

Python code corresponding to:

df['market_con'] = np.log(df['close'] / df['close'].shift())

Third, the cumulative market rate of return

1. Discrete

\[ \begin{align} 1+r_{cum}&=(1+r_{1})(1+r_{2})\cdots(1+r_{n})\\[1.5ex] &=\frac{P_{1}}{P_{0}}\cdot \frac{P_{2}}{P_{1}}\cdots\frac{P_{n}}{P_{n-1}}\\[1.5ex] &=\frac{P_{n}}{P_{0}} \end{align} \]

Python code corresponding to:

# 注意:这里的累积收益率是以净值形式体现的,在实际应用中可能需要在此结果基础上-1
df['market_dis_cum'] = (1+df['market_dis']).cumprod()

2. Continuous

\[ \begin{align} \text{exp}(r_{cum})& = \text{exp}(r_{1}+r_{2}+\cdots+r_{n}) \\[1.5ex] & = \text{exp}\left({ln\frac{P_{1}}{P_{0}}+ln\frac{P_{2}}{P_{1}}+\cdots+ln\frac{P_{n}}{P_{n-1}}}\right)\\[1.5ex] & =\frac{P_{1}}{P_{0}}\cdot\frac{P_{2}}{P_{1}}\cdots\frac{P_{n}}{P_{n-1}}\\[1.5ex] & =\frac{P_{n}}{P_{0}}\\[2ex] \end{align} \]

Python code corresponding to:

# 注意:这里的累积收益率是以净值形式体现的,在实际应用中可能需要在此结果基础上取np.log()
df['market_con_cum'] = df['market_con'].cumsum().apply(np.exp)

Four, one-day return policy

1. Discrete

\[ r_t= \begin{cases} 0&,t=1\\[2ex] s_{t-1}\left(\cfrac{P_t}{P_{t-1}}-1\right)&,t=2,3,\cdots,n\\[2ex] \end{cases} \]

Python code corresponding to:

df['strategy_dis'] = df['position'].shift()*df['market_dis']

2. Continuous

\[ r_t= \begin{cases} 0&,t=1\\[2ex] s_{t-1}ln\cfrac{P_t}{P_{t-1}}&,t=2,3,\cdots,n\\[2ex] \end{cases} \]

Python code corresponding to:

df['strategy_con'] = df['position'].shift()*df['market_con']

Fifth, the cumulative rate of return policies

1. Discrete

\[ \begin{align} 1+r_{cum}&=(1+r_{2})(1+r_{3})\cdots(1+r_{n})\\[1.5ex] &=\left[1+s_{1}\left(\frac{P_{2}}{P_{1}}-1\right)\right]\left[1+s_{2}\left(\frac{P_{3}}{P_{2}}-1\right)\right]\cdots\left[1+s_{n-1}\left(\frac{P_{n}}{P_{n-1}}-1\right)\right]\\[1.5ex] \end{align}\\ \]

Python code corresponding to:

# 注意:这里的累积收益率是以净值形式体现的,在实际应用中可能需要在此结果基础上-1
df['strategy_dis_cum'] = (1+df['strategy_dis']).cumprod()

2. Continuous

\[ \begin{align} \text{exp}(r_{cum})& = \text{exp}(r_{2}+r_{3}\cdots+r_{n}) \\[1.5ex] & = \text{exp}\left({s_1ln\frac{P_{2}}{P_{1}}+s_2ln\frac{P_{3}}{P_{2}}+\cdots+s_{n-1}ln\frac{P_{n}}{P_{n-1}}}\right)\\[1.5ex] & =\left(\frac{P_{2}}{P_{1}}\right)^{s_1}\left(\frac{P_{3}}{P_{2}}\right)^{s_2}\cdots\left(\frac{P_{n}}{P_{n-1}}\right)^{s_{n-1}}\\[1.5ex] \end{align} \]

Python code corresponding to:

# 注意:这里的累积收益率是以净值形式体现的,在实际应用中可能需要在此结果基础上取np.log()
df['strategy_con_cum'] = df['strategy_con'].cumsum().apply(np.exp)

Guess you like

Origin www.cnblogs.com/oddgod/p/11516169.html