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 十分感谢)
分别使用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()