同花顺Supermind量化交易 运用SVM算法“打板”-- SVM算法应用

73 阅读19分钟

课程简介:介绍股票涨跌停板机制,对股票涨停进行分类,并研究股票涨停后的收益情况,最终采用SVM算法进行“打板”研究。

dt = dataclosedf
dt = dt.sort_values(by='date')

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np


plt.style.use('seaborn')
fig = plt.figure()
axes = fig.add_axes([0.1, 0.1, 1, 0.618]) #插入面板

x1_list=list(dt['1日收盘价'])
y=np.array(x1_list)
x=np.array(range(0,len(x1_list)))
axes.scatter(x,y,c='tomato')
axes.set_xlabel('time',fontsize=15)
axes.set_ylabel('down_up',fontsize=15)
axes.set_title(' one day distribution',fontsize=18)
# #设置X轴
axes.set_xticks([0,100,200,300,400,500])
axes.set_xticklabels([list(dt.index)[0],list(dt.index)[100],list(dt.index)[200],list(dt.index)[300],list(dt.index)[400],list(dt.index)[500]],fontsize=15)

plt.style.use('seaborn')
fig = plt.figure()
axes = fig.add_axes([0.1, 0.1, 1, 0.618]) #插入面板

x1_list=list(dt['2日收盘价'])
y=np.array(x1_list)
x=np.array(range(0,len(x1_list)))
axes.scatter(x,y,c='tomato')
axes.set_xlabel('time',fontsize=15)
axes.set_ylabel('down_up',fontsize=15)
axes.set_title(' two day distribution',fontsize=18)
# #设置X轴
axes.set_xticks([0,100,200,300,400,500])
axes.set_xticklabels([list(dt.index)[0],list(dt.index)[100],list(dt.index)[200],list(dt.index)[300],list(dt.index)[400],list(dt.index)[500]],fontsize=15)

plt.style.use('seaborn')
fig = plt.figure()
axes = fig.add_axes([0.1, 0.1, 1, 0.618]) #插入面板

x1_list=list(dt['3日收盘价'])
y=np.array(x1_list)
x=np.array(range(0,len(x1_list)))
axes.scatter(x,y,c='tomato')
axes.set_xlabel('time',fontsize=15)
axes.set_ylabel('down_up',fontsize=15)
axes.set_title(' three day distribution',fontsize=18)
# #设置X轴
axes.set_xticks([0,100,200,300,400,500])
axes.set_xticklabels([list(dt.index)[0],list(dt.index)[100],list(dt.index)[200],list(dt.index)[300],list(dt.index)[400],list(dt.index)[500]],fontsize=15)

Out[13]:

[<matplotlib.text.Text at 0x7f1b30cd7630>,
 <matplotlib.text.Text at 0x7f1b313e20b8>,
 <matplotlib.text.Text at 0x7f1b30b9b630>,
 <matplotlib.text.Text at 0x7f1b313b9cc0>,
 <matplotlib.text.Text at 0x7f1b31aee0f0>,
 <matplotlib.text.Text at 0x7f1b30c90b00>]

In [14]:

labeldt = dataclosedf
labeldt['1日收盘价'] = labeldt['1日收盘价'].apply(lambda x:1 if x>0 else -1)
labeldt['2日收盘价'] = labeldt['2日收盘价'].apply(lambda x:1 if x>0 else -1)
labeldt['3日收盘价'] = labeldt['3日收盘价'].apply(lambda x:1 if x>0 else -1)
labeldt = labeldt.sort_values(by='date')
labeldt

Out[14]:

