股票研究
import pandas as pd import numpy as np import os import matplotlib.pyplot as plt all_stock_data = pd.DataFrame() stock_code_list = [] t = os.listdir('all_stock_data1') for f in t: stock_code_list.append(f.split('.csv')[0]) for code in stock_code_list: print('正在计算%s'%code) stock_data = pd.read_csv('all_stock_data1\\'+code+'.csv',encoding='gbk') stock_data = stock_data[['日期','最低价','开盘价','最高价','收盘价','成交量']]#.ix[:300] stock_data.replace(0,np.nan,inplace=True) stock_data.sort_values('日期',inplace=True) stock_data['本日长阴'] = ((stock_data['收盘价']/stock_data['开盘价'] - 1)*100)<-3 stock_data['买入点'] = '' #print(stock_data) stock_data.dropna(how='any', inplace=True) stock_data.replace(np.inf,np.nan,inplace=True) # 计算KDJ指标 low_list=stock_data['最低价'].rolling(window=9).min() low_list.fillna(value=stock_data['最低价'].expanding().min(), inplace=True) high_list = stock_data['最高价'].rolling(window=9).max() high_list.fillna(value=stock_data['最高价'].expanding().max(), inplace=True) rsv = (stock_data['收盘价'] - low_list) / (high_list - low_list) * 100 stock_data['KDJ_K'] = rsv.ewm(com=2).mean() stock_data['KDJ_D'] = stock_data['KDJ_K'].ewm(com=2).mean() stock_data['KDJ_J'] = 3 * stock_data['KDJ_K'] - 2 * stock_data['KDJ_D'] #print(stock_data) tiao1 = stock_data['成交量'].shift() == stock_data['成交量'].shift().rolling(window=100).min() tiao2 = stock_data['成交量']>(stock_data['成交量'].shift())*2 tiao3 = stock_data['本日长阴'].rolling(window=5).sum()>=1 tiao4 = stock_data['收盘价']==stock_data['最高价'] tiao5 = stock_data['收盘价']>stock_data['开盘价']*1.2 tiao6 = (stock_data['KDJ_J']>0) & (stock_data['KDJ_J'].shift()<0) tiao7 = stock_data['收盘价']>(stock_data['收盘价'].shift().rolling(window=30).max()) tiaojian = tiao2&tiao7 for i in [1,2,3,5,10,20]: stock_data[str(i)+'日涨幅'] = (stock_data['收盘价'].shift(-i)/stock_data['收盘价'] - 1)*100 stock_data.ix[stock_data[tiaojian==True].index,'买入点'] = '是' stock_data.replace(np.inf,np.nan,inplace=True) stock_data.dropna(how='any', inplace=True) stock_data = stock_data[(stock_data['买入点'] == '是')] all_stock_data = all_stock_data.append(stock_data, ignore_index=True) #print(stock_data) print('-------------------------------') print('收益测试:') print('共发出%d次买出提示'%all_stock_data.shape[0]) print('-------------------------------') for i in [1,2,3,5,10,20]: print('买入'+str(i)+'日后平均涨幅%.2f%%' % all_stock_data[str(i)+'日涨幅'].mean()) print("其中上涨股票的比例是%.2f%%。" % \ (all_stock_data[all_stock_data[str(i)+'日涨幅'] > 0].shape[0]/float(all_stock_data.shape[0]) * 100)) print('------------------------------------')