The secret behind the chart | How to synthesize K-line with Python

In this article, we will teach you how to synthesize K-line using tick data. The tick data we use is a pre-saved excel file:

This is the tick data of the domestic futures variety of rebar RB, including the latest price, selling from one price to five prices, selling one quantity to five quantities, buying one price to buying five prices, buying one quantity to buying five quantities, Data such as daily trading volume, turnover, and open interest.

The tick of domestic futures is not a transaction detail, but a snapshot every half a second, which is the reproduction of the market transaction situation at a certain moment, and the tick data will be pushed only when there is a transaction or a change in the order within 500ms.

Before synthesizing bar with this tick data, we first import the required modules:

Then load the data file in excel format into DataFrame format:

We can see that the first column has a column called "Unnamed: 0", which is because we saved its index when saving the data. Now we don't need this column, we can delete it. We also don't need the 'id', 'duration' columns in the data file:

Calculate the volume of each tick:

Change the format of the datetime2 column to only be accurate to minutes, because we will use this data for grouping later. The column datetime2 is obtained by converting the datetime of int type when we download the data.

Then grouping, we group by minutes, and the data for each minute is divided into a group:

You can see that the grouped minute_grouped variable is not a DataFrame, but a DataFrameGroupBy object.

Finally, calculate the K-line data:

The df variable here already contains the data required by the K-line, including open, high, low, close, volume and time data.

The above is a simple code for synthesizing tick data into 1-minute data. The above code is not optimal, but it basically realizes this function. You can try other methods to synthesize.

If you want to synthesize 1-minute K-line data into K-lines of other time periods such as 5 minutes, 15 minutes, and one hour, a similar method is used.

Guess you like

Origin blog.csdn.net/m0_62038975/article/details/123814423