stockdate1日收盘价2日收盘价3日收盘价
2014-01-03600010.SH2014-01-03-1-1-1
2014-01-16000767.SZ2014-01-16-1-1-1
2014-02-12000971.SZ2014-02-12-1-1-1
2014-02-21000554.SZ2014-02-21-1-1-1
2014-02-28600680.SH2014-02-28111
2014-03-10300116.SZ2014-03-10-1-1-1
2014-03-13000923.SZ2014-03-13-111
2014-03-27000687.SZ2014-03-27-1-1-1
2014-04-03000918.SZ2014-04-03-1-1-1
2014-04-10300278.SZ2014-04-10111
2014-04-30601001.SH2014-04-301-1-1
2014-05-21002660.SZ2014-05-21111
2014-06-04600601.SH2014-06-04-1-1-1
2014-06-05600074.SH2014-06-05-1-1-1
2014-06-06002535.SZ2014-06-06111
2014-06-16300198.SZ2014-06-16-1-1-1
2014-06-30002093.SZ2014-06-30-1-1-1
2014-07-02300288.SZ2014-07-02111
2014-07-04002070.SZ2014-07-04-1-1-1
2014-07-07600568.SH2014-07-07-111
2014-07-08600435.SH2014-07-08111
2014-07-11002629.SZ2014-07-111-1-1
2014-07-16000868.SZ2014-07-16-1-1-1
2014-07-25600319.SH2014-07-25-111
2014-07-28600157.SH2014-07-28111
2014-08-07600556.SH2014-08-07-1-1-1
2014-08-11600222.SH2014-08-11-1-1-1
2014-08-14300208.SZ2014-08-14-111
2014-08-18002125.SZ2014-08-181-11
2014-08-21000922.SZ2014-08-21111
..................
2018-12-19002845.SZ2018-12-191-11
2018-12-25000531.SZ2018-12-25-1-1-1
2018-12-26300687.SZ2018-12-26-1-1-1
2018-12-27300407.SZ2018-12-27-1-1-1
2018-12-28601619.SH2018-12-28-1-1-1
2019-01-04300125.SZ2019-01-04111
2019-01-07300588.SZ2019-01-07-1-1-1
2019-01-08600677.SH2019-01-08-1-1-1
2019-01-09600452.SH2019-01-09111
2019-01-10300076.SZ2019-01-10-1-1-1
2019-01-11002941.SZ2019-01-11-1-1-1
2019-01-14603220.SH2019-01-14-1-11
2019-01-16600446.SH2019-01-16-1-1-1
2019-01-18600470.SH2019-01-18111
2019-01-21300693.SZ2019-01-21-111
2019-01-22300503.SZ2019-01-22-1-1-1
2019-01-24002170.SZ2019-01-24-1-1-1
2019-01-25600721.SH2019-01-251-1-1
2019-01-28000637.SZ2019-01-28-1-1-1
2019-01-31300250.SZ2019-01-31111
2019-02-01002011.SZ2019-02-01-1-1-1
2019-02-11600532.SH2019-02-11-1-1-1
2019-02-12600318.SH2019-02-121-1-1
2019-02-13300097.SZ2019-02-13111
2019-02-14300256.SZ2019-02-14-111
2019-02-18002099.SZ2019-02-1811-1
2019-02-19300468.SZ2019-02-19-1-1-1
2019-02-20600393.SH2019-02-20-1-11
2019-02-21002565.SZ2019-02-2111-1
2019-02-22300063.SZ2019-02-221-1-1

503 rows × 5 columns

In [15]:

tradeday = list(get_trade_days(startdate, '20200202', count=None).strftime('%Y-%m-%d'))

timelist = []
trlist= []
lplist =[]
q_trlist = []
for d in tradeday:
    time = 0
    tr = 0
    lp = 0
    q_tr = 0
    if d in list(labeldt['date']):
        stock = labeldt['stock'][d]
        day = tradeday[tradeday.index(d)+1]
        stockdata = get_price(stock,None,day,'1m',['close','high','low','open','turnover_rate'],True,'pre',bar_count=241,is_panel=0)
        
        pc = stockdata.iloc[0].close
        highlimit = round(pc*1.1,2)
        
        stockdata = stockdata.iloc[-240:]
        
        for m in list(range(0,240)):
            
            rc = stockdata.iloc[m].close
            h = stockdata.iloc[(m+1)].high
            
            if rc == highlimit:
                q_tr +=stockdata.iloc[m].turnover_rate
                
            if rc<highlimit:
                time += 1
                tr +=stockdata.iloc[m].turnover_rate
                lp = min(stockdata.iloc[m].low/highlimit-1,lp)
            
            if rc<highlimit and h == highlimit:
                break
        if time>0:
            timelist.append(time)
            trlist.append(round(tr,2))
            lplist.append(round(lp*100,2))
            q_trlist.append(round(q_tr,2))
timelist

Out[15]:

