时间序列

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

(finyle) #1
克隆策略

start_date = '2019-05-07' end_date = '2019-07-04' instrument = D.instruments(start_date=start_date, end_date=end_date) len(instrument)

In [25]:
# 沪深300指数成分
df = D.history_data(instrument, start_date, end_date, ['in_csi300'])
instruments = list(set(df[df['in_csi300']==1]['instrument']))
print('沪深300指数成分股预览10只股票:', instruments[:10])
沪深300指数成分股预览10只股票: ['600339.SHA', '002739.SZA', '603260.SHA', '600157.SHA', '300027.SZA', '601688.SHA', '000709.SZA', '600019.SHA', '600018.SHA', '600518.SHA']
In [49]:
# (举例)获取国防军工行业股票列表
df = D.history_data(instrument, start_date, end_date, ['industry_sw_level1'])
instruments = list(set(df[df['industry_sw_level1'] == 650000]['instrument']))
D.history_data(instruments, '2017-07-27', '2017-07-27', ['company_name']).head() 
Out[49]:
company_name date instrument
441666 中兵红箭股份有限公司 2017-07-27 000519.SZA
441690 航天工业发展股份有限公司 2017-07-27 000547.SZA
441833 中国航发动力控制股份有限公司 2017-07-27 000738.SZA
441849 中航飞机股份有限公司 2017-07-27 000768.SZA
442030 中航工业机电系统股份有限公司 2017-07-27 002013.SZA
In [50]:
df = D.history_data(instrument, '2017-08-23', '2017-08-23', ['concept']).dropna()   # 'concept' 是股票的概念字段
df['is_ai'] = df['concept'].map(lambda x: '人工智能' in x)  # 以人工智能为例,找到相关概念股票
st = list(df[df['is_ai'] == True]['instrument'])
D.history_data(st,'2017-08-23','2017-08-23',['name', 'concept']).head()
Out[50]:
name date instrument concept
0 浪潮信息 2017-08-23 000977.SZA 云计算;IPV6;大数据;网络安全;去IOE;人工智能;雄安新区;融资融券;融资融券标的
1 紫光国芯 2017-08-23 002049.SZA 物联网;重组;智能IC卡;芯片国产化;国家队;高校;人工智能;融资融券;融资融券标的
2 软控股份 2017-08-23 002073.SZA 机器人;工业4.0;国家队;人工智能;融资融券;融资融券标的
3 海得控制 2017-08-23 002184.SZA 智能电网;浦东新区;机器人;工业4.0;军民融合;人工智能
4 江南化工 2017-08-23 002226.SZA 重组;人工智能;预增;并购阶段(需定增)
In [178]:
#涨停股票日志
df =D.history_data(instrument, start_date, end_date, ['close','amount','high','price_limit_status','suspended','st_status'])

import talib as ta
from numpy import float as f

df = df[lambda df:df['st_status']==0]
df_zt = df[lambda df:df['price_limit_status']==3]
df_zt.head()
Out[178]:
amount high price_limit_status st_status date close instrument suspended
183 533123040.0 82.521004 3 0 2016-07-01 82.521004 000603.SZA False
521 772057024.0 199.645813 3 0 2016-07-01 199.645813 002055.SZA False
677 568780800.0 19.883762 3 0 2016-07-01 19.883762 002211.SZA False
1056 252999632.0 90.986679 3 0 2016-07-01 90.986679 002592.SZA False
1069 431659040.0 76.095001 3 0 2016-07-01 76.095001 002605.SZA False
In [191]:
#不同涨停次数对应的股票数
from collections import Counter

list_zt = df_zt['instrument']
count_zt_l = list_zt.drop_duplicates()
#list_zt[lambda list_zt :list_zt['instrument'] == '000603.SZA']
#for i in enumerate(count_zt_l):
#    while i == list_zt['instrument']:
 #       list_zt['instrument'] = i
  #      j = j + 1
   #     list_zt['counts'] = j
