请 [注册] 或 [登录]  | 返回主站

量化交易吧 /  量化平台 帖子:3364679 新帖:0

股票自适应均线

汇市江湖百晓生发表于:9 月 15 日 20:00回复(1)

股票自回归均线研究¶

import numpy as np
import pandas as pd
import sklearn.model_selection 
security='000001.XSHE'
price=get_price(security, start_date=None, end_date='2019-8-1', 
                frequency='daily', fields=None, skip_paused=False, fq='pre', count=2000).close
#建立dataFrame数据模板,便于后面处理
data=pd.DataFrame()
data['close']=price
data['date']=price.index
data=data.reset_index(drop=True)
# 计算20日移动平均价格
# import time
# it=time.time()
# for i in data.index:
#     if i<19:
#         data['x']=None
#     else:
#         #非法赋值,会导致处理时间大幅延迟,和发生不可预知的风险,赋值用标准Pandas定位df.loc或者df.iloc
#         data['x'][i]=data['close'][i-19:i+1].mean()
# ct1=time.time()-it
# ct1
import time
it=time.time()
for i in data.index:
    if i<19:
        data['x']=None
    else:
        data.loc[i,'x']=data['close'][i-19:i+1].mean()
ct=time.time()-it
ct
1.9210963249206543
# 计算路径效率
def alpha(data):
    for i in data.index:
        if i<9:
            data['a']=None
        else:
            b=np.abs(data.loc[i,'close']-data.loc[i-9,'close'])
            c=0
            for j in range(i-9,i):
                d=np.abs(data.loc[j+1,'close']-data.loc[j,'close'])
                d=d+c
                c=d
            data.loc[i,'a']=b/d
alpha(data)
#计算变换因子
def cur_b(data):
    if data.a.empty:
        data['b']=None
    else:
        data['b']=np.square(data.a*(2/3-2/14)+2/14)
    return data
data=cur_b(data)
# 以20日均线为基础的,自适应均线
def cur_data(data):
    for i in data.index:
        if i<19:
            data['y']=None
        elif i==19:
            data['y']=data['x']
        elif i>19:
            aa=data['b'][i]
            bb=data['x'][i]
            cc=data['y'][i-1]
            data.loc[i,'y']=aa*bb+(1-aa)*cc
cur_data(data)
# 以收盘价为基础的自适应均线
def cur_data1(data):
    for i in data.index:
        if i<19:
            data['y2']=None
        elif i==19:
            data['y2']=data['close']
        elif i>19:
            aa=data['b'][i]
            bb=data['close'][i]
            cc=data['y2'][i-1]
            data.loc[i,'y2']=aa*bb+(1-aa)*cc
cur_data1(data)
# 画图查看均线之间的关系
import matplotlib.pyplot as plt
plt.figure(figsize=(16,8))
plt.plot(data.date,data.x,label='ma')
plt.plot(data.date,data.y,label='zishiying')
plt.plot(data.date,data.y2,label='zishiying2')
#plt.plot(data.date,data.close,label='close')
plt.legend()
plt.show()
 从图形上看绿线的变化速度要远快于黄色可以利用绿线和黄线的变化来构建交易策略
