akshare写etf动量滚动策略
生活随笔
收集整理的這篇文章主要介紹了
akshare写etf动量滚动策略
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
導入包:
import akshare as ak import pandas as pd import numpy as np import matplotlib日線換周線:
#日線換為周線數據 def transferToWeekLine(df,period='W'):data1=dfstock_data = pd.DataFrame(data1)#設定轉換周期period_type 轉換為周是'W',月'M',季度線'Q',五分鐘'5min',12天'12D'stock_data["date"] = pd.to_datetime(stock_data["date"])period_type = periodstock_data.set_index('date',inplace=True)#進行轉換,周線的每個變量都等于那一周中最后一個交易日的變量值period_stock_data = stock_data.resample(period_type).last()#周線的volume和money等于那一周中volume和money各自的和period_stock_data['chg_pct'] = stock_data['chg_pct'].resample(period_type).last()#股票在有些周一天都沒有交易,將這些周去除period_stock_data = period_stock_data[period_stock_data['chg_pct'].notnull()]period_stock_data.reset_index(inplace=True)data = np.array(period_stock_data) #先將數據框轉換為數組data_list = data.tolist() #其次轉換為列表for i in data_list:i[0]=str(i[0]).split(" ")[0]return data_list獲得etf列表
#etf基本數據 fund_etf_fund_daily_em_df = ak.fund_etf_fund_daily_em() print(fund_etf_fund_daily_em_df)獲取etf歷史行情
#獲取etf行情 #策略1,etf輪動現象的直觀表征:相對強弱 ind = pd.DataFrame()fund_etf_fund_daily_em_df = ak.fund_etf_fund_daily_em() for i in range(len(fund_etf_fund_daily_em_df[:])):print(fund_etf_fund_daily_em_df.iloc[i,0])fund_etf_fund_info_em_df = ak.fund_etf_fund_info_em(fund=fund_etf_fund_daily_em_df.iloc[i,0], start_date="20000101", end_date="20500101")fund_etf_fund_info_em_df['code'] = fund_etf_fund_daily_em_df.iloc[i,0]fund_etf_fund_info_em_df.rename(columns={'凈值日期':'date','日增長率':'chg_pct'},inplace=True)fund_etf_fund_info_em_df = pd.DataFrame(transferToWeekLine(fund_etf_fund_info_em_df,'W'))fund_etf_fund_info_em_df.rename(columns={0:'date',3:'chg_pct',6:'code'},inplace=True)fund_etf_fund_info_em_df = fund_etf_fund_info_em_df[['date','chg_pct','code']]fund_etf_fund_info_em_df['ret'] = fund_etf_fund_info_em_df['chg_pct'].shift(-1)ind = ind.append(fund_etf_fund_info_em_df)繪圖:?
ind = ind.sort_values(by='date') last = pd.DataFrame() l = [] #獲取每個交易周的行業指數,并買入排名前五,(均值買入),并計算持倉一個禮拜的收益。 for i in ind['date'].unique():d = ind.loc[ind['date']==i].sort_values('chg_pct',ascending=True).head(20)l = (l+[d.ret.mean()/100]) pd.DataFrame(l).cumsum().plot()總結
以上是生活随笔為你收集整理的akshare写etf动量滚动策略的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 前端笔试题小结(一)
- 下一篇: mac地址和ip地址的区别(转)