counted_l = Counter(list_zt)
d = sorted(counted_l.items(),key= lambda x:x[1],reverse=True )
df_d = pd.DataFrame(d)
df_d = df_d.rename(columns={0:'instrument',1:'counts'})
list_zt10puls = df_d[lambda df_d: df_d['counts'] > 10]['instrument']
list(list_zt10puls)
Out[191]:
['603032.SHA',
 '002806.SZA',
 '600903.SHA',
 '300542.SZA',
 '002813.SZA',
 '002848.SZA',
 '603690.SHA',
 '603016.SHA',
 '300618.SZA',
 '300561.SZA',
 '002907.SZA',
 '002836.SZA',
 '601882.SHA',
 '300526.SZA',
 '300520.SZA',
 '300663.SZA',
 '300556.SZA',
 '300647.SZA',
 '002805.SZA',
 '300514.SZA',
 '300531.SZA',
 '603322.SHA',
 '300545.SZA',
 '300698.SZA',
 '300578.SZA',
 '300573.SZA',
 '002846.SZA',
 '300612.SZA',
 '300730.SZA',
 '603533.SHA',
 '300688.SZA',
 '300609.SZA',
 '300598.SZA',
 '300562.SZA',
 '603058.SHA',
 '300589.SZA',
 '603559.SHA',
 '603177.SHA',
 '601619.SHA',
 '002346.SZA',
 '002356.SZA',
 '300708.SZA',
 '300684.SZA',
 '603986.SHA',
 '603336.SHA',
 '002888.SZA',
 '603881.SHA',
 '603603.SHA',
 '300649.SZA',
 '600604.SHA',
 '600776.SHA',
 '300675.SZA',
 '002865.SZA',
 '603363.SHA',
 '300603.SZA',
 '300638.SZA',
 '300565.SZA',
 '300538.SZA',
 '300536.SZA',
 '600182.SHA',
 '002824.SZA',
 '300560.SZA',
 '300667.SZA',
 '300546.SZA',
 '300678.SZA',
 '300624.SZA',
 '002895.SZA',
 '300571.SZA',
 '300534.SZA',
 '603722.SHA',
 '300629.SZA',
 '002819.SZA',
 '300588.SZA',
 '300635.SZA',
 '300615.SZA',
 '000935.SZA',
 '300682.SZA',
 '300597.SZA',
 '300706.SZA',
 '300670.SZA',
 '002826.SZA',
 '002927.SZA',
 '300543.SZA',
 '300643.SZA',
 '603903.SHA',
 '002703.SZA',
 '002847.SZA',
 '300552.SZA',
 '300586.SZA',
 '300585.SZA',
 '300611.SZA',
 '603602.SHA',
 '300525.SZA',
 '002803.SZA',
 '002845.SZA',
 '002795.SZA',
 '002600.SZA',
 '603335.SHA',
 '002755.SZA',
 '002877.SZA',
 '300648.SZA',
 '300540.SZA',
 '300619.SZA',
 '300539.SZA',
 '603069.SHA',
 '603676.SHA',
 '300604.SZA',
 '300666.SZA',
 '002903.SZA',
 '000595.SZA',
 '002830.SZA',
 '603386.SHA',
 '002243.SZA',
 '002825.SZA',
 '300522.SZA',
 '600679.SHA',
 '300519.SZA',
 '603383.SHA',
 '300563.SZA',
 '300582.SZA',
 '603777.SHA',
 '002886.SZA',
 '300746.SZA',
 '300591.SZA',
 '002451.SZA',
 '603067.SHA',
 '603189.SHA',
 '002915.SZA',
 '603186.SHA',
 '601212.SHA',
 '300555.SZA',
 '002302.SZA',
 '603929.SHA',
 '300692.SZA',
 '002858.SZA',
 '000670.SZA',
 '002881.SZA',
 '002856.SZA',
 '300521.SZA',
 '002863.SZA',
 '300576.SZA',
 '603007.SHA',
 '002908.SZA',
 '002708.SZA',
 '603159.SHA',
 '603501.SHA',
 '300541.SZA',
 '603843.SHA',
 '002802.SZA',
 '300550.SZA',
 '000760.SZA',
 '002837.SZA',
 '002862.SZA',
 '603663.SHA',
 '603712.SHA',
 '603859.SHA',
 '603577.SHA',
 '300676.SZA',
 '300549.SZA',
 '300601.SZA',
 '300527.SZA',
 '300711.SZA',
 '300705.SZA',
 '002565.SZA',
 '603090.SHA',
 '000638.SZA',
 '300579.SZA',
 '603895.SHA',
 '601330.SHA',
 '002774.SZA',
 '603499.SHA',
 '603036.SHA',
 '300686.SZA',
 '300104.SZA',
 '300312.SZA',
 '300715.SZA',
 '300537.SZA',
 '300725.SZA',
 '300613.SZA',
 '002931.SZA',
 '300216.SZA',
 '002864.SZA',
 '002547.SZA',
 '000605.SZA',
 '603138.SHA',
 '002893.SZA',
 '002607.SZA',
 '603045.SHA',
 '601116.SHA',
 '300356.SZA',
 '603633.SHA',
 '300743.SZA',
 '002839.SZA',
 '300428.SZA',
 '601500.SHA',
 '603183.SHA',
 '300175.SZA',
 '300634.SZA',
 '002853.SZA',
 '002809.SZA',
 '002871.SZA',
 '300528.SZA',
 '603648.SHA',
 '300672.SZA',
 '002909.SZA',
 '002835.SZA',
 '300345.SZA',
 '300653.SZA',
 '300659.SZA',
 '300471.SZA',
 '002796.SZA',
 '000923.SZA',
 '002098.SZA',
 '600939.SHA',
 '300152.SZA',
 '603977.SHA',
 '603888.SHA',
 '300710.SZA',
 '300340.SZA',
 '002695.SZA',
 '300727.SZA',
 '603133.SHA',
 '300557.SZA',
 '000068.SZA',
 '300532.SZA',
 '002857.SZA',
 '002843.SZA',
 '300551.SZA',
 '300662.SZA',
 '600113.SHA',
 '300606.SZA',
 '300745.SZA',
 '002808.SZA',
 '002930.SZA',
 '300548.SZA',
 '300657.SZA',
 '300553.SZA',
 '601313.SHA',
 '300487.SZA',
 '603505.SHA',
 '300547.SZA',
 '000971.SZA',
 '300701.SZA',
 '002264.SZA',
 '002211.SZA',
 '603667.SHA',
 '603389.SHA',
 '002842.SZA',
 '600695.SHA',
 '603966.SHA',
 '002898.SZA',
 '300566.SZA',
 '603628.SHA',
 '300325.SZA',
 '300099.SZA',
 '002017.SZA',
 '000159.SZA',
 '002164.SZA',
 '601865.SHA',
 '002052.SZA',
 '300277.SZA',
 '601086.SHA',
 '300581.SZA',
 '601700.SHA',
 '300693.SZA',
 '603569.SHA',
 '300640.SZA',
 '000856.SZA',
 '002192.SZA',
 '300631.SZA',
 '603127.SHA',
 '603160.SHA',
 '002922.SZA',
 '603776.SHA',
 '603050.SHA',
 '300535.SZA',
 '300517.SZA',
 '300466.SZA',
 '603813.SHA',
 '002040.SZA',
 '300023.SZA',
 '603106.SHA',
 '600614.SHA',
 '300655.SZA',
 '300390.SZA',
 '300139.SZA',
 '600929.SHA',
 '601606.SHA',
 '000993.SZA',
 '603988.SHA',
 '002137.SZA',
 '002054.SZA',
 '600689.SHA',
 '000025.SZA',
 '603612.SHA',
 '300593.SZA',
 '300107.SZA',
 '002921.SZA',
 '603283.SHA',
 '300530.SZA',
 '603083.SHA',
 '002828.SZA',
 '000610.SZA',
 '300491.SZA',
 '002917.SZA',
 '603031.SHA',
 '300633.SZA',
 '603089.SHA',
 '603220.SHA',
 '300411.SZA',
 '002575.SZA',
 '600053.SHA',
 '002822.SZA',
 '300644.SZA',
 '002807.SZA',
 '603721.SHA',
 '002829.SZA',
 '002265.SZA',
 '000622.SZA',
 '002458.SZA',
 '300523.SZA',
 '300559.SZA',
 '603716.SHA',
 '300279.SZA',
 '603009.SHA',
 '002300.SZA',
 '603080.SHA',
 '601811.SHA',
 '002919.SZA',
 '002892.SZA',
 '300515.SZA',
 '002725.SZA',
 '300707.SZA',
 '300680.SZA',
 '000885.SZA',
 '300731.SZA',
 '300674.SZA',
 '300696.SZA',
 '603683.SHA',
 '603607.SHA',
 '600218.SHA',
 '002878.SZA',
 '603882.SHA',
 '002827.SZA',
 '300697.SZA',
 '300748.SZA',
 '300085.SZA',
 '603165.SHA',
 '002077.SZA',
 '603079.SHA',
 '000736.SZA',
 '603817.SHA',
 '002816.SZA',
 '601990.SHA',
 '002869.SZA',
 '300608.SZA',
 '300665.SZA',
 '000615.SZA',
 '002333.SZA',
 '603626.SHA',
 '603797.SHA',
 '002684.SZA',
 '603909.SHA',
 '000633.SZA',
 '300671.SZA',
 '603616.SHA',
 '002307.SZA',
 '300620.SZA',
 '000912.SZA',
 '601326.SHA',
 '002897.SZA',
 '603320.SHA',
 '603912.SHA',
 '601519.SHA',
 '603319.SHA',
 '002883.SZA',
 '603738.SHA',
 '002676.SZA',
 '300402.SZA',
 '300700.SZA',
 '300570.SZA',
 '603963.SHA',
 '300076.SZA',
 '300691.SZA',
 '603329.SHA',
 '600536.SHA',
 '603105.SHA',
 '002910.SZA',
 '300689.SZA',
 '300265.SZA',
 '300103.SZA',
 '002158.SZA',
 '002820.SZA',
 '002849.SZA',
 '603477.SHA',
 '002457.SZA',
 '603655.SHA',
 '002750.SZA',
 '300572.SZA',
 '600462.SHA',
 '000509.SZA',
 '002076.SZA',
 '002175.SZA',
 '601858.SHA',
 '600734.SHA',
 '300587.SZA',
 '300719.SZA',
 '300584.SZA',
 '601066.SHA',
 '002181.SZA',
 '300506.SZA',
 '002817.SZA',
 '002840.SZA',
 '300722.SZA',
 '300299.SZA',
 '300128.SZA',
 '300462.SZA',
 '600149.SHA',
 '603444.SHA',
 '300529.SZA',
 '300134.SZA',
 '600131.SHA',
 '600146.SHA',
 '002288.SZA',
 '603060.SHA',
 '002113.SZA',
 '002923.SZA',
 '300703.SZA',
 '300645.SZA',
 '300668.SZA',
 '002691.SZA',
 '300479.SZA',
 '000070.SZA',
 '300153.SZA',
 '603033.SHA',
 '002902.SZA',
 '603887.SHA',
 '603819.SHA',
 '600532.SHA',
 '300554.SZA',
 '300568.SZA',
 '000927.SZA',
 '600207.SHA',
 '601949.SHA',
 '300018.SZA',
 '603305.SHA',
 '603823.SHA',
 '300292.SZA',
 '002889.SZA',
 '300503.SZA',
 '600652.SHA',
 '002112.SZA',
 '603039.SHA',
 '603330.SHA',
 '603787.SHA',
 '002184.SZA',
 '300599.SZA',
 '603906.SHA',
 '300602.SZA',
 '300366.SZA',
 '300308.SZA',
 '600517.SHA',
 '603098.SHA',
 '603500.SHA',
 '300590.SZA',
 '603908.SHA',
 '300600.SZA',
 '600775.SHA',
 '603826.SHA',
 '300699.SZA',
 '603360.SHA',
 '603040.SHA',
 '002231.SZA',
 '300370.SZA',
 '603042.SHA',
 '300736.SZA',
 '300685.SZA',
 '300592.SZA',
 '300702.SZA',
 '603056.SHA',
 '000862.SZA',
 '002810.SZA',
 '601162.SHA',
 '300749.SZA',
 '002692.SZA',
 '600155.SHA',
 '600621.SHA',
 '000958.SZA',
 '002417.SZA',
 '002941.SZA',
 '300610.SZA',
 '600624.SHA',
 '300677.SZA',
 '300461.SZA',
 '603110.SHA',
 '002870.SZA',
 '002896.SZA',
 '600209.SHA',
 '600139.SHA',
 '300116.SZA',
 '603803.SHA',
 '300505.SZA',
 '603956.SHA',
 '601375.SHA',
 '002875.SZA',
 '300617.SZA',
 '000677.SZA',
 '600119.SHA',
 '002504.SZA',
 '000868.SZA',
 '603637.SHA',
 '300747.SZA',
 '002916.SZA',
 '603557.SHA',
 '002925.SZA',
 '300493.SZA',
 '603713.SHA',
 '600463.SHA',
 '603578.SHA',
 '300407.SZA',
 '002486.SZA',
 '603323.SHA',
 '603429.SHA',
 '002447.SZA',
 '002761.SZA',
 '603258.SHA',
 '603289.SHA',
 '600975.SHA',
 '300374.SZA',
 '002851.SZA',
 '300630.SZA',
 '000023.SZA',
 '300492.SZA',
 '300654.SZA',
 '000721.SZA',
 '603169.SHA',
 '002823.SZA',
 '300333.SZA',
 '002801.SZA',
 '603778.SHA',
 '603829.SHA',
 '000017.SZA',
 '300392.SZA',
 '603990.SHA',
 '603969.SHA',
 '600722.SHA',
 '600698.SHA',
 '300278.SZA',
 '000806.SZA',
 '603987.SHA',
 '600112.SHA',
 '000723.SZA',
 '002799.SZA',
 '603955.SHA',
 '603658.SHA',
 '002141.SZA',
 '000672.SZA',
 '600520.SHA',
 '603259.SHA',
 '300627.SZA',
 '603896.SHA',
 '002937.SZA',
 '603668.SHA',
 '000590.SZA',
 '300046.SZA',
 '300621.SZA',
 '300558.SZA',
 '000820.SZA',
 '600086.SHA',
 '000416.SZA',
 '603388.SHA',
 '300344.SZA',
 '000586.SZA',
 '002476.SZA',
 '603229.SHA',
 '300348.SZA',
 '002110.SZA',
 '601228.SHA',
 '603421.SHA',
 '002885.SZA',
 '300713.SZA',
 '000791.SZA',
 '600156.SHA',
 '300240.SZA',
 '000710.SZA',
 '300062.SZA',
 '300044.SZA',
 '603638.SHA',
 '603286.SHA',
 '300694.SZA',
 '300650.SZA',
 '300766.SZA',
 '600831.SHA',
 '002899.SZA',
 '600874.SHA',
 '603269.SHA',
 '002932.SZA',
 '000815.SZA',
 '600213.SHA',
 '603918.SHA',
 '603976.SHA',
 '603706.SHA',
 '600460.SHA',
 '002208.SZA',
 '000790.SZA',
 '002651.SZA',
 '300533.SZA',
 '002780.SZA',
 '603466.SHA',
 '603991.SHA',
 '300167.SZA',
 '300622.SZA',
 '603630.SHA',
 '000635.SZA',
 '300502.SZA',
 '603232.SHA',
 '300762.SZA',
 '300268.SZA',
 '300304.SZA',
 '002786.SZA',
 '300362.SZA',
 '300605.SZA',
 '002811.SZA',
 '300759.SZA',
 '600908.SHA',
 '300425.SZA',
 '600860.SHA',
 '002057.SZA',
 '601595.SHA',
 '603937.SHA',
 '300690.SZA',
 '300140.SZA',
 '300500.SZA',
 '300720.SZA',
 '300716.SZA',
 '000859.SZA',
 '300310.SZA',
 '600446.SHA',
 '000917.SZA',
 '300742.SZA',
 '002467.SZA',
 '002177.SZA',
 '300717.SZA',
 '603157.SHA',
 '002282.SZA',
 '600268.SHA',
 '600084.SHA',
 '300656.SZA',
 '002906.SZA',
 '300056.SZA',
 '002800.SZA',
 '603321.SHA',
 '002629.SZA',
 '002350.SZA',
 '002662.SZA',
 '000713.SZA',
 '002769.SZA',
 '300334.SZA',
 '603527.SHA',
 '300567.SZA',
 '603000.SHA',
 '002124.SZA',
 '300242.SZA',
 '600677.SHA',
 '002606.SZA',
 '300718.SZA',
 '300518.SZA',
 '603366.SHA',
 '000019.SZA',
 '000151.SZA',
 '603978.SHA',
 '000020.SZA',
 '603717.SHA',
 '002477.SZA',
 '600800.SHA',
 '002783.SZA',
 '600962.SHA',
 '002873.SZA',
 '601128.SHA',
 '600191.SHA',
 '002861.SZA',
 '002234.SZA',
 '603378.SHA',
 '603486.SHA',
 '000957.SZA',
 '002213.SZA',
 '000571.SZA',
 '002668.SZA',
 '603960.SHA',
 '603886.SHA',
 '601011.SHA',
 '300626.SZA',
 '002157.SZA',
 '600550.SHA',
 '603580.SHA',
 '300412.SZA',
 '603677.SHA',
 '002423.SZA',
 '300019.SZA',
 '300738.SZA',
 '603822.SHA',
 '603181.SHA',
 '600165.SHA',
 '300397.SZA',
 '002628.SZA',
 '601068.SHA',
 '603203.SHA',
 '600318.SHA',
 '002016.SZA',
 '603516.SHA',
 '002058.SZA',
 '603958.SHA',
 '300317.SZA',
 '603238.SHA',
 '002569.SZA',
 '600093.SHA',
 '603660.SHA',
 '603356.SHA',
 '300623.SZA',
 '603013.SHA',
 '002782.SZA',
 '600868.SHA',
 '603879.SHA',
 '300464.SZA',
 '000626.SZA',
 '002733.SZA',
 '002687.SZA',
 '002515.SZA',
 '603838.SHA',
 '300176.SZA',
 '600555.SHA',
 '300191.SZA',
 '300642.SZA',
 '002850.SZA',
 '300008.SZA',
 '600082.SHA',
 '300726.SZA',
 '002618.SZA',
 '002605.SZA',
 '300595.SZA',
 '000955.SZA',
 '002901.SZA',
 '002256.SZA',
 '000506.SZA',
 '300661.SZA',
 '002785.SZA',
 '002432.SZA',
 '000890.SZA',
 '300469.SZA',
 '002011.SZA',
 '600331.SHA',
 '601881.SHA',
 '600864.SHA',
 '002670.SZA',
 '300454.SZA',
 '002549.SZA',
 '603506.SHA',
 '000576.SZA',
 '300313.SZA',
 '000503.SZA',
 '002161.SZA',
 '300117.SZA',
 '300490.SZA',
 '300221.SZA',
 '600783.SHA',
 '002289.SZA',
 '600751.SHA',
 '600235.SHA',
 '300607.SZA',
 '600876.SHA',
 '603595.SHA',
 '300664.SZA',
 '000810.SZA',
 '300293.SZA',
 '000852.SZA',
 '000673.SZA',
 '600313.SHA',
 '603928.SHA',
 '002497.SZA',
 '002689.SZA',
 '603727.SHA',
 '002143.SZA',
 '603679.SHA',
 '002860.SZA',
 '300071.SZA',
 '601860.SHA',
 '300431.SZA',
 '300504.SZA',
 '002748.SZA',
 '300391.SZA',
 '000007.SZA',
 '000510.SZA',
 '300508.SZA',
 '000532.SZA',
 '300225.SZA',
 '002210.SZA',
 '000613.SZA',
 '300069.SZA',
 '600666.SHA',
 '300596.SZA',
 '603739.SHA',
 '600425.SHA',
 '002542.SZA',
 '002855.SZA',
 '000533.SZA',
 '603078.SHA',
 '300709.SZA',
 '300141.SZA',
 '603029.SHA',
 '002700.SZA',
 '300632.SZA',
 '603396.SHA',
 '600936.SHA',
 '000038.SZA',
 '600552.SHA',
 '601003.SHA',
 '002089.SZA',
 '300637.SZA',
 '002812.SZA',
 '300687.SZA',
 '300465.SZA',
 '000536.SZA',
 '603196.SHA',
 '603345.SHA',
 '002560.SZA',
 '603656.SHA',
 '002118.SZA',
 '002852.SZA',
 '603659.SHA',
 '300353.SZA',
 '300073.SZA',
 '002655.SZA',
 '300250.SZA',
 '000518.SZA',
 '000757.SZA',
 '300733.SZA',
 '002866.SZA',
 '603800.SHA',
 '603129.SHA',
 '603666.SHA',
 '002323.SZA',
 '002833.SZA',
 '000517.SZA',
 '603260.SHA',
 '600212.SHA',
 '002337.SZA',
 '600588.SHA',
 '000877.SZA',
 '600825.SHA',
 '600721.SHA',
 '002876.SZA',
 '002481.SZA',
 '300173.SZA',
 '300510.SZA',
 '600647.SHA',
 '601878.SHA',
 '002494.SZA',
 '300658.SZA',
 '601108.SHA',
 '600117.SHA',
 '002190.SZA',
 '002526.SZA',
 '002723.SZA',
 '600128.SHA',
 '600353.SHA',
 '603598.SHA',
 '002148.SZA',
 '300236.SZA',
 '600157.SHA',
 '300123.SZA',
 '603811.SHA',
 '600579.SHA',
 '300162.SZA',
 '002103.SZA',
 '601008.SHA',
 '002072.SZA',
 '300331.SZA',
 '601319.SHA',
 '300084.SZA',
 '300639.SZA',
 '002524.SZA',
 '300768.SZA',
 '600099.SHA',
 '603179.SHA',
 '603693.SHA',
 '603214.SHA',
 '603618.SHA',
 '603725.SHA',
 '000818.SZA',
 '002120.SZA',
 '000669.SZA',
 '603615.SHA',
 '002821.SZA',
 '603086.SHA',
 '000996.SZA',
 '600610.SHA',
 '002681.SZA',
 '000593.SZA',
 '603488.SHA',
 '002841.SZA',
 '000678.SZA',
 '603131.SHA',
 '600152.SHA',
 '300616.SZA',
 '600909.SHA',
 '603596.SHA',
 '002342.SZA',
 '600311.SHA',
 '601020.SHA',
 '603315.SHA',
 '002722.SZA',
 '300223.SZA',
 '603538.SHA',
 '002945.SZA',
 '002592.SZA',
 '002530.SZA',
 '300256.SZA',
 '603348.SHA',
 '300035.SZA',
 '300628.SZA',
 '600620.SHA',
 '300384.SZA',
 '603650.SHA',
 '002134.SZA',
 '603790.SHA',
 '300226.SZA',
 '000037.SZA',
 '603938.SHA',
 '002172.SZA',
 '603617.SHA',
 '300427.SZA',
 '300238.SZA',
 '002194.SZA',
 '600288.SHA',
 '300052.SZA',
 '002818.SZA',
 '002702.SZA',
 '000553.SZA',
 '300580.SZA',
 '300468.SZA',
 '603917.SHA',
 '603856.SHA',
 '002591.SZA',
 '603729.SHA',
 '603880.SHA',
 '002882.SZA',
 '002913.SZA',
 '600148.SHA',
 '002567.SZA',
 '601608.SHA',
 '300110.SZA',
 '000058.SZA',
 '600275.SHA',
 '603848.SHA',
 '600756.SHA',
 '600766.SHA',
 '300213.SZA',
 '300181.SZA',
 '600571.SHA',
 '300220.SZA',
 '300249.SZA',
 '300290.SZA',
 '300194.SZA',
 '000505.SZA',
 '000557.SZA',
 '603496.SHA',
 '600175.SHA',
 '002297.SZA',
 '600821.SHA',
 '603081.SHA',
 '300119.SZA',
 '600069.SHA',
 '002571.SZA',
 '002912.SZA',
 '600249.SHA',
 '300577.SZA',
 '300409.SZA',
 '603085.SHA',
 '002946.SZA',
 '300281.SZA',
 '300368.SZA',
 '603998.SHA',
 '600996.SHA',
 '002771.SZA',
 '300171.SZA',
 '000018.SZA',
 '600818.SHA',
 '603301.SHA',
 '000572.SZA',
 '300569.SZA',
 '603707.SHA',
 '002766.SZA',
 '300063.SZA',
 '300080.SZA',
 '603017.SHA',
 '600895.SHA',
 '600853.SHA',
 '300447.SZA',
 '600088.SHA',
 '002868.SZA',
 ...]
