日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

akshare写etf动量滚动策略

發布時間:2023/12/19 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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动量滚动策略的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。