-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathHanson_LogMarketScoreRule_VerySimpleV1.r
149 lines (114 loc) · 3.62 KB
/
Hanson_LogMarketScoreRule_VerySimpleV1.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
143
144
145
146
source("standardFunctions.R")
###########################
## SIMULATORS #############
############################
Bidders <- function(n=10,prob=.8,sd=.1){
#given number of biddors, and info about their distribution of prior probabilities,
# returns a list of biddors with certain priors, and some cash to use
result <- rbind(
data.frame(
Time = 0,
Prob = 0,
Cash = 0
),
data.frame(
Time = round(runif(n,0,9999), 0),
Prob = rnorm(n,prob,sd),
Cash = rexp(n,0.5)
))
result = result %>%
dplyr::mutate(
Prob = ifelse(Prob > 1, yes = 1, no = Prob),
Prob = ifelse(Prob < 0, yes = 0, no = Prob)
)
return(
result[with(result, order(Time)),]
)
}
##########################################
# simulator one ----
# poeple have prior, if cost of new share is lowe rthan prior, buy until out of cash
# set base, fee, size, mean and distribution of people'e priors
base = 10
fee = 0.01
Dat <- Bidders(100,.8,0.1)
system.time({
Dat$Buy = NA
Dat$q1 = 0
Dat$q2 = 0
Dat$spend = 0
Dat1 = Dat
Book = data.frame(
Time = 0,
q1 = 0,
q2 = 0,
Income = 0,
ImpProb = 0.5,
base = base,
fees_toMrkMaker = 0,
type = NA
)
for (i in 2:nrow(Dat)){
Dati = Dat[i,]
CurrBook = tail(Book,1)
#buy or sell?
if ((TraderPay(CurrBook$base,CurrBook$q1,CurrBook$q2,1) + fee) < Dati$Prob){
action = "Buy"
} else {
action = "Sell"
}
##############
##############
# Need function for intelligent trader, see's price and cash, adn acts appropriately....
if (action == "Buy"){
while( Dati$Cash >= (TraderPay(CurrBook$base,CurrBook$q1,CurrBook$q2,1) + fee)
& ((TraderPay(CurrBook$base,CurrBook$q1,CurrBook$q2,1) + fee) < Dati$Prob)){
Dati$q1 = Dati$q1 + 1
Dati$spend = Dati$spend + (TraderPay(CurrBook$base,CurrBook$q1,CurrBook$q2,1) + fee)
Dati$Cash = Dati$Cash - (TraderPay(CurrBook$base,CurrBook$q1,CurrBook$q2,1) + fee)
CurrBook$base = CurrBook$base + (fee/2)
CurrBook$q1 = CurrBook$q1 + 1
CurrBook$fees_toMrkMaker = CurrBook$fees_toMrkMaker + (fee/2)
}
} else {
while(Dati$Cash >= (TraderPay(CurrBook$base,CurrBook$q2,CurrBook$q1,1) + fee)
& ((TraderPay(CurrBook$base,CurrBook$q1,CurrBook$q2,1) + fee) > Dati$Prob)){
Dati$q2 = Dati$q2 + 1
Dati$spend = Dati$spend + (TraderPay(CurrBook$base,CurrBook$q2,CurrBook$q1,1) + fee)
Dati$Cash = Dati$Cash - (TraderPay(CurrBook$base,CurrBook$q2,CurrBook$q1,1) + fee)
CurrBook$base = CurrBook$base + (fee/2)
CurrBook$q2 = CurrBook$q2 + 1
CurrBook$fees_toMrkMaker = CurrBook$fees_toMrkMaker + (fee/2)
}
}
CurrBook <- CurrBook %>%
dplyr::mutate(
Time = Dati$Time[1],
Income = (Income + Dati$spend[1]),
ImpProb = Price1(tail(CurrBook$base,1), q1, q2),
type = as.character(action)
)
if (Dati$spend == 0) CurrBook$type = NA
#update book
Book = rbind(
Book,
CurrBook)
Dati$Buy = action
Dat[i,] = Dati
}
MarketResult <- function(Dat=Dat, Book1=Book, Result=T){
# Result = T if option one, q1, pays
BookEnd <- Book1[dim(Book1)[1],]
if (Result){
print("Option 1 pays")
print(paste("Total Payment", BookEnd$q1[1]))
print(paste("Income", BookEnd$Income[1] - BookEnd$q1[1]))
print(paste("Fees", BookEnd$fees_toMrkMaker[1]))
}
}
})
Dat1
Dat
Book
plot(Book$ImpProb, type = "l", ylim = c(0,1))
MarketResult()