In [143]:
counted_l_s = Counter(df_d['counts'])
counted_l_s = sorted(counted_l_s.items(),key=lambda x: x[1],reverse=True)
df_cls = pd.DataFrame(counted_l_s)
#df_cls
Out[143]:
0 1
0 1 281
1 2 272
2 4 247
3 3 235
4 5 235
5 6 206
6 7 189
7 8 167
8 9 141
9 10 135
10 11 125
11 12 119
12 14 115
13 13 87
14 16 84
15 17 73
16 15 70
17 18 64
18 19 62
19 20 55
20 21 44
21 23 37
22 24 37
23 22 36
24 25 27
25 27 27
26 29 27
27 28 24
28 26 22
29 30 19
30 34 15
31 31 14
32 33 12
33 32 11
34 37 10
35 35 8
36 41 8
37 36 6
38 39 5
39 40 4
40 43 4
41 44 3
42 38 2
43 45 2
44 50 2
45 52 2
46 42 1
47 46 1
48 49 1
49 54 1
50 55 1
51 58 1
52 61 1
53 74 1
In [168]:
#不同涨停次数对应的股票数可视化
import matplotlib.pyplot as plt  
  
#name_list = ['Monday','Tuesday','Friday','Sunday'] 
name_list = list(df_cls[0])
num_list = list(df_cls[1])  
#plt.figure(figsize(30,74),dpi=200)
# 设置横轴标签
plt.xlabel('涨停次数')
# 设置纵轴标签
plt.ylabel('股票数')
plt.bar(range(len(num_list)), num_list,color='rgb',tick_label=name_list)
plt.show() 
In [61]:
df_zt[lambda df_zt:df_zt['instrument']=='000603.SZA']
Out[61]:
amount high close date price_limit_status instrument
344817 5.331230e+08 82.521004 82.521004 2016-07-01 3 000603.SZA
347725 3.403318e+08 90.768997 90.768997 2016-07-04 3 000603.SZA
350633 1.464988e+09 99.837692 99.837692 2016-07-05 3 000603.SZA
1135362 1.201452e+08 60.330936 60.330936 2017-07-19 3 000603.SZA
1575102 3.807881e+08 57.282623 57.282623 2018-01-23 3 000603.SZA
2309400 1.197618e+08 37.426342 37.426342 2018-11-29 3 000603.SZA
2497186 2.801805e+08 45.343452 45.343452 2019-02-20 3 000603.SZA
In [63]:
df_zt['instrument'].drop_duplicates().count()
Out[63]:
3378
In [56]:
df['date'].drop_duplicates().count()
Out[56]:
699
In [52]:
df =D.history_data(instrument, start_date, end_date, ['close', 'amount','high'])

