课程设计
平台:Matlab App designer
功能实现:基金连涨连跌提醒
目的:
1.Matlab代码编写能力进阶;
2.学会利用Matlab GUI设计图形交互界面,开发有趣的应用;
3.利用爬虫程序爬取网络上的感兴趣信息并进行数据处理;
演示:
1.根据多数人“追涨杀跌”的心态,连涨或连跌天数是购买基金的重要参考信息。创建一个基金连涨连跌提醒界面,具备添加基金、删除基金、插入基金、基金净值数据图绘制、基金连涨连跌天数提醒等功能。
2.通过基金号在网络上爬取对应的基金名称,并将基金添加到基金列表中。
3.通过选择基金列表中的基金,查看该基金近一个月的净值走势,并给出连涨或连跌天数提醒。
4.删除基金列表中的基金。
5.往基金列表中选定的位置插入基金。
6.鼠标放在净值走势图上,可以显示出日期及净值
7.通过批量查询直接查看基金列表中所有基金的连涨连跌天数。
8.此外,每次退出后会保存基金列表中的基金数据,重新运行后无须再重复添加。
9.程序中只查看了近一个月的单位净值,这样子程序运行比较快。其实可以爬取所选基金的自创立以来的所有净值数据并显示在界面上,但是这样子运行就变慢了。考虑到连涨连跌几乎不会超过20天,所以就查看近一个月的单位净值的趋势就ok了。
代码示例:
列表框回调函数:
try
[aa,~]=find(strcmp(app.Str,app.ListBox.Value));
value=app.Str{aa,1};
catch
cla(app.UIAxes,"reset");
app.Label_4.Text='';
return
end
ans2=cell(2,20);
ans1=webread(app.url,'callback','jQuery1223','symbol',value,'datefrom','','dateto','','page',1,'_','123',app.options);
expression_day='(\d+-\d+-\d+)';
[tokens_day,~] = regexp(ans1,expression_day,'tokens','match');
expression_value= '"jjjz":"(\d+.\d+)';
[tokens_value,~] = regexp(ans1,expression_value,'tokens','match');
ans2(1,1:length(tokens_day)-1)=tokens_day(1:length(tokens_day)-1);
ans2(2,1:length(tokens_value)-1)=tokens_value(1:length(tokens_value)-1);
plo=zeros(1,length(ans2(2,:)));
for j=1:length(ans2(2,:))
plo(j)=str2double(ans2{2,j});
end
day1=string(ans2(1,:));
ck=datenum(day1);
dcm1 = datacursormode(app.UIFigure);
show_date_num=4;%横轴分5截
plot(app.UIAxes,ck,plo(1:length(ck)));
datetick(app.UIAxes,'x','yyyy/mm/dd')
set(app.UIAxes,'XTick',ck(end:-show_date_num:1))
set(app.UIAxes,'XTickLabel',day1(end:-show_date_num:1))
set(app.UIAxes,'XTickLabelRotation',30)
set(dcm1,'UpdateFcn',@myfunction);
ylabel(app.UIAxes,'单位净值')
xlim(app.UIAxes,[min(ck),max(ck)])
grid(app.UIAxes,"on")
aa=diff(plo);
if aa(1)<0
str1='连涨';
Color=[1,0,0];
elseif aa(1)>0
str1='连跌';
Color=[0,1,0];
else
str1='横盘';
Color=[0,0,0];
end
num=1;
for ii=1:20
if sign(aa(ii))==sign(aa(ii+1))
num=num+1;
else
break;
end
end
app.Label_4.Text=[str1,num2str(num),'天'];
app.Label_4.FontColor=Color;
function output_txt = myfunction(~,event_obj)
% Display the position of the data cursor
% obj Currently not used (empty)
% event_obj Handle to event object
% output_txt Data cursor text string (string or cell array of strings).
pos = get(event_obj,'Position');
output_txt = {['X: ',datestr(pos(1),'yyyy/mm/dd')],...
['Y: ',num2str(pos(2),4)]};
% If there is a Z-coordinate in the position, display it as well
if length(pos) > 2
output_txt{end+1} = ['Z: ',num2str(pos(3),4)];
end
end
注:GUI里面的爬虫程序参考了B站博主longwq的视频:Matlab爬取基金数据并进行数据分析