python实验报告三
作者:互联网
编写程序对所有股票数据进行分析,挑选出符合一种底部启动的所有股票,并分析其成功率。
写出你对形态和成功率的定义,并把程序复制过来。
我的想法是,将每一个股票的数据读取出来,放进一个个列表,画出曲线,找到最低点以及最低点左右的两个最高点,将两个最高点之间的曲线与圆弧曲线进行数据拟合,
上图为示例
找到拟合优度较高的曲线,以文件名为键,数据存放的列表为值,组成字典的键值对写入json文件
import struct
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
import numpy as np
import os
import json
def readDayFile(filename, dict):
alist = []
with open(filename, 'rb') as fp:
buffer = fp.read() # 读取数据到缓存
size = len(buffer)
rowSize = 32 # day数据,每32个字节一组数据
for i in range(0, size, rowSize): # 步长为32遍历buffer
row = list(struct.unpack('iiiiifii', buffer[i:i + rowSize]))
row[2] = row[2] / 100
row.pop() # 移除最后无意义字段
alist.append(row[2])
filework = mathan(alist)
filename = filename.rstrip('.day')
filename = filename.strip('.\\z\\')
if filework > 0.75:
dict[filename] = alist
def mathan(alist):
minnum = min(alist)
bottom = alist.index(minnum)
if bottom == len(alist) or bottom == 0:
return None
maxnum = max(alist[:bottom])
maxindex1 = alist.index(maxnum)
maxnum = max(alist[bottom:])
maxindex2 = alist.index(maxnum, bottom)
#print(len(alist),bottom,maxindex1,maxindex2)
return task(alist, alist[maxindex1:maxindex2], maxindex1, maxindex2)
def func(x, a, b, c):#拟合函数
return a*x**2+b*x+c
def task(alist, calist, maxindex1, maxindex2):
x = np.arange(maxindex1, maxindex2)
y = np.array(calist)
popt, pcov = curve_fit(func, x, y)
# popt数组中,三个值分别是待求参数a,b,c
y2 = [func(i, popt[0], popt[1], popt[2]) for i in x]
'''绘制原曲线和拟合曲线'''
'''plot1 = plt.plot(x, y, '*', label='original values')
plot2 = plt.plot(x, y2, 'r', label='curve_fit values')
plt.xlabel('x axis')
plt.ylabel('y axis')
plt.legend(loc=4) # 指定legend的位置
plt.title('curve_fit')
plt.show()
plt.savefig('p2.png')
'''
if popt[0]<0:
return None
return mathnum(calist, y2)
'''简单计算并返回图线拟合度'''
def mathnum(alist,yi):
avg = np.mean(alist)
sy = syi = 0
for i in alist:
sy += (i-avg)**2
j = 0
syi += (yi[j] - i) ** 2
j+=1
E = 1 - (sy/syi)
return E
'''json文件数据写入操作'''
def writejson(json_data):
json_data = json.dumps(json_data, indent=4, ensure_ascii=False)
with open('test.json', 'a+', encoding="utf-8") as fp:
fp.write(json_data)
'''文件写入操作'''
def writefile(data,param = True):
with open('test.txt', 'a+', encoding="utf-8") as fp:
fp.write(data)
fp.write('\n')
if param == False:
fp.close()
def main():
dict = {}
rootdir = os.curdir
rootdir = os.path.join(rootdir, 'z')
list = os.listdir(rootdir)
for i in range(0, len(list)):
path = os.path.join(rootdir, list[i])
if os.path.isfile(path):
try:
readDayFile(path, dict)
except Exception:
writefile(path)
print('all is done')
writejson(dict)
main()
然后读取json文件的字典,以底部启动过程中的时间与一个月后的数值进行比较,判断底部启动的成功率,以此进行分析
import json
def caljson():
with open('test.json', 'r', encoding="utf-8") as fp:
data = json.load(fp)
#设定一个月的时间
for i in data.keys():
alist = data[i]
minnum = min(alist)
bottom = alist.index(minnum)
maxnum = max(alist[bottom:])
top = alist.index(maxnum,bottom)
n = 0
for j in range(bottom, top):
if j+30 >= top:
break
if(alist[j] <= alist[j+30]):
n+=1
data[i] = str(n/len(alist))+'%'
writejson(data)
'''json文件数据写入操作'''
def writejson(json_data):
json_data = json.dumps(json_data, indent=4, ensure_ascii=False)
with open('tey.json', 'a+', encoding="utf-8") as fp:
fp.write(json_data)
caljson()
理论上,我觉得这个图线拟合判断底部启动会比较简单,但是事实上,我发现,没有一条曲线拟合优度大于0.9,可能是我选取曲线函数产生了问题,亦可能本身股票数据有太多的复杂性,不容置疑的是,我的实验产生了较大的误差,尽管我认为我的想法具有很大的可行性。
标签:plt,bottom,python,alist,filename,popt,import,实验报告 来源: https://blog.csdn.net/qq_39383017/article/details/96427140