1.subplot函数:
写成subplot(m,n,p)或者subplot(mnp)。
subplot是将多个图画到一个平面上的工具。其中,m表示是图排成m行,n表示图排成n列,也就是整个figure中有n个图是排成一行的,一共m行,如果第一个数字是2就是表示2行图。p是指你现在要把曲线画到figure中哪个图上,最后一个如果是1表示是从左到右第一个位置。
2.num2str函数:
str = num2str(A, precision)
把数组A转换成字符串形式表示,precision表示精度, 比如precision为3表示保留最多3位有效数字, 例如0.5345转换后为0.534,1.2345转换后为1.23。即从左边第一个不为0的数开始保留3个数值。
3.legend('Position','Cumulative Return','Location','Best')
location和best是把注释(奇解,通解)放到图中,并且放到最佳的位置,location可以设置放置的位置,best是放到最佳的位置。
4.linkaxes函数
linkaxes(ax,'x'),linkaxes命令,可以将一个figure中的各个axes指定相同的坐标范围,实现各个绘图同 步放大、缩小。在缩放一个绘图时,其它也会同步缩放。可以同时关联x、y轴,也可以只关联一个坐标轴。
5.
clc, clear all, close all
load CSI300.mat
CSI300_1Mn=raw_CSI300_1Mn(:,4);
CSI300_1Mn=CSI300_1Mn(1:end);
CSI300_EOD=raw_CSI300_EOD(:,4);
testPts=floor(0.8*length(CSI300_EOD));
CSIClose=CSI300_EOD(1:testPts);
CSICloseV=CSI300_EOD(testPts+1:end);
%计算Sharpe指数
[lead,lag]=movavg(CSIClose,20,30,'e');
%假设全年有250个交易日
s=zeros(size(CSIClose));
s(lead>lag)=1;%Buy
s(lead<lag)=-1;%Sell
r=[0;s(1:end-1).*diff(CSIClose)];%Return
sh=sqrt(250)*sharpe(r,0);%Annual Sharpe Ratio
%绘制初步策略的评估结果
ax(1)=subplot(2,1,1);
plot([CSIClose,lead,lag]),grid on
legend('Close','Lead','Lag','Location','Best')
title(['First Pass Results, Annual Sharpe Ratio=',num2str(sh,3)])
ax(2)=subplot(2,1,2);
plot([s,cumsum(r)]);grid on
title(['Final Return = ',num2str(sum(r),3),' (',num2str(sum(r)/CSIClose(1)*100,3),'%)'])
legend('Position','Cumulative Return','Location','Best')
linkaxes(ax,'x')
annualScaling=sqrt(250);