def get_daily_data(self, code, expire=60 * 6): """ 获取一支股票所有历史数据保存到本地 """ UT.check_dir(CT.DAILY_DIR) file_path = CT.DAILY_DIR + code expired = UT.check_file_expired(file_path, expire) if expired or not os.path.exists(file_path): symbol = self._code_to_symbol(code) start_date = CT.START end_date = date_time.get_today_str() adjust = 'qfq' if is_index(code): d = ak.stock_zh_index_daily(symbol) else: d = ak.stock_zh_a_daily(symbol, start_date, end_date, adjust) if d is None: return d d.to_csv(file_path, sep='\t') if not os.path.exists(file_path): return None #d = pd.read_csv(file_path, sep='\t', index_col=0) d = pd.read_csv(file_path, sep='\t', skiprows=0, parse_dates=True, header=0, index_col=0) return d
def perchase_plan(self): for i in range(len(self.stock_list)): stock = self.stock_list[i] df = ak.stock_zh_index_daily(symbol=stock) money = self.PortWts[i] * self.assets_amount amount = math.floor(money / (df['close'][len(df) - 1] * 100)) * 100 if amount > 0: print('股票%s(%s)购买%s股' % (stock, code_to_name(stock), amount))
def save_index(code: str, mongo=None): if not mongo: mongo = Mongo() if code == 'sh000985': df = AkData.index_price_daily(code, 'em') else: df = ak.stock_zh_index_daily(code) if mongo.has_collection(code): mongo.drop(code) mongo.save(code, df)
def get_daily_data(self, start_date, end_date): df = ak.stock_zh_index_daily(symbol="sh510050") df1 = df.loc[start_date:end_date] print('') print(df1) #dates = df['date'] open1 = df1['open'][start_date] print('open1: {0};'.format(open1)) print('df1[2020-06-01]: {0};'.format(df1.loc[start_date])) '''
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 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 download(self): for type in ['trade','favourite']: tickers = pd.read_csv(self.BASIC + type +'.csv', encoding='gb18030')['tag'].values.tolist() alltickers = tickers.copy() removed_tickers = [] # tickers = list(set(tickers).difference(set([i[:-4] for i in os.listdir(self.AKSHARE + type + '/') ]))) for ticker in tickers: if ticker[:2]=='E_': try: data = ak.fund_etf_hist_sina(symbol=ticker[2:]) data = data.drop_duplicates() data.to_csv(self.AKSHARE + type + '/' + ticker + '.csv', encoding='gb18030', index=False) except : print('Can not download ETF:',ticker) removed_tickers.append(ticker) elif ticker[:2]=='S_': try: data = ak.stock_zh_a_daily(symbol=ticker[2:], adjust="qfq") data.drop(['outstanding_share','turnover'],axis=1,inplace=True) data = data.drop_duplicates() data.to_csv(self.AKSHARE + type + '/' + ticker + '.csv', encoding='gb18030', index=True) except : print('Can not download stock:',ticker) removed_tickers.append(ticker) elif ticker[:2]=='I_': try: data = ak.stock_zh_index_daily(symbol=ticker[2:]) data = data.drop_duplicates() data.to_csv(self.AKSHARE + type + '/' + ticker + '.csv', encoding='gb18030', index=True) except : print('Can not download index:',ticker) removed_tickers.append(ticker) for ticker in removed_tickers: # Remove empty record try: os.remove(raw_path + ticker + '.csv') except: pass if type == 'favourite': data = pd.DataFrame(list(set(alltickers).difference(set(removed_tickers))),columns =['tag']) data.sort_values(by=['tag'],ascending=[True],inplace=True) data.to_csv(self.BASIC + 'favourite.csv', encoding='gb18030',index=False) elif type == 'archive': data = pd.DataFrame(list(set(alltickers).difference(set(removed_tickers))),columns =['tag']) data.sort_values(by=['tag'],ascending=[True],inplace=True) data.to_csv(self.BASIC + 'archive.csv', encoding='gb18030',index=False) return
def download_data(target_stocks): for target_stock in target_stocks: print(target_stock) if target_stock in ex_list: print('Skip: ' + target_stock) continue if os.path.exists('daily_summary/' + target_stock + '.csv'): os.remove('daily_summary/' + target_stock + '.csv') else: print('file not exists: ' + target_stock + '.csv') stock_zh_ah_daily_df = ak.stock_zh_index_daily( symbol=str(target_stock)) # print(stock_zh_ah_daily_df) stock_zh_ah_daily_df.to_csv('daily_summary/' + target_stock + '.csv') time.sleep(6)
def TrackingErrorLimit(upper_limit, benchmark='sz399552', hard=True): """ 追踪误差约束。基准为优化时指定的基准。 :param upper_limit: 追踪误差上界 :param benchmark: 基准(代码格式参照‘sz399552’) :param hard: 软约束/硬约束 """ start_d, end_d = returns.index[0], returns.index[-1] # start = '2019-01-09' try: index_df = ak.stock_zh_index_daily(symbol=benchmark)['close'] except ValueError: print('The symbol is invalid.') exit(1) else: try: start = np.where(index_df.index == datetime.datetime.strptime( start_d, '%Y-%m-%d'))[0][0] end = np.where(index_df.index == datetime.datetime.strptime( end_d, '%Y-%m-%d'))[0][0] if end < len(index_df): bm = index_df[start:end + 1] else: bm = index_df[start:] except ValueError: print( 'The benchmark you choose is invalid with akshare or does not have enough supported data.' ) def cumulative(lst, t): prod = 1 for k in range(t): prod *= lst[t] return prod def fun(omega): TD = [] for i in range(len(returns.columns)): TD.append( cumulative(returns.dot(omega), i) - cumulative(bm, i)) exp = np.mean(TD) TE = np.sqrt(np.sum((TD - exp)**2) / (len(TD) - 1)) return upper_limit - TE constraints.append({'type': 'eq', 'fun': fun}) constraints_hard.append(hard)
def get_stock_index(): # 沪深300 csi300 = ak.stock_zh_index_daily(symbol="sh000300") csi300.columns = columns_name csi300.loc[:, 'gain'] = (csi300['close'] - csi300['open'])/csi300['open']*100 print(csi300.shape) csi300.to_csv(store_path+r".\csi300.csv", index=False, sep=',') # 美国 us500 us500 = ak.index_investing_global_from_url( url="https://www.investing.com/indices/us-spx-500", period="每日", start_date="20060101", end_date="20211224") us500.columns = columns_name us500.loc[:, 'gain'] = (us500['close'] - us500['open']) / us500['open'] * 100 print(us500.shape) us500.to_csv(store_path+r".\us500.csv", index=False, sep=',') # 日本 jp225 jp225 = ak.index_investing_global_from_url( url="https://www.investing.com/indices/japan-ni225", period="每日", start_date="20060101", end_date="20211224") jp225.columns = columns_name jp225.loc[:, 'gain'] = (jp225['close'] - jp225['open']) / jp225['open'] * 100 print(jp225.shape) jp225.to_csv(store_path+r".\jp225.csv", index=False, sep=',') # 英国 uk100 uk100 = ak.index_investing_global_from_url( url="https://www.investing.com/indices/uk-100", period="每日", start_date="20060101", end_date="20211224") uk100.columns = columns_name uk100.loc[:, 'gain'] = (uk100['close'] - uk100['open']) / uk100['open'] * 100 print(uk100.shape) uk100.to_csv(store_path+r".\uk100.csv", index=False, sep=',') # 德国 DAX30 dax30 = ak.index_investing_global_from_url( url="https://www.investing.com/indices/germany-30", period="每日", start_date="20060101", end_date="20211224") dax30.columns = columns_name dax30.loc[:, 'gain'] = (dax30['close'] - dax30['open']) / dax30['open'] * 100 print(dax30.shape) dax30.to_csv(store_path+r".\dax30.csv", index=False, sep=',') # 法国 CAC40 cac40 = ak.index_investing_global_from_url( url="https://www.investing.com/indices/france-40", period="每日", start_date="20060101", end_date="20211224") cac40.columns = columns_name cac40.loc[:, 'gain'] = (cac40['close'] - cac40['open']) / cac40['open'] * 100 cac40.to_csv(store_path+r".\cac40.csv", index=False, sep=',')
def extract_index_records(code='sh000016', start=None, end=None): #code should start with 'sh' or 'sz' result = ak.stock_zh_index_daily(symbol=code) result.index = result.index.tz_localize(None) result['date'] = result.index result = result[['date','open','close','high','low','volume']] result['total'] = 'NaN' result['amp'] = 'NaN' result.columns = ['date','open_price','close_price','high_price','low_price','trancaction','total','amp'] result['rate'] = pd.Series([0]).append(pd.Series((result['close_price'].iloc[1:].values-result['close_price'].iloc[0:-1].values)/result['close_price'].iloc[0:-1].values)).values*100 if start==None and end==None: data_in_target = result else: data_in_target = result[(result['date']<=pd.Timestamp(end)) & (result['date']>=pd.Timestamp(start))] data_in_target['date'] = data_in_target['date'].apply(lambda x: (x.date()-datetime.date(1, 1, 1)).days) data_in_target.index = data_in_target.index.rename('') data_in_target = data_in_target.reset_index() return data_in_target[['date','open_price','close_price','low_price','high_price','trancaction','total','rate','amp']]
ag[short] += df.loc[20,short] ag[long_chg] += df.loc[20,long_chg] ag[short_chg] += df.loc[20,short_chg] stat = (ag[long]+ag[short])/ag[vol] ts = (ag[long_chg]-ag[short_chg])/np.abs(ag[long_chg]+ag[short_chg]) # how to use TS? print(ag) print(stat,ts) return ag if __name__ == "__main__": rank = spider(['AG']) print("------------------------------------------------") aurank = time.time_ns() raw_index_df = ak.stock_zh_index_daily(symbol="sz399300") sma1 = range(20,61,4) sma2 = range(180,281,10) results = pd.DataFrame() for SMA1, SMA2 in product(sma1, sma2): data: DataFrame = pd.DataFrame(raw_index_df['close']) data.dropna(inplace=True) data['returns'] = np.log(data['close'] / data['close'].shift(1)) data['sma1'] = data['close'].rolling(SMA1).mean() data['sma2'] = data['close'].rolling(SMA2).mean() data.dropna(inplace=True) data['position'] = np.where(data['sma1'] > data['sma2'], 1, 0) data['strategy'] = data['position'].shift(1) * data['returns'] data.dropna(inplace=True) perf = np.exp(data[['returns', 'strategy']].sum()) results = results.append(
import matplotlib.dates as mdates df['日期'] = df['日期'].map(mdates.date2num) # 初始化fig, axes fig, ax = plt.subplots(figsize=(25, 8)) ax.plot(df['日期'], df['收盘'], label='十年期国债收益率') ax.xaxis_date() ax.xaxis.set_major_locator(years) ax.xaxis.set_minor_locator(months) ax.xaxis.set_major_formatter(years_fmt) ax.set_ylabel(ylabel='到期收益率', fontproperties=fontprop) ax.grid(True) # 沪深300数据 stock_df = ak.stock_zh_index_daily(symbol="sh000300") stock_df.reset_index(inplace=True) stock_df['date'] = stock_df['date'].map(mdates.date2num) # 初始化fig, axes ax2 = ax.twinx() ax2.plot(stock_df['date'], stock_df['close'], color='red', alpha=0.8, label='沪深300') ax2.set_ylabel(ylabel='沪深300', fontproperties=fontprop) from datetime import datetime updated_at = datetime.now().strftime('%Y-%m-%d') fig.suptitle('中国十年期国债收益率 v.s. 沪深300\n公众号:结丹记事本儿,更新于{}'.format(updated_at), fontproperties=fontprop,
def index_a_daily(code, *args, **kwargs): '''return stock a index daily data for given code symbol to latest datetime ''' code = _convert_code(ak.stock_zh_index_daily)(code) return ak.stock_zh_index_daily(code, *args, **kwargs)
def beat_benchmark_ratio(fCode, cPeriod=0, sDate='', eDate=''): # 排除非法参数: 1) cPeriod与(sDate或eDate)同时出现 2) 有sDate无eDate 3) 有eDate无sDate if (cPeriod and (sDate or eDate)) or (sDate and not eDate) or (eDate and not sDate): raise ValueError("Illegal Argument") # 用akshare获取:开放式基金-历史数据 fInfo = ak.fund_em_open_fund_info(fund=fCode, indicator="单位净值走势") # 舍弃无用的数据包 fInfo = fInfo[["净值日期", "单位净值"]] # 计算收益率为多少 rate = [(fInfo["单位净值"][i] - fInfo["单位净值"][i - 1]) / (fInfo["单位净值"][i - 1]) for i in range(1, len(fInfo))] fInfo = fInfo[1:] fInfo["rate"] = rate # 用cPeriod参数:选出历史数据区间 if cPeriod: fInfo = fInfo[-cPeriod:].reset_index(drop=True) # 用sDate和eDate参数:选出历史数据区间 elif sDate and eDate: f_sDate, f_eDate = datetime.strptime( sDate, "%Y-%m-%d").date(), datetime.strptime(eDate, "%Y-%m-%d").date() fInfo = fInfo.loc[(fInfo["净值日期"] >= f_sDate) & (fInfo["净值日期"] <= f_eDate)].reset_index(drop=True) # 舍弃无用的数据包 fInfo = fInfo["rate"] # 用akshare获取:沪深300-历史数据 benchmark = ak.stock_zh_index_daily(symbol="sh000300") # 把date转化为column而不是作为index,方便后面的历史数据区间的选中 benchmark = benchmark.reset_index().rename({'index': 'date'}, axis='columns') benchmark["date"] = benchmark["date"].apply( lambda x: x.to_pydatetime().date()) # 舍弃无用的数据包 benchmark = benchmark[["date", "close"]] # 计算收益率为多少 rate = [(benchmark["close"][i] - benchmark["close"][i - 1]) / (benchmark["close"][i - 1]) for i in range(1, len(benchmark))] benchmark = benchmark[1:] benchmark["rate"] = rate # 用cPeriod参数:选出历史数据区间 if cPeriod: benchmark = benchmark[-cPeriod:].reset_index(drop=True) # 用sDate和eDate参数:选出历史数据区间 elif sDate and eDate: b_sDate, b_eDate = datetime.strptime( sDate, "%Y-%m-%d").date(), datetime.strptime(eDate, "%Y-%m-%d").date() benchmark = benchmark.loc[(benchmark["date"] >= b_sDate) & ( benchmark["date"] <= b_eDate)].reset_index(drop=True) # 舍弃无用的数据包 benchmark = benchmark["rate"] # 计算区间内收益率击败沪深300的比例 benchmark = [ 1 if fInfo[i] > benchmark[i] else 0 for i in range(len(fInfo)) ] benchmark_ratio = Counter(benchmark)[1] / len(benchmark) return "{:.2%}".format(benchmark_ratio)
} fieldDict_stock = { '开盘价': 'open', '收盘价': 'close', '最高价': 'high', '最低价': 'low', '成交量': 'volume', '换手率': 'turnover' } codes = allList[0].split(',') for code in codes: if code[:3] == 'zh_': index_code = code.split('_')[1] try: df_temp = ak.stock_zh_index_daily(symbol=index_code).reset_index() df_target = pd.DataFrame() df_target['date'] = df_temp['date'].apply( lambda x: x.strftime('%Y-%m-%d')) for text in allList[1:]: if text in fieldDict_index: col_name = fieldDict_index[text] df_target[text] = df_temp[col_name] df_indexes[index_code] = df_target except: continue else: stock_code = ('sh' if code[0] == '6' else 'sz') + code try: if '后复权' in allList[1:]: df_temp = ak.stock_zh_a_daily(symbol=stock_code,
def get_daily_data(self, start_date, end_date): df = ak.stock_zh_index_daily(symbol="sh510050") return df.loc[start_date:end_date]
df = macro_china_gksccz_df[::-1][ macro_china_gksccz_df['operation_from_date'] > last_date] es_bulk(df, gen_macro_china_gksccz_doc) # 获取Shibor隔夜利率数据 last_date = getLastRecordDateInEs_pyfy_rate_interbank(es) page = '100' if last_date == '1970-01-01' else '5' rate_interbank = ak.rate_interbank(market="上海银行同业拆借市场", symbol="Shibor人民币", indicator="隔夜", need_page=page) df = rate_interbank[rate_interbank['日期'] > last_date] es_bulk(df, gen_rate_interbank_doc) # 获取上证指数 df = ak.stock_zh_index_daily(symbol="sh000001") last_date = getLastRecordDateInEs(es, "pyfy_stock_zh_index_daily") es_bulk(df[df.index > last_date], gen_stock_zh_index_daily_doc) # 美国10年期国债 last_date = getLastRecordDateInEs(es, "pyfy_bond_investing_global_us_10") df = ak.bond_investing_global(country="美国", index_name="美国10年期国债", period="每日", start_date=last_date, end_date=time.strftime('%Y-%m-%d', time.localtime())) es_bulk(df[df.index > last_date], gen_bond_investing_global_us_10_doc) # bitcoin last_date = getLastRecordDateInEs(es, "pyfy_crypto_hist_bitcoin")
#!/usr/bin/python # -*- coding: UTF-8 -*- """ @author:Administrator @file:20201209.py @time:2020/12/09 """ # 示例 import pandas as pd import akshare as ak # 列名与数据对其显示 pd.set_option('display.unicode.ambiguous_as_wide', True) pd.set_option('display.unicode.east_asian_width', True) # 显示所有列 pd.set_option('display.max_columns', None) # 显示所有行 pd.set_option('display.max_rows', None) stock_zh_index_daily_df = ak.stock_zh_index_daily(symbol="sz399552") with open('stock_zh.txt', 'w') as f: f.write(str(stock_zh_index_daily_df))