【模板案例】MACD底背离

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

(iQuant) #1

7月30日 Meetup 策略模板:

背离股票统计1

克隆策略
In [2]:
import talib as tl
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import datetime
In [3]:
hq_df = DataSource('bar1d_CN_STOCK_A').read(start_date='2019-01-01',
                                            end_date=datetime.datetime.now().strftime('%Y-%m-%d'))
hq_df['close'] = hq_df['close']/hq_df['adjust_factor']
In [21]:
def MACD(df, fastperiod, slowperiod, signalperiod):
    columns = list(df.columns)+['dif','gold','dead']
    macdDIFF, macdDEA, macd = tl.MACDEXT(df['close'], fastperiod=fastperiod, fastmatype=1, slowperiod=slowperiod, slowmatype=1, signalperiod=signalperiod, signalmatype=1)
    macd = macd * 2
    df['dif'] = macdDIFF
    df['dea'] = macdDEA
    df['macd'] = macd
    df['dif_1'] = df['dif'].shift(1)
    df['dea_1'] = df['dea'].shift(1)
    df['gold'] = np.where((df['dif_1'] < df['dea_1'])&(df['dif'] > df['dea']),1,0)
    df['dead'] = np.where((df['dif_1'] > df['dea_1'])&(df['dif'] < df['dea']),1,0)
    return df[columns]
In [22]:
cal_df = hq_df.groupby('instrument').apply(lambda x:MACD(x,12,26,9))
In [23]:
cal_df_filter = cal_df[cal_df.gold+cal_df.dead>0]
cal_df_filter['dead_1'] = cal_df_filter.groupby('instrument')['dead'].shift(1)
cal_df_filter['gold_2'] = cal_df_filter.groupby('instrument')['gold'].shift(2)
cal_df_filter['dead_3'] = cal_df_filter.groupby('instrument')['dead'].shift(3)
cal_df_filter['dif_1'] = cal_df_filter.groupby('instrument')['dif'].shift(1)
cal_df_filter['dif_2'] = cal_df_filter.groupby('instrument')['dif'].shift(2)
cal_df_filter['dif_3'] = cal_df_filter.groupby('instrument')['dif'].shift(3)
cal_df_filter['gold_1'] = cal_df_filter.groupby('instrument')['gold'].shift(1)
cal_df_filter['dead_2'] = cal_df_filter.groupby('instrument')['dead'].shift(2)
cal_df_filter['gold_3'] = cal_df_filter.groupby('instrument')['gold'].shift(3)
In [24]:
cal_df_filter['gold_cond1'] = np.where((cal_df_filter['gold']==1)&(cal_df_filter['dif']<0),1,0)
cal_df_filter['gold_cond2'] = np.where((cal_df_filter['dead_1']==1)&(cal_df_filter['dif_1']<0),1,0)
cal_df_filter['gold_cond3'] = np.where((cal_df_filter['gold_2']==1)&(cal_df_filter['dif_2']<0),1,0)
cal_df_filter['gold_cond4'] = np.where((cal_df_filter['dead_3']==1)&(cal_df_filter['dif_3']>0),1,0)
cal_df_filter['dead_cond1'] = np.where((cal_df_filter['dead']==1)&(cal_df_filter['dif']>0),1,0)
cal_df_filter['dead_cond2'] = np.where((cal_df_filter['gold_1']==1)&(cal_df_filter['dif_1']>0),1,0)
cal_df_filter['dead_cond3'] = np.where((cal_df_filter['dead_2']==1)&(cal_df_filter['dif_2']>0),1,0)
cal_df_filter['dead_cond4'] = np.where((cal_df_filter['gold_3']==1)&(cal_df_filter['dif_3']<0),1,0)
In [25]:
cal_df_filter['gold_bl'] =np.where(cal_df_filter['gold_cond1']+cal_df_filter['gold_cond2']+\
                                   cal_df_filter['gold_cond3']+cal_df_filter['gold_cond4']>3,1,0)
cal_df_filter['dead_bl'] =np.where(cal_df_filter['dead_cond1']+cal_df_filter['dead_cond2']+\
                                   cal_df_filter['dead_cond3']+cal_df_filter['dead_cond4']>3,1,0)
