# 7.4. Khởi tạo OrderBook để thao tác với các hàm đặt lệnh

Kể từ phần này về sau sẽ thao tác thông qua class orderbook, việc khởi tạo orderbook cần thông tin về account\_id(tiểu khoản) và loan\_id (nguồn tiền). Từ đó ta sử dụng orderbook để thao tác với các hàm đặt lệnh, mỗi orderbook sẽ đi kèm với thông tin về mã tiểu khoản và nguồn tiền (loan\_id).

### 7.4.1. Orderbook cho thị trường cơ sở

```python
username_dnse = 'REPLACE_WITH_YOUR_DNSE_USERNAME'
password_dnse = 'REPLACE_WITH_YOUR_DNSE_PASSWORD'

client_order = client.FiinQuantConnector(
    broker='DNSE',
    username=username_dnse,
    password=password_dnse,
    smart_otp=True
).login()

orderbook = client_order.get_orderbook(account_id='0001009212', loan_id='13720')
```

### 7.4.2. Orderbook cho thị trường phái sinh

```python
username_dnse = 'REPLACE_WITH_YOUR_DNSE_USERNAME'
password_dnse = 'REPLACE_WITH_YOUR_DNSE_PASSWORD'

client_order = client.FiinQuantConnector(
    broker='DNSE',
    username=username_dnse,
    password=password_dnse,
    smart_otp=True
).login()

orderbook_der = client_order.get_derivative_orderbook(
    account_id='0001009212',
    loan_id='2278'
)
```

Ở phái sinh có hàm cài chiến lược chốt lời cắt lỗ theo tiểu khoản (nếu muốn).

Cách dùng:

```python
username_dnse = 'REPLACE_WITH_YOUR_DNSE_USERNAME'
password_dnse = 'REPLACE_WITH_YOUR_DNSE_PASSWORD'

client_order = client.FiinQuantConnector(
    broker='DNSE',
    username=username_dnse,
    password=password_dnse,
    smart_otp=True
).login()

orderbook_der = client_order.get_derivative_orderbook(
    account_id='0001009212',
    loan_id='2278'
)

orderbook_der.set_derivative_tp_sl_strategy(
    take_profit_rate = 0.05,
    stop_loss_rate = -0.05
)
```

**Tham số:**

| Tham số                    | Kiểu dữ liệu | Bắt buộc                 | Mô tả                                                                                                                                                              |
| -------------------------- | ------------ | ------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| take\_profit\_rate         | float        | Không *(mặc định = 0.0)* | <p>Tỷ lệ chốt lời (%), tính trên giá vốn. Khi giá tăng đến mức tương ứng, hệ thống sẽ đặt lệnh chốt lời.</p><p>Yêu cầu nếu có: x>0</p>                             |
| stop\_loss\_rate           | float        | Không *(mặc định = 0.0)* | <p>Tỷ lệ cắt lỗ (%), tính trên giá vốn. Khi giá giảm đến mức tương ứng, hệ thống sẽ đặt lệnh cắt lỗ.</p><p>Yêu cầu nếu có: -1<=x<0</p>                             |
| take\_profit\_delta\_price | float        | Không *(mặc định = 0.0)* | <p>Khoảng chênh lệch giá tuyệt đối (theo điểm giá) dùng để xác định mức chốt lời. Ưu tiên sử dụng nếu không dùng take\_profit\_rate.</p><p>Yêu cầu nếu có: x>0</p> |
| stop\_loss\_delta\_price   | float        | Không *(mặc định = 0.0)* | <p>Khoảng chênh lệch giá tuyệt đối (theo điểm giá) dùng để xác định mức cắt lỗ. Ưu tiên sử dụng nếu không dùng stop\_loss\_rate.</p><p>Yêu cầu nếu có: x>0</p>     |

* LƯU Ý: orderbook hay orderbook\_der chỉ là tên biến giả định (user đặt là gì cũng được, chỉ có tên hàm là cố định). Giả định ta đã khởi tạo 2 orderbook này rồi, từ các bước sau sẽ sử dụng 2 biến này cho các ví dụ (orderbook và orderbook\_der).


---

# Agent Instructions: 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:

```
GET https://docs.fiinquant.vn/ham-va-cong-thuc/7.-ham-dat-lenh/7.4.-khoi-tao-orderbook-de-thao-tac-voi-cac-ham-dat-lenh.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
