gjy499 2021-09-01 00:21 采纳率: 78.6%
浏览 99
已结题

用tushare数据球每日收益率

import matplotlib.pyplot as plt
import tushare as ts
import pandas as pd
from concurrent.futures import ThreadPoolExecutor
#df=pd.readcLipboard()
def get_data(tick):
return ts.get_k_data(tick, start='2020-09-01', end='2021-06-30')
with ThreadPoolExecutor(max_workers=3) as ex:
res=ex.map(get_data,['002254','002224','000507'])
for df in res:
try:
x = df['date']
y = df['close']
plt.plot(x, y)
except:
pass
plt.tight_layout()
plt.show()
使用已有数据,以收盘价作为标准,计算三只股票2020年9月1日到2021年6月30日的每日收益率,即(今日收盘价-昨日收盘价)/昨日收盘价,以及累积收益率。最后将累积收益率以折线图形式可视化。(请附上code 十分感谢)

  • 写回答

1条回答 默认 最新

  • CSDN专家-HGJ 2021-09-01 00:45
    关注

    分别使用shift函数和 cumsum()函数来完成计算。实现代码如下:

    import matplotlib.pyplot as plt
    import tushare as ts
    import pandas as pd
    from concurrent.futures import ThreadPoolExecutor
    #df=pd.readcLipboard()
    def get_data(tick):
        return ts.get_k_data(tick, start='2020-09-01', end='2021-06-30')
    ticks = ['002254', '002224', '000507']
    with ThreadPoolExecutor(max_workers=3) as ex:
        res=ex.map(get_data,ticks)
        for i,df in enumerate(res):
            #df.to_excel(f'stock_{ticks[i]}.xlsx',index=False)
            x = df['date']
            y = (df['close']-df['close'].shift(1))/df['close'].shift(1)
            ys=y.cumsum()
            plt.plot(x, ys,label=ticks[i])
    plt.legend()
    plt.tight_layout()
    plt.show()
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 9月1日
  • 已采纳回答 9月1日
  • 创建了问题 9月1日

悬赏问题

  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序