-
Notifications
You must be signed in to change notification settings - Fork 9
/
interact.R
142 lines (117 loc) · 5.03 KB
/
interact.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
Interact <- function (
g
){
######
## Useful variables, reset, initialize
######
n.secu <- length((V(g)$secu)[[1]])
n.traders <- length(V(g))
V(g)$buy.order <- rep(0,n.traders)
V(g)$sell.order <- rep(0,n.traders)
# n.approx <- length(unique(V(g)$approx))
############
######
## Market interactions
######
############
# Draw a random order in which traders will come to the market
turns <- sample(1:n.traders, n.traders, replace = FALSE)
for (i in turns){
# Player i places a buy order on the order book
V(g)$buy.order[i] <- 1
# Player i places a sell order on the order book, if she owns
# a positive amount of some security
if (V(g)$sell.which[i] > 0){ # 0 is the code for sell.which
V(g)$sell.order[i] <- 1 # corresponding to a trader not selling
}
########
### Complete deals if possible
########
### Buying ###
# Determine who the trader can buy from
potential.sellers <- which ( V(g)$sell.order == 1
# who has a pending sell order
& V(g)$sell.which == V(g)$buy.which[i]
# and sells the security i wants to buy
& V(g)$sell.price < V(g)$buy.price[i]
# at a price lower than the max i is willing to pay
)
potential.sellers <- potential.sellers[potential.sellers != i]
# i cannot sell to herself
### Realize trades if possible ###
if(length(potential.sellers) > 0){
# determine minimum selling price among potential sellers
min.price <- min(V(g)$sell.price[potential.sellers])
# pick a potential seller who sells at min price.
almost.seller <- potential.sellers[V(g)$sell.price[potential.sellers] == min.price]
if (length(almost.seller) == 1){
seller <- almost.seller
} else {
if (length(almost.seller) > 1) {
seller <- sample(almost.seller,1)
} else stop("almost.seller must contain at least one seller")
}
#Safeguard
if (V(g)$sell.order[seller] != 1){
stop("Some buyer is going to buy a security from a agent who does not sell")
}
# pay for the security
V(g)$money[[seller]] <- V(g)$money[[seller]] + min.price
V(g)$money[[i]] <- V(g)$money[[i]] - min.price
# transfer the security
the_one <- V(g)$buy.which[i]
V(g)$secu[[seller]][the_one] <- V(g)$secu[[seller]][the_one] - 1
V(g)$secu[[i]][the_one] <- V(g)$secu[[i]][the_one] + 1
# remove orders
V(g)$sell.order[[seller]] <- 0
V(g)$buy.order[[i]] <- 0
}
### Selling ###
# Determine who the trader can sell to
potential.buyers <- which ( V(g)$buy.order == 1
# who has a pending buy order
& V(g)$buy.which == V(g)$sell.which[i]
# and buys the security i wants to sell
& V(g)$buy.price > V(g)$sell.price[i]
# at a price lower than max i is willing to pay
)
potential.buyers <- potential.buyers[potential.buyers != i]
# i cannot buy from herself
# Realize trade if possible
if(length(potential.buyers) > 0){
# determine max buying price
max.price <- max(V(g)$buy.price[potential.buyers])
# pick a potential buyer who buys at max price
almost.buyer <- potential.buyers[V(g)$buy.price[potential.buyers] == max.price]
if (length(almost.buyer) == 1) {
buyer <- almost.buyer
} else{
if (length(almost.buyer) > 1) {
buyer <- sample(almost.buyer,1)
} else stop("almost.buyer must contain at least one seller")
}
#Safeguard
if (V(g)$buy.order[buyer] != 1){
stop("Some seller is going to sell a security to an agent who does not buy")
}
# pay for the security
V(g)$money[[buyer]] <- V(g)$money[[buyer]] - max.price
V(g)$money[[i]] <- V(g)$money[[i]] + max.price
# transfer the security
V(g)$secu[[buyer]][V(g)$sell.which[i]] <- V(g)$secu[[buyer]][V(g)$sell.which[i]] + 1
V(g)$secu[[i]][V(g)$sell.which[i]] <- V(g)$secu[[i]][V(g)$sell.which[i]] - 1
# remove orders
V(g)$buy.order[[buyer]] <- 0
V(g)$sell.order[[i]] <- 0
}
}
# Reset buying and selling choices
V(g)$buy.which <- rep(0,n.traders)
V(g)$sell.which <- rep(0,n.traders)
V(g)$buy.price <- rep(0,n.traders)
V(g)$sell.price <- rep(0,n.traders)
#######
## Return the network
#######
g
}