In [27]:
cal_df_filter[cal_df_filter.gold_bl>0]
Out[27]:
adjust_factor amount close date deal_number high instrument low open turn ... gold_cond1 gold_cond2 gold_cond3 gold_cond4 dead_cond1 dead_cond2 dead_cond3 dead_cond4 gold_bl dead_bl
237766 4.564998 4.555305e+08 6.660000 2019-04-12 21898.0 30.402887 600011.SHA 29.581186 29.672487 0.657308 ... 1 1 1 1 0 0 0 0 1 0
242095 1.518425 1.762325e+08 32.590000 2019-04-15 8928.0 51.474609 002792.SZA 48.589600 48.589600 2.366495 ... 1 1 1 1 0 0 0 0 1 0
248206 1.829745 8.737824e+07 20.760000 2019-04-17 5850.0 38.790592 300615.SZA 37.564667 38.076992 10.403522 ... 1 1 1 1 0 0 0 0 1 0
249116 1.000000 1.270143e+08 35.630001 2019-04-17 5930.0 36.150002 300724.SZA 34.599998 34.599998 4.492020 ... 1 1 1 1 0 0 0 0 1 0
249562 1.000000 8.631218e+07 33.549999 2019-04-17 5518.0 33.980000 002931.SZA 32.299999 32.549999 9.798587 ... 1 1 1 1 0 0 0 0 1 0
252702 1.015609 4.050055e+07 59.999996 2019-04-18 2157.0 61.220909 603096.SHA 58.407673 59.047508 1.267733 ... 1 1 1 1 0 0 0 0 1 0
260400 5.038402 3.035771e+08 19.240000 2019-04-22 16339.0 98.500755 002602.SZA 94.218117 94.973877 0.966551 ... 1 1 1 1 0 0 0 0 1 0
266605 2.525480 1.810864e+08 6.830000 2019-04-24 16824.0 17.552086 000622.SZA 16.668167 17.148008 6.304229 ... 1 1 1 1 0 0 0 0 1 0
270098 16.526554 1.348701e+08 3.480000 2019-04-25 9764.0 61.644047 000659.SZA 55.529221 55.529221 2.962313 ... 1 1 1 1 0 0 0 0 1 0
273004 4.165662 6.319240e+08 20.299999 2019-04-26 22379.0 87.062332 002411.SZA 78.731010 78.897636 2.152499 ... 1 1 1 1 0 0 0 0 1 0
283267 1.680672 3.224242e+08 8.520000 2019-04-30 16177.0 14.336132 601006.SHA 14.033611 14.067225 0.257126 ... 1 1 1 1 0 0 0 0 1 0
298519 1.304948 4.922694e+07 93.030006 2019-05-10 1995.0 124.622536 300661.SZA 116.531853 120.577194 1.243229 ... 1 1 1 1 0 0 0 0 1 0
298645 3.397572 2.917177e+07 13.850000 2019-05-10 1858.0 47.124325 002647.SZA 46.139027 46.240955 0.378417 ... 1 1 1 1 0 0 0 0 1 0
298755 1.163985 5.499997e+07 12.700000 2019-05-10 7446.0 14.840809 601098.SHA 14.468333 14.642931 0.242274 ... 1 1 1 1 0 0 0 0 1 0
300135 5.944516 1.290821e+08 2.630000 2019-05-10 8873.0 15.752968 000897.SZA 15.158516 15.752968 3.062982 ... 1 1 1 1 0 0 0 0 1 0
300615 1.991428 5.457588e+07 13.179999 2019-05-10 6320.0 26.525822 603225.SHA 25.231392 25.808907 2.499645 ... 1 1 1 1 0 0 0 0 1 0
300670 1.542654 2.271913e+08 36.489998 2019-05-10 11781.0 56.754242 002796.SZA 52.481091 53.807770 5.117916 ... 1 1 1 1 0 0 0 0 1 0
301590 1.010273 7.320094e+07 52.779999 2019-05-10 3645.0 53.625290 300653.SZA 49.816563 52.433170 3.248383 ... 1 1 1 1 0 0 0 0 1 0
304498 12.522413 2.689041e+08 27.960001 2019-05-13 13073.0 357.765350 002410.SZA 340.359192 341.486206 1.077838 ... 1 1 1 1 0 0 0 0 1 0
306123 4.622173 5.376041e+08 23.010000 2019-05-14 31610.0 109.453056 002458.SZA 103.998894 106.309982 6.534963 ... 1 1 1 1 0 0 0 0 1 0
310585 1.014938 4.100856e+07 28.450001 2019-05-15 2427.0 28.895285 603043.SHA 28.367517 28.458862 1.157862 ... 1 1 1 1 0 0 0 0 1 0
312479 22.391562 2.589978e+08 9.150001 2019-05-16 19012.0 207.121948 000848.SZA 195.926163 196.821823 2.934547 ... 1 1 1 1 0 0 0 0 1 0
314071 27.592602 3.576097e+08 21.320002 2019-05-16 21651.0 591.309448 300146.SZA 567.855774 579.444641 1.928325 ... 1 1 1 1 0 0 0 0 1 0
314376 4.574394 1.563404e+08 6.720000 2019-05-16 12500.0 30.739927 300224.SZA 27.858059 27.949547 3.236300 ... 1 1 1 1 0 0 0 0 1 0
314615 1.011570 7.115832e+07 27.550001 2019-05-16 5010.0 28.576853 002942.SZA 26.725679 26.917877 8.662740 ... 1 1 1 1 0 0 0 0 1 0
315144 1.004375 1.262775e+08 22.990002 2019-05-16 7090.0 23.090582 300748.SZA 20.991438 20.991438 13.591459 ... 1 1 1 1 0 0 0 0 1 0
315396 13.945981 1.929704e+08 1.990000 2019-05-16 19847.0 28.868181 603077.SHA 26.357904 26.636824 1.109109 ... 1 1 1 1 0 0 0 0 1 0
319806 1.000000 7.091544e+07 30.799999 2019-05-20 5620.0 31.000000 603629.SHA 27.959999 28.930000 9.722536 ... 1 1 1 1 0 0 0 0 1 0
319840 2.036828 2.103091e+08 55.070000 2019-05-20 9174.0 112.168121 002812.SZA 103.531967 105.874321 3.082667 ... 1 1 1 1 0 0 0 0 1 0
319942 5.088783 1.560042e+08 6.950000 2019-05-20 10201.0 37.148117 300249.SZA 34.400173 35.723255 7.275154 ... 1 1 1 1 0 0 0 0 1 0
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
1391203 6.221407 1.177758e+08 4.710000 2020-07-21 8210.0 30.173824 300510.SZA 28.556257 28.618473 3.335093 ... 1 1 1 1 0 0 0 0 1 0
1394117 4.297906 6.807109e+06 2.530000 2020-07-21 803.0 11.045618 002323.SZA 10.357953 10.443912 0.850333 ... 1 1 1 1 0 0 0 0 1 0
1395353 1.716504 2.134103e+07 11.250001 2020-07-22 2422.0 19.533815 002875.SZA 18.915874 19.121855 2.504327 ... 1 1 1 1 0 0 0 0 1 0
1396496 43.758972 1.208014e+09 3.740000 2020-07-22 60052.0 163.658554 002131.SZA 154.906754 159.720245 5.736819 ... 1 1 1 1 0 0 0 0 1 0
1396906 3.816120 6.561383e+07 1.490000 2020-07-22 4393.0 5.686019 002716.SZA 5.609696 5.686019 5.630315 ... 1 1 1 1 0 0 0 0 1 0
1398443 1.544848 4.142057e+08 80.440002 2020-07-22 17712.0 125.441658 300653.SZA 113.855301 114.581375 4.356917 ... 1 1 1 1 0 0 0 0 1 0
1399077 1.968206 1.015318e+08 9.740000 2020-07-23 10879.0 19.504921 603879.SHA 18.579865 19.386829 3.746183 ... 1 1 1 1 0 0 0 0 1 0
1400885 1.602002 4.219334e+08 77.059998 2020-07-23 20859.0 127.006721 603233.SHA 121.111351 123.498337 4.125355 ... 1 1 1 1 0 0 0 0 1 0
1401514 2.773818 3.486576e+08 99.740005 2020-07-23 15138.0 279.822754 603127.SHA 266.924500 270.724640 3.172195 ... 1 1 1 1 0 0 0 0 1 0
1401665 1.208567 1.016327e+08 28.589998 2020-07-23 6361.0 34.770473 300817.SZA 31.773226 32.897194 14.563913 ... 1 1 1 1 0 0 0 0 1 0
1403515 5.047483 4.037666e+07 1.480000 2020-07-24 3439.0 7.722649 002638.SZA 7.419800 7.571225 2.745755 ... 1 1 1 1 0 0 0 0 1 0
1407621 1.543275 3.659187e+07 10.080000 2020-07-27 4128.0 15.587077 603339.SHA 14.676545 14.969768 1.206491 ... 1 1 1 1 0 0 0 0 1 0
1408277 4.269219 8.343137e+07 1.570000 2020-07-27 7093.0 6.916135 002470.SZA 6.574597 6.916135 1.874179 ... 1 1 1 1 0 0 0 0 1 0
1408993 6.042765 3.273872e+07 1.440000 2020-07-27 4761.0 9.003720 000662.SZA 8.641154 8.882865 2.069024 ... 1 1 1 1 0 0 0 0 1 0
1410855 3.226859 1.446061e+09 58.149998 2020-07-28 58868.0 189.739304 002557.SZA 175.928360 176.670532 4.985296 ... 1 1 1 1 0 0 0 0 1 0
1410874 1.014302 1.101647e+08 36.230000 2020-07-28 6680.0 37.417603 002976.SZA 36.474300 36.484444 12.125040 ... 1 1 1 1 0 0 0 0 1 0
1411225 1.612986 1.332183e+08 27.160002 2020-07-28 5974.0 43.808701 300793.SZA 39.921402 39.921402 7.447941 ... 1 1 1 1 0 0 0 0 1 0
1411602 5.162990 3.016333e+07 1.180000 2020-07-28 2498.0 6.092328 601258.SHA 5.989069 6.092328 0.252493 ... 1 1 1 1 0 0 0 0 1 0
1411707 3.472164 1.984043e+08 44.720001 2020-07-28 12377.0 158.677902 300630.SZA 153.400208 156.802933 1.440642 ... 1 1 1 1 0 0 0 0 1 0
1412995 6.364722 6.819153e+06 2.280000 2020-07-28 403.0 14.575213 600815.SHA 14.320624 14.511566 0.169210 ... 1 1 1 1 0 0 0 0 1 0
1414245 3.073919 6.954828e+07 11.889999 2020-07-28 5326.0 36.548897 300469.SZA 33.474979 33.505718 3.662036 ... 1 1 1 1 0 0 0 0 1 0
1414556 28.612551 2.735404e+08 1.260000 2020-07-29 26798.0 36.910191 600226.SHA 33.476685 36.910191 11.202228 ... 1 1 1 1 0 0 0 0 1 0
1414896 1.513474 8.558945e+07 20.090000 2020-07-29 6236.0 30.541904 002915.SZA 29.210049 29.437069 8.463956 ... 1 1 1 1 0 0 0 0 1 0
1415035 14.787128 1.264831e+07 2.550000 2020-07-29 998.0 38.002918 000422.SZA 37.411434 38.002918 0.553949 ... 1 1 1 1 0 0 0 0 1 0
1415814 3.701583 8.973378e+07 14.460001 2020-07-29 11614.0 53.821018 600167.SHA 52.414413 53.487873 0.272227 ... 1 1 1 1 0 0 0 0 1 0
1416295 2.871955 3.325898e+08 22.950001 2020-07-29 20513.0 65.911369 300627.SZA 59.162273 59.937702 5.729928 ... 1 1 1 1 0 0 0 0 1 0
1416988 1.529320 2.015329e+07 10.990000 2020-07-29 2469.0 16.807226 300621.SZA 16.256672 16.470776 0.938516 ... 1 1 1 1 0 0 0 0 1 0
1417331 1.807364 1.657344e+09 439.000000 2020-07-29 24156.0 794.643738 300782.SZA 762.707581 767.226013 3.614961 ... 1 1 1 1 0 0 0 0 1 0
1417414 1.511885 4.137062e+07 17.769999 2020-07-29 3234.0 27.077860 002862.SZA 24.704201 24.840271 3.747778 ... 1 1 1 1 0 0 0 0 1 0
1418301 2.605570 2.881031e+08 83.159996 2020-07-29 14545.0 216.757370 002912.SZA 209.227264 211.077225 4.230537 ... 1 1 1 1 0 0 0 0 1 0

