akshare获取etf及lof的数据
AI量化实验室 玩法
2
帖子
1
发布者
130
浏览
-
获取ETF数据, symbol是fund code, start_date:起始日期
def get_fund_quotes(symbol: str, start_date: str):
code = symbol[:6]
if not start_date:
start_date = '20050101'delta = 1 from zoneinfo import ZoneInfo # Python 3.9+ tz = ZoneInfo('Asia/Shanghai') current_hour = datetime.now(tz).hour logger.debug(f'current_hour:{current_hour}') if current_hour > 18: delta = 0 # end_date = datetime.now().strftime('%Y%m%d') end_date = (datetime.now() - timedelta(days=delta)).strftime('%Y%m%d') logger.debug(f'更新到最新日期:{end_date},delta:{delta}') if '16' != code[:2]: df = ak.fund_etf_hist_em(symbol=code, period="daily", start_date=start_date, end_date=end_date, adjust="hfq") else: df = ak.fund_lof_hist_em(symbol=code, period="daily", start_date=start_date, end_date=end_date, adjust="hfq") cols = {'日期': 'date', '开盘': 'open', '收盘': 'close', '最高': 'high', '最低': 'low', '成交量': 'volume', '换手率': 'turn_over', '成交额': 'amount'} df.rename(columns=cols, inplace=True) df['date'] = df['date'].apply(lambda x: x.replace('-', '')) df = df[list(cols.values())] df['symbol'] = symbol return df