# 【小散学量化】-2-动量模型的简单实践

• 1、线性回归
• 2、动量模型回测
• 3、小散的思考

## 1.线性回归

``````import numpy as np
from statsmodels import regression
import statsmodels.api as sm
import matplotlib.pyplot as plt
import math
def linreg(X,Y):
# Running the linear regression
model = regression.linear_model.OLS(Y, X).fit()
a = model.params[0]
b = model.params[1]
X = X[:, 1]
# Return summary of the regression and plot results
X2 = np.linspace(X.min(), X.max(), 100)
Y_hat = X2 * b + a
plt.scatter(X, Y, alpha=0.3) # Plot the raw data
plt.plot(X2, Y_hat, 'r', alpha=0.9);  # Add the regression line, colored in red
plt.xlabel('X Value')
plt.ylabel('Y Value')
return model.summary()
``````

``````data1=DataAPI.MktIdxdGet(ticker='000016',beginDate='20150101',endDate='20151201')
asset=data1['closeIndex']
data2=DataAPI.MktIdxdGet(ticker='000001',beginDate='20150101',endDate='20151201')
benchmark=data2['closeIndex']
r_a = asset.pct_change()[1:]
r_b = benchmark.pct_change()[1:]
linreg(r_b.values, r_a.values)

OLS Regression Results
Dep. Variable:    y      R-squared:                0.823
Method:    Least Squares      F-statistic:              1021.
Date:    Fri, 18 Dec 2015      Prob (F-statistic):    2.11e-84
Time:    13:40:10      Log-Likelihood:          685.41
No. Observations:       221      AIC:                     -1367.
Df Residuals:       219      BIC:                     -1360.
Df Model:         1
Covariance Type:    nonrobust

coef    std err    t    P>|t|    [95.0% Conf. Int.]
const       -0.0006        0.001       -0.877     0.381       -0.002     0.001
x1        0.9317        0.029       31.952     0.000        0.874     0.989

Omnibus:    43.308      Durbin-Watson:            1.857
Prob(Omnibus):     0.000      Jarque-Bera (JB):        85.122
Skew:     0.968      Prob(JB):              3.28e-19
Kurtosis:     5.344      Cond. No.                  39.6
``````

``````X = np.random.rand(100)
Y = np.random.rand(100)
linreg(X, Y)

OLS Regression Results
Dep. Variable:    y      R-squared:                0.001
Method:    Least Squares      F-statistic:            0.06682
Date:    Fri, 18 Dec 2015      Prob (F-statistic):     0.797
Time:    13:40:15      Log-Likelihood:         -15.737
No. Observations:       100      AIC:                      35.47
Df Residuals:        98      BIC:                      40.68
Df Model:         1
Covariance Type:    nonrobust

coef    std err    t    P>|t|    [95.0% Conf. Int.]
const        0.5214        0.056        9.245     0.000        0.410     0.633
x1        0.0253        0.098        0.258     0.797       -0.169     0.219

Omnibus:    22.647      Durbin-Watson:            2.366
Prob(Omnibus):     0.000      Jarque-Bera (JB):         5.557
Skew:    -0.164      Prob(JB):                0.0621
Kurtosis:     1.893      Cond. No.                  4.32
``````

## 2 动量模型回测

``````from statsmodels import regression
import statsmodels.api as sm
import scipy.stats as stats
import scipy.spatial.distance as distance
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from CAL.PyCAL import *
from datetime import datetime, timedelta
import numpy as np
cal = Calendar('China.SSE')
def signal_gzs_calc(today,before):
start=start.toDateTime()
end=end.toDateTime()

# 对收盘价数据进行直线拟合
X = np.arange(len(asset))
model = regression.linear_model.OLS(asset, x).fit() # 使用OLS计算y=ax+b对应的a和b
a = model.params[0]
b = model.params[1]
Y_fit = X * b + a
return Y_fit[-1],asset[-1],np.std(asset - Y_fit)
``````
``````start = '2013-01-01'                       # 回测起始时间
today = datetime.today()
delta = timedelta(days = -1)
end = (today+delta).strftime('%Y-%m-%d')
benchmark = 'SH50'                            # 策略参考标准
universe = ['510050.XSHG']    # 股票池                      # 策略参考标准
refresh_rate = 1                          # 调仓频率，即每 refresh_rate 个交易日执行一次 handle_data() 函数
def initialize(account):                   # 初始化虚拟账户状态
pass

def handle_data(account):                  # 每个交易日的买入卖出指令
today=account.current_date
a_1,b_1,c_1=signal_gzs_calc(today,Period('-2B'))
a_2,b_2,c_2=signal_gzs_calc(today,Period('-1B'))
for stk in account.universe:
if (a_1-b_1)< -c_1 and (a_2-b_2)> -c_2 and  not stk in account.valid_secpos:
order(stk, 200000)

if (a_1-b_1)<c_1 and (a_2-b_2)>c_2 and stk in account.valid_secpos:
order(stk, -200000)
``````

## 3、小散的思考

3年回测期内，共发出买卖信号11次，最近一次空仓信号发出在11月19日，产生的信号较少，可以考虑修改两个if条件增加信号量（小散设定为单位标准差）。

1、量化分析师日记，新手必看。

2、zilong.li https://uqer.io/community/share/561e3a65f9f06c4ca82fb5ec 用5日均线和10日均线进行判断--改进版。