自定义函数内外调用的问题(复制了Q群主的大部分内容)

策略分享
标签: #<Tag:0x00007f5b90b75dc0>

(189) #1
克隆策略
In [16]:
start_date = '2017-05-01' # 开始日期
end_date = '2017-11-08' # 结束日期
bm_price = D.history_data(['000300.SHA'], start_date, end_date, fields=['close'])
df = D.history_data(D.instruments(),start_date,end_date,fields=['in_csi800'])
instruments = list(set(df[df['in_csi800']==1]['instrument']))
stock_raw_data = D.history_data(instruments, start_date, end_date,fields=[ 'close'])
df = pd.merge(bm_price, stock_raw_data, how="outer", on="date")#合并指数基准及股票池
df1=pd.DataFrame(np.random.random(len(df)),columns=['a_10'])
df2=pd.DataFrame(np.random.random(len(df)),columns=['a_20'])
df3=pd.DataFrame(np.random.random(len(df)),columns=['a_30'])
df= pd.concat([df1, df2,df3,df], axis=1, join='outer')
df
#上面如果输出df都没有问题,但是往下继续执行,就会出错

# def get_EMA(df,para_df=para_df):
#     df.index = range(len(df))
#     for i in range(len(df)):
#         for j in [10,20,30,60]:
#             a = para_df.ix[i]['a_%s'%str(j)]
#             if i==0:
#                 df.ix[i,'ema_'+str(j)]=df.ix[i,'close']
#             if i>0:
#                 df.ix[i,'ema_'+str(j)]=(1-a)*df.ix[i-1,'ema_'+str(j)]+a*df.ix[i,'close']
#     return df
# para_df =df
# m_df=df.groupby('instrument_y').apply(get_EMA)
# m_df
Out[16]:
a_10 a_20 a_30 close_x instrument_x date close_y instrument_y
0 0.482209 0.818846 0.141332 3426.576660 000300.SHA 2017-05-02 936.538757 000001.SZA
1 0.206270 0.558137 0.185158 3426.576660 000300.SHA 2017-05-02 2549.423340 000002.SZA
2 0.046052 0.508128 0.767467 3426.576660 000300.SHA 2017-05-02 269.193878 000006.SZA
3 0.167119 0.758427 0.108727 3426.576660 000300.SHA 2017-05-02 175.726974 000008.SZA
4 0.654266 0.968464 0.511413 3426.576660 000300.SHA 2017-05-02 63.158310 000009.SZA
5 0.098612 0.478940 0.059634 3426.576660 000300.SHA 2017-05-02 176.535233 000012.SZA
6 0.265073 0.243776 0.221468 3426.576660 000300.SHA 2017-05-02 110.165367 000021.SZA
7 0.676572 0.815359 0.966584 3426.576660 000300.SHA 2017-05-02 79.228981 000025.SZA
8 0.535761 0.045790 0.616007 3426.576660 000300.SHA 2017-05-02 89.792709 000027.SZA
9 0.825719 0.695523 0.152539 3426.576660 000300.SHA 2017-05-02 295.867279 000028.SZA
10 0.829914 0.364308 0.497087 3426.576660 000300.SHA 2017-05-02 114.841644 000031.SZA
11 0.112543 0.522306 0.286662 3426.576660 000300.SHA 2017-05-02 379.077332 000039.SZA
12 0.661177 0.642673 0.870698 3426.576660 000300.SHA 2017-05-02 298.421692 000049.SZA
13 0.175383 0.471902 0.882804 3426.576660 000300.SHA 2017-05-02 143.416916 000050.SZA
14 0.160792 0.770823 0.948610 3426.576660 000300.SHA 2017-05-02 241.101898 000060.SZA
15 0.384056 0.068269 0.152937 3426.576660 000300.SHA 2017-05-02 195.412476 000061.SZA
16 0.635949 0.333880 0.945127 3426.576660 000300.SHA 2017-05-02 111.916557 000062.SZA
17 0.734061 0.174492 0.006343 3426.576660 000300.SHA 2017-05-02 278.490997 000063.SZA
18 0.912761 0.901766 0.428160 3426.576660 000300.SHA 2017-05-02 102.619072 000066.SZA
19 0.083327 0.827959 0.336759 3426.576660 000300.SHA 2017-05-02 296.818756 000069.SZA
20 0.023937 0.627392 0.857549 3426.576660 000300.SHA 2017-05-02 118.699860 000078.SZA
21 0.029421 0.515591 0.521212 3426.576660 000300.SHA 2017-05-02 59.383221 000089.SZA
22 0.466597 0.799027 0.715281 3426.576660 000300.SHA 2017-05-02 43.301903 000090.SZA
23 0.302965 0.933482 0.425666 3426.576660 000300.SHA 2017-05-02 41.924057 000099.SZA
24 0.362700 0.265262 0.274353 3426.576660 000300.SHA 2017-05-02 10.016178 000100.SZA
25 0.597058 0.209685 0.283145 3426.576660 000300.SHA 2017-05-02 15.565258 000156.SZA
26 0.332268 0.329063 0.698688 3426.576660 000300.SHA 2017-05-02 283.782867 000157.SZA
27 0.059465 0.872995 0.613564 3426.576660 000300.SHA 2017-05-02 34.823483 000158.SZA
28 0.579719 0.373457 0.065395 3426.576660 000300.SHA 2017-05-02 8.062081 000166.SZA
29 0.873891 0.776933 0.090137 3426.576660 000300.SHA 2017-05-02 145.818588 000333.SZA
... ... ... ... ... ... ... ... ...
111640 0.636819 0.906117 0.468447 4048.005615 000300.SHA 2017-11-08 38.445343 603188.SHA
111641 0.494174 0.335669 0.021265 4048.005615 000300.SHA 2017-11-08 19.787848 603198.SHA
111642 0.581034 0.750509 0.558633 4048.005615 000300.SHA 2017-11-08 56.355305 603228.SHA
111643 0.656997 0.106686 0.242985 4048.005615 000300.SHA 2017-11-08 33.697224 603328.SHA
111644 0.416776 0.581779 0.737114 4048.005615 000300.SHA 2017-11-08 49.536457 603355.SHA
111645 0.820943 0.811466 0.649960 4048.005615 000300.SHA 2017-11-08 41.422764 603369.SHA
111646 0.173632 0.927200 0.428614 4048.005615 000300.SHA 2017-11-08 37.302593 603377.SHA
111647 0.009601 0.547488 0.142597 4048.005615 000300.SHA 2017-11-08 227.397110 603444.SHA
111648 0.218891 0.899994 0.803583 4048.005615 000300.SHA 2017-11-08 48.337280 603515.SHA
111649 0.367164 0.464221 0.233869 4048.005615 000300.SHA 2017-11-08 33.334148 603528.SHA
111650 0.594926 0.241443 0.359646 4048.005615 000300.SHA 2017-11-08 21.855188 603555.SHA
111651 0.893031 0.380068 0.605280 4048.005615 000300.SHA 2017-11-08 40.554962 603556.SHA
111652 0.951017 0.945350 0.037826 4048.005615 000300.SHA 2017-11-08 32.677135 603567.SHA
111653 0.048193 0.318852 0.431087 4048.005615 000300.SHA 2017-11-08 34.351925 603568.SHA
111654 0.780388 0.959886 0.387831 4048.005615 000300.SHA 2017-11-08 30.679689 603569.SHA
111655 0.586339 0.836478 0.915559 4048.005615 000300.SHA 2017-11-08 53.936703 603589.SHA
111656 0.408505 0.125760 0.825957 4048.005615 000300.SHA 2017-11-08 55.443039 603658.SHA
111657 0.553074 0.754624 0.149420 4048.005615 000300.SHA 2017-11-08 23.442738 603698.SHA
111658 0.338292 0.760531 0.791230 4048.005615 000300.SHA 2017-11-08 20.314941 603766.SHA
111659 0.942863 0.851681 0.429659 4048.005615 000300.SHA 2017-11-08 93.978195 603799.SHA
111660 0.595384 0.271316 0.739039 4048.005615 000300.SHA 2017-11-08 39.816074 603806.SHA
111661 0.063906 0.693571 0.022927 4048.005615 000300.SHA 2017-11-08 57.351646 603816.SHA
111662 0.737099 0.986915 0.425264 4048.005615 000300.SHA 2017-11-08 73.360283 603858.SHA
111663 0.717358 0.886159 0.377360 4048.005615 000300.SHA 2017-11-08 43.018509 603866.SHA
111664 0.440049 0.223670 0.605476 4048.005615 000300.SHA 2017-11-08 79.754585 603868.SHA
111665 0.260272 0.117342 0.810199 4048.005615 000300.SHA 2017-11-08 28.651810 603877.SHA
111666 0.424216 0.161467 0.494196 4048.005615 000300.SHA 2017-11-08 59.164948 603883.SHA
111667 0.015384 0.331824 0.618140 4048.005615 000300.SHA 2017-11-08 39.497005 603885.SHA
111668 0.344018 0.300631 0.784264 4048.005615 000300.SHA 2017-11-08 72.329971 603888.SHA
111669 0.687368 0.148245 0.247127 4048.005615 000300.SHA 2017-11-08 22.336355 603993.SHA