[111,
 2,
 209,
 1,
 1,
 1,
 1,
 25,
 1,
 2,
 1,
 2,
 1,
 1,
 3,
 1,
 5,
 228,
 19,
 11,
 4,
 60,
 1,
 1,
 1,
 6,
 95,
 3,
 1,
 211,
 1,
 6,
 2,
 1,
 6,
 5,
 1,
 91,
 135,
 2,
 1,
 1,
 236,
 149,
 52,
 6,
 3,
 2,
 2,
 1,
 6,
 1,
 1,
 1,
 4,
 38,
 1,
 6,
 1,
 14,
 180,
 1,
 1,
 1,
 1,
 1,
 2,
 1,
 1,
 172,
 1,
 142,
 43,
 26,
 1,
 1,
 4,
 1,
 5,
 30,
 3,
 5,
 5,
 5,
 4,
 2,
 5,
 5,
 20,
 1,
 102,
 6,
 166,
 1,
 1,
 24,
 1,
 1,
 136,
 26,
 3,
 2,
 4,
 1,
 3,
 1,
 2,
 25,
 5,
 2,
 83,
 1,
 5,
 1,
 1,
 1,
 1,
 1,
 8,
 4,
 19,
 2,
 1,
 7,
 3,
 25,
 1,
 2,
 76,
 8,
 2,
 28,
 1,
 1,
 2,
 1,
 1,
 1,
 1,
 3,
 1,
 1,
 1,
 1,
 1,
 1,
 2,
 2,
 1,
 24,
 2,
 1,
 1,
 1,
 3,
 4,
 60,
 2,
 1,
 1,
 1,
 1,
 2,
 3,
 1,
 228,
 5,
 27,
 179,
 1,
 1,
 1,
 1,
 1,
 1,
 1,
 1,
 82,
 1,
 1,
 1,
 2,
 5,
 2,
 1,
 148,
 4,
 1,
 1,
 1,
 1,
 2,
 29,
 2,
 9,
 3,
 1,
 2,
 3,
 11,
 1,
 1,
 1,
 1,
 1,
 2,
 1,
 2,
 1,
 1,
 2,
 2,
 171,
 1,
 157,
 1,
 2,
 4,
 3,
 1,
 2,
 2,
 120,
 1,
 30,
 3,
 1,
 1,
 1,
 1,
 1,
 1,
 106,
 1,
 1,
 26,
 1,
 127,
 5,
 1,
 2,
 4,
 1,
 1,
 2,
 1,
 1,
 2,
 1,
 201,
 7,
 88,
 1,
 1,
 1,
 53,
 3,
 1,
 2,
 1,
 1,
 1,
 2,
 1,
 5,
 1,
 9,
 8,
 28,
 1,
 156,
 1,
 43,
 5,
 24,
 16,
 2,
 1,
 5,
 1,
 10,
 1,
 1,
 33,
 203,
 2,
 1,
 7,
 31,
 2,
 229,
 1,
 1,
 1,
 1,
 118,
 45,
 41,
 2,
 2,
 2,
 1,
 77,
 2,
 120,
 2,
 45,
 58,
 3,
 3,
 22,
 1,
 1,
 1,
 4,
 1,
 6,
 1,
 1,
 140,
 6,
 40,
 1,
 43,
 10,
 1,
 1,
 10,
 1,
 1,
 1,
 6,
 181,
 5,
 19,
 1,
 1,
 1,
 34,
 3,
 2,
 1,
 6,
 1,
 1,
 1,
 10,
 1,
 2,
 1,
 1,
 1,
 10,
 1,
 1,
 114,
 1,
 1,
 1,
 1,
 12,
 189,
 1,
 1,
 7,
 1,
 11,
 1,
 35,
 1,
 12,
 1,
 2,
 1,
 17,
 1,
 3,
 42,
 1,
 1,
 3,
 31,
 1,
 1,
 1,
 53,
 3,
 1,
 1,
 1,
 13,
 1,
 1,
 75,
 15,
 1,
 21,
 3,
 1,
 3,
 13,
 1,
 16,
 2,
 10,
 1,
 1,
 1,
 1,
 1,
 2,
 9,
 1,
 2,
 3,
 1,
 1,
 3,
 125,
 1,
 6,
 40,
 1,
 47,
 1,
 2,
 1,
 5,
 1,
 1,
 2,
 26,
 1,
 1,
 5,
 1,
 1,
 1,
 3,
 93,
 2,
 5,
 2,
 1,
 108,
 23,
 1,
 4,
 1,
 3,
 2,
 1,
 1,
 28,
 1,
 35,
 104,
 1,
 2,
 1,
 5,
 27,
 176,
 1,
 11,
 1,
 1,
 58,
 1,
 1,
 42,
 84,
 5,
 2,
 1,
 7,
 26,
 11,
 6,
 22,
 1,
 27,
 9,
 3,
 2,
 1,
 1,
 1,
 1,
 1,
 175,
 1,
 15,
 1,
 1,
 1,
 7,
 108,
 1,
 88,
 149,
 10,
 14]

In [16]:

