Simple R Implementation of Roll's Model
So I've been trying to learn R in order to implement some of the stuff I've been reading in my microstructure books and had my first success today implementing Roll's model of a dealer market.
Background of Roll's model
Roll's model models a market in which all trades go through a dealer, pricing is competitive, and prices follow a random walk. The efficient price (the security's "real" value) is denoted as and is assumed to be a random walk which follows this equation:
Where is a zero-mean random variable (I just used R's built in rnorm function because I'm lazy).
We will call the best dealer quotes at time t: and (bid and ask, respectively). The dealer assumes a long run cost of c per transaction. Since the market is efficient, we can assume the bid and asks are:
Knowing all of this, we can model the transaction price as:
Where is a directional indicator set to +1 if the customer is buying and -1 if the customer is selling. Buys and sells are assumed to be equally likely, serially independent, and that agents buy or sell independent of drift ().
Blue is the bid, red is the ask, and green is the transaction price.
The above is a chart of a security following Roll's model with a starting efficient price of $10 and a c of $1 (Really high, but it makes it easy to see what is going on).
The chart makes it really easy to see some of the cooler conclusions. The bid ask spread in a dealer market is a constant (2c) related to dealer costs (here it's the gap between the blue and red lines) and the bid-ask bounce are readily apparent. (Since green is the transaction price, and trades must occur at the bid or ask, the price fluctuates between the two lines. Some extrapolation from this and you can get the basics of the first high frequency trading strategies.)
##Time of course
t <- 0:50
##Drift in our model
Ut <- rnorm(51)
##Seed Our efficient price function with $10 as starting price of security
Mt <- c(10)
##Generate Efficient Price
for (n in 2:51){
x <- Mt[n-1]
Mt[n] <- x + Ut[n]
}
##Create Directional Indicator Qt
BuyorSell <- c(1, -1)
Qt <- sample(BuyorSell, 51, replace=TRUE)
##Set transaction costs / long run cost of competitive dealer per transaction
c <- 1
##Generate bids
Bt <- vector()
for (n in 1:51){
Bt[n] <- Mt[n] - c
}
##Generate Asks
At <- vector()
for (n in 1:51){
At[n] <- Mt[n] + c
}
##Generate Transaction Price
Pt <- vector()
for (n in 1:51){
Pt[n] <- Mt[n] + (Qt[n] * c)
}
##plot transaction prices V Time
xlim <- range(t)
ylim <- range(c(At, Bt))
plot(t, At, type="l", col="red", xlim=xlim, ylim=ylim, ann=FALSE)
lines(t, Bt, col="blue")
lines(t, Pt, col="green", lty="dashed")
title(main="Roll's Model: Price V Time", xlab="Time", ylab="Price")