111670 rows × 8 columns


(189) #2

先把df打#,
然后把下面一段的#去掉,有如下出错信息

难道是para_df=df有明显问题??这句的意思是我直接复制了df到para_df
而不是像群主那样生成一个新的para_df


NameError Traceback (most recent call last)
in ()
13 #上面如果输出df都没有问题,但是往下继续执行,就会出错
14
—> 15 def get_EMA(df,para_df=para_df):
16 df.index = range(len(df))
17 for i in range(len(df)):

NameError: name ‘para_df’ is not defined


(iQuant) #3
克隆策略
In [24]:
start_date = '2017-05-01' # 开始日期
end_date = '2017-11-08' # 结束日期
bm_price = D.history_data(['000300.SHA'], start_date, end_date, fields=['close'])
df = D.history_data(D.instruments(),start_date,end_date,fields=['in_csi800'])
instruments = list(set(df[df['in_csi800']==1]['instrument']))
stock_raw_data = D.history_data(instruments[:10], start_date, end_date,fields=[ 'close']) # 只有10只股票,股票数越大,运行时间越长
df = pd.merge(bm_price, stock_raw_data, how="outer", on="date")#合并指数基准及股票池
 
df1=pd.DataFrame(np.random.random(len(df)),columns=['a_10'])
df2=pd.DataFrame(np.random.random(len(df)),columns=['a_20'])
df3=pd.DataFrame(np.random.random(len(df)),columns=['a_30'])
df= pd.concat([df1, df2,df3,df], axis=1, join='outer')
In [25]:
def get_EMA(df,para_df=df):
    df.index = range(len(df))
    for i in range(len(df)):
        for j in [10,20,30]:
            a = para_df.ix[i]['a_%s'%str(j)]
            if i==0:
                df.ix[i,'ema_'+str(j)]=df.ix[i,'close_y']
            if i>0:
                df.ix[i,'ema_'+str(j)]=(1-a)*df.ix[i-1,'ema_'+str(j)]+a*df.ix[i,'close_y']
    return df
