📄 fangzhengushi.m
字号:
agent=struct( 'pf',[0],'stock',[0],'stocksell',[0],'g',[-1],'capital0',[0],'capital1',[0],'pb',[],'ps',[]);
% 定义一个构架变量;
n0=100;
z0=1200;
k0=5000;
stock0=100000;
a=abs(normrnd(0,1,n0,1));
p=zeros(z0,1);
count7=0;
count8=0;
s=zeros(n0,1);
p(1)=20; % 初始价格
for t=2:z0
for i=1:n0
agent(i).capital0=(a(i)/sum(a))*k0;
end
b=abs(normrnd(0,1,n0,1));
%给每个投资者随机分配资金;
for i=1:n0
agent(i).stock=round((b(i)/sum(b))*stock0);
end
% 给每个投资者随机分配股票;
count1=0;
count2=0;
count5=0;
count6=0;
for i=1:n0
q=unifrnd(-0.5,0.5,1,1);
if t>4
n=t;
for m=(n-4):n
count6=count6+p(m);
if count6<=100
q=0.01;
end
end
count5=0;
end
if t>4
n=t;
for m=(n-4):n
count5=count5+p(m);
if count5>180
q=-0.01;
end
end
count5=0;
end
r1=unifrnd(-0.1,0,1,1);
if q>0.0
agent(i).pf=unifrnd(p(t-1),p(t-1)*1.3,1,1);
if ((agent(i).pf-p(t-1))/(p(t-1)))>=0.1
agent(i).g=1;
count1=count1+1;
agent(i).pb=((p(t-1)+agent(i).pf)/2)+unifrnd(r1,0,1,1);
elseif ((agent(i).pf-p(t-1))/(p(t-1)))<0.1
agent(i).g=0;
count2=count2+1;
agent(i).ps=((p(t-1)+agent(i).pf)/2)+unifrnd(0,-r1,1,1);
end
end
if q<=0.0
agent(i).pf=unifrnd((p(t-1)*0.7),p(t-1),1,1);
if ((p(t-1)-agent(i).pf)/p(t-1))>=0.2
agent(i).g=0;
count2=count2+1;
agent(i).ps=((p(t-1)+agent(i).pf)/2)+unifrnd(0,-r1,1,1);
elseif ((p(t-1)-agent(i).pf)/p(t-1))<0.2
agent(i).g=1;
count1=count1+1;
agent(i).pb=((p(t-1)+agent(i).pf)/2);
end
end
end
% 分别产生投资者的买卖价格;
j=1;
l=1;
e=zeros(count1,1);
f=zeros(count2,1);
for i=1:n0
if agent(i).g==1
e(j)=agent(i).pb;
j=j+1;
end
end
for i=1:n0
if agent(i).g==0
f(l)=agent(i).ps;
l=l+1;
end
end
% 分别利用数组存储买卖双方投资者的出价策略;
for i=1:(j-1)
for x=1:(j-2)
if e(x)>e(x+1)
h=e(x);
e(x)=e(x+1);
e(x+1)=h;
end
end
end
for i=1:(l-1)
for x=1:(l-2)
if f(x)>f(x+1)
h=f(x);
f(x)=f(x+1);
f(x+1)=h;
end
end
end
% 将买卖双方的价格升序排列;
count3=0;
count4=0;
for i=1:(j-1)
if e(i)<=max(f)
count3=count3+1;
end
end
for i=1:(l-1)
if f(i)>=min(e)
count4=l-i;
break;
end
end
% 找出买卖双方出价的交集;
sum1=0;
sum2=0;
s=(mean(f))*sum(f)/(sum(f)+sum(e))+mean(e)*sum(e)/(sum(f)+sum(e));
if (s-p(t-1))/p(t-1)>=0.5
p(t)=p(t-1)*1.5;
elseif ((s-p(t-1))/p(t-1)<=-0.2)&((s-p(t-1))/p(t-1)>=-0.5)
p(t)=p(t-1)*0.65;
elseif (s-p(t-1))/p(t-1)<=-0.5
p(t)=p(t-1)*0.5;
else
p(t)=s;
end
% if (count3~=0&count4~=0)
% for i=count4:(l-1)
% sum1=sum1+f(i);
% end
% for i=1:count3
% sum2=sum2+e(i);
% end
% p(t)=(sum1+sum2)/(count3+l-count4-1);
% else
% end
% 确定成交价格;
sum3=0;
sum4=0;
v=rand(n0,1)/3;
for i=1:(l-1)
if agent(i).g==0
if agent(i).ps<p(t)
agent(i).stocksell=fix(agent(i).stock*v(i));
elseif agent(i).ps==p(t)
agent(i).stocksell=fix(agent(i).stock*0.6*v(i));
end
sum3=sum3+agent(i).stocksell;
end
end
% 卖方的卖出策略 ;
w=rand(n0,1)/3;
for i=1:(j-1)
if agent(i).g==1
if agent(i).pb>p(t)
agent(i).capital1=agent(i).capital0*w(i);
elseif agent(i).pb==p(t)
agent(i).capital1=agent(i).capital0*0.6*w(i);
end
sum4=sum4+agent(i).capital1;
end
end
% 买方的买进策略;
if sum3>round(sum4/p(t))
r=round(sum4/p(t));
for i=1:(l-1)
if agent(i).g==0
if agent(i).ps<=p(t)
agent(i).stock=agent(i).stock -fix((agent(i).stocksell/sum3)*r);
agent(i).capital0=agent(i).capital0+p(t)*(fix((agent(i).stocksell/sum3)*r));
end
end
end
for i=1:(j-1)
if agent(i).g==1
if agent(i).pb>=p(t)
agent(i).stock=agent(i).stock+fix(r*(agent(i).capital1/sum4));
agent(i).capital0=agent(i).capital0-fix(r*(agent(i).capital1/sum4))*p(t);
end
end
end
end
% 供给大于需求时的买卖双方成交的情况;
if sum3<round(sum4/p(t))
for i=1:(l-1)
if agent(i).g==0
if agent(i).ps<p(t)
agent(i).stock=fix(agent(i).stock*(1-v(i)));
agent(i).capital0=agent(i).capital0+p(t)*(fix(agent(i).stock*v(i)));
elseif agent(i).ps==p(t)
agent(i).stock=fix(agent(i).stock(1-0.6*v(i)));
agent(i).capital0=agent(i).capital0+p(t)*(fix(agent(i).stock*0.6*v(i)));
end
end
end
for i=1:(j-1)
if agent(i).g==1
if agent(i).pb>p(t)
agent(i).stock=fix(agent(i).stock+sum3*(agent(i).capital1/sum4));
agent(i).capital0=agent(i).capital0-(fix(sum3*(agent(i).capital1/sum4)))*p(t);
elseif agent(i).pb==p(t)
agent(i).stock=fix(agent(i).stock+sum3*(agent(i).capital1/sum4));
agent(i).capital0=agent(i).capital0-(fix(sum3*(agent(i).capital1/sum4)))*p(t);
end
end
end
end
% 供给小于需求时的买卖双方成交的情况;
if sum3>round(sum4/p(t))
count7=count7+1;
elseif sum3<round(sum4/p(t))
count8=count8+1;
end
if ((count7-count8)>0)&(rem((count7-count8),8)==0)
p(t)=p(t)*(1-0.015);
elseif ((count8-count7)>0)&(rem((count8-count7),5)==0)
p(t)=p(t)*(1+0.15);
end
end
plot(p);
xlabel('迭代周期数');
ylabel('成交价格');
title('股市价格变动');
legend('股票价格');
e=zeros(n0,1);
f=zeros(n0,1);
for i=1:n0
e(i)=agent(i).capital0;
f(i)=agent(i).stock;
end
figure;
plot(e,'-o');
figure;
plot(f,'-*');
hold off;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -