%%%%%%%%%从含有中文 英文 数据混合格式的文件中读取数据的程序%%%%%%%%%%%%%
close all
clear all
fidin=fopen('20130916.txt'); %打开文件one minute
fidout=fopen('data.txt','w'); %创建文件,用于写进数据
while ~feof(fidin) %判断是否为文件末尾
tline=fgetl(fidin); %从文件中读取行
if double(tline(1))>=48&&double(tline(1))<=57 %判断首字符是否是数值
fprintf(fidout,'%s\n\n',tline); %如果是数字行,把此行数据写入文件data中
continue %如果是非数字继续下一次循环
end
end
fclose(fidout);
X=importdata('data.txt'); %将生成的data.txt文件导入到工作空间。
% M=importdata('one minute.txt');
%%%%%%%%%%%%%%%%%%数据处理%%%%%%%%%%%%%%%%%%%
p=X.data(:,4); %取倒数第4行数据,即第6行数据。
s = sprintf('A%d:I%d',1,fidin);
xlswrite('C:\Users\Administrator\Desktop\2345.xlsx',fidin,'Sheet1',s);
subplot(2,1,1)
plot(p) %画出这行数据
lgstr=cell(1,2);
%%%%%%%%%%%%%%%%%%%%%求出N=12周期的EMA12%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
N=12;
a=2/(N+1);
%%%%%%%%%%%%求EMA0,以用递推公式求出其他EMA1 EMA2....等%%%%%%%%%%%%%%%%
son0=p(1);
mum0=1;
for i=2:N
son0=son0+((1-a)^(i-1))*p(i); %求出EMA0的分子
mum0=mum0+((1-a)^(i-1)); %求出EMA0的分母
end
EMA0=son0/mum0; %求出EMA0
EMA1=EMA0;
%%%%%%%%%%%%求EMA123..........到数据尾部%%%%%%%%%%%%%%%%%%%%%%%%%%
for j=N+1:length(p) %以上已经计算第一个数据EMA0,因此从第N+1周期开始计算EMA作为有效数据的第二点,前面自动补充为0.加上第一个EMA0,总共有效长度为length(p)-N+1.
EMA0=EMA0+a*(p(j)-EMA0); %EMA(t1)=EMA(t0)+a(p-EMA(t0))以计算EMA(t1)。
EMA(j)=EMA0;
EMA1=[EMA1,EMA(j)];
end
x0=EMA1(1)*ones(1,N-1);
EMA12=[x0,EMA1];
clear N EMA0 EMA1 a suno mumo
%%%%%%%%%%%%%%%%%%%%%求出N=26周期的EMA26,复制以上程序即可%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
N=26;
a=2/(N+1);
%%%%%%%%%%%%求EMA0,以用递推公式求出其他EMA1 EMA2....等%%%%%%%%%%%%%%%%
son0=p(1);
mum0=1;
for i=2:N
son0=son0+((1-a)^(i-1))*p(i); %求出EMA0的分子
mum0=mum0+((1-a)^(i-1)); %求出EMA0的分母
end
EMA0=son0/mum0; %求出EMA0
EMA1=EMA0;
%%%%%%%%%%%%求EMA123..........到数据尾部%%%%%%%%%%%%%%%%%%%%%%%%%%
for j=N+1:length(p) %以上已经计算第一个数据EMA0,因此从第N+1周期开始计算EMA作为有效数据的第二点,前面自动补充为0.加上第一个EMA0,总共有效长度为length(p)-N+1.
EMA0=EMA0+a*(p(j)-EMA0); %EMA(t1)=EMA(t0)+a(p-EMA(t0))以计算EMA(t1)。
EMA(j)=EMA0;
EMA1=[EMA1,EMA(j)];
end
x0=EMA1(1)*ones(1,N-1);
EMA26=[x0,EMA1];
legend('原始图形')
DIF=EMA12-EMA26;
subplot(2,1,2)
plot(DIF,'g')
clear N EMA0 EMA1 a suno mumo
%%%%%%%%%%%%%%%%%%%%%%%%%%求DIF的9点平均,即DEM%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
p=DIF;
N=9;
a=2/(N+1);
%%%%%%%%%%%%求EMA0,以用递推公式求出其他EMA1 EMA2....等%%%%%%%%%%%%%%%%
son0=p(1);
mum0=1;
for i=2:N
son0=son0+((1-a)^(i-1))*p(i); %求出EMA0的分子
mum0=mum0+((1-a)^(i-1)); %求出EMA0的分母
end
EMA0=son0/mum0; %求出EMA0
EMA1=EMA0;
%%%%%%%%%%%%求EMA123..........到数据尾部%%%%%%%%%%%%%%%%%%%%%%%%%%
for j=N+1:length(p) %以上已经计算第一个数据EMA0,因此从第N+1周期开始计算EMA作为有效数据的第二点,前面自动补充为0.加上第一个EMA0,总共有效长度为length(p)-N+1.
EMA0=EMA0+a*(p(j)-EMA0); %EMA(t1)=EMA(t0)+a(p-EMA(t0))以计算EMA(t1)。
EMA(j)=EMA0;
EMA1=[EMA1,EMA(j)];
end
x0=EMA1(1)*ones(1,N-1);
DEM=[x0,EMA1];
hold on
plot(DEM,'r')
legend('DIF ','DEM')
lgstr{1,1} = sprintf('J%d:J%d', fidin-26+1,fidin);
lgstr{1,2} = sprintf('K%d:K%d', fidin-9+1,fidin);
xlswrite('C:\Users\Administrator\Desktop\2345.xlsx',DIF','Sheet1',lgstr{1,1});
xlswrite('C:\Users\Administrator\Desktop\2345.xlsx',DEM','Sheet1',lgstr{1,2});
%%%%%%%%%%%%%%%%%%%%%%%求OSC%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
OSC=DIF-DEM;
hold on
bar(OSC,'k')
请求帮助,数据见附件!
错误提示如下:
Error using xlswrite (line 220)
Excel returned: Error: Object returned error code: 0x800A03EC.
Error in MACD1 (line 115)
xlswrite('C:\Users\Administrator\Desktop\2345.xlsx',DIF','Sheet1',lgstr{1,1});
请各位路过大侠指点!谢谢!
2013-11-18 21:17 上传
点击文件名下载附件
12.69 KB, 下载次数: 6