Hướng dẫn đặt lệnh giao dịch trên Entrade bằng API
· 3 min read
Bài viết này hướng dẫn cách sử dụng Python để gửi lệnh giao dịch chứng khoán trên nền tảng Entrade bằng API.
Bước 1: Đăng nhập để lấy JWT Token
và Trading Token
Trước khi có thể đặt lệnh, bạn cần lấy JWT Token
và Trading Token
. Nếu chưa có, hãy tham khảo bài viết:
Hướng dẫn lấy JWT Token và Trading Token
Bước 2: Cấu trúc API đặt lệnh
API đặt lệnh sử dụng POST
request với endpoint:
https://services.entrade.com.vn/dnse-order-service/v2/orders
Dưới đây là đoạn mã Python để thực hiện việc đặt lệnh:
import requests
import json
# Thay thế bằng thông tin thực tế của bạn
jwt_token = "your_actual_jwt_token_here"
trading_token = "your_actual_trading_token_here"
account_id = "your_actual_account_id_here"
loan_package_id = 1037 # Thay thế bằng ID gói vay thực tế của bạn
# URL API đặt lệnh
url = 'https://services.entrade.com.vn/dnse-order-service/v2/orders'
# Headers yêu cầu
headers = {
'Authorization': f'Bearer {jwt_token}',
'Content-Type': 'application/json',
'Trading-Token': trading_token
}
# Dữ liệu đặt lệnh
order_data = {
"symbol": "ACB", # Mã chứng khoán
"side": "NB", # NB: Mua, NS: Bán
"orderType": "ATC", # Loại lệnh: ATC, LO, MP, v.v.
"price": 100000, # Giá đặt lệnh (VNĐ)
"quantity": 100, # Số lượng đặt
"loanPackageId": loan_package_id, # ID gói vay (hoặc null nếu không dùng)
"accountNo": account_id # Số tài khoản giao dịch
}
# Gửi yêu cầu đặt lệnh
response = requests.post(url, headers=headers, data=json.dumps(order_data))
# Kiểm tra phản hồi từ API
if response.status_code in [200, 201]:
print("Lệnh đặt thành công:", response.json())
else:
print(f"Lỗi khi đặt lệnh. Mã lỗi: {response.status_code}")
print(response.text)
Dưới đây là đoạn mã Python để thực hiện việc đặt lệnh:
```python
import requests
import json
# Thay thế bằng thông tin thực tế của bạn
jwt_token = "your_actual_jwt_token_here"
trading_token = "your_actual_trading_token_here"
account_id = "your_actual_account_id_here"
loan_package_id = 1037 # Thay thế bằng ID gói vay thực tế của bạn
# URL API đặt lệnh
url = 'https://services.entrade.com.vn/dnse-order-service/v2/orders'
# Headers yêu cầu
headers = {
'Authorization': f'Bearer {jwt_token}',
'Content-Type': 'application/json',
'Trading-Token': trading_token
}
# Dữ liệu đặt lệnh
order_data = {
"symbol": "ACB", # Mã chứng khoán
"side": "NB", # NB: Mua, NS: Bán
"orderType": "ATC", # Loại lệnh: ATC, LO, MP, v.v.
"price": 100000, # Giá đặt lệnh (VNĐ)
"quantity": 100, # Số lượng đặt
"loanPackageId": loan_package_id, # ID gói vay (hoặc null nếu không dùng)
"accountNo": account_id # Số tài khoản giao dịch
}
# Gửi yêu cầu đặt lệnh
response = requests.post(url, headers=headers, data=json.dumps(order_data))
# Kiểm tra phản hồi từ API
if response.status_code in [200, 201]:
print("Lệnh đặt thành công:", response.json())
else:
print(f"Lỗi khi đặt lệnh. Mã lỗi: {response.status_code}")
print(response.text)