Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to work with the orderbook with R "by" function?

Tags:

r

plyr

I have a data (let's call it mydata) with the following data frame.

datetime|side(0=Bid,1=Ask)| distance(1:best price, 2: 2nd best, etc.)| price
2008/01/28,09:11:28.000,0,1,1.6066
2008/01/28,09:11:28.000,0,2,1.6065
2008/01/28,09:11:28.000,0,3,1.6064
2008/01/28,09:11:28.000,0,4,1.6063
2008/01/28,09:11:28.000,0,5,1.6062

2008/01/28,09:11:28.000,1,1,1.6067
2008/01/28,09:11:28.000,1,2,1.6068
2008/01/28,09:11:28.000,1,3,1.6069
2008/01/28,09:11:28.000,1,4,1.6070
2008/01/28,09:11:28.000,1,5,1.6071

I want to calculate minAsk-maxBid, in this case=1.6067-1.6066. I want to do this for my whole data. I was thinking using "by" but even using this simple code:

by(mydata,mydata$datetime, min(mydata$price)) 

to find just the minimum price in each block I get the following error: Error in FUN(X[[1L]], ...) : could not find function "FUN"

Any idea how to implement that? Should I use a different function ddply perhaps?

like image 995
mitra Avatar asked Aug 28 '13 00:08

mitra


People also ask

How do you trade with orderbook?

Order Book – UsesThe order is being bought or sold according to the current market price. Another example is when a trader employs limit order strategies. In such a case, traders can set a certain price level at which they want to buy and sell the security.

How do you read bid ASK order books?

There are two sides of the Order Book. The green buy side (Bid) and the red sell side (Ask). Both display the prices, amounts, and totals. The larger the totals, the larger the green or red colored depth to that area of the order book.


1 Answers

Try

by(mydata,mydata$datetime, function(d)with(d, min(price[side==1])-max(price[side==0])))
like image 125
Ferdinand.kraft Avatar answered Sep 20 '22 12:09

Ferdinand.kraft