7522 rows × 33 columns

背离股票统计2

克隆策略
In [1]:
import talib as tl
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import datetime
In [2]:
hq_df = DataSource('bar1d_CN_STOCK_A').read(start_date='2019-01-01',
                                            end_date=datetime.datetime.now().strftime('%Y-%m-%d'))
hq_df['close'] = hq_df['close']/hq_df['adjust_factor']
In [3]:
def MACD(df, fastperiod, slowperiod, signalperiod):
    macdDIFF, macdDEA, macd = tl.MACDEXT(df['close'], fastperiod=fastperiod, fastmatype=1, slowperiod=slowperiod, slowmatype=1, signalperiod=signalperiod, signalmatype=1)
    macd = macd * 2
    df['dif'] = macdDIFF
    df['dea'] = macdDEA
    df['macd'] = macd
    return df
In [6]:
cal_df = hq_df.groupby('instrument').apply(lambda x:MACD(x,12,26,9))
cal_df['dif_1'] = cal_df.groupby('instrument')['dif'].shift(1)
cal_df['dea_1'] = cal_df.groupby('instrument')['dea'].shift(1)
cal_df['gold'] = np.where((cal_df['dif_1'] < cal_df['dea_1'])&(cal_df['dif'] > cal_df['dea']),1,0)
cal_df['dead'] = np.where((cal_df['dif_1'] > cal_df['dea_1'])&(cal_df['dif'] < cal_df['dea']),1,0)
In [22]:
cal_df_filter = cal_df[cal_df.gold+cal_df.dead>0]
cal_df_filter['DEA1'] = cal_df_filter.groupby('instrument')['dea'].shift(1)
cal_df_filter['DEA2'] = cal_df_filter.groupby('instrument')['dea'].shift(3)
cal_df_filter['DEA3'] = cal_df_filter.groupby('instrument')['dea'].shift(2)
cal_df_filter['P1'] = cal_df_filter.groupby('instrument')['close'].shift(1)
cal_df_filter['P2'] = cal_df_filter.groupby('instrument')['close'].shift(3)
cal_df_filter['A1'] = cal_df_filter.groupby('instrument')['date'].shift(1)
cal_df_filter['A2'] = cal_df_filter.groupby('instrument')['date'].shift(3)
cal_df_filter['delta1_A'] = (cal_df_filter['A1']-cal_df_filter['A2']).apply(lambda x:x.days)
In [23]:
cal_df_filter['gold_cond'] = np.where( (cal_df_filter['gold']>0)&
                                       (cal_df_filter['DEA1']<0)&
                                       (cal_df_filter['DEA2']<0)&
                                       (cal_df_filter['DEA3']<0)&
                                       (cal_df_filter['P1']<cal_df_filter['P2']*1.05)&
                                       (cal_df_filter['DEA1']>cal_df_filter['DEA2'])&
                                       (cal_df_filter['delta1_A']>5)
                                       ,1,0)
In [27]:
cal_df_filter['dead_cond'] = np.where( (cal_df_filter['dead']>0)&
                                       (cal_df_filter['DEA1']>0)&
                                       (cal_df_filter['DEA2']>0)&
                                       (cal_df_filter['DEA3']>0)&
                                       (abs(cal_df_filter['P1']/cal_df_filter['P2']-1)<0.03)&
                                       (cal_df_filter['DEA1']<cal_df_filter['DEA2'])&
                                       (cal_df_filter['delta1_A']>5)&
                                       (cal_df_filter['delta1_A']<30)
                                       ,1,0)

BigQuant AI量化专家Meetup(7月30日场回放及案例模板)