聚宽机器学习判断大盘SVM使用

这里写自定义目录标题

import talib
from jqdata import *

test_stock = '399300.XSHE'
start_date = datetime.date(2007, 1, 4)
end_date = datetime.date(2016, 6, 8)

trading_days = get_all_trade_days()
start_date_index = trading_days.tolist().index(start_date)
end_date_index = trading_days.tolist().index(end_date)

x_all = []
y_all = []

for index in range(start_date_index, end_date_index):
    # 得到计算指标的所有数据
    start_day = trading_days[index - 30]
    end_day = trading_days[index]
    stock_data = get_price(test_stock, start_date=start_day, end_date=end_day, frequency='daily', fields=['close','volume'])
    close_prices = stock_data['close'].values
    
    #通过数据计算指标
    # -2是保证获取的数据是昨天的,-1就是通过今天的数据计算出来的指标
    sma_data = talib.SMA(close_prices)[-2] 
    wma_data = talib.WMA(close_prices)[-2]
    mom_data = talib.MOM(close_prices)[-2]
    volume = stock_data['volume'].values[-2]
    features = []
    features.append(sma_data)
    features.append(wma_data)
    features.append(mom_data)
    features.append(volume)
    
    label = False
    if close_prices[-1] > close_prices[-2]:
        label = True
    x_all.append(features)
    y_all.append(label)
    

# 准备算法需要用到的数据
x_train = x_all[: -1]
y_train = y_all[: -1]
x_test = x_all[-1]
y_test = y_all[-1]
print('data done')

data done

from sklearn import svm
from sklearn import tree
model = tree.DecisionTreeClassifier(criterion='gini') #
model.fit(x_train, y_train)
print(model.predict(x_test)==y_test)
#开始利用机器学习算法计算
clf = svm.SVC()
#训练的代码
clf.fit(x_train, y_train)
#得到测试结果的代码
prediction = clf.predict(x_test)

# 看看预测对了没
print(prediction == y_test)
print('all done')

/opt/conda/lib/python3.5/site-packages/sklearn/utils/validation.py:395: DeprecationWarning: Passing 1d arrays as data is deprecated in 0.17 and will raise ValueError in 0.19. Reshape your data either using X.reshape(-1, 1) if your data has a single feature or X.reshape(1, -1) if it contains a single sample.
DeprecationWarning)
[ True]
[False]
all done
/opt/conda/lib/python3.5/site-packages/sklearn/utils/validation.py:395: DeprecationWarning: Passing 1d arrays as data is deprecated in 0.17 and will raise ValueError in 0.19. Reshape your data either using X.reshape(-1, 1) if your data has a single feature or X.reshape(1, -1) if it contains a single sample.
DeprecationWarning)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值