In [26]:
m_df=df.groupby('instrument_y').apply(get_EMA)
m_df.head()
Out[26]:
a_10 a_20 a_30 close_x date instrument_x close_y instrument_y ema_10 ema_20 ema_30
instrument_y
000063.SZA 0 0.005628 0.647064 0.691144 3426.576660 2017-05-02 000300.SHA 278.490997 000063.SZA 278.490997 278.490997 278.490997
1 0.103874 0.452517 0.539688 3413.128174 2017-05-03 000300.SHA 281.332764 000063.SZA 280.164520 279.264526 279.078369
2 0.986415 0.646492 0.049334 3404.386475 2017-05-04 000300.SHA 281.490631 000063.SZA 281.012146 280.003815 279.243591
3 0.225828 0.633779 0.989545 3382.550293 2017-05-05 000300.SHA 277.859497 000063.SZA 278.493835 279.762207 278.653656
4 0.762796 0.829557 0.824639 3358.812500 2017-05-08 000300.SHA 268.860657 000063.SZA 269.132660 279.683624 270.109344

(189) #4
克隆策略
In [2]:
import pandas as pd 
from pandas import Series,DataFrame
start_date = '2015-05-01' # 开始日期
end_date = '2017-11-08' # 结束日期
bm_price = D.history_data(['000300.SHA'], start_date, end_date, fields=['close'])
df = D.history_data(D.instruments(),start_date,end_date,fields=['in_csi300'])
instruments = list(set(df[df['in_csi300']==1]['instrument']))
stock_raw_data = D.history_data(instruments, start_date, end_date,fields=['close'])
df = pd.merge(stock_raw_data,bm_price, how="outer", on="date")#合并指数基准及股票池

