Big Data 13 min read

Tick Data Analysis: Calculating Multi‑Short Positions with Python

This article explains how to interpret each tick in futures market data, compute directional open‑close positions, and visualize multi‑short and multi‑long volumes using Python libraries such as pandas, matplotlib, and TA‑Lib, providing a step‑by‑step tutorial with full source code.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Tick Data Analysis: Calculating Multi‑Short Positions with Python

The article treats tick data as a battlefield where each tick represents a trade action, emphasizing the importance of understanding both sides (long and short) in real‑time market analysis.

It introduces the basic fields of a tick (time, price, volume, position change, and nature) and outlines a four‑step method to calculate multi‑short and multi‑long volumes:

Identify and record non‑directional open/close actions (e.g., opening, closing, double opening, double closing, turnover, unknown).

Determine price direction (up, down, unchanged) based on comparisons between the latest price and the best bid/ask.

Combine the results of steps 1 and 2 to assign directional open/close labels (e.g., long open, short close, unknown).

Calculate the actual open/close volumes and visualize the results.

Preparation includes using PyCharm, importing pandas , matplotlib , and talib , and loading the tick CSV file into a DataFrame. The following code shows the initial data loading and cleaning:

<code>import pandas as pd
import matplotlib.pyplot as plt
import talib
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
count = 6000
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.append(RB_data['时间'][i])
    position.append(RB_data['持仓量'][i])
    volume.append(RB_data['现手'][i])
    close.append(RB_data['最新价'][i])
    sellshort_1.append(RB_data['卖一'][i])
    buy_1.append(RB_data['买一'][i])
</code>

Next, the script computes the per‑tick hand volume ( Now_hand ) and position change ( Entrepot ) by differencing successive ticks:

<code>Now_hand = []
Entrepot = []
for i in range(len(volume)):
    if i == 0:
        Now_hand.append(0)
        Entrepot.append(0)
    else:
        Now_hand.append(volume[i] - volume[i-1])
        Entrepot.append(position[i] - position[i-1])
</code>

Using these arrays, the script classifies each tick’s non‑directional type ( trade_type ) with conditional logic, then determines price direction ( trade_direction ) and finally assigns a directional type ( trade_netrue ) such as "多开" (long open), "空平" (short close), "双开" (double open), etc.

<code># Example of directional classification
if trade_type[i] == '换手' and trade_direction[i] == '向上':
    trade_netrue.append('多换')
elif trade_type[i] == '开仓' and trade_direction[i] == '向上':
    trade_netrue.append('多开')
# ... (other conditions omitted for brevity)
</code>

Finally, the article shows how to translate the directional labels into actual volume series for long buys, short sells, covers, and unknown actions, and presents visualizations of the aggregated multi‑short/long volumes over the trading day.

Overall, the tutorial provides a complete pipeline—from raw tick CSV to cleaned DataFrame, through stepwise classification, to volume calculation and plotting—enabling quantitative analysts to extract market micro‑structure signals from high‑frequency futures data.

pandasfinancial analysismarket microstructuretick data
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.