In this post, we’ll explore a Python pandas package feature. We frequently find queries about converting tick-by-tick data to OHLC (Open, High, Low and Close). Using pandas kit this can be done with minimum effort. The OHLC data is used over a unit of time (1 day, 1 hour etc.) to perform a technical analysis of price movement.
The First Step:
The first step relates to the collection of sample data. Let’s import tick sample tick by tick data. We will use the January data for AUD / JPY (Australian Dollar / Japanese Yen) pair which was downloaded from Pepperstone (an external source) for this tutorial.
Pepperstone offers free historical tick data for specific pairs of currencies. The.csv file contains top of the book, tick-by-tick market data, with millisecond details of fractional pip spreads. For our study, the data is more than adequate.
Steps in Python:
As you can see there is no header to the data. We’ll include the header and programmatically accomplish the necessary mission.
Code: Loading the data.
Data is stored with the name ‘AUDJPY-2016-01.csv’ in the working directory. As we saw earlier, there is no header to the data. We will then add a header to the data when importing it. Importing and adding headers thus occurs in the same line of code.
The resample attribute of a data frame for pandas is used. The resample feature allows standard time-series data to be re-examined. For 15 minutes, we must resample the data and partition it into OHLC format. Using L for milliseconds, U for microseconds, and S for seconds if you want to resample for smaller time frames (milliseconds/microseconds/seconds), etc.
Code: ‘Ask’ Dataframe
Code: ‘Bid’ Dataframe
Code: Merging of ‘ask’ and ‘bid’ dataframe
This is a fast way of using TBT data to compute the OHLC. This can be applied across assets, and based on the OHLC data, one can devise various strategies. We can also plot OHLC-based maps, and generate trade signals. Another way to use the data is to build technical indicators in python, or to calculate risk-adjusted returns.
- Python | Convert list of nested dictionary into Pandas dataframe
- Python | Convert an HTML table into excel
- How to Convert Dataframe column into an index in Python-Pandas?
- How to convert pandas DataFrame into SQL in Python?
- How to Convert Pandas DataFrame into a List?
- Convert given Pandas series into a dataframe with its index as another column on the dataframe
- Python | Pandas Series.astype() to convert Data type of series
- Python | Pandas Split strings into two List/Columns using str.split()
- Python | Pandas Reverse split strings into two List/Columns using str.rsplit()
- How to import an excel file into Python using Pandas?
- Convert CSV to Excel using Pandas in Python
- Convert CSV to HTML Table in Python
- Convert Excel to CSV in Python
- How to convert a Pandas Series to Python list?
- Convert Text File to CSV using Python Pandas
- Split a text column into two columns in Pandas DataFrame
- Join two text columns into a single column in Pandas
- Split a String into columns using regex in pandas DataFrame
- Combine two Pandas series into a DataFrame
- Load JSON String into Pandas DataFrame
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.