Skip to content

Commit ce83f81

Browse files
committed
Cập nhật API url và payload khi truy xuất dữ liệu giá lịch sử - quote history từ VCI
1 parent 567b03e commit ce83f81

File tree

2 files changed

+44
-6
lines changed

2 files changed

+44
-6
lines changed

vnstock/explorer/vci/const.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# _BASE_URL = 'https://mt.vietcap.com.vn/api/'
22
_TRADING_URL = 'https://trading.vietcap.com.vn/api/'
3-
_CHART_URL = 'chart/OHLCChart/gap'
3+
_CHART_URL = 'chart/OHLCChart/gap-chart'
44
_INTRADAY_URL = 'market-watch'
55
_GRAPHQL_URL = 'https://trading.vietcap.com.vn/data-mt/graphql'
66

vnstock/explorer/vci/quote.py

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,28 +79,66 @@ def history(self, start: str, end: Optional[str]=None, interval: Optional[str]="
7979
ticker = self._input_validation(start, end, interval)
8080

8181
start_time = datetime.strptime(ticker.start, "%Y-%m-%d")
82-
82+
8383
# Calculate end timestamp
8484
if end is not None:
8585
end_time = datetime.strptime(ticker.end, "%Y-%m-%d") + pd.Timedelta(days=1)
8686
if start_time > end_time:
8787
raise ValueError("Thời gian bắt đầu không thể lớn hơn thời gian kết thúc.")
8888
end_stamp = int(end_time.timestamp())
8989
else:
90-
end_stamp = int((datetime.now() + pd.Timedelta(days=1)).timestamp())
90+
end_time = datetime.now() + pd.Timedelta(days=1)
91+
end_stamp = int(end_time.timestamp())
9192

92-
start_stamp = int(start_time.timestamp())
9393
interval_value = self.interval_map[ticker.interval]
9494

95+
# Tính count_back nếu chưa truyền vào
96+
if count_back is None:
97+
# Chuẩn hóa interval_value về 3 loại gốc của VCI
98+
# _INTERVAL_MAP = {'1m' : 'ONE_MINUTE', '5m' : 'ONE_MINUTE', '15m' : 'ONE_MINUTE', '30m' : 'ONE_MINUTE',
99+
# '1H' : 'ONE_HOUR', '1D' : 'ONE_DAY', '1W' : 'ONE_DAY', '1M' : 'ONE_DAY'}
100+
interval = ticker.interval.lower()
101+
if interval in ["1d", "1w", "1m"]:
102+
# Dùng ONE_DAY
103+
delta = end_time.date() - start_time.date()
104+
count_back = delta.days
105+
if interval == "1w":
106+
count_back = count_back // 7
107+
elif interval == "1m":
108+
count_back = (end_time.year - start_time.year) * 12 + (end_time.month - start_time.month)
109+
elif interval in ["1h"]:
110+
# Dùng ONE_HOUR
111+
total_hours = int((end_time - start_time).total_seconds() // 3600)
112+
count_back = total_hours
113+
elif interval in ["1m", "5m", "15m", "30m"]:
114+
# Dùng ONE_MINUTE
115+
total_minutes = int((end_time - start_time).total_seconds() // 60)
116+
if interval == "1m":
117+
count_back = total_minutes
118+
elif interval == "5m":
119+
count_back = total_minutes // 5
120+
elif interval == "15m":
121+
count_back = total_minutes // 15
122+
elif interval == "30m":
123+
count_back = total_minutes // 30
124+
else:
125+
count_back = total_minutes
126+
else:
127+
# fallback: 30
128+
count_back = 30
129+
if count_back <= 0:
130+
count_back = 1
131+
95132
# Prepare request
96133
url = self.base_url + _CHART_URL
97134
payload = {
98135
"timeFrame": interval_value,
99136
"symbols": [self.symbol],
100-
"from": start_stamp,
101-
"to": end_stamp
137+
"to": end_stamp,
138+
"countBack": count_back
102139
}
103140

141+
104142
# Use the send_request utility from api_client
105143
json_data = send_request(
106144
url=url,

0 commit comments

Comments
 (0)