Home >  > 使用机器学习预测股价

使用机器学习预测股价

0

一、预测股价

代码:

import pandas as pd
import numpy as np
from pandas_datareader import data,wb
import matplotlib.pyplot as plt 
from scipy.spatial.distance import euclidean
from fastdtw import fastdtw

pd.set_option('display.max_colwidth',200)


import pandas_datareader as pdr

start_date = pd.to_datetime('2000-01-01')
stop_date = pd.to_datetime('2016-03-01')

# sp = pdr.data.get_data_yahoo('sp',start_date,stop_date) 
# sp.to_csv('sp_long.csv')
sp = pd.read_csv('spy_long.csv',index_col =0)
# print(sp)


def dtw_dist(x,y):
	distance,path = fastdtw(x,y,dist= euclidean)
	return distance

tseries =[]
tlen = 5
for i in range(tlen,len(sp),tlen):
	pctc = sp['Close'].iloc[i-tlen:i].pct_change()[1:].values*100
	res = sp['Close'].iloc[i-tlen:i+1].pct_change()[-1]*100
	tseries.append((pctc,res))

# print(tseries[0])

dist_pairs = []
for i in range(len(tseries)):
	for j in range(len(tseries)):
		dist = dtw_dist(tseries[i][0],tseries[j][0])
		dist_pairs.append((i,j,dist,tseries[i][1],tseries[j][1]))

dist_frame = pd.DataFrame(dist_pairs,columns = ['A','B','Dist','A Ret','B Ret'])
sf = dist_frame[dist_frame['Dist']>0].sort_values(['A','B']).reset_index(drop=1)
sfe = sf[sf['A']<sf['B']]
winf = sfe[(sfe['Dist']<=1)&(sfe['A Ret']>0)]
print(winf)

plt.plot(np.arange(4),tseries[6][0])
# plt.show()


def get_stats(s, n=252):
	s = s.dropna()
	wins = len(s[s>0])
	losses = len(s[s<0])
	events = len(s[s==0])    #不亏不赚
	mean_w = round(s[s>0].mean(),3)
	mean_l = round(s[s<0].mean(),3)
	win_r = round(wins/losses,3)  
	mean_trd = round(s.mean(),3)
	sd = round(np.std(s),3)
	max_l = round(s.min(),3)
	max_w = round(s.max(),3)
	sharp_r = round((s.mean()/np.std(s))*np.sqrt(n),4)
	cnt = len(s)  
	win_t = round(wins/cnt,3)   #胜率
	print('Trades(交易次数):',cnt,
		  '\nWins(盈利次数):',wins,
		  '\nLosses(亏损次数):',losses,
		  '\nBreakeven(盈亏平衡次数):',events,
		  '\nWin/Loss Ratio(盈利次数/亏损次数):',win_r,
		  '\nMean Win(盈利的平均值):',mean_w,
		  '\nMean Loss(亏损的平均值):',mean_l,
		  '\nMean(平均收益):',mean_trd,
		  '\nStd Dev(标准差):',sd,
		  '\nMax loss(最大亏损):',max_l,
		  '\nMax Win(最大盈利):',max_w,
		  '\nwin/Total(胜率):',win_t,
		  '\nSharp Ratio(夏普比率):',sharp_r,
		)


exclueded = {}
return_list = []
def get_returns(r):
	if exclueded.get(r['A']) is None:
		return_list.append(r['B Ret'])
		if r['B Ret'] < 0:
			 exclueded.update({r['A']:1})
winf.apply(get_returns,axis = 1)
print(get_stats(pd.Series(return_list)))

二、上证
再来一个上证指数的,不知为什么,使用了更多的数据,但是胜率却没有这么高。

将数据量提高到1万

本文暂无标签

发表评论

*

*