> For the complete documentation index, see [llms.txt](https://docs.fiinquant.vn/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.fiinquant.vn/ham-va-cong-thuc/2.-du-lieu-giao-dich/2.5.-ham-theo-doi-huy-lenh.md).

# 2.5. Hàm theo dõi huỷ lệnh

{% hint style="info" %}
Hàm theo dõi hủy lệnh theo nguyên tắc theo dõi giá theo từng bước giá. Xác định khối lượng được thêm hay bớt.
{% endhint %}

<pre class="language-python"><code class="lang-python">from FiinQuantX import FiinSession

username = "REPLACE_WITH_YOUR_USERNAME"
password = "REPLACE_WITH_YOUR_PASSWORD"

client = FiinSession(username=username, password=password).login()

order_book = client.OrderBook()
<strong>
</strong>ticker_config = {
    "VJC": {"min_volume": 100_000},
    "VNM": {"min_volume": 100_000},
    "VPB": {"min_volume": 100_000},
    "VRE": {"min_volume": 100_000}
}

def process_data(data: dict):
    print(data)

# Theo dõi hủy lệnh bất thường
order_book.track_order_book_changes(ticker_config=ticker_config, action="add", accumulate_window=60, callback=process_data)

</code></pre>

| **Tham số**         | **Kiểu dữ liệu** | **Mô tả**                                                                                                 |
| ------------------- | ---------------- | --------------------------------------------------------------------------------------------------------- |
| ticker\_config      | dict             | Từ điển chứa từng mã chứng khoán và ngưỡng khối lượng thay đổi (min\_volume) cần cảnh báo                 |
| side                | str              | 'bid', 'ask' hoặc 'all' – chỉ định hướng theo dõi                                                         |
| action              | str              | 'cancel', 'add' hoặc 'all' – chỉ định loại thay đổi lệnh muốn theo dõi                                    |
| accumulate\_window  | int              | Đơn vị giây. Cho phép user định nghĩa tần suất tính lại để cộng tổng các giá trị hủy lệnh.                |
| min\_action\_volume | int              | Khối lượng cổ phiếu hủy/thêm tối thiểu để được tính là 1 lệnh hủy/thêm. Phục vụ thống kê số lệnh hủy/thêm |

1. Theo dõi bám theo giá cố định:
   * Mỗi mã được theo dõi ở mức giá cụ thể: bid2.price hoặc ask2.price (tùy side) ngay tại thời điểm khởi đầu.
   * Trong quá trình theo dõi, chỉ quan sát khối lượng tại đúng mức giá đó.
2. Ngưng theo dõi khi giá vượt giới hạn số bước giá hiển thị (3 bước/10 bước):
   * Nếu mức giá ban đầu bị đẩy ra khỏi vùng nhìn thấy:
     * Với HOSE: ngoài 3 bước giá tốt nhất (ví dụ: từ *bid1 đến bid3*)
     * Với HNX/UPCoM: ngoài 10 bước giá tốt nhất
   * Hệ thống sẽ ngưng theo dõi ticker đó, nhằm tránh bị nhiễu (noise) do sự thay đổi không thực chất.
3. Xử lý giá bid1 / ask1:
   * Giá bid1 / ask1 sẽ được trừ khối lượng đã khớp để tránh nhầm lẫn là hủy lệnh, khi thực tế là do khớp lệnh.
4. Tính OTR:

$$
OTR = \frac{Số lượng lệnh (đặt, sửa, hủy)}{Số lượng giao dịch thực hiện}
$$

Kết quả:

<figure><img src="/files/p0Mz3ihJUrXEtBy1sPTf" alt=""><figcaption></figcaption></figure>

***

#### Ứng dụng thực tế:

* Phát hiện các dấu hiệu thao túng lệnh như đẩy giả lệnh mua/bán rồi hủy ngay sau đó.
* Phát hiện đột biến lượng lệnh đặt mới bất thường tại các vùng nhạy cảm.
* Phân tích hành vi nhà đầu tư/cá mập trong thời gian thực.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.fiinquant.vn/ham-va-cong-thuc/2.-du-lieu-giao-dich/2.5.-ham-theo-doi-huy-lenh.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
