DataFrame的数据处理问题求教

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

(Hugo999) #1
克隆策略
In [1]:
import talib
import pandas as pd
import numpy as np

df1 = DataSource('bar1d_CN_STOCK_A').read(instruments = ['600837.SHA'],
                                          start_date='2018-08-02',end_date='2019-02-01',
                                          fields=['close','adjust_factor']).set_index('date')

df1['后复权'] = df1['close'] / df1['adjust_factor']

#通过rolling_mean 函数来计算移动平均值

df1['MA10_rolling'] = pd.rolling_mean(df1['close'],10)


#将价格数据转化成float类型

close = [float(x) for x in df1['close']]

df1['MA10_rolling']

#  计算海通的日bar的macd和signal数值

df1['MACD'],df1['MACDsignal'],df1['MACDhist'] = talib.MACD(np.array(close),
                            fastperiod=6, slowperiod=12, signalperiod=9)   

#   macd数据是黄线
#   signal数据是白线

#  单纯的拿出这一列MACDsignal

df5 = df1[['MACDsignal']]

df5
Out[1]:
MACDsignal
date
2018-08-02 NaN
2018-08-03 NaN
2018-08-06 NaN
2018-08-07 NaN
2018-08-08 NaN
2018-08-09 NaN
2018-08-10 NaN
2018-08-13 NaN
2018-08-14 NaN
2018-08-15 NaN
2018-08-16 NaN
2018-08-17 NaN
2018-08-20 NaN
2018-08-21 NaN
2018-08-22 NaN
2018-08-23 NaN
2018-08-24 NaN
2018-08-27 NaN
2018-08-28 NaN
2018-08-29 -1.056776
2018-08-30 -0.866929
2018-08-31 -0.754868
2018-09-03 -0.710082
2018-09-04 -0.637967
2018-09-05 -0.643370
2018-09-06 -0.679982
2018-09-07 -0.709775
2018-09-10 -0.861712
2018-09-11 -1.006656
2018-09-12 -1.075816
... ...
2018-12-20 -0.276195
2018-12-21 -0.653561
2018-12-24 -1.089597
2018-12-25 -1.664915
2018-12-26 -2.262431
2018-12-27 -2.788375
2018-12-28 -3.112867
2019-01-02 -3.298995
2019-01-03 -3.262230
2019-01-04 -2.723195
2019-01-07 -1.961349
2019-01-08 -1.190963
2019-01-09 -0.417668
2019-01-10 0.166610
2019-01-11 0.626494
2019-01-14 0.985786
2019-01-15 1.405292
2019-01-16 1.752501
2019-01-17 1.962679
2019-01-18 2.147552
2019-01-21 2.286055
2019-01-22 2.242872
2019-01-23 2.126425
2019-01-24 2.092507
2019-01-25 2.172074
2019-01-28 2.260704
2019-01-29 2.300219
2019-01-30 2.214896
2019-01-31 2.188704
2019-02-01 2.298194

124 rows × 1 columns

现在的得到一个date和signal的df;观察MACDsignal的数据变化是“正-正-正-负-负-负-正-正-正-负…”这样变化的;我现在希望把连续的两行(两个日期是相邻的,date_1 date_2),使得这个date中的MACDsignal数据正好是“由正变负”或者是“由负变正”,然后把这样模式中的date_2,输出成一个df,该怎么处理?谢谢


(iQuant) #2

您好,收到您的提问,我们已提交至策略工程师,会尽快为您解答。


(Hugo999) #3

十分感谢


(达达) #4
#计算模式condition
df5['condition'] = df5['MACDsignal'].rolling(2).apply(lambda x:np.where(x[0]>0 and x[1]<0,1,0))
# 获取满足模式的MACDsignal所在行
result = df5[df5.condition>0]'MACDsignal[]