def index_price_daily(code: str, source: str = None) -> dict: if source == 'tx': df = ak.stock_zh_index_daily_tx(symbol=code) elif source == 'em': df = ak.stock_zh_index_daily_em(symbol=code) else: df = ak.stock_zh_index_daily(symbol=code) df['date'] = pd.to_datetime(df['date']) df = df.rename({'date': '_id'}, axis=1) print(df) return json.loads(df.T.to_json()) # use default date_format='epoch' instead of 'iso'
def get_index_price_25(self, code): # newest_data df = ak.stock_zh_index_spot() # print(df) newest_data = float( pd.to_numeric(df.loc[df[u'代码'] == code, '最新价']).astype('float')) # history_data history_data = ak.stock_zh_index_daily_em(symbol=code) #print(original_data) return (newest_data, history_data.iloc[-25, 2])
def index_price_daily(code: str, source: str = None) -> pd.DataFrame: if source == 'tx': df = ak.stock_zh_index_daily_tx(symbol=code) elif source == 'em': df = ak.stock_zh_index_daily_em(symbol=code) else: df = ak.stock_zh_index_daily(symbol=code) df['date'] = pd.to_datetime(df['date']) df = df.rename({'date': '_id'}, axis=1) print(df) return df
def load_fund_price(self, code): """ 历史行情数据-东方财富 目标地址: http://quote.eastmoney.com/center/hszs.html 描述: 东方财富股票指数数据, 历史数据按日频率更新 限量: 单次返回具体指数的所有历史行情数据 输入参数 名称 类型 必选 描述 symbol str Y symbol="sz399552" :param code: :return: """ df = ak.stock_zh_index_daily_em(symbol="sz399812") return df
def draw(len): stock_zh_index_daily_em_df = ak.stock_zh_index_daily_em(symbol="sh000300") data = stock_zh_index_daily_em_df[-1000:] last_k = data["close"] last_ten = stock_zh_index_daily_em_df[-len:]["close"] last_ten = last_ten / last_ten.max() min = 1000000 index = 0 for i in range(450): temp = last_k[i:i+len] temp = temp / temp.max() current_dist = DtwDist(temp.values, last_ten.values) if current_dist < min : min = current_dist index = i print(data[index:index+len]) plt.plot(stock_zh_index_daily_em_df[-len:]["close"].values, label='The last 10 days') plt.plot(data[index:index + len + 20]["close"].values, label='Match') plt.legend() plt.show()
import akshare as ak import pandas as pd import numpy as np import random import time import os code_num = 50 minYears = 2 # 最小年份 index_code = 'sh000300' # 指数 codes = pd.read_excel('fund_codes.xlsx', index_col=0, dtype={'基金代码': 'object'})['基金代码'] # 获取指数组合index_code的收益率数据 df_M = ak.stock_zh_index_daily_em(symbol=index_code) df_M['updown'] = 0.0 for index, row in df_M.iterrows(): if index > 0: df_M['updown'][index] = (float(row['close']) - float( df_M['close'][index - 1])) / float(df_M['close'][index - 1]) df_M = df_M[['date', 'updown']] # 获取最新Shibor作为无风险收益率 df_rf = ak.rate_interbank(market="上海银行同业拆借市场", symbol="Shibor人民币", indicator="3月", need_page="1") print('无风险利率:' + str(df_rf['利率(%)'][0]) + '%\n') rf = np.power(1 + df_rf['利率(%)'][0] / 100, 1 / 360) - 1 # 转为日利率
def query_his_index_by_code(code='sz399812') -> pd.DataFrame: stock_zh_index_daily_em_df = ak.stock_zh_index_daily_em(symbol=code) stock_zh_index_daily_em_df['code'] = code stock_zh_index_daily_em_df.rename(columns={'代码': 'code'}, inplace=True) return stock_zh_index_daily_em_df