import akshare as ak import pandas as pd start_date='2024/12/01' end_date='2024/12/20' # 获取000300的历史行情数据 df_000300 = ak.index_zh_a_hist(symbol="000300")# 获取sh000852的历史行情数据 df_sh000852 = ak.stock_zh_index_daily(symbol="sh000852") # 获取HSI的历史行情数据 df_HSI= ak.stock_hk_index_daily_sina(symbol="HSI") # 获取IXIC的历史行情数据 df_IXIC = ak.index_us_stock_sina(symbol=".IXIC") df_Au = ak.spot_hist_sge(symbol='Au99.99') df_300etf = ak.stock_zh_index_daily_em(symbol="sh510300") df_1000etf = ak.stock_zh_index_daily_em(symbol="sh512100") df_hz = ak.stock_zh_index_daily_em(symbol="sh513600") # 获取黄金指数的历史行情数据 df_goldetf = ak.stock_zh_index_daily_em(symbol="sz159834") # 获取国债指数的历史行情数据 df_bondetf = ak.stock_zh_index_daily_em(symbol="sh511100") # 获取纳指指数的历史行情数据 df_nzetf = ak.stock_zh_index_daily_em(symbol="sz159509") # 将日期列转换为日期时间类型 dataframes = [df_000300, df_sh000852, df_HSI, df_IXIC, df_Au, df_300etf, df_1000etf, df_hz, df_goldetf, df_bondetf, df_nzetf]# 遍历列表中的每个数据框,将名为'date'或'日期'的列转换为日期时间类型 for df in dataframes:date_column_name = 'date' if 'date' in df.columns else '日期'df[date_column_name] = pd.to_datetime(df[date_column_name]) date_column_names = ['date' if 'date' in df.columns else '日期' for df in dataframes] # 筛选出指定日期范围内的数据 dfs = {'df_sh000852': df_sh000852,'df_000300': df_000300,'df_HSI': df_HSI,'df_IXIC': df_IXIC,'df_Au': df_Au,'df_300etf': df_300etf,'df_1000etf': df_1000etf,'df_hz': df_hz,'df_goldetf': df_goldetf,'df_bondetf': df_bondetf,'df_nzetf': df_nzetf }# 遍历字典中的所有DataFrame,并应用日期过滤 for name, df in dfs.items():# 根据DataFrame中的日期列名称来选择正确的列date_column = 'date' if 'date' in df.columns else '日期'# 应用日期过滤dfs[name] = df[(df[date_column] >= start_date) & (df[date_column] <= end_date)] df_000300 = dfs['df_000300'][['日期', '收盘', '最高', '最低', '成交量']].rename(columns={'日期': 'date','收盘': '000300_close','最高': '000300_high','最低': '000300_low','成交量': '000300_volume' })df_sh000852 = df_sh000852[['date', 'close', 'high', 'low', 'volume']].rename(columns={'close': 'sh000852_close','high': 'sh000852_high','low': 'sh000852_low','volume': 'sh000852_volume' })df_HSI = df_HSI[['date', 'close', 'high', 'low', 'volume']].rename(columns={'close': 'HSI_close','high': 'HSI_high','low': 'HSI_low','volume': 'HSI_volume' })df_IXIC = df_IXIC[['date', 'close', 'high', 'low', 'volume']].rename(columns={'close': 'IXIC_close','high': 'IXIC_high','low': 'IXIC_low','volume': 'IXIC_volume' })df_Au = df_Au[['date', 'close', 'high', 'low']].rename(columns={'close': 'Au_close','high': 'Au_high','low': 'Au_low' })df_300etf = df_300etf[['date','close']].rename(columns={'close': '300etf_close'})df_1000etf = df_1000etf[['date','close']].rename(columns={'close': '1000etf_close'})df_hz = df_hz[['date','close']].rename(columns={'close': 'hz_close'}) df_goldetf = df_goldetf[['date','close']].rename(columns={'close': 'goldetf_close'}) df_bondetf = df_bondetf[['date','close']].rename(columns={'close': 'bondetf_close'}) df_nzetf = df_nzetf[['date','close']].rename(columns={'close': 'nzetf_close'})# Merge the two DataFrames on the 'date' column for n,i in enumerate([df_000300, df_sh000852, df_HSI,df_IXIC,df_Au,df_300etf,df_1000etf,df_hz,df_goldetf,df_bondetf,df_nzetf]):if n == 0:result = ielse:result = pd.merge(result, i, on='date', how='inner')# Display the merged DataFrame result