1纸淮、前置準(zhǔn)備
數(shù)據(jù)來源使用tushare pro平斩,具體操作請看鏈接,注冊就可以使用了
Tushare金融大數(shù)據(jù)開放社區(qū)
2咽块、直接放源碼
封裝沒有做好绘面,很多重復(fù)代碼,湊合看吧侈沪,python不太會揭璃,有大佬看到麻煩指教下是否有問題
# -*- coding: utf-8 -*-
import datetime
import os
import time
import numpy as np
import tushare as ts
from sklearn import svm
import joblib
class SvmUtil(object):
def __init__(self):
self.pro = ts.pro_api('此處放tushare的token')
def svm_learning(self, stockCode):
end_time = time.strftime('%Y%m%d', time.localtime(time.time()))
start_year = int(time.strftime('%Y', time.localtime(time.time()))) - 2
month_day = time.strftime('%m%d', time.localtime(time.time()))
start_time = '{}{}'.format(start_year, month_day)
# 獲取數(shù)據(jù)
df = self.pro.daily(ts_code=stockCode, start_date=start_time, end_date=end_time)
days_value = df['trade_date'].values[::-1]
days_close = df['close'].values[::-1]
days = []
# 獲取行情日期列表
for i in range(len(days_value)):
days.append(str(days_value[i]))
x_all = []
y_all = []
for index in range(15, (len(days) - 5)):
# 計算三星期共15個交易日相關(guān)數(shù)據(jù)
start_day = days[index - 15]
end_day = days[index]
data = self.pro.daily(ts_code=stockCode, start_date=start_day, end_date=end_day)
open = data['open'].values[::-1]
close = data['close'].values[::-1]
max_x = data['high'].values[::-1]
min_n = data['low'].values[::-1]
amount = data['amount'].values[::-1]
volume = []
for i in range(len(close)):
volume_temp = amount[i] / close[i]
volume.append(volume_temp)
open_mean = open[-1] / np.mean(open) # 開盤價/均值
close_mean = close[-1] / np.mean(close) # 收盤價/均值
diff_close_open_mean = close_mean - open_mean # 收盤價均值-開盤價均值
volume_mean = volume[-1] / np.mean(volume) # 現(xiàn)量/均量
max_mean = max_x[-1] / np.mean(max_x) # 最高價/均價
min_mean = min_n[-1] / np.mean(min_n) # 最低價/均價
diff_max_min_mean = max_mean - min_mean # 最高價均值-最低價均值
vol = volume[-1]
return_now = close[-1] / close[0] # 區(qū)間收益率
std = np.std(np.array(close), axis=0) # 區(qū)間標(biāo)準(zhǔn)差
# 將計算出的指標(biāo)添加到訓(xùn)練集X
# features用于存放因子
# features = [close_mean, volume_mean, max_mean, min_mean, vol, return_now, std]
features = [open_mean, close_mean, diff_close_open_mean, volume_mean, max_mean, min_mean, diff_max_min_mean,
vol, return_now, std]
x_all.append(features)
# 準(zhǔn)備算法需要用到的數(shù)據(jù)
for i in range(len(days_close) - 20):
if days_close[i + 20] > days_close[i + 15]:
label = 1
else:
label = 0
y_all.append(label)
x_train = x_all[: -1]
y_train = y_all[: -1]
# 訓(xùn)練SVM
model = svm.SVC(C=1.0, kernel='rbf', degree=3, gamma='auto', coef0=0.0, shrinking=True, probability=False,
tol=0.001, cache_size=400, verbose=False, max_iter=-1,
decision_function_shape='ovr', random_state=None)
model.fit(x_train, y_train)
joblib.dump(model, stockCode[:-3] + "_model.m")
def svm_predict(self, stockCode):
if not (os.path.exists(stockCode[:-3] + "_model.m")):
self.svm_learning(stockCode)
today = datetime.date.today()
first = today.replace(day=1)
last_month = first - datetime.timedelta(days=15)
start_time = last_month.strftime("%Y%m%d")
end_time = time.strftime('%Y%m%d', time.localtime(time.time()))
model = joblib.load(stockCode[:-3] + "_model.m")
df = self.pro.daily(ts_code=stockCode, start_date=start_time, end_date=end_time)
open = df['open'].values[::-1]
close = df['close'].values[::-1]
train_max_x = df['high'].values[::-1]
train_min_n = df['low'].values[::-1]
train_amount = df['amount'].values[::-1]
volume = []
for i in range(len(close)):
volume_temp = train_amount[i] / close[i]
volume.append(volume_temp)
open_mean = open[-1] / np.mean(open)
close_mean = close[-1] / np.mean(close)
diff_close_open_mean = close_mean - open_mean
volume_mean = volume[-1] / np.mean(volume)
max_mean = train_max_x[-1] / np.mean(train_max_x)
min_mean = train_min_n[-1] / np.mean(train_min_n)
diff_max_min_mean = max_mean - min_mean
vol = volume[-1]
return_now = close[-1] / close[0]
std = np.std(np.array(close), axis=0)
# 得到本次輸入模型的因子
# features = [close_mean, volume_mean, max_mean, min_mean, vol, return_now, std]
features = [open_mean, close_mean, diff_close_open_mean, volume_mean, max_mean, min_mean, diff_max_min_mean,
vol, return_now, std]
features = np.array(features).reshape(1, -1)
prediction = model.predict(features)[0]
return prediction
if __name__ == '__main__':
code = '002277.SZ'
# SvmUtil().svm_learning(code)
SvmUtil().svm_predict(code)
本文由博客一文多發(fā)平臺 OpenWrite 發(fā)布!