clc
close all
C=xlsread('1.xlsx');
inputnum=10;
hiddennum=4;
outputnum=1;
% Opening stock value for the day
Open = C(1:750,4);
Open = Open';
% 昨天的收盘价
shoupan = C(1:750,5);
shoupan =shoupan';
% Highest stock value for the day
High = C(1:750,2);
High = High';
% Lowest stock value for the day
Low = C(1:750,3);
Low = Low';
% Closing stock value for the day
Close = C(1:750,1);
Close = Close';
%均线
ma5 = C(1:750,8);
ma5 = ma5';
ma10 = C(1:750,9);
ma10 = ma10';
ma20 = C(1:750,10);
ma20 = ma20';
ma50 = C(1:750,11);
ma50 = ma50';
%换手率
change=C(1:750,6);
change = change';
%kdj d
d=C(1:750,14);
d = d';
%macd
macd= C(1:750,19);
macd=macd';
%% 遗传算法参数初始化
maxgen=10; %进化代数,即迭代次数
sizepop=10; %种群规模
pcross=[0.2]; %交叉概率选择,0和1之间
pmutation=[0.1]; %变异概率选择,0和1之间
%节点总数
numsum=10*4+4+4*1+1;
lenchrom=ones(1,numsum); %个体长度
bound=[-3*ones(numsum,1) 3*ones(numsum,1)]; %个体范围
% Input vector of the input variables
Input = {Open;shoupan;High; Low;ma5;ma10;ma20;ma50;change;macd};
Input = cell2mat(Input);
% Construction of feed-forward neural network
net = newff([minmax(Open);minmax(shoupan);minmax(High); minmax(Low);minmax(ma5);minmax(ma10);minmax(ma20);minmax(ma50);minmax(change);minmax(macd)], [abs(floor(4)), 1],{'purelin', 'purelin', 'transIm'},'traingdx');
% eveluating the performance of the neural network - using mse as
% the measuring standard
individuals=struct('fitness',zeros(1,sizepop), 'chrom',[]); %将种群信息定义为一个结构体
avgfitness=[]; %每一代种群的平均适应度
bestfitness=[]; %每一代种群的最佳适应度
bestchrom=[]; %适应度最好的染色体
%计算个体适应度值
for i=1:sizepop
%随机产生一个种群
individuals.chrom(i,:)=Code(lenchrom,bound); %编码(binary和grey的编码结果为一个实数,float的编码结果为一个实数向量)
x=individuals.chrom(i,:);
%计算适应度
individuals.fitness(i)=fun(x,10,4,1,net,Input,Close); %染色体的适应度
end
FitRecord=[];
%找最好的染色体
[bestfitness ,bestindex]=min(individuals.fitness);
bestchrom=individuals.chrom(bestindex,:); %最好的染色体
avgfitness=sum(individuals.fitness)/sizepop; %染色体的平均适应度
%记录每一代进化中最好的适应度和平均适应度
trace=[avgfitness bestfitness];
%% 迭代求解最佳初始阀值和权值
% 进化开始
for i=1:maxgen
% 选择
individuals=Select(individuals,sizepop);
avgfitness=sum(individuals.fitness)/sizepop;
%交叉
individuals.chrom=Cross(pcross,lenchrom,individuals.chrom,sizepop,bound);
% 变异
individuals.chrom=Mutation(pmutation,lenchrom,individuals.chrom,sizepop,i,maxgen,bound);
% 计算适应度
for j=1:sizepop
x=individuals.chrom(j,:); %个体
individuals.fitness(j)=fun(x,10,4,1,net,Input,Close);
end
%找到最小和最大适应度的染色体及它们在种群中的位置
[newbestfitness,newbestindex]=min(individuals.fitness);
[worestfitness,worestindex]=max(individuals.fitness);
%最优个体更新
if bestfitness>newbestfitness
bestfitness=newbestfitness;
bestchrom=individuals.chrom(newbestindex,:);
end
individuals.chrom(worestindex,:)=bestchrom;
individuals.fitness(worestindex)=bestfitness;
%记录每一代进化中最好的适应度和平均适应度
avgfitness=sum(individuals.fitness)/sizepop;
trace=[trace;avgfitness bestfitness];
FitRecord=[FitRecord;individuals.fitness];
end
%% 把最优初始阀值权值赋予网络预测
% %用遗传算法优化的BP网络进行值预测
w1=x(1:inputnum*hiddennum);
B1=x(inputnum*hiddennum+1:inputnum*hiddennum+hiddennum);
w2=x(inputnum*hiddennum+hiddennum+1:inputnum*hiddennum+hiddennum+hiddennum*outputnum);
B2=x(inputnum*hiddennum+hiddennum+hiddennum*outputnum+1:inputnum*hiddennum+hiddennum+hiddennum*outputnum+outputnum);
net.iw{1,1}=reshape(w1,hiddennum,inputnum);
net.lw{2,1}=reshape(w2,outputnum,hiddennum);
net.b{1}=reshape(B1,hiddennum,1);
net.b{2}=B2;
% Maximum number of iterations
net.trainparam.epochs = 65000;
% Desired Tolerance value
net.trainparam.goal = 1e-5;
% learning rate initialisation
net.trainparam.lr = 0.001;
% using full data to train the neural network
net.divideFcn ='dividetrain';
net = train(net, Input, Close);
t = net(Input);
perf = perform(net, Close, t);
view(net);
%% Testing the constructed neural network
% Opening sample test data
C_t = xlsread('1.xlsx');
Open_t = C_t(751:1500,4);
Open_t = Open_t';
High_t = C_t(751:1500,2);
High_t = High_t';
Low_t = C_t(751:1500,3);
Low_t = Low_t';
Close_t = C_t(750:1500,1);
Close_t = Close_t';
shoupan_t = C(751:1500,5);
shoupan_t =shoupan_t';
ma5_t = C(751:1500,8);
ma5_t = ma5_t';
ma10_t = C(751:1500,9);
ma10_t = ma10_t';
ma20_t = C(751:1500,10);
ma20_t = ma20_t';
ma50_t = C(751:1500,11);
ma50_t = ma50_t';
%换手率
change_t=C(751:1500,6);
change_t = change_t';
%d
d_t= C(751:1500,14);
d_t=d_t';
%macd
macd_t= C(751:1500,19);
macd_t=macd_t';
Input_t = {Open_t; shoupan_t;High_t; Low_t;ma5_t;ma10_t;ma20_t;ma50_t;change_t;macd_t};
Input_t = cell2mat(Input_t);
answer = ones(1,750);
answer_t = ones(1,750);
for i=1:750
answer(i) = net([Input_t(1, i); Input_t(2,i); Input_t(3, i); Input_t(4, i);Input_t(5, i);Input_t(6, i);Input_t(7, i);Input_t(8, i);Input_t(9, i);Input_t(10, i)]);
answer_t(i) = Close_t(i);
end
x = 1:750;
plot(x, answer,'r' ,x, answer_t(1:750),'b');
legend('真值','预测值')
xlabel('数据点');
ylabel('股票价格');
title('神经网络预测股价');
acc=corr(answer(1:end)',answer_t(1:end)');
pr=sprintf('Acc is %2f',acc);
msgbox(pr)
figure(2)
[r c]=size(trace);
plot([1:r]',trace(:,1),'b',[1:r]',trace(:,2),'k');
title(['适应度曲线 ' '终止代数=' num2str(maxgen)]);
xlabel('进化代数');ylabel('适应度');
legend('平均适应度','最佳适应度');