Skip to content

Instantly share code, notes, and snippets.

@yanring
Created May 21, 2017 11:32
Show Gist options
  • Save yanring/623cfd58c444f0e8d0042aa130e53c89 to your computer and use it in GitHub Desktop.
Save yanring/623cfd58c444f0e8d0042aa130e53c89 to your computer and use it in GitHub Desktop.
begintime='2014-01-01';
endtime='2017-01-01';
total_value = 1000000;
name=['YNBY','GLDQ','WLY','SQJT','WHHX','GTDL','ZSYH','ZGPA','GTJA','DEEJ']';
YNBY=w.wsd('000538.SZ','LOW,OPEN,CLOSE,HIGH',begintime,endtime); %调入云南白药股票四数据
GLDQ=w.wsd('000651.SZ','LOW,OPEN,CLOSE,HIGH',begintime,endtime); %调入格力电器数据
num=size(YNBY,1); %得到总历史数据个数
data =[];%所有股票的数据
a=[0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1]; %总仓位配比,为云南白药30%,格力电器30%,现金40%
p=[1,1,1]; %三者初始成本价格均为1,即云南白药单位一份1,格力电器单位一份1,现金单位一份为1
c(1)=YNBY(1,2); %云南白药成本价格
c(2)=GLDQ(1,2); %格力电器成本价格
u=0.1;
d=0.1;
item_val=total_value*a;%每个股票以及现金的价值,a
for n=1:size(a,2)-1
cost(n)=data(1,3*(n-1)+2);%每个股票的成本价
end
summary=zeros(num,size(a,2));%存每天闭盘后每只股票投了多少钱,第11列是包括现金的总金额
for i=1:num
a=item_val./sum(item_val);%更新占比
for n=1:size(a,2)-1
if data(i,3*(n-1)+3)/cost(n)>(1+u)&a(n)>0.01%如果第二天第n股的价格满足卖出条件
item_val(n)=item_val(n)*data(i,3*(n-1)+3)/cost(n);%算现在股票值多少钱
item_val(11)=item_val(11)+item_val(n)*(0.1);%算现金价值
item_val(n)=item_val(n)*(0.9);%算股票价值
cost(n) = data(i,3*(n-1)+1);%更新成本价
elseif data(i+1,3*(n-1)+1)/data(i,3*(n-1)+1)>(1-d)&a(n)>0.01%如果第二天第n股的价格满足买入出条件
item_val(n)=item_val(n)*data(i,3*(n-1)+1)/cost(n);%算现在股票值多少钱
item_val(11)=item_val(11)-item_val(n)*(0.1);%算现金
item_val(n)=item_val(n)*(0.9);
cost(n) = data(i,3*(n-1)+1);%更新成本价
end
summary(i,n)=data(i,3*(n-1)+4)/cost(n)*item_val(n);
end
summary(i,11)=sum(i,1:10)+item_val(11);%总共金额
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment