def download_one(self, symbol): stock_data = pd.DataFrame(columns=['date', 'change_perc1', 'change_perc2', 'open', 'high', 'low', 'close', 'avg', 'volume_match', 'volume_reconcile']) last_page = self.get_last_page(symbol) # logging.info('Last page {}'.format(last_page)) for i in range(last_page): stock_slice_batch = self.download_batch(i+1, symbol) stock_data = pd.concat([stock_data, stock_slice_batch], axis=0) stock_data = stock_data.set_index('date').apply(pd.to_numeric, errors='coerce') stock_data.index = list(map(utils.convert_date, stock_data.index)) stock_data.index.name = 'date' stock_data = stock_data.sort_index() stock_data.fillna(0, inplace=True) stock_data['volume'] = stock_data.volume_match + stock_data.volume_reconcile # Create multiple columns iterables = [stock_data.columns.tolist(), [symbol]] mulindex = pd.MultiIndex.from_product(iterables, names=['Attributes', 'Symbols']) stock_data.columns = mulindex logging.info('data {} from {} to {} have already cloned!' \ .format(symbol, utils.convert_text_dateformat(self.start, origin_type = '%d/%m/%Y', new_type = '%Y-%m-%d'), utils.convert_text_dateformat(self.end, origin_type='%d/%m/%Y', new_type='%Y-%m-%d'))) return stock_data
def download_one(self, symbol): stock_data = pd.DataFrame(columns=[ 'date', 'change_perc1', 'change_perc2', 'open', 'high', 'low', 'close', 'avg', 'volume_match', 'volume_reconcile' ]) for i in range(1000): stock_slice_batch = self.download_batch(i + 1, symbol) stock_data = pd.concat([stock_data, stock_slice_batch], axis=0) try: date_end_batch = stock_slice_batch.date.values[-1] except: # start date is holiday or weekend break is_touch_end = utils.convert_date( self.start, '%d/%m/%Y') == utils.convert_date(date_end_batch, '%d/%m/%Y') # logging.info('batch: {}; start date out range: {}; date_end_batch: {}'.format(i + 1, is_touch_end, date_end_batch)) if is_touch_end: break stock_data['change_perc1'], stock_data[ 'change_perc2'] = stock_data.change_perc.apply( utils.split_change_col).str if 'change_perc' in stock_data.columns: stock_data.pop('change_perc') if 'avg' in stock_data.columns: stock_data.pop('avg') stock_data = stock_data.set_index('date').apply(pd.to_numeric, errors='coerce') stock_data.index = list( map( lambda text: utils.convert_date(text, date_type='%d/%m/%Y' ), stock_data.index)) stock_data.index.name = 'date' stock_data = stock_data.sort_index() stock_data.fillna(0, inplace=True) stock_data[ 'volume'] = stock_data.volume_match + stock_data.volume_reconcile # Create multiple columns iterables = [stock_data.columns.tolist(), [symbol]] mulindex = pd.MultiIndex.from_product(iterables, names=['Attributes', 'Symbols']) stock_data.columns = mulindex logging.info('data {} from {} to {} have already cloned!' \ .format(symbol, utils.convert_text_dateformat(self.start, origin_type = '%d/%m/%Y', new_type = '%Y-%m-%d'), utils.convert_text_dateformat(self.end, origin_type='%d/%m/%Y', new_type='%Y-%m-%d'))) return stock_data
def download_one_new(self, symbol): start_date = utils.convert_text_dateformat(self.start, origin_type='%d/%m/%Y', new_type='%Y-%m-%d') end_date = utils.convert_text_dateformat(self.end, origin_type='%d/%m/%Y', 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) stock_data = data[[ 'date', 'adClose', 'close', 'pctChange', 'average', 'nmVolume', 'nmValue', 'ptVolume', 'ptValue', 'open', 'high', 'low' ]].copy() stock_data.columns = [ 'date', 'adjust', 'close', 'change_perc', 'avg', 'volume_match', 'value_match', 'volume_reconcile', 'value_reconcile', 'open', 'high', 'low' ] stock_data = stock_data.set_index('date').apply(pd.to_numeric, errors='coerce') stock_data.index = list(map(utils.convert_date, stock_data.index)) stock_data.index.name = 'date' stock_data = stock_data.sort_index() stock_data.fillna(0, inplace=True) stock_data[ 'volume'] = stock_data.volume_match + stock_data.volume_reconcile # Create multiple columns iterables = [stock_data.columns.tolist(), [symbol]] mulindex = pd.MultiIndex.from_product(iterables, names=['Attributes', 'Symbols']) stock_data.columns = mulindex logging.info('data {} from {} to {} have already cloned!' \ .format(symbol, utils.convert_text_dateformat(self.start, origin_type = '%d/%m/%Y', new_type = '%Y-%m-%d'), utils.convert_text_dateformat(self.end, origin_type='%d/%m/%Y', new_type='%Y-%m-%d'))) return stock_data
def __init__(self, symbols, start, end, *arg, **karg): self.symbols = symbols self.start = utils.convert_text_dateformat(start, new_type = '%d/%m/%Y') self.end = utils.convert_text_dateformat(end, new_type = '%d/%m/%Y')