关于SHIFT函数请教


(189) #1

bm_price[‘temp’]=bm_price[‘close’]-bm_price.shift(bm_price[‘mm10’])
这样表达式反馈出来的错误是MM10不是数字??
bm_price[‘mm10’]是其中一列,等于我引用了mm10来确定shift的偏移值

比如mm10=8,那么用当天收盘价减去8天前的收盘价,mm10=10时用当天收盘价减去10天前的收盘价,形成一个“temp”序列

想不通还有其他不会出错的表达方式????


(iQuant) #2
克隆策略
In [4]:
df = D.history_data(['000002.SZA'],'2017-05-05','2017-11-11',['close'])
df.head()
Out[4]:
date instrument close
0 2017-05-05 000002.SZA 2508.260986
1 2017-05-08 000002.SZA 2452.492188
2 2017-05-09 000002.SZA 2472.409668
3 2017-05-10 000002.SZA 2479.048828
4 2017-05-11 000002.SZA 2554.734863
In [5]:
df['pre_close_5'] = df['close'].shift(5)
df.head(20)
Out[5]:
date instrument close pre_close_5
0 2017-05-05 000002.SZA 2508.260986 NaN
1 2017-05-08 000002.SZA 2452.492188 NaN
2 2017-05-09 000002.SZA 2472.409668 NaN
3 2017-05-10 000002.SZA 2479.048828 NaN
4 2017-05-11 000002.SZA 2554.734863 NaN
5 2017-05-12 000002.SZA 2575.979980 2508.260986
6 2017-05-15 000002.SZA 2568.012939 2452.492188
7 2017-05-16 000002.SZA 2553.406982 2472.409668
8 2017-05-17 000002.SZA 2541.456543 2479.048828
9 2017-05-18 000002.SZA 2528.178223 2554.734863
10 2017-05-19 000002.SZA 2545.439941 2575.979980
11 2017-05-22 000002.SZA 2532.161621 2568.012939
12 2017-05-23 000002.SZA 2553.406982 2553.406982
13 2017-05-24 000002.SZA 2548.095703 2541.456543
14 2017-05-25 000002.SZA 2722.040771 2528.178223
15 2017-05-26 000002.SZA 2728.679688 2545.439941
16 2017-05-31 000002.SZA 2822.955322 2532.161621
17 2017-06-01 000002.SZA 2800.382324 2553.406982
18 2017-06-02 000002.SZA 2781.792725 2548.095703
19 2017-06-05 000002.SZA 2801.710205 2722.040771