In this post, we will explore a feature of Python pandas package. We usually find queries about converting tick-by-tick data into OHLC (Open, High, Low and Close) frequently. This can be accomplished with minimal effort using pandas package. The OHLC data is used for performing technical analysis of price movement over a unit of time (1 day, 1 hour etc.). We have already seen How OHLC data is used to calculate pivot points which traders use to identify key areas where reversal of price movement is possible, using which they can ideate their investment strategy.
The First StepThe first step involves fetching sample data. Let us download sample tick by tick data. For this tutorial, we will use the January data for AUD/JPY (Australian Dollar/Japanese Yen) pair that was downloaded from Pepperstone.
Pepperstone provides free historical tick data for various currency pairs. The .csv file contains top of the book, tick-by-tick market data, with fractional pip spreads in millisecond details. This data is more than sufficient for our analysis.
The data that we downloaded will look like this:
Steps In PythonAs you can see the data is without any header. We will include the header and accomplish the required task programmatically.
Import pandas package
import pandas as pd
Load the dataData is stored in my working directory with a name 'AUDJPY-2016-01.csv'. As we saw earlier, the data is without a header. Hence we would add header to the data while importing it. Thus importing and adding header take place in the same line of code.
data_frame = pd.read_csv('AUDJPY-2016-01.csv', names=['Symbol', 'Date_Time', 'Bid', 'Ask'],
This is how the data frame looks like:-
We use the resample attribute of pandas data frame. The resample attribute allows to resample a regular time-series data. We shall resample the data every 15 minutes and divide it into OHLC format. If you want to resample for smaller time frames (milliseconds/microseconds/seconds), use L for milliseconds, U for microseconds, and S for seconds.
data_ask = data_frame['Ask'].resample('15Min').ohlc() data_bid =data_frame['Bid'].resample('15Min').ohlc()
A snapshot of tick-by-tick data converted into OHLC format can be viewed with the following commands:-
You may concatenate ask price and bid price to have a combined data frame
data_ask_bid=pd.concat([data_ask, data_bid], axis=1, keys=['Ask', 'Bid'])
The Complete Code
import pandas as pd data_frame = pd.read_csv('AUDJPY-2016-01.csv', names=['Symbol', 'Date_Time', 'Bid', 'Ask'], index_col=1, parse_dates=True) data_frame.head() data_ask = data_frame['Ask'].resample('15Min').ohlc() data_bid = data_frame['Bid'].resample('15Min').ohlc() data_ask.head() data_bid.head() data_ask_bid=pd.concat([data_ask, data_bid], axis=1, keys=['Ask', 'Bid'])