分析的数据下载地址:
链接:https://pan.baidu.com/s/1PWsgYr6NepNnDRtuAHhIjw 密码:flgt
import pandas as pd
import time
import numpy
class Stock():#初始化时传入路径,读入股票的数据
#shift表示计算统计多长时间窗口的最高价,shift表示要计算每个窗口未来多长时间的涨跌幅
#startDate表示从哪一个日期开始计算
def __init__(self,stockPath,frame,shift,startDate):
f=open(stockPath,'r',encoding="UTF-8")
data=pd.read_csv(stockPath,sep=',')
# A日期、B编码、C名称、D收盘价、E开盘价、F最低价、G最高价、H平均价、I涨跌额度、J涨跌幅、K换手率、L成交量、M成交金额、N总值、O流值
data.columns=["date","code","name","tClose","tOpen","tLow","tHigh","tMean","zhangE","zhangFu","changeRate","volume","money","total","liquid"]
data=data[data["date"]>=startDate]
rows, cols = data.shape
self.frame=frame
self.shift=shift
self.colsDesc=data.columns
self.length=rows
self.data=data
self.frames=rows//(frame)#计算该股票的历史数据可以得到多少个切片
self.stockReport={}#窗口的开始日期,窗口的结束日期,窗口是否创新高,窗口未来shift个交易日的涨跌幅
def getStockReport(self):
for i in range(0,self.length-self.shift-self.frame):#此处可以改为多进程,先规划好切片,再计算每一个切片的结果
frame=Frame(self.data,i,i+self.frame,self.shift)
frameStartDate=frame.startDate#切片的开始日期
frameEndDate=frame.endDate#切片的结束日期
frameFlag=frame.getFrameFlag()#切片的是否创新高
frameRate=frame.getFrameRate()#切片未来shif日期内的涨幅
self.stockReport[frameStartDate]={"frameStartDate":frameStartDate,"frameEndDate":frameEndDate,
"frameFlag":frameFlag,"frameRate":frameRate}
return self.stockReport
class Frame:#窗口类,
#初始化参数为,切片数据源,切片的开始索引,结束索引
def __init__(self,FrameData,startIndex,endIndex,shift):
self.frameData=FrameData.iloc[startIndex:endIndex,:]#一个切片,从startIndex到endIndex-1窗口的数据
self.startDate=self.frameData["date"].values[0]#窗口的开始日期
self.endDate=self.frameData["date"].values[-1]#窗口的结束日期
self.flag=0#1表示该窗口创出新高,0表示没有创新高
self.rate=0
self.rateList=FrameData["tClose"].values[endIndex:endIndex+shift]#在这个序列中寻找最大涨幅
self.frameClose=self.frameData["tClose"].values[-1]#窗口的收盘价
def getFrameFlag(self):#用于返回该段数据的标签,标注该切片最后一个交易日有没有创新高
maxIndex=numpy.argwhere(self.frameData["tClose"]==numpy.max(self.frameData["tClose"]))
# print(maxIndex[0])
# print(self.frameData["tClose"])
if maxIndex[0]==len(self.frameData["tClose"])-1:
self.flag=1
else:
self.flag=0
return self.flag
def getFrameRate(self):#返回窗口指定交易日内的最大涨
maxpriceOfRateList=numpy.max(self.rateList)
rate=((maxpriceOfRateList-self.frameClose)/self.frameClose)*100
self.rate=float("%.2f" % rate)
return self.rate
import os
import pandas as pd
from multiprocessing import Process
from analyseNewHigh.stockKClass import Stock,Frame
def process(path,index):
stock = Stock(path, 200, 60, '2017-01-01')
report = stock.getStockReport()
reportDataFrame = pd.DataFrame(report).T
rows, cols = reportDataFrame.shape
if rows > 0:
yesDataFrame = reportDataFrame[reportDataFrame["frameFlag"] == 1]
rows1, cols1 = yesDataFrame.shape
if rows1 > 0:
yesDataFrame.to_csv("G:\创新高分析python代码包和数据\标的结果200新高后60交易日\\"+file)
print(index,file)
print(yesDataFrame)
for (root,dirs,files) in os.walk("H:\根据标的和日期分析结果\标的数据"):
length=len(files)
for i in range(length):
file=files[i]
path=root+"\\"+file
process(path,i)
得到的结果进一步可视化: