phamdinhkhanh / vnquant

VietNam Data Stock Market Price
378 stars 178 forks source link

Báo lỗi Json decode khi lấy giá của nhiều cổ phiếu #27

Closed pgiaphu closed 3 months ago

pgiaphu commented 1 year ago

Em lấy giá của tất cả cổ phiếu sàn HOSE thì bị lỗi như sau.

lấy danh sách các mã trên sàn HOSE

df = vnstock.listing_companies() all_stock = np.asarray(df[(df.group_code == 'VNINDEX') & (df.ticker.str.len() == 3)]['ticker'])

lấy giá cổ phiếu

loader = dt.DataLoader(symbols=all_stock, start="2022-01-01", end="2022-12-05", minimal=True, data_source="vnd") data = loader.download()


JSONDecodeError Traceback (most recent call last) in 6 minimal=True, 7 data_source="vnd") ----> 8 data = loader.download() 9 data.tail()

6 frames /usr/lib/python3.8/json/decoder.py in raw_decode(self, s, idx) 353 obj, end = self.scan_once(s, idx) 354 except StopIteration as err: --> 355 raise JSONDecodeError("Expecting value", s, err.value) from None 356 return obj, end

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

khanhphamdinh-neurd commented 1 year ago

Mã chứng khoán có thể bị lệch giữa hàm vnstock.listing_company() của bạn và của API của package. Để liệt kê các mã chứng khoán thì có thể sử dụng hàm:

import vnquant as utils
print(utils.get_ind_class())
phamdinhkhanh commented 3 months ago

Bản mới nhất của vnquant đã cho phép bạn download toàn bộ cổ phiếu của một ngành, chẳng hạn ngành dầu khí, thông qua đoạn code như bên dưới:

import vnquant.data as dt
import vnquant as utils
stock_list=utils.get_ind_class()
print(f"Industry: {stock_list['data'][0]['vietnameseName']}")
industry_symbols=stock_list['data'][0]['codeList'].split(',')

loader = dt.DataLoader(
symbols=industry_symbols,
start="2022-01-01",
end="2022-12-05",
minimal=True,
data_source="vnd")
data = loader.download()
data

Kết qủa:

Industry: Dầu Khí
Attributes code  floor basic_price ceiling_price  ... volume_reconcile value_reconcile total_volume   total_value
Symbols     BSR    BSR         BSR           BSR  ...              PSH             PSH          PSH           PSH
date                                              ...                                                            
2022-12-05  BSR  UPCOM        15.0          17.2  ...              0.0             0.0     316300.0  1.981525e+09
2022-12-02  BSR  UPCOM        15.0          17.2  ...              0.0             0.0     217000.0  1.318738e+09
2022-12-01  BSR  UPCOM        14.6          16.7  ...              0.0             0.0     309400.0  1.921285e+09
2022-11-30  BSR  UPCOM        14.3          16.4  ...              0.0             0.0     274400.0  1.665155e+09
2022-11-29  BSR  UPCOM        13.9          15.9  ...              0.0             0.0     368400.0  2.160281e+09
...         ...    ...         ...           ...  ...              ...             ...          ...           ...
2022-01-10  BSR  UPCOM        24.4          28.0  ...              0.0             0.0    1611100.0  3.703546e+10
2022-01-07  BSR  UPCOM        24.0          27.6  ...              0.0             0.0     995100.0  2.392239e+10
2022-01-06  BSR  UPCOM        24.0          27.6  ...              0.0             0.0    1808300.0  4.327210e+10
2022-01-05  BSR  UPCOM        23.7          27.2  ...              0.0             0.0    1517500.0  3.653398e+10
2022-01-04  BSR  UPCOM        23.1          26.5  ...              0.0             0.0    1016200.0  2.332500e+10