五日线金叉十日测试
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_data')
#for i in t:
# print(i.split('.')[0]) for root, dirs, files in os.walk('all_stock_data1'): if files: for f in files: if '.csv' in f: 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.sort_values('日期',inplace=True) stock_data['五叉十'] = '无' stock_data['五日均线'] = stock_data['收盘价'].rolling(window=5).mean() stock_data['十日均线'] = stock_data['收盘价'].rolling(window=10).mean() tiao1 = stock_data['五日均线'] > stock_data['十日均线'] tiao2 = stock_data['五日均线'].shift() < stock_data['十日均线'].shift() for i in [1,2,3,5]: stock_data[str(i)+'日涨幅'] = (stock_data['收盘价'].shift(-i)/stock_data['收盘价'] - 1)*100 stock_data.ix[stock_data[(tiao1==True) & (tiao2 == 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('-------------------------------') print('五日线金叉十日测试:') print('共发出%d次买出提示'%all_stock_data.shape[0]) print('-------------------------------') for i in [1,2,3,5]: print('买入'+str(i)+'日后平均涨幅%f' % all_stock_data[str(i)+'日涨幅'].mean()) print(all_stock_data)