labeldt['time'] = timelist
labeldt['Change'] = trlist
labeldt['drop range'] = lplist
labeldt['limit Change'] = q_trlist
labeldt

Out[16]:

stockdate1日收盘价2日收盘价3日收盘价timeChangedrop rangelimit Change
2014-01-03600010.SH2014-01-03-1-1-11114.66-3.650.00
2014-01-16000767.SZ2014-01-16-1-1-120.23-2.270.00
2014-02-12000971.SZ2014-02-12-1-1-120910.74-7.140.00
2014-02-21000554.SZ2014-02-21-1-1-113.99-1.199.58
2014-02-28600680.SH2014-02-2811110.79-0.601.95
2014-03-10300116.SZ2014-03-10-1-1-111.16-1.1415.01
2014-03-13000923.SZ2014-03-13-11111.42-1.992.48
2014-03-27000687.SZ2014-03-27-1-1-1258.88-7.781.11
2014-04-03000918.SZ2014-04-03-1-1-110.68-4.820.43
2014-04-10300278.SZ2014-04-1011124.56-2.593.06
2014-04-30601001.SH2014-04-301-1-110.17-0.810.64
2014-05-21002660.SZ2014-05-2111122.72-1.870.88
2014-06-04600601.SH2014-06-04-1-1-111.00-2.280.00
2014-06-05600074.SH2014-06-05-1-1-111.00-1.172.63
2014-06-06002535.SZ2014-06-0611132.15-2.861.05
2014-06-16300198.SZ2014-06-16-1-1-111.01-3.841.28
2014-06-30002093.SZ2014-06-30-1-1-153.30-3.930.00
2014-07-02300288.SZ2014-07-0211122820.97-4.614.82
2014-07-04002070.SZ2014-07-04-1-1-1197.00-8.270.60
2014-07-07600568.SH2014-07-07-111113.71-5.230.00
2014-07-08600435.SH2014-07-0811141.20-2.314.53
2014-07-11002629.SZ2014-07-111-1-16014.15-5.763.86
2014-07-16000868.SZ2014-07-16-1-1-110.69-2.195.31
2014-07-25600319.SH2014-07-25-11111.57-1.833.54
2014-07-28600157.SH2014-07-2811111.48-2.114.34
2014-08-07600556.SH2014-08-07-1-1-164.43-5.580.00
2014-08-11600222.SH2014-08-11-1-1-1954.91-5.480.00
2014-08-14300208.SZ2014-08-14-11132.98-2.433.87
2014-08-18002125.SZ2014-08-181-1110.73-1.555.67
2014-08-21000922.SZ2014-08-2111121120.19-12.070.00
..............................
2018-12-19002845.SZ2018-12-191-1121.35-0.825.63
2018-12-25000531.SZ2018-12-25-1-1-110.26-1.280.00
2018-12-26300687.SZ2018-12-26-1-1-176.95-4.352.43
2018-12-27300407.SZ2018-12-27-1-1-1262.90-4.583.67
2018-12-28601619.SH2018-12-28-1-1-1111.55-2.543.17
2019-01-04300125.SZ2019-01-0411161.15-2.670.20
2019-01-07300588.SZ2019-01-07-1-1-1227.75-4.163.56
2019-01-08600677.SH2019-01-08-1-1-113.41-1.640.00
2019-01-09600452.SH2019-01-09111270.57-3.310.00
2019-01-10300076.SZ2019-01-10-1-1-195.39-6.870.00
2019-01-11002941.SZ2019-01-11-1-1-135.64-3.130.00
2019-01-14603220.SH2019-01-14-1-1129.77-3.341.07
2019-01-16600446.SH2019-01-16-1-1-110.66-0.581.80
2019-01-18600470.SH2019-01-1811110.68-0.452.56
2019-01-21300693.SZ2019-01-21-11113.01-1.633.94
2019-01-22300503.SZ2019-01-22-1-1-111.47-1.621.12
2019-01-24002170.SZ2019-01-24-1-1-112.70-1.820.00
2019-01-25600721.SH2019-01-251-1-11753.81-4.690.48
2019-01-28000637.SZ2019-01-28-1-1-111.05-2.736.09
2019-01-31300250.SZ2019-01-31111151.20-1.484.80
2019-02-01002011.SZ2019-02-01-1-1-111.27-3.881.30
2019-02-11600532.SH2019-02-11-1-1-110.60-2.151.84
2019-02-12600318.SH2019-02-121-1-110.47-0.700.47
2019-02-13300097.SZ2019-02-1311174.94-3.571.52
2019-02-14300256.SZ2019-02-14-11110811.96-8.090.50
2019-02-18002099.SZ2019-02-1811-110.28-0.180.43
2019-02-19300468.SZ2019-02-19-1-1-1883.14-6.161.44
2019-02-20600393.SH2019-02-20-1-1114916.27-8.010.00
2019-02-21002565.SZ2019-02-2111-1104.02-7.370.62
2019-02-22300063.SZ2019-02-221-1-1142.24-2.494.45