import talib as ta
from numpy import float as f

def seek_stocks(df):
    df['highest_60'] = df['high'].rolling(60).max()  # 计算60天最高点
    df['ma3_cross_ma5'] = df['close'].rolling(3).mean() - df['close'].rolling(5).mean()  > 0  # 3日均线上穿5日均线
    df['ma5_cross_ma10'] = df['close'].rolling(5).mean() - df['close'].rolling(10).mean()  > 0  # 5日均线上穿10日均线
    df['amount_cond'] = df['amount'] / df['amount'].shift(1) - 1 >= 0.4   # 当日成交量比前一日成交量大40%
    prices = df['close'].map(np.float)  # 转化成float格式
    # macd:diff线 信号线:dea 柱状图:diff-dea
    macd, signal, hist = ta.MACD(np.array(prices), 12, 26, 9)  # 计算macd各个指标
    df['is_highest'] = df['close'] == df['highest_60']  # 该列是布尔型变量,表明是否是60日最高点
    df['hist_is_red'] =  hist > 0   # macd柱状图是否在红色区域
    return df
  
managed_df = df.groupby('instrument').apply(seek_stocks).reset_index()
result= managed_df[
    (managed_df['hist_is_red'])&   # macd在红色区域
    (managed_df['is_highest'])&   # 是60日最高点
    (managed_df['ma3_cross_ma5'])&  # 3日均线上穿5日均线
    (managed_df['ma5_cross_ma10'])&   # 5日均线上穿10日均线
    (managed_df['amount_cond'])]   # 满足成交量条件
    
