Welcome to my algorithmic trading and quantitative finance pages on rpub.
In Euar Sinclair’s book, there is a great material of market making techniques. Here I wrote it in R language, and got similar results.
There are 2 types of orders buy/sell. To buy we need spend money, buy orders are indicated by -1. After we made a sell, we can get back money, so sell orders are indicated by 1.
An order sequence is length 100 of 1/-1 as sell/buy orders received randomly. Sample size is 10000, so there are 10000 random order sequences. As rows of a matrix.
When order is a buy price is reduced by 1, otherwise order is sell price is increased by 1. Conveniently price sequence is only a cumulative sum of order sequence. And initial price 100 needs to add to beginning, the last price is removed.
Offer price is 1 point higher than middle price, bid price is 1 point lower than middle price. So middle price plus order is the bid/offer price, and multiplied by order we can get trading signs even nicer.
We would highly likely to build up inventory with orders do not trade. We need a round of buy and sell to take profit. There are maximum 50 buy and sell rounds. If there are too much buy or sell orders, we can see it by summing up the order sequence as 1/-1 do not cancel out.
And the inventory also has market value as price to change. So we evaluate its market value and compare to our traded value to get profit/loss(PL).
This first code is for Figure 10.4. In the book, average profit/loss is .92, and probability of positive profit is 69%. n=1000000, I have here are 1.072676 and 0.685388.
n=100000 #n=1000000 #20 minutes to run
t=100
raw=sample(c(1,-1),n*t,replace = TRUE,p=c(.5,.5))
orders=matrix(raw,ncol = t)
PL<-function(x){
mids=c(100,100+cumsum(x))
trades=(mids[1:100]+x)*x
inven=sum(x)
invenvalue=mids[100]*inven
pl= sum(trades)-invenvalue
return(pl)
}
profit=apply(orders,1,PL)
h <- hist(profit, breaks = 30, plot=FALSE)
h$counts=h$counts/sum(h$counts)
plot(h)
c(max(profit),min(profit))
## [1] 50 -962
mean(profit)
## [1] 0.89752
sum(profit>0)/n
## [1] 0.68459
The second code is for Figure 10.6. Buy order probability 0.7, sell order probability 0.3. The average profit/loss in the book is -758, I have here -776.3096. I also have probability of loss is >99% here.
n=100000
t=100
raw=sample(c(1,-1),n*t,replace = TRUE,p=c(.3,.7))
orders=matrix(raw,ncol = t)
PL<-function(x){
mids=c(100,100+cumsum(x))
trades=(mids[1:100]+x)*x
inven=sum(x)
invenvalue=mids[100]*inven
pl= sum(trades)-invenvalue
return(pl)
}
profit=apply(orders,1,PL)
h <- hist(profit, breaks = 30, plot=FALSE)
h$counts=h$counts/sum(h$counts)
plot(h)
c(max(profit),min(profit))
## [1] 50 -2914
mean(profit)
## [1] -775.3879
sum(profit>0)/n
## [1] 0.00067