503 rows × 9 columns

In [17]:

updt = labeldt[labeldt['1日收盘价']==1]
downdt =  labeldt[labeldt['1日收盘价']==-1]
label = ['time','Change','drop range','limit Change']
from mpl_toolkits.mplot3d import Axes3D
xsup1 = updt[label[0]]
xsup2 = updt[label[1]]
xsup3 = updt[label[2]]
xsdown1 = downdt[label[0]]
xsdown2 = downdt[label[1]]
xsdown3 = downdt[label[2]]
fig = plt.figure()
ax = Axes3D(fig)
ax.scatter(xsup1,xsup2,xsup3,c='tomato')
ax.scatter(xsdown1,xsdown2,xsdown3,c='g')
ax.set_xlabel(label[0],fontsize=12)
ax.set_ylabel(label[1],fontsize=12)
ax.set_zlabel(label[2],fontsize=12)
ax.set_title('one day Data space 3D ',fontsize=20)
plt.show()

for l in label:
    fig = plt.figure()
    axes = fig.add_axes([0.1, 0.1, 1, 0.618]) 
    x1_list=list(updt[l])
    y=np.array(x1_list)
    x=np.array(range(0,len(x1_list)))
    axes.scatter(x,y,c='tomato')

    x1_list=list(downdt[l])
    y=np.array(x1_list)
    x=np.array(range(0,len(x1_list)))
    axes.scatter(x,y,c='g')
    axes.set_ylabel('value',fontsize=15)
    axes.set_title(l,fontsize=20)

In [55]:

labeldt
label = ['time','Change','drop range','limit Change']

train = labeldt[:300]
test = labeldt[-200:]
X=train[label]
Y=train['1日收盘价']
X_test=test[label]
Y_test=test['1日收盘价']

from sklearn import svm
model = svm.SVC(C=1, kernel='rbf', gamma=0.5, decision_function_shape='ovo')

model.fit(X, Y)
print('训练时,预测成功率 {}'.format(round(np.mean(model.predict(X)==Y),2)))
print('测试时,预测成功率 {}'.format(round(np.mean(model.predict(X_test)==Y_test),2)))
训练时,预测成功率 0.88
测试时,预测成功率 0.55

In [102]:

dataclosedf = datadf#[['stock','date','1日收盘价','2日收盘价','3日收盘价']]
dataclosedf = dataclosedf.sort_values(by='1日收盘价',ascending=False)


tradeday = list(get_trade_days(startdate, '20200202', count=None).strftime('%Y-%m-%d'))

timelist = []
trlist= []
lplist =[]
q_trlist = []
for d in tradeday:
    time = 0
    tr = 0
    lp = 0
    q_tr = 0
    if d in list(labeldt['date']):
        stock = labeldt['stock'][d]
        day = tradeday[tradeday.index(d)+1]
        stockdata = get_price(stock,None,day,'1m',['close','high','low','open','turnover_rate'],True,'pre',bar_count=241,is_panel=0)
        
        pc = stockdata.iloc[0].close
        highlimit = round(pc*1.1,2)
        
        stockdata = stockdata.iloc[-240:]
        
        for m in list(range(0,240)):
            
            rc = stockdata.iloc[m].close
            h = stockdata.iloc[(m+1)].high
            
            if rc == highlimit:
                q_tr +=stockdata.iloc[m].turnover_rate
                
            if rc<highlimit:
                time += 1
                tr +=stockdata.iloc[m].turnover_rate
                lp = min(stockdata.iloc[m].low/highlimit-1,lp)
            
            if rc<highlimit and h == highlimit:
                break
        if time>0:
            timelist.append(time)
            trlist.append(round(tr,2))
            lplist.append(round(lp*100,2))
            q_trlist.append(round(q_tr,2))
dataclosedf['time'] = timelist
dataclosedf['Change'] = trlist
dataclosedf['drop range'] = lplist
dataclosedf['limit Change'] = q_trlist
dataclosedf

Out[102]:

stockdatebuyprice当日收盘价1日收盘价1日开盘价1日最高价1日最低价2日收盘价2日开盘价2日最高价2日最低价3日收盘价3日开盘价3日最高价3日最低价timeChangedrop rangelimit Change
2015-05-19002014.SZ2015-05-199.449.440.1091100.1091100.1091100.0614410.2298730.1197030.2298730.1122880.3633470.3241530.3633470.3188561114.66-3.650.00
2014-07-08600435.SH2014-07-087.477.470.1030790.0388220.1030790.0240960.1686750.1566270.2141900.1405620.2677380.1646590.2824630.14324020.23-2.270.00
2015-01-12002738.SZ2015-01-1213.8513.850.1025270.0101080.102527-0.0310470.2151620.1566790.2151620.1335740.3386280.2577620.3386280.24620920910.74-7.140.00
2015-05-20000838.SZ2015-05-206.696.690.1016440.0134530.1016440.0014950.0388640.2137520.2137520.0388640.1434980.0059790.143498-0.00896913.99-1.199.58
2015-06-04300309.SZ2015-06-0410.6810.680.1011240.0880150.1011240.061798-0.0084270.1132960.113296-0.0103000.056180-0.0280900.056180-0.02902610.79-0.601.95
2015-07-30300368.SZ2015-07-309.819.810.100917-0.0020390.100917-0.0407750.2110090.1131500.2110090.0764530.3323140.3323140.3323140.29867511.16-1.1415.01
2018-08-08600470.SH2018-08-084.664.660.100858-0.0343350.100858-0.036481-0.0064380.0729610.094421-0.008584-0.062232-0.051502-0.040773-0.08154511.42-1.992.48
2014-07-28600157.SH2014-07-282.482.480.100806-0.0040320.100806-0.0120970.2137100.1532260.2137100.1370970.2661290.2620970.3387100.241935258.88-7.781.11
2015-04-02002006.SZ2015-04-0211.6111.610.1007750.0887170.1007750.0637380.1360900.1937980.1937980.0740740.0904390.1145560.1145560.05598610.68-4.820.43
2014-08-22300356.SZ2014-08-229.039.030.1007750.0143960.1007750.0143960.0753050.0719820.0874860.0476190.1605760.1074200.1827240.09191624.56-2.593.06
2015-08-04603616.SH2015-08-0418.1618.160.100771-0.0110130.100771-0.0903080.2109030.2109030.2109030.0346920.3325990.1541850.3325990.12665210.17-0.810.64
2018-08-07300392.SZ2018-08-076.756.750.1007410.0444440.1007410.0385190.2103700.2103700.2103700.1333330.3318520.3318520.3318520.33185222.72-1.870.88
2017-03-15002850.SZ2017-03-1584.8184.810.1006960.0470460.1006960.0313640.0549460.1011670.1417290.0470460.0393820.0312460.0530600.00000011.00-2.280.00
2015-07-17002392.SZ2015-07-176.566.560.1006100.0929880.1006100.0609760.2118900.1448170.2118900.1036590.3216460.2896340.3338410.23932911.00-1.172.63
2018-03-16002264.SZ2018-03-168.958.950.1005590.0491620.1005590.0391060.1284920.0983240.1731840.0614530.2413410.0927370.2413410.09273732.15-2.861.05
2015-03-16600207.SH2015-03-167.167.160.1005590.0055870.100559-0.0055870.1508380.1662010.2108940.1173180.1131280.1578210.1578210.07960911.01-3.841.28
2014-09-12300161.SZ2014-09-1214.8214.820.1005400.0087720.1005400.0000000.0897440.1025640.1761130.0890690.0573550.0755740.0755740.03373853.30-3.930.00
2016-11-01300044.SZ2016-11-019.559.550.1005240.0219900.1005240.0083770.0670160.1581150.2115180.0565450.0869110.0712040.1068060.05340322820.97-4.614.82
2018-11-21002337.SZ2018-11-214.284.280.1004670.1004670.1004670.0794390.2102800.1682240.2102800.1261680.0887850.3317760.3317760.088785197.00-8.270.60
2018-05-21002828.SZ2018-05-2117.9217.920.1004460.0468750.1004460.0195310.2109370.2109370.2109370.155692NaNNaNNaNNaN113.71-5.230.00
2015-05-04600744.SH2015-05-049.069.060.1004420.1004420.1004420.0044150.1214130.0342160.2030910.0242830.0088300.0883000.1136870.00883041.20-2.314.53
2015-08-26000628.SZ2015-08-2611.4511.450.1004370.0829690.1004370.0611350.2104800.2104800.2104800.2104800.0890830.2227070.2227070.0890836014.15-5.763.86
2014-09-19000852.SZ2014-09-1916.1316.130.1004340.0031000.1004340.0006200.1159330.0756350.1419710.0588960.1153130.1122130.1351520.08865510.69-2.195.31
2017-08-29603042.SH2017-08-2933.5633.560.100417-0.0214540.100417-0.0274140.0762810.0315850.1033970.0086410.0306910.0563170.0935640.02771211.57-1.833.54
2015-02-04000875.SZ2015-02-045.285.280.1003790.0416670.1003790.0094700.0909090.1155300.1818180.0625000.0094700.0454550.077652-0.01704511.48-2.114.34
2017-03-13601212.SH2017-03-1310.6610.660.100375-0.0168860.100375-0.0318950.2110690.1585370.2110690.1378990.3320830.2804880.3320830.26735564.43-5.580.00
2018-08-09300392.SZ2018-08-098.178.170.1003670.1003670.1003670.1003670.2105260.1884940.2105260.1444310.0979190.2007340.2007340.089351954.91-5.480.00
2015-12-02000668.SZ2015-12-0226.6126.610.1003380.0507330.1003380.0330700.0668920.1048480.1623450.0623830.0717780.0695230.0868090.04434432.98-2.433.87
2017-08-22300678.SZ2017-08-2229.0129.010.1003100.0613580.1003100.042744NaNNaNNaNNaNNaNNaNNaNNaN10.73-1.555.67
2014-02-28600680.SH2014-02-2813.2613.260.1003020.0452490.1003020.0452490.2104070.1184010.2104070.1055810.3318250.2722470.3318250.27224721120.19-12.070.00
...............................................................
2016-02-25300280.SZ2016-02-2531.5229.89-0.122462-0.084074-0.054569-0.146574-0.210343-0.147525-0.147525-0.210343-0.216371-0.216688-0.191942-0.26491121.35-0.825.63
2015-10-23300081.SZ2015-10-2316.3515.89-0.125994-0.078287-0.056881-0.125994-0.099694-0.173089-0.083792-0.176758-0.185321-0.124771-0.116820-0.18899110.26-1.280.00
2018-06-14002096.SZ2018-06-148.448.16-0.130332-0.092417-0.092417-0.130332-0.218009-0.210900-0.187204-0.218009-0.172986-0.254739-0.169431-0.25592476.95-4.352.43
2018-06-15002423.SZ2018-06-1514.6214.08-0.133379-0.110807-0.110807-0.133379-0.112859-0.220246-0.056772-0.220246-0.178523-0.158687-0.083447-0.189466262.90-4.583.67
2016-12-19300268.SZ2016-12-1950.4148.50-0.134100-0.067249-0.048006-0.134100-0.218806-0.176552-0.167824-0.220591-0.218409-0.226146-0.199564-0.238643111.55-2.543.17
2018-08-02002400.SZ2018-08-023.623.46-0.140884-0.093923-0.088398-0.140884-0.174033-0.157459-0.151934-0.182320-0.140884-0.149171-0.135359-0.16850861.15-2.670.20
2017-04-14000616.SZ2017-04-146.125.78-0.145425-0.101307-0.084967-0.150327-0.145425-0.153595-0.111111-0.156863-0.156863-0.156863-0.140523-0.181373227.75-4.163.56
2015-05-22300201.SZ2015-05-2211.6410.16-0.146048-0.185567-0.088488-0.214777-0.060137-0.158935-0.060137-0.162371-0.066151-0.005155-0.001718-0.09536113.41-1.640.00
2015-04-27300188.SZ2015-04-2717.9616.97-0.150334-0.084076-0.055122-0.150334NaNNaNNaNNaN-0.149220-0.116927-0.097439-0.149777270.57-3.310.00
2018-12-06300006.SZ2018-12-064.924.42-0.154472-0.097561-0.085366-0.168699-0.203252-0.172764-0.166667-0.203252-0.199187-0.201220-0.191057-0.20325295.39-6.870.00
2015-03-05300364.SZ2015-03-0516.0414.58-0.154613-0.085411-0.026808-0.175810-0.149002-0.158354-0.125312-0.175810-0.143392-0.155860-0.092893-0.16396535.64-3.130.00
2018-11-29600936.SH2018-11-295.104.77-0.154902-0.109804-0.107843-0.158824-0.117647-0.158824-0.090196-0.162745-0.133333-0.133333-0.129412-0.15098029.77-3.341.07
2018-04-11300705.SZ2018-04-1128.8526.79-0.164298-0.071404-0.049567-0.164298-0.224263-0.198614-0.164298-0.235355-0.236742-0.250260-0.228076-0.25338010.66-0.581.80
2018-10-26603999.SH2018-10-265.755.25-0.177391-0.142609-0.142609-0.177391-0.160000-0.184348-0.137391-0.205217-0.161739-0.172174-0.149565-0.18087010.68-0.452.56
2019-01-10300076.SZ2019-01-105.685.13-0.179577-0.140845-0.125000-0.184859-0.179577-0.158451-0.144366-0.197183-0.170775-0.188380-0.160211-0.19894413.01-1.633.94
2014-12-22600169.SH2014-12-228.817.95-0.181612-0.113507-0.091941-0.187287-0.205448-0.205448-0.185017-0.225880-0.125993-0.187287-0.125993-0.19750311.47-1.621.12
2015-05-14300431.SZ2015-05-14105.3291.51-0.187524-0.191132-0.137486-0.218002-0.106248-0.165970-0.106248-0.173092-0.016901-0.050893-0.016901-0.10624812.70-1.820.00
2018-11-05300748.SZ2018-11-0536.3231.29-0.217786-0.174009-0.146476-0.223568-0.185022-0.208700-0.177037-0.236509-0.229075-0.215584-0.201542-0.2436671753.81-4.690.48
2015-09-14000797.SZ2015-09-147.666.38-0.251958-0.223238-0.184073-0.251958-0.199739-0.259791-0.176240-0.283290-0.227154-0.202350-0.154047-0.22846011.05-2.736.09
2015-07-06002234.SZ2015-07-0612.7310.44-0.261587-0.260801-0.231736-0.261587-0.226237-0.335428-0.206599-0.335428NaNNaNNaNNaN151.20-1.484.80
2018-01-10600652.SH2018-01-108.948.94NaNNaNNaNNaNNaNNaNNaNNaN-0.083893-0.051454-0.020134-0.09060411.27-3.881.30
2017-04-11002774.SZ2017-04-1122.7222.72NaNNaNNaNNaNNaNNaNNaNNaN-0.100352-0.078785-0.034771-0.10035210.60-2.151.84
2017-07-07002885.SZ2017-07-0731.9131.91NaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaN10.47-0.700.47
2016-03-03000982.SZ2016-03-038.228.22NaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaN74.94-3.571.52
2017-11-10002409.SZ2017-11-1041.1541.15NaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaN10811.96-8.090.50
2015-03-24300310.SZ2015-03-249.989.98NaNNaNNaNNaN-0.071142-0.046092-0.039078-0.100200-0.044088-0.090180-0.002004-0.09719410.28-0.180.43
2014-02-12000971.SZ2014-02-123.643.64NaNNaNNaNNaNNaNNaNNaNNaN-0.027473-0.098901-0.002747-0.098901883.14-6.161.44
2016-02-17600234.SH2016-02-1722.8522.22NaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaN14916.27-8.010.00
2018-05-14002930.SZ2018-05-1441.5341.53NaNNaNNaNNaNNaNNaNNaNNaN0.0999280.0594750.0999280.037804104.02-7.370.62
2017-04-12600550.SH2017-04-1212.5412.54NaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaN142.24-2.494.45

503 rows × 20 columns

In [184]:

xdf = dataclosedf.dropna(axis=0)

In [185]:

xdf = xdf.sort_values(by='date')
xlabel = ['time','Change','drop range','limit Change']
ylabel = '3日收盘价'
xdf[ylabel] =xdf[ylabel].apply(lambda x:1 if x>0.02 else -1)

train = xdf[:300]
test = xdf[-200:]

X=train[xlabel]
Y=train[ylabel]

X_test=test[xlabel]
Y_test=test[ylabel]

from sklearn import svm
model = svm.SVC(C=5, kernel='rbf', gamma=0.5)

model.fit(X, Y)
print('训练时,预测成功率 {}'.format(round(np.mean(model.predict(X)==Y),2)))
print('测试时,预测成功率 {}'.format(round(np.mean(model.predict(X_test)==Y_test),2)))
len(list(test[test[ylabel]==1].index)),len(list(test[test[ylabel]==-1].index))
训练时,预测成功率 0.92
测试时,预测成功率 0.6

Out[185]:

(49, 151)

查看以上策略详情请到supermind量化交易官网查看:同花顺Supermind量化交易 运用SVM算法“打板”-- SVM算法应用