这里写自定义目录标题
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)