def get_ratio(df):
    df.index = range(len(df))
    df['rc']=df['close_x']*10000//df['close_y']#与基准比值
    for i in [1,10,20,30,60]:
        df['rc_%s'%str(i)]=abs(df['rc']-df['rc'].shift(i))
    for j in range(4):
        for i in [10,20,30,60]:
            df['a_%s'%str(i)]=(df['rc_%s'%str(i)]/pd.rolling_sum(df['rc_1'], i)*0.8)**2#两个循环计算出a_10、20等
    return df
gr_df=df.groupby('instrument_x').apply(get_ratio)#instrument_x是股票池代码
gr_df.drop(['close_y', 'instrument_y','rc_1','rc_10','rc_20','rc_30','rc_60'], axis=1,inplace=True)
df=gr_df.fillna(0)
df.tail()
Out[2]:
close_x date instrument_x rc a_10 a_20 a_30 a_60
instrument_x
603993.SHA 612 22.626858 2017-11-02 603993.SHA 56.0 0.003787 0.082944 0.064343 0.004265
613 22.271799 2017-11-03 603993.SHA 55.0 0.047603 0.094675 0.044321 0.002500
614 22.239521 2017-11-06 603993.SHA 55.0 0.047603 0.082944 0.056567 0.003475
615 22.271799 2017-11-07 603993.SHA 54.0 0.071111 0.071111 0.063823 0.003475
616 22.336355 2017-11-08 603993.SHA 55.0 0.034083 0.030246 0.079007 0.002500
In [3]:
def get_EMA(df,para_df=df):
    df.index = range(len(df))
    for i in range(len(df)):
        for j in [10,20,30,60]:
            a = para_df.ix[i,'a_%s'%str(j)]
            if i==0:
                df.ix[i,'ema_'+str(j)]=df.ix[i,'rc']
            if i>0:
                df.ix[i,'ema_'+str(j)]=(1-a)*df.ix[i-1,'ema_'+str(j)]+a*df.ix[i,'rc']
    return df
In [4]:
m_df=df.groupby('instrument_x').apply(get_EMA)
m_df.tail()
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-4-c452c3c5147d> in <module>()
----> 1 m_df=df.groupby('instrument_x').apply(get_EMA)
      2 m_df.tail()

<ipython-input-3-f7b720f82e32> in get_EMA(df, para_df)
      7                 df.ix[i,'ema_'+str(j)]=df.ix[i,'rc']
      8             if i>0:
----> 9                 df.ix[i,'ema_'+str(j)]=(1-a)*df.ix[i-1,'ema_'+str(j)]+a*df.ix[i,'rc']
     10     return df

KeyboardInterrupt: 

(189) #5

最后一段运行半天没有结果,只有中断,问题不知道出现在哪??


(iQuant) #6

那可能是因为你数据量大


(189) #7

不会吧?这个只有300个股票,我用你那个写800个股票,算的很快


(iQuant) #8

在:df=gr_df.fillna(0)这一句代码后面加上下面这行代码看看呢:

df = df.drop('instrument_x',axis=1).reset_index().drop('level_1',axis=1)

(189) #9

上面一段的格式改了,我也考虑过是不是这个格式问题;
但是下面一段还是运行半天,死机状出不来


(189) #10

解决了,多谢!!!