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 Step
The 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 Python
As 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 data
Data 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'], index_col=1, parse_dates=True)
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'])
This was a quick way of computing the OHLC using TBT data. This can be applied across assets and one can devise different strategies based on the OHLC data. We can also plot charts based on OHLC, and generate trade signals. Some other ways in which the data can be used is to build technical indicators in python or to compute risk-adjusted returns.
In our post, learn Turtle Trading using Python. We have coded the crux of this strategy and traded on stocks such as Apple Inc., Kinder Morgan Inc., and Ford Motor Company. We have explained the core of the turtle trading strategy which is to take a position on futures on a 55-day breakout.
Disclaimer: All investments and trading in the stock market involve risk. Any decisions to place trades in the financial markets, including trading in stock or options or other financial instruments is a personal decision that should only be made after thorough research, including a personal risk and financial assessment and the engagement of professional assistance to the extent you believe necessary. The trading strategies or related information mentioned in this article is for informational purposes only.