Course Name: Python For Trading!, Section No: 4, Unit No: 4, Unit type: Notebook
Hello,
Let's supose that I only have data from an Order Book which contains the following columns: 'created_at' —as the time index—,
'amount' —refers to transaction volume—,
'maker_side' —buy(ask) or sell(bid)—,
'price'
Could you tell us about how to convert ask/bid data (from an given Order Book) to OHLC?
This can also be done using the same resample method. Here, instead of fours columns (OHLC), you have only one column that stores the price at every tick. Let's say you want to convert it into daily data. Try this:
data.resample('1d', how={'price': 'ohlc'})
Can read more about pandas ohlcv resampling here.
Well, that simlifies things a lot but, how accurate is it to mix up 'buy'(ask) and 'sell'(bid) prices? Is there possibly a specific method to handle it?
There's no other ready to use method in pandas for this purpose. When there is a single column in a table for prices, say, the last prices, the resampling becomes much easier. However, when we have a single column containing bids and asks, the mess up is bound to happen. One way to handle is, you filter the bids or asks and perform the resampling to lower frequencies. Another approach is to keep bids and asks as it is, and resample the prices. In this case, pandas will consider both prices and resamples the data accordingly. Sure, there would be some mess up (due to the underlying data), but we can ignore that or live with it.