请教如何写出股价突破均线的语句??


(189) #1

def get_cross(p1,p2):
return p1.shift(1)<=p2.shift(1) and p1>p2

比如 P1就是close ,p2就是ma_10

新生成一列为df.金叉,值为1或0


(189) #2

最近看map 和apply 的用法头大,知道map 用在单个series,但是apply用在整个dateframe还是不熟练


(iQuant) #3

python本身是很优美的,你可以这样处理:

克隆策略
In [14]:
df = D.features(['000001.SZA'],'2017-09-01','2017-10-20',['close_0','ta_sma_20_0'])
df.head()
Out[14]:
instrument close_0 date ta_sma_20_0
0 000001.SZA 1191.721558 2017-09-01 1148.879150
1 000001.SZA 1245.939087 2017-09-04 1152.706177
2 000001.SZA 1237.434326 2017-09-05 1155.842285
3 000001.SZA 1243.812866 2017-09-06 1160.998291
4 000001.SZA 1216.172607 2017-09-07 1165.356934
In [15]:
def g(x):
    if x.close_0 > x.ta_sma_20_0:
        return 1
    else:
        return 0
df['signal'] = df.apply(g, axis=1)
In [16]:
df
Out[16]:
instrument close_0 date ta_sma_20_0 signal
0 000001.SZA 1191.721558 2017-09-01 1148.879150 1
1 000001.SZA 1245.939087 2017-09-04 1152.706177 1
2 000001.SZA 1237.434326 2017-09-05 1155.842285 1
3 000001.SZA 1243.812866 2017-09-06 1160.998291 1
4 000001.SZA 1216.172607 2017-09-07 1165.356934 1
5 000001.SZA 1221.488037 2017-09-08 1173.170654 1
6 000001.SZA 1209.794067 2017-09-11 1179.336548 1
7 000001.SZA 1226.803467 2017-09-12 1185.874512 1
8 000001.SZA 1215.109497 2017-09-13 1191.668457 1
9 000001.SZA 1203.415527 2017-09-14 1196.452271 1
10 000001.SZA 1200.226196 2017-09-15 1200.970459 0
11 000001.SZA 1195.973877 2017-09-18 1205.169678 0
12 000001.SZA 1183.216797 2017-09-19 1207.721069 0
13 000001.SZA 1200.226196 2017-09-20 1209.794067 0
14 000001.SZA 1218.298706 2017-09-21 1212.611206 1
15 000001.SZA 1216.172607 2017-09-22 1214.365356 1
16 000001.SZA 1200.226196 2017-09-25 1214.046387 0
17 000001.SZA 1174.712158 2017-09-26 1210.750854 0
18 000001.SZA 1161.955078 2017-09-27 1208.093140 0
19 000001.SZA 1156.639648 2017-09-28 1205.966919 0
20 000001.SZA 1181.090698 2017-09-29 1205.435425 0
21 000001.SZA 1201.289307 2017-10-09 1203.202881 0
22 000001.SZA 1219.361816 2017-10-10 1202.299316 1
23 000001.SZA 1225.740356 2017-10-11 1201.395630 1
24 000001.SZA 1227.866577 2017-10-12 1201.980347 1
25 000001.SZA 1207.667847 2017-10-13 1201.289307 1
26 000001.SZA 1232.118896 2017-10-16 1202.405518 1
27 000001.SZA 1223.614136 2017-10-17 1202.246094 1
28 000001.SZA 1242.749756 2017-10-18 1203.628174 1
29 000001.SZA 1236.371216 2017-10-19 1205.275879 1
30 000001.SZA 1220.424927 2017-10-20 1206.285889 1


求一个针对dateframe的函数定义及调用范例!
(189) #4

我想问一下,如果参数不止是X的dataframe,而是包括了另外两个p1和p2的参数,比如p1可以代表close,p2可以代表ma5;又或者p1代表ma5,p2代表ma10等,这样方便自己多次调用这个函数,应该怎么写比较合适?多谢


(189) #5

我还是没想明白怎么改写才合适?
除非最简单的做法就是每次重复写一遍代码