# Venice market, version 6.1, 23/4/03 # Copyright 2002 Marco LiCalzi and Paolo Pellizzari comment <- "benchmark case as described in paper" # initialize price <- 1000 howmanystock <- c(1,1,1,1,1) howmany <- c(256,64,16,4,1)*5 cumhm <- cumsum(howmany) #cumulative howmany numagent <- sum(howmany) #number of agents in the simulation v <- runif(numagent,960,1040) cash <- rep(0,numagent) stock <- rep(0,numagent) stock[1:howmany[1]] <- howmanystock[1] for(i in 2:length(howmany)) stock[(cumhm[i-1]+1):cumhm[i]] <- howmanystock[i] # observe that initial cash is worth double stock cash <- stock*price*2 lb <- 300 # length of book h <- trunc(rexp(numagent,1/249)-249)+1 #horizon exponentially distributed print(summary(h)) r <- 0.02 # riskfree rate of return per year gam <- runif(numagent,0,0.03) # premium in percent per year flag <- rep(1,numagent) #all alive, 1 means active, <0 waiting to revive index <- 1:numagent epochs <- 3000 closets <- rep(0,epochs) avgts <- rep(0,epochs) volume <- rep(0,epochs) maxts <- rep(0,epochs) mints <- rep(0,epochs) meanvts <- rep(0,epochs) meanvbookts <- rep(0,epochs) avgprice <- 0 maxprice <- price minprice <- price buybook <- as.data.frame(matrix(0,ncol=3,nrow=lb)) colnames(buybook) <- c("ind","price","q") sellbook <- as.data.frame(matrix(0,ncol=3,nrow=lb)) colnames(sellbook) <- c("ind","price","q") ord <- as.data.frame(t(c(0,0,0))) colnames(ord) <- c("ind","price","q") # init book with orders having zero quantity (just to have non empty books) # ord is a variable to contain order checksellbook <- function(ord,b){ i <- 1 if (ord$price0 && i<=lb){ if (b$price[i]<=ord$price && b$q[i]>0.5) { qexchanged <- min(ord$q,b$q[i]) #q exchanged ord$q <<- ord$q-qexchanged #adjust quantity of order b$q[i] <<- b$q[i]-qexchanged #adjust quantity of book cash[b$ind[i]] <<- cash[b$ind[i]] + qexchanged*b$price[i] #increase cash of book (that is selling) stock[b$ind[i]] <<- stock[b$ind[i]] - qexchanged #decrease q of book cash[ord$ind] <<- cash[ord$ind] - qexchanged*b$price[i] #decrease cash of the owner of buy order stock[ord$ind] <<- stock[ord$ind] + qexchanged price <<- b$price[i] #transaction occurred, hence new price ntrans <<- ntrans + 1 avgprice <<- avgprice + price totexch <<- totexch+qexchanged if (price>maxprice) maxprice <<- price if (price0){ buybook[lb,] <<- ord #insert in last position buybook <<- buybook[rev(order(buybook$price)),] # rearrange book } } checkbuybook <- function(ord,b){ i <- 1 if (ord$price>b$price[1]) i<- lb+1 #price too high to sell while(ord$q>0 && i<=lb){ if (b$price[i]>=ord$price && b$q[i]>0.5) { qexchanged <- min(ord$q,b$q[i]) ord$q <<- ord$q-qexchanged b$q[i] <<- b$q[i]-qexchanged cash[b$ind[i]] <<- cash[b$ind[i]] - qexchanged*b$price[i] #decrease cash of book stock[b$ind[i]] <<- stock[b$ind[i]] + qexchanged #increase q of book cash[ord$ind] <<- cash[ord$ind] + qexchanged*b$price[i] #increase cash of owner of order stock[ord$ind] <<- stock[ord$ind] - qexchanged #decrease q of the owner price <<- b$price[i] #transaction occurred, hence new price ntrans <<- ntrans + 1 avgprice <<- avgprice + price totexch <<- totexch+qexchanged if (price>maxprice) maxprice <<- price if (price0){ sellbook[lb,] <<- ord#inserimento all'ultimo posto sellbook <<- sellbook[order(sellbook$price),] # rearrange book } } # book structure: one row for each order # # buy book sorted with decreasing prices # sell book sorted with increasing prices # # ind: agent that submits order # price: limit price # q: quantity plot(0,t="n",xlim=c(0,epochs),ylim=c(900,1100)) print(date()) for(time in 1:epochs){ perm <- sample(1:numagent,numagent,replace=F) v<-v[perm] cash<-cash[perm] stock<- stock[perm] h<-h[perm] gam<-gam[perm] flag<-flag[perm] buybook <- as.data.frame(matrix(0,ncol=3,nrow=lb)) colnames(buybook) <- c("ind","price","q") sellbook <- as.data.frame(matrix(0,ncol=3,nrow=lb)) colnames(sellbook) <- c("ind","price","q") sellbook$price <- rep(10000,lb) #the book is erased, a brand new day is beginning! avgprice <- 0 ntrans<-0 totexch <-0 maxprice <- price minprice <- price for(j in 1:numagent){ if (time==h[j]){ ord[1,] <- c(j,price,stock[j]) if (ord$q>0) {checkbuybook(ord,buybook); points(time,price,pch=19,col=2)} h[j] <- -20 } temp <- (h[j]-time)/250 if (time0 && abs(ord$price/price)-1< 0.10) checksellbook(ord,sellbook) #trying to buy, hence look at sellbook temprice <- ceiling(v[j]/(r*temp+1)) ord[1,] <- c(j,temprice,stock[j]) if (ord$q>0 && abs(ord$price/price)-1< 0.10) checkbuybook(ord,buybook) #trying to sell, hence look at buybook } } #end j #looking at inactive agents h[h<0] <- h[h<0]+1 v[h==0] <- price*(1+(runif(sum(h==0))-0.5)/5) #adjust v around price h[h==0] <- time+1+trunc(rexp(sum(h==0),1/249)) trunc(rexp(sum(h==0),1/249)) if (ntrans>0) avgts[time]<- avgprice/ntrans else avgts[time] <- price points(time,avgts[time],pch=19,col=4) lines(c(time,time),c(maxprice,minprice)) points(time,meanvts[time],col=2,pch=19) closets[time] <- price volume[time] <- totexch maxts[time] <- maxprice;if (time%%10==0)print(c(time,price,volume[time])) mints[time] <- minprice meanvts[time] <- weighted.mean(v[h>0],cash[h>0]+stock[h>0]*avgts[time]) if (sum(cash<0)>0) {save.image(file="innatural_stop.Rimage");stop()} if (time%%200==1) save.image(file="vm6.Rimage") } #time print(date()) save.image(file="vm6.Rimage")