data
.dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; }
close date x a b y y2
0 5.88 2011-05-17 None None NaN None 5.880000
1 5.96 2011-05-18 None None NaN None 5.960000
2 5.90 2011-05-19 None None NaN None 5.900000
3 6.04 2011-05-20 None None NaN None 6.040000
4 5.89 2011-05-23 None None NaN None 5.890000
5 5.98 2011-05-24 None None NaN None 5.980000
6 5.98 2011-05-25 None None NaN None 5.980000
7 5.86 2011-05-26 None None NaN None 5.860000
8 5.79 2011-05-27 None None NaN None 5.790000
9 5.78 2011-05-30 None 0.138889 0.046487 None 5.780000
10 5.81 2011-05-31 None 0.223881 0.0676665 None 5.810000
11 5.74 2011-06-01 None 0.235294 0.0708126 None 5.740000
12 5.50 2011-06-02 None 0.692308 0.255525 None 5.500000
13 5.50 2011-06-03 None 0.619048 0.218201 None 5.500000
14 5.55 2011-06-07 None 0.728814 0.275223 None 5.550000
15 5.49 2011-06-08 None 0.753846 0.289152 None 5.490000
16 5.45 2011-06-09 None 0.719298 0.270018 None 5.450000
17 5.44 2011-06-10 None 0.686275 0.25234 None 5.440000
18 5.37 2011-06-13 None 0.719298 0.270018 None 5.370000
19 5.44 2011-06-14 5.7175 0.606557 0.212132 5.7175 5.440000
20 5.35 2011-06-15 5.691 0.619048 0.218201 5.71172 5.420362
21 5.26 2011-06-16 5.656 0.5 0.163832 5.70259 5.394089
22 5.23 2011-06-17 5.6225 0.529412 0.176541 5.68845 5.365121
23 5.27 2011-06-20 5.584 0.56 0.190262 5.66858 5.347023
24 5.36 2011-06-21 5.5575 0.245283 0.0736247 5.6604 5.347978
25 5.35 2011-06-22 5.526 0.2 0.0613152 5.65216 5.348102
26 5.46 2011-06-23 5.5 0.0333333 0.0257017 5.64825 5.350978
27 5.59 2011-06-24 5.4865 0.333333 0.100781 5.63195 5.375067
28 5.63 2011-06-27 5.4785 0.301587 0.0904996 5.61806 5.398138
29 5.61 2011-06-28 5.47 0.464286 0.149038 5.59599 5.429714
... ... ... ... ... ... ... ...
1970 13.50 2019-06-21 12.384 0.654822 0.236059 12.3614 13.043431
1971 13.55 2019-06-24 12.4505 0.602339 0.210101 12.3801 13.149862
1972 13.29 2019-06-25 12.503 0.449735 0.143211 12.3977 13.169931
1973 13.37 2019-06-26 12.5535 0.466667 0.150003 12.4211 13.199942
1974 13.71 2019-06-27 12.6255 0.616438 0.216926 12.4654 13.310587
1975 13.78 2019-06-28 12.71 0.596154 0.207142 12.5161 13.407822
1976 13.93 2019-07-01 12.804 0.6 0.20898 12.5762 13.516946
1977 14.18 2019-07-02 12.924 0.598086 0.208064 12.6486 13.654904
1978 14.01 2019-07-03 13.038 0.228758 0.0690023 12.6755 13.679406
1979 13.99 2019-07-04 13.145 0.352518 0.107262 12.7258 13.712721
1980 13.92 2019-07-05 13.251 0.262411 0.0785741 12.7671 13.729008
1981 13.59 2019-07-08 13.32 0.202703 0.0620183 12.8014 13.720387
1982 13.59 2019-07-09 13.3735 0.157143 0.0507016 12.8304 13.713776
1983 13.56 2019-07-10 13.4295 0.137615 0.0461996 12.8581 13.706672
1984 13.54 2019-07-11 13.4835 0.230769 0.0695568 12.9016 13.695079
1985 14.12 2019-07-12 13.5715 0.129252 0.0443357 12.9313 13.713918
1986 14.00 2019-07-15 13.6445 0.134328 0.0454626 12.9637 13.726924
1987 13.75 2019-07-16 13.6985 0.183099 0.0570092 13.0056 13.728239
1988 13.69 2019-07-17 13.7365 0.205479 0.0627449 13.0515 13.725840
1989 13.67 2019-07-18 13.737 0.177305 0.0555692 13.0895 13.722737
1990 13.99 2019-07-19 13.7615 0.285714 0.0855662 13.147 13.745606
1991 13.85 2019-07-22 13.7765 0.168831 0.0534962 13.1807 13.751191
1992 13.76 2019-07-23 13.8 0.125 0.0434028 13.2076 13.751573
1993 13.88 2019-07-24 13.8255 0.2 0.0613152 13.2455 13.759447
1994 14.20 2019-07-25 13.85 0.0555556 0.0295694 13.2634 13.772474
1995 14.23 2019-07-26 13.8725 0.17037 0.0538698 13.2962 13.797121
1996 14.29 2019-07-29 13.8905 0.465517 0.149537 13.385 13.870825
1997 14.37 2019-07-30 13.9 0.576271 0.19777 13.4869 13.969547
1998 14.13 2019-07-31 13.906 0.328571 0.0992036 13.5285 13.985464
1999 14.10 2019-08-01 13.9115 0.0990991 0.0379339 13.543 13.989809

2000 rows × 7 columns

 

全部回复

0/140

量化课程

    移动端课程