获取宁德时代从2018年到2023年的股票价格数据:
import tushare as ts
# 设置tushare token
ts.set_token("e24da88a1094c6df42a125dd28932ec3c81ca64eaf462a1c1ec0d85")
# 创建tushare数据接口
pro = ts.pro_api()
# 获取宁德时代股票数据
df = pro.daily(ts_code='300750.SZ', start_date='20180601', end_date='20230319')
# 将数据保存为CSV文件
df.to_csv('Ningde data.csv', index=False)
导入tushare库并使用token爬取宁德时代的数据,并将其保存为csv文件,以便接下来使用。
描述宁德时代的价格变化情况
import pandas as pd
import matplotlib.pyplot as plt
# Ningde Times,读取文件
path = r'C:\Users\35033\Desktop\Ningde data.csv'
df = pd.read_csv(path)
df = pd.read_csv(path, parse_dates=['trade_date'], dayfirst=True)
df['daily_return'] = df['close'].pct_change()
#Ningde Timesprice
plt.plot(df['trade_date'], df['close'], color='dodgerblue')
plt.title('Ningde Times Price')
plt.xlabel('Date')
plt.ylabel('Price')
plt.gca().spines['right'].set_visible(False)
plt.gca().spines['top'].set_visible(False)
plt.gca().yaxis.set_ticks_position('left')
plt.gca().xaxis.set_ticks_position('bottom')
plt.show()
导入pandas库,读取csv文件,绘制价格曲线变化图,设置所绘图形相关参数
所作图形如下:
图形分析:该图表展示了宁德时代股票价格的变化情况。可以看出,宁德时代的股票价格在2019年末有明显的上涨趋势,之后呈现震荡下跌的走势,但总体来说呈现了一个整体上涨的趋势,同时,宁德时代的股价在2021年末达到近几年的峰值,在之后的2022年到2023年宁德时代的股价呈现震荡下降的趋势,该图表展示了股票价格的走势,可以让投资者对该股票的价格变化趋势有一个基本的了解。
计算简单日回报率,并绘制日回报率曲线:
代码如下:
# daily return
df['date'] = pd.to_datetime(df['trade_date']) # 将Date列转换为DatetimeIndex类型
df.set_index('date', inplace=True)
df['daily_return'] = df['close'].pct_change()
plt.plot(df['daily_return'], color='dodgerblue')
plt.title('Ningde Time daily returns')
plt.xlabel('date')
plt.ylabel('daily return')
plt.gca().spines['right'].set_visible(False)
plt.gca().spines['top'].set_visible(False)
plt.gca().yaxis.set_ticks_position('left')
plt.gca().xaxis.set_ticks_position('bottom')
plt.show()
利用函数计算回报率,并绘制回报率曲线
所得图形如下所示:
图形分析:该图表展示了宁德时代股票的日回报率变化情况。可以看出,股票的日回报率在大部分时间内呈现出比较平稳的变化,但是也存在着一些较大的波动,如2019年3月、6月以及2021年初等时间点,这也反映了市场对宁德时代前景的一些疑虑和担忧。
计算简单月回报率并绘制图形:
代码如下;
#monthly return
df['date'] = pd.to_datetime(df['trade_date']) # 将Date列转换为DatetimeIndex类型
df.set_index('date', inplace=True)
Ningde_monthly_returns = df['close'].resample('M').ffill().pct_change()
plt.plot(Ningde_monthly_returns, color='dodgerblue')
plt.title('Ningde Times monthly returns')
plt.xlabel('date')
plt.ylabel('Monthly return')
plt.gca().spines['right'].set_visible(False)
plt.gca().spines['top'].set_visible(False)
plt.gca().yaxis.set_ticks_position('left')
plt.gca().xaxis.set_ticks_position('bottom')
plt.show()
同上,计算其简单月回报率,并绘制月回报率曲线
所的图形如下所示:
图形分析:该图表展示了宁德时代股票的月回报率变化情况。可以看出,股票的月回报率变化幅度较大,存在着明显的上涨和下跌趋势,且相较于日回报率的变化更加平滑。该图表展示了股票的长期趋势,可以让投资者对该股票的长期表现有一个更为清晰的了解。
计算累计回报率并绘制图形:
代码如下:
#daily cumulative return
df_cum_returns_daily = (df['daily_return'] + 1).cumprod()
# plot daily cumulative return
plt.plot(df_cum_returns_daily, color='dodgerblue')
plt.title('Ningde Times daily cumulative returns')
plt.xlabel('Date')
plt.ylabel('Cumulative return')
plt.gca().spines['right'].set_visible(False)
plt.gca().spines['top'].set_visible(False)
plt.gca().yaxis.set_ticks_position('left')
plt.gca().xaxis.set_ticks_position('bottom')
plt.show()
所得图形如下:
图形分析:这个图表展示了从2018年中旬至今宁德时代股票的累积回报率,它是从每日回报率计算而来。我们可以看到,股票价格整体上呈现出增长的趋势,但是存在价格波动较大的时间段,如2020年初、2021年初以及2021年末和2022年年初,股票的回报率显然下降了很多。同时可以发现,在2022年12月之前,宁德时代的日累积回报率表现出的增长趋势明显,但是在2022年1月之后,股价波动加剧,日累积回报率也出现了大幅波动。
计算简单月回报率并绘制图形:
代码如下:
# monthly cumulative return
df_cum_returns_monthly = (Ningde_monthly_returns + 1).cumprod()
# plot monthly cumulative return
plt.plot(df_cum_returns_monthly, color='dodgerblue')
plt.title('Ninede_monthly cumulative returns')
plt.xlabel('Date')
plt.ylabel('Cumulative return')
plt.gca().spines['right'].set_visible(False)
plt.gca().spines['top'].set_visible(False)
plt.gca().yaxis.set_ticks_position('left')
plt.gca().xaxis.set_ticks_position('bottom')
plt.show()
所得图形如下:
图形分析:这个图表显示了宁德时代股票的累积回报率,它是基于每月收盘价计算而来的。与日累计回报率图相似,此图表也展示了股票的整体变化趋势,但相对来说更为平滑。
从上述所绘图形来看,宁德时代不愧是现今新能源的领头羊,自从上市到现在,其股价暴增百分之五百,可谓一匹黑马,但在2022年,其股价从600多元跌至现在的300多元,价格可谓“腰斩”。
具体来说,宁德时代2022年一季度整体呈现“增收不增利”的局面,实现营业收入486.78亿元,同比增长153.97%,环比下降14.59%;实现归母净利润14.93亿元,同比下降23.62%;实现扣非归母净利润9.77亿元,同比下降41.57%。
但就其现在股价396元来说,宁德时代仍为行业的龙头!