# 整理出每日符合买入条件的列表               
daily_buy_stock = result.groupby('date').apply(lambda df:list(df.instrument)).reset_index().rename(columns={0:'stocks'})
daily_buy_stock.head()
Out[52]:
date stocks
0 2016-09-26 [000035.SZA, 000409.SZA, 000617.SZA, 000912.SZ...
1 2016-09-27 [000404.SZA, 002614.SZA, 300163.SZA, 600159.SH...
2 2016-09-28 [002085.SZA, 002613.SZA, 603669.SHA]
3 2016-09-29 [600238.SHA, 600419.SHA]
4 2016-09-30 [000635.SZA, 000731.SZA, 002266.SZA, 300308.SZ...
In [231]:
list_zt10puls
Out[231]:
0       603032.SHA
1       002806.SZA
2       600903.SHA
3       300542.SZA
4       002813.SZA
5       002848.SZA
6       603690.SHA
7       603016.SHA
8       300618.SZA
9       300561.SZA
10      002907.SZA
11      002836.SZA
12      601882.SHA
13      300526.SZA
14      300520.SZA
15      300663.SZA
16      300556.SZA
17      300647.SZA
18      002805.SZA
19      300514.SZA
20      300531.SZA
21      603322.SHA
22      300545.SZA
23      300698.SZA
24      300578.SZA
25      300573.SZA
26      002846.SZA
27      300612.SZA
28      300730.SZA
29      603533.SHA
           ...    
1127    603900.SHA
1128    000785.SZA
1129    600278.SHA
1130    603970.SHA
1131    300481.SZA
1132    300174.SZA
1133    002753.SZA
1134    002815.SZA
1135    601789.SHA
1136    600321.SHA
1137    300681.SZA
1138    603799.SHA
1139    000691.SZA
1140    002125.SZA
1141    002558.SZA
1142    600072.SHA
1143    603313.SHA
1144    002622.SZA
1145    300435.SZA
1146    600187.SHA
1147    600107.SHA
1148    300209.SZA
1149    600359.SHA
1150    603733.SHA
1151    300740.SZA
1152    002305.SZA
1153    600506.SHA
1154    603028.SHA
1155    300739.SZA
1156    603076.SHA
Name: instrument, Length: 1157, dtype: object

    {"Description":"实验创建于2017/8/26","Summary":"","Graph":{"EdgesInternal":[{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-15:instruments","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-8:data"},{"DestinationInputPortId":"-215:instruments","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-8:data"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-53:data1","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-15:data"},{"DestinationInputPortId":"-215:features","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"DestinationInputPortId":"-222:features","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"DestinationInputPortId":"-231:features","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"DestinationInputPortId":"-238:features","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"DestinationInputPortId":"-118:features","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"DestinationInputPortId":"-132:input_data","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-53:data"},{"DestinationInputPortId":"-250:options_data","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-60:predictions"},{"DestinationInputPortId":"-231:instruments","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-62:data"},{"DestinationInputPortId":"-250:instruments","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-62:data"},{"DestinationInputPortId":"-222:input_data","SourceOutputPortId":"-215:data"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-53:data2","SourceOutputPortId":"-222:data"},{"DestinationInputPortId":"-238:input_data","SourceOutputPortId":"-231:data"},{"DestinationInputPortId":"-136:input_data","SourceOutputPortId":"-238:data"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-60:model","SourceOutputPortId":"-118:model"},{"DestinationInputPortId":"-118:training_ds","SourceOutputPortId":"-132:data"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-60:data","SourceOutputPortId":"-136:data"}],"ModuleNodes":[{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-8","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2016-07-01","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"2018-12-31","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"market","Value":"CN_STOCK_A","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_list","Value":"603032.SHA\n002806.SZA\n600903.SHA\n300542.SZA\n002813.SZA\n002848.SZA\n603690.SHA\n300618.SZA\n002907.SZA\n002836.SZA\n300526.SZA\n300520.SZA\n300663.SZA\n300556.SZA\n300647.SZA\n002805.SZA\n300514.SZA\n300531.SZA\n603322.SHA\n300545.SZA\n300698.SZA\n300578.SZA\n300573.SZA\n002846.SZA\n300612.SZA\n300730.SZA\n603533.SHA","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"max_count","Value":"0","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"rolling_conf","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-8"}],"OutputPortsInternal":[{"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-8","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":1,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-15","ModuleId":"BigQuantSpace.advanced_auto_labeler.advanced_auto_labeler-v2","ModuleParameters":[{"Name":"label_expr","Value":"# #号开始的表示注释\n# 0. 每行一个,顺序执行,从第二个开始,可以使用label字段\n# 1. 可用数据字段见 https://bigquant.com/docs/develop/datasource/deprecated/history_data.html\n# 添加benchmark_前缀,可使用对应的benchmark数据\n# 2. 可用操作符和函数见 `表达式引擎 <https://bigquant.com/docs/develop/bigexpr/usage.html>`_\n\n# 计算收益:5日收盘价(作为卖出价格)除以明日开盘价(作为买入价格)\nshift(close, -5) / shift(open, -1)\n\n# 极值处理:用1%和99%分位的值做clip\nclip(label, all_quantile(label, 0.01), all_quantile(label, 0.99))\n\n# 将分数映射到分类,这里使用20个分类\nall_wbins(label, 20)\n\n# 过滤掉一字涨停的情况 (设置label为NaN,在后续处理和训练中会忽略NaN的label)\nwhere(shift(high, -1) == shift(low, -1), NaN, label)\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"benchmark","Value":"000300.SHA","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"drop_na_label","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"cast_label_int","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_functions","Value":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-15"}],"OutputPortsInternal":[{"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-15","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":2,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-24","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"# #号开始的表示注释\n# 多个特征,每行一个,可以包含基础特征和衍生特征\nreturn_5\nreturn_10\nreturn_20\navg_amount_0/avg_amount_5\navg_amount_5/avg_amount_20\nrank_avg_amount_0/rank_avg_amount_5\nrank_avg_amount_5/rank_avg_amount_10\nrank_return_0\nrank_return_5\nrank_return_10\nrank_return_0/rank_return_5\nrank_return_5/rank_return_10\npe_ttm_0\n","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24"}],"OutputPortsInternal":[{"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":3,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-53","ModuleId":"BigQuantSpace.join.join-v3","ModuleParameters":[{"Name":"on","Value":"date,instrument","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"how","Value":"inner","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"sort","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"data1","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-53"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"data2","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-53"}],"OutputPortsInternal":[{"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-53","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":7,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-60","ModuleId":"BigQuantSpace.stock_ranker_predict.stock_ranker_predict-v5","ModuleParameters":[{"Name":"m_lazy_run","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"model","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-60"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-60"}],"OutputPortsInternal":[{"Name":"predictions","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-60","OutputType":null},{"Name":"m_lazy_run","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-60","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":8,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-62","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2019-01-01","ValueType":"Literal","LinkedGlobalParameter":"交易日期"},{"Name":"end_date","Value":"2019-05-16","ValueType":"Literal","LinkedGlobalParameter":"交易日期"},{"Name":"market","Value":"CN_STOCK_A","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_list","Value":"603032.SHA\n002806.SZA\n600903.SHA\n300542.SZA\n002813.SZA\n002848.SZA\n603690.SHA\n300618.SZA\n002907.SZA\n002836.SZA\n300526.SZA\n300520.SZA\n300663.SZA\n300556.SZA\n300647.SZA\n002805.SZA\n300514.SZA\n300531.SZA\n603322.SHA\n300545.SZA\n300698.SZA\n300578.SZA\n300573.SZA\n002846.SZA\n300612.SZA\n300730.SZA\n603533.SHA\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"max_count","Value":"0","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"rolling_conf","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-62"}],"OutputPortsInternal":[{"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-62","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":9,"IsPartOfPartialRun":null,"Comment":"预测数据,用于回测和模拟","CommentCollapsed":false},{"Id":"-215","ModuleId":"BigQuantSpace.general_feature_extractor.general_feature_extractor-v7","ModuleParameters":[{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"before_start_days","Value":0,"ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-215"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-215"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-215","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":15,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-222","ModuleId":"BigQuantSpace.derived_feature_extractor.derived_feature_extractor-v3","ModuleParameters":[{"Name":"date_col","Value":"date","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_col","Value":"instrument","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"drop_na","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"remove_extra_columns","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_functions","Value":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-222"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-222"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-222","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":16,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-231","ModuleId":"BigQuantSpace.general_feature_extractor.general_feature_extractor-v7","ModuleParameters":[{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"before_start_days","Value":0,"ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-231"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-231"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-231","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":17,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-238","ModuleId":"BigQuantSpace.derived_feature_extractor.derived_feature_extractor-v3","ModuleParameters":[{"Name":"date_col","Value":"date","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_col","Value":"instrument","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"drop_na","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"remove_extra_columns","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_functions","Value":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-238"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-238"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-238","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":18,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-250","ModuleId":"BigQuantSpace.trade.trade-v4","ModuleParameters":[{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"initialize","Value":"# 回测引擎:初始化函数,只执行一次\ndef bigquant_run(context):\n # 加载预测数据\n context.ranker_prediction = context.options['data'].read_df()\n\n # 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数\n context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))\n # 预测数据,通过options传入进来,使用 read_df 函数,加载到内存 (DataFrame)\n # 设置买入的股票数量,这里买入预测股票列表排名靠前的5只\n stock_count = 5\n # 每只的股票的权重,如下的权重分配会使得靠前的股票分配多一点的资金,[0.339160, 0.213986, 0.169580, ..]\n context.stock_weights = T.norm([1 / math.log(i + 2) for i in range(0, stock_count)])\n # 设置每只股票占用的最大资金比例\n context.max_cash_per_instrument = 0.2\n context.options['hold_days'] = 5\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"handle_data","Value":"# 回测引擎:每日数据处理函数,每天执行一次\ndef bigquant_run(context, data):\n # 按日期过滤得到今日的预测数据\n ranker_prediction = context.ranker_prediction[\n context.ranker_prediction.date == data.current_dt.strftime('%Y-%m-%d')]\n\n # 1. 资金分配\n # 平均持仓时间是hold_days,每日都将买入股票,每日预期使用 1/hold_days 的资金\n # 实际操作中,会存在一定的买入误差,所以在前hold_days天,等量使用资金;之后,尽量使用剩余资金(这里设置最多用等量的1.5倍)\n is_staging = context.trading_day_index < context.options['hold_days'] # 是否在建仓期间(前 hold_days 天)\n cash_avg = context.portfolio.portfolio_value / context.options['hold_days']\n cash_for_buy = min(context.portfolio.cash, (1 if is_staging else 1.5) * cash_avg)\n cash_for_sell = cash_avg - (context.portfolio.cash - cash_for_buy)\n positions = {e.symbol: p.amount * p.last_sale_price\n for e, p in context.portfolio.positions.items()}\n\n # 2. 生成卖出订单:hold_days天之后才开始卖出;对持仓的股票,按机器学习算法预测的排序末位淘汰\n if not is_staging and cash_for_sell > 0:\n equities = {e.symbol: e for e, p in context.portfolio.positions.items()}\n instruments = list(reversed(list(ranker_prediction.instrument[ranker_prediction.instrument.apply(\n lambda x: x in equities)])))\n\n for instrument in instruments:\n context.order_target(context.symbol(instrument), 0)\n cash_for_sell -= positions[instrument]\n if cash_for_sell <= 0:\n break\n\n # 3. 生成买入订单:按机器学习算法预测的排序,买入前面的stock_count只股票\n buy_cash_weights = context.stock_weights\n buy_instruments = list(ranker_prediction.instrument[:len(buy_cash_weights)])\n max_cash_per_instrument = context.portfolio.portfolio_value * context.max_cash_per_instrument\n for i, instrument in enumerate(buy_instruments):\n cash = cash_for_buy * buy_cash_weights[i]\n if cash > max_cash_per_instrument - positions.get(instrument, 0):\n # 确保股票持仓量不会超过每次股票最大的占用资金量\n cash = max_cash_per_instrument - positions.get(instrument, 0)\n if cash > 0:\n context.order_value(context.symbol(instrument), cash)\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"prepare","Value":"# 回测引擎:准备数据,只执行一次\ndef bigquant_run(context):\n pass\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"before_trading_start","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"volume_limit","Value":0.025,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"order_price_field_buy","Value":"open","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"order_price_field_sell","Value":"close","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"capital_base","Value":1000000,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"auto_cancel_non_tradable_orders","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"data_frequency","Value":"daily","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"price_type","Value":"后复权","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"product_type","Value":"股票","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"plot_charts","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"backtest_only","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"benchmark","Value":"000300.SHA","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-250"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"options_data","NodeId":"-250"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"history_ds","NodeId":"-250"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"benchmark_ds","NodeId":"-250"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"trading_calendar","NodeId":"-250"}],"OutputPortsInternal":[{"Name":"raw_perf","NodeId":"-250","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":19,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-118","ModuleId":"BigQuantSpace.stock_ranker_train.stock_ranker_train-v6","ModuleParameters":[{"Name":"learning_algorithm","Value":"排序","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"number_of_leaves","Value":30,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"minimum_docs_per_leaf","Value":1000,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"number_of_trees","Value":20,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"learning_rate","Value":0.1,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"max_bins","Value":1023,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"feature_fraction","Value":1,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"data_row_fraction","Value":1,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"ndcg_discount_base","Value":1,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"m_lazy_run","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"training_ds","NodeId":"-118"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-118"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"test_ds","NodeId":"-118"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"base_model","NodeId":"-118"}],"OutputPortsInternal":[{"Name":"model","NodeId":"-118","OutputType":null},{"Name":"feature_gains","NodeId":"-118","OutputType":null},{"Name":"m_lazy_run","NodeId":"-118","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":4,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-132","ModuleId":"BigQuantSpace.dropnan.dropnan-v2","ModuleParameters":[],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-132"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-132"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-132","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":5,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-136","ModuleId":"BigQuantSpace.dropnan.dropnan-v2","ModuleParameters":[],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-136"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-136"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-136","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":10,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true}],"SerializedClientData":"<?xml version='1.0' encoding='utf-16'?><DataV1 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Meta /><NodePositions><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-8' Position='179,41,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-15' Position='70,183,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-24' Position='765,21,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-53' Position='249,375,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-60' Position='906,647,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-62' Position='1107,98,200,200'/><NodePosition Node='-215' Position='386,182,200,200'/><NodePosition Node='-222' Position='385,274,200,200'/><NodePosition Node='-231' Position='1078,236,200,200'/><NodePosition Node='-238' Position='1081,327,200,200'/><NodePosition Node='-250' Position='1037,751,200,200'/><NodePosition Node='-118' Position='638,561,200,200'/><NodePosition Node='-132' Position='376,467,200,200'/><NodePosition Node='-136' Position='1078,418,200,200'/></NodePositions><NodeGroups /></DataV1>"},"IsDraft":true,"ParentExperimentId":null,"WebService":{"IsWebServiceExperiment":false,"Inputs":[],"Outputs":[],"Parameters":[{"Name":"交易日期","Value":"","ParameterDefinition":{"Name":"交易日期","FriendlyName":"交易日期","DefaultValue":"","ParameterType":"String","HasDefaultValue":true,"IsOptional":true,"ParameterRules":[],"HasRules":false,"MarkupType":0,"CredentialDescriptor":null}}],"WebServiceGroupId":null,"SerializedClientData":"<?xml version='1.0' encoding='utf-16'?><DataV1 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Meta /><NodePositions></NodePositions><NodeGroups /></DataV1>"},"DisableNodesUpdate":false,"Category":"user","Tags":[],"IsPartialRun":true}
    In [241]:
    # 本代码由可视化策略环境自动生成 2020年8月19日 22:21
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    # 回测引擎:初始化函数,只执行一次
    def m19_initialize_bigquant_run(context):
        # 加载预测数据
        context.ranker_prediction = context.options['data'].read_df()
    
        # 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数
        context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))
        # 预测数据,通过options传入进来,使用 read_df 函数,加载到内存 (DataFrame)
        # 设置买入的股票数量,这里买入预测股票列表排名靠前的5只
        stock_count = 5
        # 每只的股票的权重,如下的权重分配会使得靠前的股票分配多一点的资金,[0.339160, 0.213986, 0.169580, ..]
        context.stock_weights = T.norm([1 / math.log(i + 2) for i in range(0, stock_count)])
        # 设置每只股票占用的最大资金比例
        context.max_cash_per_instrument = 0.2
        context.options['hold_days'] = 5
    
    # 回测引擎:每日数据处理函数,每天执行一次
    def m19_handle_data_bigquant_run(context, data):
        # 按日期过滤得到今日的预测数据
        ranker_prediction = context.ranker_prediction[
            context.ranker_prediction.date == data.current_dt.strftime('%Y-%m-%d')]
    
        # 1. 资金分配
        # 平均持仓时间是hold_days,每日都将买入股票,每日预期使用 1/hold_days 的资金
        # 实际操作中,会存在一定的买入误差,所以在前hold_days天,等量使用资金;之后,尽量使用剩余资金(这里设置最多用等量的1.5倍)
        is_staging = context.trading_day_index < context.options['hold_days'] # 是否在建仓期间(前 hold_days 天)
        cash_avg = context.portfolio.portfolio_value / context.options['hold_days']
        cash_for_buy = min(context.portfolio.cash, (1 if is_staging else 1.5) * cash_avg)
        cash_for_sell = cash_avg - (context.portfolio.cash - cash_for_buy)
        positions = {e.symbol: p.amount * p.last_sale_price
                     for e, p in context.portfolio.positions.items()}
    
        # 2. 生成卖出订单:hold_days天之后才开始卖出;对持仓的股票,按机器学习算法预测的排序末位淘汰
        if not is_staging and cash_for_sell > 0:
            equities = {e.symbol: e for e, p in context.portfolio.positions.items()}
            instruments = list(reversed(list(ranker_prediction.instrument[ranker_prediction.instrument.apply(
                    lambda x: x in equities)])))
    
            for instrument in instruments:
                context.order_target(context.symbol(instrument), 0)
                cash_for_sell -= positions[instrument]
                if cash_for_sell <= 0:
                    break
    
        # 3. 生成买入订单:按机器学习算法预测的排序,买入前面的stock_count只股票
        buy_cash_weights = context.stock_weights
        buy_instruments = list(ranker_prediction.instrument[:len(buy_cash_weights)])
        max_cash_per_instrument = context.portfolio.portfolio_value * context.max_cash_per_instrument
        for i, instrument in enumerate(buy_instruments):
            cash = cash_for_buy * buy_cash_weights[i]
            if cash > max_cash_per_instrument - positions.get(instrument, 0):
                # 确保股票持仓量不会超过每次股票最大的占用资金量
                cash = max_cash_per_instrument - positions.get(instrument, 0)
            if cash > 0:
                context.order_value(context.symbol(instrument), cash)
    
    # 回测引擎:准备数据,只执行一次
    def m19_prepare_bigquant_run(context):
        pass
    
    
    m1 = M.instruments.v2(
        start_date='2016-07-01',
        end_date='2018-12-31',
        market='CN_STOCK_A',
        instrument_list="""603032.SHA
    002806.SZA
    600903.SHA
    300542.SZA
    002813.SZA
    002848.SZA
    603690.SHA
    300618.SZA
    002907.SZA
    002836.SZA
    300526.SZA
    300520.SZA
    300663.SZA
    300556.SZA
    300647.SZA
    002805.SZA
    300514.SZA
    300531.SZA
    603322.SHA
    300545.SZA
    300698.SZA
    300578.SZA
    300573.SZA
    002846.SZA
    300612.SZA
    300730.SZA
    603533.SHA""",
        max_count=0
    )
    
    m2 = M.advanced_auto_labeler.v2(
        instruments=m1.data,
        label_expr="""# #号开始的表示注释
    # 0. 每行一个,顺序执行,从第二个开始,可以使用label字段
    # 1. 可用数据字段见 https://bigquant.com/docs/develop/datasource/deprecated/history_data.html
    #   添加benchmark_前缀,可使用对应的benchmark数据
    # 2. 可用操作符和函数见 `表达式引擎 <https://bigquant.com/docs/develop/bigexpr/usage.html>`_
    
    # 计算收益:5日收盘价(作为卖出价格)除以明日开盘价(作为买入价格)
    shift(close, -5) / shift(open, -1)
    
    # 极值处理:用1%和99%分位的值做clip
    clip(label, all_quantile(label, 0.01), all_quantile(label, 0.99))
    
    # 将分数映射到分类,这里使用20个分类
    all_wbins(label, 20)
    
    # 过滤掉一字涨停的情况 (设置label为NaN,在后续处理和训练中会忽略NaN的label)
    where(shift(high, -1) == shift(low, -1), NaN, label)
    """,
        start_date='',
        end_date='',
        benchmark='000300.SHA',
        drop_na_label=True,
        cast_label_int=True
    )
    
    m3 = M.input_features.v1(
        features="""# #号开始的表示注释
    # 多个特征,每行一个,可以包含基础特征和衍生特征
    return_5
    return_10
    return_20
    avg_amount_0/avg_amount_5
    avg_amount_5/avg_amount_20
    rank_avg_amount_0/rank_avg_amount_5
    rank_avg_amount_5/rank_avg_amount_10
    rank_return_0
    rank_return_5
    rank_return_10
    rank_return_0/rank_return_5
    rank_return_5/rank_return_10
    pe_ttm_0
    """
    )
    
    m15 = M.general_feature_extractor.v7(
        instruments=m1.data,
        features=m3.data,
        start_date='',
        end_date='',
        before_start_days=0
    )
    
    m16 = M.derived_feature_extractor.v3(
        input_data=m15.data,
        features=m3.data,
        date_col='date',
        instrument_col='instrument',
        drop_na=False,
        remove_extra_columns=False
    )
    
    m7 = M.join.v3(
        data1=m2.data,
        data2=m16.data,
        on='date,instrument',
        how='inner',
        sort=False
    )
    
    m5 = M.dropnan.v2(
        input_data=m7.data
    )
    
    m4 = M.stock_ranker_train.v6(
        training_ds=m5.data,
        features=m3.data,
        learning_algorithm='排序',
        number_of_leaves=30,
        minimum_docs_per_leaf=1000,
        number_of_trees=20,
        learning_rate=0.1,
        max_bins=1023,
        feature_fraction=1,
        data_row_fraction=1,
        ndcg_discount_base=1,
        m_lazy_run=False
    )
    
    m9 = M.instruments.v2(
        start_date=T.live_run_param('trading_date', '2019-01-01'),
        end_date=T.live_run_param('trading_date', '2019-05-16'),
        market='CN_STOCK_A',
        instrument_list="""603032.SHA
    002806.SZA
    600903.SHA
    300542.SZA
    002813.SZA
    002848.SZA
    603690.SHA
    300618.SZA
    002907.SZA
    002836.SZA
    300526.SZA
    300520.SZA
    300663.SZA
    300556.SZA
    300647.SZA
    002805.SZA
    300514.SZA
    300531.SZA
    603322.SHA
    300545.SZA
    300698.SZA
    300578.SZA
    300573.SZA
    002846.SZA
    300612.SZA
    300730.SZA
    603533.SHA
    """,
        max_count=0
    )
    
    m17 = M.general_feature_extractor.v7(
        instruments=m9.data,
        features=m3.data,
        start_date='',
        end_date='',
        before_start_days=0
    )
    
    m18 = M.derived_feature_extractor.v3(
        input_data=m17.data,
        features=m3.data,
        date_col='date',
        instrument_col='instrument',
        drop_na=False,
        remove_extra_columns=False
    )
    
    m10 = M.dropnan.v2(
        input_data=m18.data
    )
    
    m8 = M.stock_ranker_predict.v5(
        model=m4.model,
        data=m10.data,
        m_lazy_run=False
    )
    
    m19 = M.trade.v4(
        instruments=m9.data,
        options_data=m8.predictions,
        start_date='',
        end_date='',
        initialize=m19_initialize_bigquant_run,
        handle_data=m19_handle_data_bigquant_run,
        prepare=m19_prepare_bigquant_run,
        volume_limit=0.025,
        order_price_field_buy='open',
        order_price_field_sell='close',
        capital_base=1000000,
        auto_cancel_non_tradable_orders=True,
        data_frequency='daily',
        price_type='后复权',
        product_type='股票',
        plot_charts=True,
        backtest_only=False,
        benchmark='000300.SHA'
    )
    
    设置测试数据集,查看训练迭代过程的NDCG
    bigcharts-data-start/{"__id":"bigchart-b3ccd091305e43719f6b5e2cf7f1bd56","__type":"tabs"}/bigcharts-data-end
    • 收益率49.52%
    • 年化收益率216.42%
    • 基准收益率24.36%
    • 阿尔法0.72
    • 贝塔0.73
    • 夏普比率3.63
    • 胜率0.57
    • 盈亏比1.71
    • 收益波动率32.48%
    • 信息比率0.13
    • 最大回撤13.88%
    bigcharts-data-start/{"__id":"bigchart-ca292dda2b1f43109b268cc1817e5bc9","__type":"tabs"}/bigcharts-data-end
    In [ ]: