If the futures market is defined as a battlefield, then the many and the short side is the soldiers fighting on the battlefield.

Therefore, it is particularly important to keep abreast of the situation on the battlefield. Know yourself and know your enemy, and you can win a hundred battles.

1. First understanding of TICK data

There is a rectangular window in the lower right corner of each futures software. Each tick of the rectangular window is a tick. That is, someone is operating on and flat on the tick. See the picture below:

Beijing: Transaction time

Price: The current price of the latest transaction

Current Deal: Current TICK volume (bilateral)

Spread: in the current tick, long/short open contention results in an increase, decrease, or constant open position in the entire contract

Nature: This needs to consider many aspects to consider, the following to introduce you focus on the introduction

Second, calculate the game quantity of long and short.

In order to calculate the long short position of a variety, the following four steps must be taken:

1, judge and record the open and flat without direction (such as: open, open, double flat, double open, change hands, unknown) a total of six

2, judge and record the direction of the price transaction (such as: up, down, unchanged) in three directions

3, according to the above two steps to judge and record the opening of the belt direction (such as: open, open, open, flat, flat, double open, double flat, multiple change, empty change, unknown open, unknown flat, unknown change)

4. According to the opening with direction recorded in the third step, calculate the current TICK and the change of all data with multiple opening (e.g., multiple opening, air opening, multiple levelling, air levelling, unknown opening, unknown levelling, unknown change)

It only needs to go through the above four steps to calculate the multi-space open level. The following steps will describe the judgment and calculation in detail according to each step, and implement the whole process of calculation in Python.

The preparatory work

1. The software used by the author is Pycharm.

2. Import ‘pandas’ and ‘matplotlib’ into the library.

3. Data preparation:

Import tick data, rB1910. CSV file, and convert the data to DataFrame format. And store the corresponding data in each list (custom list processing).

Import pandas as pd import matplotlib.pyplot as PLT import talib plt.rcparams ['font. Sans-serif ']=['SimHei']# Plt.rcparams ['axes. Unicode_minus ']=False # used to display normal negative sign count = 6000 # multiple space calculation period RB_data_csv =pd.read_csv('RB1910.csv',encoding='UTF-8') RB_data = pd.DataFrame(RB_data_csv) # -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- data cleaning time = check_close = position = [] [] [] volume = close = [] [] sellshort_1 = [] buy_1 = [] for I in range (len (RB_data [' time '])) : Time. Append (RB_data [' time '] [I]) position. The append (RB_data [' holdings'] [I]) volume. Append (RB_data [' now hand] [I]) Close. Append (RB_data [' latest price] [I]) sellshort_1. Append (RB_data [' sell a '] [I]) buy_1. Append (RB_data [' buy a '] [I])Copy the code

Running results:

Note: although there is a live transaction in the above, it can not be used directly because the data derived from a platform is cumulative volume. So we use:

Current deal = Current tick volume – Last tick volume

In the same way:

Spread = Current tick open positions – Previous tick open positions

4, calculation, hand and warehouse difference.

Now_hand.append(volume[i]-volume[i-1])
Entrepot.append(position[i] - position[i - 1])Copy the code

Complete code:

# -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- and now hand calculation storehouse Now_hand = # - now hand Entrepot = [] [] poor trade_type = [] # # - storehouse calculated without the direction of the kaiping (unknown, in hand, double drive, two flat, positions, Trade_type. Append (0) trade_type. Append (I) trade_type. Append (I) trade_type. Now_hand.append(volume[i]-volume[i-1]) Entrepot.append(position[i] - position[i - 1])Copy the code

Let’s print it and see what our data looks like!

Data: data show a tick | -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - | start time: 20190514090001.0 over time: 20190514150000.0 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - | the latest price:, sold for a price 3674.0:3674.0 to buy a price: 3672.0 now hand: the poor: 1716.0 storehouse 156.0 | -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - |Copy the code

Ok! The data for our article has been collected. Let’s start with the first step.

The first step is flattening without direction

Judge and record the open and flat without direction (e.g., open, open, double open, double open, change hands, unknown) altogether six kinds:

As shown in the figure:

Check to see if it is correct: The following is a section of TICK transaction data intercepted by me on a market software, and then I randomly check it according to the 6 methods mentioned above.

The first: present hand 26, position spread +2 nature more open; The second: present hand 6, position difference -2 nature of empty; Get the result according to the above judgment formula: demonstrate two pens, and you can calculate the ones you are interested in. First: position spread >0 and now! = Position spread, judged as' open '. <0 and open + open! =0, judged to be 'flat'. No multi - space directionCopy the code

Code:

for i in range(len(volume)): if i ==0 : Now_hand.append(0) Entrepot.append(0) trade_type.append(i) if i>0 : Now_hand.append(volume[i]-volume[i-1]) Entrepot.append(position[i] - position[i - 1]) if Entrepot[i] == 0 and Trade_hand [I]==0: trade_type. Append (' unknown ') if Entrepot[I]==0 and Now_hand[I]>0: Trade_type. Append (' Entrepot ') if Entrepot[I] > 0 and Now_hand[I] == Entrepot[I]: trade_type. Append (' Entrepot ') Trade_type. Append (' double ') if Entrepot[I] > 0 and Now_hand[I]! = Entrepot [I] : trade_type. Append (' open ') if Entrepot [I] < 0 and Now_hand Entrepot [I] + [I] = = 0: Trade_type. Append (' double ') if Entrepot[I] < 0 and Now_hand[I]+Entrepot[I]! = 0: Print ('trade_type: ', trade_type[0:3]) print('Now_hand: ', Now_hand[0:3]) print('Entrepot: ', Entrepot[0:3])Copy the code

Running results:

And then the next step.

The second step, calculate clinch a deal direction

Determine and record the direction of the price transaction (i.e., up, down, unchanged) in three directions:

Up: Latest price >= Sell price & latest price >= Previous sell price

Down: latest price <= buy price & latest price <= buy price

Unchanged: Others

Code:

# -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the direction trade_direction = [] # - price direction (up, down, the same) for I in range (len (Now_hand) : if I = = 0: Trade_direction.append (0) # trade_direction.append(0) # trade_direction.append(0) # trade_direction.append(0) # trade_direction.append(0) # trade_direction.append(0) # trade_direction. Trade_direction.append (' up ') elif close[I]>= SellShort_1 [I]: trade_direction.append(' up ') else: trade_direction.append(' up ') elif close[I]>= SellShort_1 [I]: trade_direction.append(' up ') else: If close[I] <= buy_1[i-1]: trade_direction. Append (' down ') elif close[I] <= buy_1[i-1]: trade_direction. Append (' down ') else: The same trade_direction. Append (' ')Copy the code

At this point, the second step is complete. Now we can judge the opening level with direction of current TICK transaction by combining the previous two steps. Let’s come together!

The third step, with the direction of the level

Judge and record the opening of the tape direction according to the above two steps (e.g., multiple opening, air opening, multiple opening, air opening, double opening, double opening, multiple changing, air changing, unknown opening, unknown changing, unknown changing)

According to the elaboration in the third step, we can now judge whether the current tick is overopen, short open, overchange, short change and so on.

First, let’s take a look at the formula: please click on the larger image to see.

Open positions:

More open = open position + up; Open = open position + down; Unknown open = open position + unchanged;

Positions:

Long flat = close position + down; Short = open position + up; Open unknown = open position + unchanged;

Changed hands:

More change = change hands + up; Empty change = change hands + down; Unknown change = change hands + unchanged;

Double open:

Double open = double open + up, double open + down, double open + unchanged;

Double flat:

Double flat = double flat + up, double flat + down, double flat + unchanged;

For example:

Stickline (c >0 and c >0 and c >0 and c >0 and C >0 and C >0 and C >0 and C >0 = Position spread) + up (last offer >= last offer or last offer >= last offer)

The method of judgment in parentheses has been described in the previous two steps, which will not be repeated here. If you do not understand, please refer to the first two steps again.

Code:

# -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- with the direction of the kaiping trade_netrue = [] # - with the direction of the kaiping (long-short change, change is unknown; Double open; Double flat; Air flat more flat, unknown flat; Open, additional unknown) for I in range (len (Now_hand) : if I = = 0: trade_netrue. Append (0) # kaiping properties if I > 0: # 'changing hands if trade_type [I] = =' changing hands and trade_direction [I] = = 'up' : Trade_netrue. Append (' ') elif trade_type[I]==' 'and trade_direction[I]=='' ': Trade_netrue. append(' null ') #' elif 'trade_value [I]==' double' and trade_direction[I]==' up ': Trade_netrue. Append (' double ') elif trade_type[I]==' double 'and trade_direction[I]==' down ': Trade_netrue. Append (' double ') elif trade_type[I]==' double 'and trade_direction[I]==' constant ': Trade_netrue. append(' double ') #' double 'elif trade_type[I]==' double' and trade_direction[I]==' up ': Trade_netrue. Append (' double ') elif trade_type[I]==' double 'and trade_direction[I]==' down ': Trade_netrue. Append (' double ') elif trade_type[I]==' double 'and trade_direction[I]==' constant ': Trade_netrue. Append (' trade_netrue ') #' trade_direction 'and trade_direction[I]==' up ': Trade_netrue. Append (' trade_netrue ') elif trade_type[I]==' trade_direction 'and trade_direction[I]==' down ': Trade_netrue.append (' trade_netrue.append ') #' elif [I]==' trade_direction[I]==' up ': Trade_netrue. Append (' open ') elif trade_type[I]==' open 'and trade_direction[I]==' down ': trade_netrue. Append (' open ') else: Trade_direction [I]==' 'and trade_direction[I]=='' Trade_netrue. Append (' x ') elif trade_type[I]==' x 'and trade_direction[I]==' x ': Elif [I]==' open 'and trade_direction[I]==' constant ': trade_netrue. Append (' open ') elif [I]==' open' and trade_direction[I]==' constant ': trade_netrue. Append (' open ')Copy the code

Until now, our third step has been perfectly completed, and we already know whether the current transaction is open or open or other directions.

Because the last step calculation of opening and closing volume is a little round, we will use a separate point to introduce, details please look down.

The fourth step, the calculation of opening and closing volume and visualization

Before we do that, we have to look at the opposition sheet;

Counterorder as understood by the author: that is, the open volume of the opposite side of the active transaction when the current tick occurs. I’ll give you an example, a simple example of what the opposite side is: properties, flat space.

Active transaction: empty square

Opposite side: active side and opposite side (multi flat) = double flat, active side and opposite side (open) = empty change, etc., and the open quantity of the opposite side is what we call the opponent order

1, calculate the opening volume.

To calculate the multiple open volume of all current data, it is necessary to know the multiple open volume contained in a single tick, and then summarize. And I’m going to use a template, and I’m going to do it directly from the template. It’s very simple, we just need to judge the participants by their ‘nature’.

The methods used in this article follow this template as well

s =abs(Entrepot[i]) / 2 o = Now_hand[i] / 2 - sCopy the code

A closer look reveals a pattern:

s =abs(Entrepot[i]) / 2 o = Now_hand[i] / 2 - sCopy the code

The participants who know each tick cannot calculate the real number of counterorders at present. We need one last step. Let the two variables be: S,O. Forget about the “S” and “O”, we’ll explain it later, just follow the steps.

s =abs(Entrepot[i]) / 2 o = Now_hand[i] / 2 - sCopy the code

Code:

s =abs(Entrepot[i]) / 2 o = Now_hand[i] / 2 - sCopy the code

So how do you use these two variables to figure out the real play? Look at the case directly below!

Here’s an example:

--------------------------------------- property: air flat; Opponents: Open air (O), multi flat (S); Active side: air level (hand / 2) property: air open; Opponents: Flat (O), open (S); Active side open (now hand / 2) -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- nature: more flat; Opponents: open (O), flat (S); Active side: Duo ping (present hand / 2) Nature: more open; Opponents: Multi flat (O), open air (S); Active party rolled (now hand / 2) -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- nature: empty change; The empty open (now hand / 2), flat (| | now hand / 2); Nature: multiple change; Open (present hand / 2), flat (present hand / 2); -------------------------------- property: double open; Open (present hand / 2), open (present hand / 2); Property: double flat; Duo Ping (present hand / 2), Air ping (present hand / 2); -------------------------------- nature: unknown; Opponents: unknown; --------------------------------Copy the code

Experiment!

Such as:

Present hand 24, position difference -8, nature of air flat;

Analysis:

Active: Air level single volume = present hand / 2 =12 (hand)

Opposite party empty billing amount = O (now hand / 2 – S) = 8 (hand), more flat single = S (poor | | storehouse / 2) = 4 (hand)

Therefore, the current tick orders are: 12 blanks, 8 blanks and 4 blanks. Reverse position spread: -12-4+8=-8.

Such as:

Present hand 24, warehouse difference 0, the nature of double flat;

Analysis: double flat double open direct hand /2 calculation can be.

Empty flat = present /2=12 hands, multiple flat (opponent) = present /2=12 hands

Therefore, the current tick transaction orders are: 12 lots of empty flat, 12 lots of flat

Now that you’ve covered all the calculations, let’s look at how to calculate the exact open dimensions in Python code.

Code:

The unknown kaiping is recorded by hand (unknown Kaiping is not discussed here).

Elif trade_netrue[I] == 'trade_netrue ': Trade_buytocover.append (Now_hand[I] / 2) # trade_buytocover.append(0) trade_sellShort. Append (0) # trade_buytocover.append(0 trade_sell.append(Now_hand[i] / 2) trade_unknown_exchange.append(0) trade_unknown_entry.append(0) Trade_unknown_exit. Append (0) elif trade_netrue[I] == 'double ': trade_unknown_exit. Append (0) elif trade_netrue[I] ==' double ': Trade_buytocover. Append (Now_hand[I] / 2) trade_sellShort. Append (0) # trade_buytocover trade_sell.append(Now_hand[i] / 2) trade_unknown_exchange.append(0) trade_unknown_entry.append(0) Trade_unknown_exit. Append (0) elif trade_netrue[I] == 'double ': trade_unknown_exit. Append (0) elif trade_netrue[I] ==' double ': Append (Now_hand[I] / 2) # trade_buytocover. Append (0) trade_sellShort. Append (Now_hand[I] / 2) # trade_buytocover trade_sell.append(0) trade_unknown_exchange.append(0) trade_unknown_entry.append(0) trade_unknown_exit.append(0) else: Trade_netrue [I] == 'trade_netrue ': Trade_unknown_entry.append (Now_hand[I]) # trade_unknown_exit.append(0) trade_unknown_exchange.append(0) trade_buy.append(0) trade_buytocover.append(0) trade_sellshort.append(0) trade_sell.append(0) elif trade_netrue[i] == 'Unknown flat ': Trade_unknown_entry.append (Now_hand[I]) # trade_unknown_exchange.append(0) trade_unknown_entry.append(0) trade_buy.append(0) trade_buytocover.append(0) trade_sellshort.append(0) trade_sell.append(0) elif trade_netrue[i] == 'Unknown change ': Trade_unknown_exchange.append (Now_hand[I]) # trade_unknown_entry.append(0) trade_unknown_exit.append(0) trade_buy.append(0) trade_buytocover.append(0) trade_sellshort.append(0) trade_sell.append(0)Copy the code

All the code has been shown, let’s Run to see what it looks like.

Running results:

2. Data visualization.

The last step, is not very excited, don’t worry about the following we will take a look at the multi – short opponent single exactly what it looks like!

First, let’s look at the original counterorder of the tick cycle.

It doesn’t look regular. It looks like it’s thick. Let’s zoom in a little bit.

It’s really hard to see patterns in the original pattern. So the author simply moves the sum, using df.rolling(count).mean() to find the former count and this count =6000 ticks. Get the picture below, there seems to be a pattern, do you see it?

Let’s zoom in.

At this point, we have the whole multi-space open volume calculation has been completely explained, as for how to apply in the quantitative strategy, but also need to find readers themselves, the author is just to realize the process of calculation.

Since the multi-space direction of each tick has been calculated, I think it should not be a problem to calculate the multi-space cost line, which is worth trying. Convert it to minutes, hours, daily line cycles and you may find something new!

Wenyuan network, for learning purposes only, delete.

You will definitely encounter difficulties in learning Python. Don’t panic, I have a set of learning materials, including 40+ e-books, 800+ teaching videos, covering Python basics, crawlers, frameworks, data analysis, machine learning, etc. Shimo. Im/docs/JWCghr… Python Learning Materials

Follow the Python circle and get good articles delivered daily.