phamdinhkhanh / vnquant

VietNam Data Stock Market Price
378 stars 178 forks source link

Cannot clone VN30 or another Indices with "VND" datasource #14

Closed trnhattan closed 3 years ago

trnhattan commented 3 years ago
from vnquant import DataLoader as crawler
loader = crawler.DataLoader(symbols="VN30", start="2021-01-01", end="2021-07-01", minimal=True, data_source="vnd")
temp_df = loader.download()

image

KeyError: "None of [Index(['date', 'adClose', 'close', 'pctChange', 'average', 'nmVolume',\n 'nmValue', 'ptVolume', 'ptValue', 'open', 'high', 'low'],\n dtype='object')] are in the [columns]"

ZzerozZ commented 3 years ago

DataLoader is using old API, it's not work anymore. New class DataLoaderVND may be solving your issue.

Or try this:

def convert_date(text, date_type = '%Y-%m-%d'):
    return datetime.strptime(text, date_type)

def convert_text_dateformat(text, origin_type = '%Y-%m-%d', new_type = '%Y-%m-%d'):
    return convert_date(text, origin_type).strftime(new_type)

id_batch = 1
symbol ='VIC'
start = "2021-01-01"
end= "2021-07-24"

start_date = convert_text_dateformat(start, origin_type = '%Y-%m-%d', new_type = '%Y-%m-%d')
end_date = convert_text_dateformat(end, origin_type = '%Y-%m-%d', new_type = '%Y-%m-%d')
API_VNDIRECT = 'https://finfo-api.vndirect.com.vn/v4/stock_prices/'
query = 'code:' + symbol + '~date:gte:' + start_date + '~date:lte:' + end_date
delta = datetime.strptime(end_date, '%Y-%m-%d') - datetime.strptime(start_date, '%Y-%m-%d')
params = {
    "sort": "date",
    "size": delta.days + 1,
    "page": 1,
    "q": query
}
res = requests.get(API_VNDIRECT, params=params)
data = res.json()['data']  
data = pd.DataFrame(data)

data.tail()
trnhattan commented 3 years ago

DataLoader is using old API, it's not work anymore. New class DataLoaderVND may be solving your issue.

Or try this:

def convert_date(text, date_type = '%Y-%m-%d'):
    return datetime.strptime(text, date_type)

def convert_text_dateformat(text, origin_type = '%Y-%m-%d', new_type = '%Y-%m-%d'):
    return convert_date(text, origin_type).strftime(new_type)

id_batch = 1
symbol ='VIC'
start = "2021-01-01"
end= "2021-07-24"

start_date = convert_text_dateformat(start, origin_type = '%Y-%m-%d', new_type = '%Y-%m-%d')
end_date = convert_text_dateformat(end, origin_type = '%Y-%m-%d', new_type = '%Y-%m-%d')
API_VNDIRECT = 'https://finfo-api.vndirect.com.vn/v4/stock_prices/'
query = 'code:' + symbol + '~date:gte:' + start_date + '~date:lte:' + end_date
delta = datetime.strptime(end_date, '%Y-%m-%d') - datetime.strptime(start_date, '%Y-%m-%d')
params = {
    "sort": "date",
    "size": delta.days + 1,
    "page": 1,
    "q": query
}
res = requests.get(API_VNDIRECT, params=params)
data = res.json()['data']  
data = pd.DataFrame(data)

data.tail()

Thanks for solving my problem!