Đăng ký phân tích thị trường tổng thể
⚠️ Nên dùng API mới: Đăng ký SSE phân tích thị trường mới.
signaldùng thuật ngữ giao dịch quốc tế (LONG/SHORT/SIDEWAYS) và kèmh24Summary,verdict.
URL kết nối:
https://stream.valuescan.ai/stream/market/subscribe
1. Tổng quan
Phân tích thị trường tổng thể là kết quả ValueAgent tổng hợp đa chiều dữ liệu về diễn biến BTC/ETH. Đăng ký thị trường tổng thể là cơ chế máy chủ chủ động đẩy: sau khi người dùng đăng ký, khi tình hình thị trường rộng thay đổi, máy chủ đẩy nội dung phân tích dạng văn bản tới client qua kênh SSE.
2. URL đăng ký
GET https://stream.valuescan.ai/stream/market/subscribeTham số xác thực (Query String):
| Tham số | Kiểu | Bắt buộc | Mô tả |
|---|---|---|---|
apiKey | string | Có | ValueScan Access Key |
sign | string | Có | Chữ ký HMAC-SHA256 |
timestamp | long | Có | Unix timestamp đơn vị mili giây |
nonce | string | Có | Chuỗi ngẫu nhiên (chống replay) |
Thuật toán ký:
sign = HMAC-SHA256(SK, timestamp毫秒 + nonce)Credits consumption
- Cost:
50Credits mỗi 30 phút
3. Định dạng sự kiện SSE
Kết nối thành công
event: connected
data: subscribedHeartbeat (duy trì kết nối)
: heartbeat
event: heartbeat
data: pingMáy chủ gửi heartbeat mỗi 20 giây.
Đẩy phân tích thị trường tổng thể
event: market
data: {"ts":1775604300144,"uniqueId":"market_analysis_20260408103000","content":"BTC dominates the market with high capital concentration; watch BTC price action."}Các trường payload sự kiện market:
| Trường | Kiểu | Mô tả |
|---|---|---|
ts | long | Thời điểm đẩy (timestamp mili giây) |
uniqueId | string | Định danh duy nhất (dùng để khử trùng lặp) |
content | string | Nội dung phân tích thị trường tổng thể dạng văn bản thuần |
4. Ví dụ đăng ký
Python
import hashlib
import hmac
import json
import time
import uuid
import urllib.request
from urllib.parse import urlencode
AK = "your-access-key"
SK = "your-secret-key"
ts = int(time.time() * 1000)
nonce = uuid.uuid4().hex
sign = hmac.new(
SK.encode(),
(str(ts) + nonce).encode(),
hashlib.sha256,
).hexdigest()
params = urlencode({
"apiKey": AK,
"sign": sign,
"timestamp": ts,
"nonce": nonce,
})
url = f"https://stream.valuescan.ai/stream/market/subscribe?{params}"
req = urllib.request.Request(url, headers={"Accept": "text/event-stream"})
with urllib.request.urlopen(req, timeout=300) as resp:
event_name = ""
data_str = ""
for raw in resp:
line = raw.decode("utf-8").rstrip("\r\n")
if line.startswith(":"):
print("♥ Heartbeat")
continue
if line == "":
if data_str:
handle_event(event_name, data_str)
event_name = ""
data_str = ""
elif line.startswith("event:"):
event_name = line[6:].strip()
elif line.startswith("data:"):
data_str = line[5:].strip()
def handle_event(event_name: str, data_str: str) -> None:
if event_name == "connected":
print(f"[Connected] {data_str}")
elif event_name == "market":
payload = json.loads(data_str)
ts = payload["ts"]
content = payload["content"]
dt = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(ts / 1000))
print(f"[Market analysis {dt}] {content}")cURL (kiểm tra nhanh)
python3 -c "
import hashlib, hmac, time, uuid, urllib.parse
SK='your-secret-key'
ts=int(time.time()*1000)
nonce=uuid.uuid4().hex
sign=hmac.new(SK.encode(), (str(ts)+nonce).encode(), hashlib.sha256).hexdigest()
params=urllib.parse.urlencode({'apiKey':'your-access-key','sign':sign,'timestamp':ts,'nonce':nonce})
print(f"curl -N 'https://stream.valuescan.ai/stream/market/subscribe?{params}' -H 'Accept: text/event-stream'")
'"5. Kết nối lại khi mất kết nối
Giống đăng ký tín hiệu, nên triển khai backoff theo cấp số nhân khi kết nối lại:
def listen_with_retry(url: str, max_retries: int = 10) -> None:
retries = 0
while retries <= max_retries:
try:
req = urllib.request.Request(url, headers={"Accept": "text/event-stream"})
with urllib.request.urlopen(req, timeout=300) as resp:
_read_sse(resp)
break
except KeyboardInterrupt:
print("Người dùng ngắt, thoát.")
break
except Exception as e:
retries += 1
wait = min(2 ** retries, 60)
print(f"Mất kết nối ({e}), thử lại lần {retries} sau {wait}s...")
time.sleep(wait)
else:
print("Đã hết số lần thử, thoát.")6. Mã lỗi
Lỗi nghiệp vụ về xác thực, tham số và hạn mức khi thiết lập kết nối trả về HTTP 200 với JSON nghiệp vụ; code trong body cho biết lỗi thực tế.
Ví dụ:
{
"code": 20021,
"message": "Signature verification failed",
"data": null,
"requestId": "a7d7ce3053e140dca91d93df173648f8"
}Mã lỗi phổ biến:
| code | Mô tả |
|---|---|
20001 | Thiếu API Key |
20002 | API Key không hợp lệ, bị vô hiệu, hết hạn hoặc chưa được duyệt |
20010 | Thiếu timestamp |
20011 | Định dạng timestamp sai |
20012 | Timestamp hết hạn |
20020 | Thiếu chữ ký |
20021 | Xác thực chữ ký thất bại |
40001 | Thiếu tham số bắt buộc |
40002 | Định dạng hoặc giá trị tham số không hợp lệ |
60001 | Kích hoạt giới hạn tốc độ |
70001 | OpenApi Credits không đủ |
Sau khi kết nối thiết lập, nếu Credits không đủ, client nhận sự kiện credit-exhausted rồi kết nối đóng:
event: credit-exhausted
data: {"message":"Hạn mức đã hết, vui lòng nạp thêm"}