# 获取yahoo金融苹果公司的股票数据。 # 1、分析拉取的数据,找到收盘数据列的列名。 # 2、绘制收盘价格柱状图。 # 3、分析拉取的数据涨跌率,股价移动平均和波动率。 # 4、 找出开盘价和收盘价最高的那一天 # 5、 获取股票表格总共有多少行多少列 # 6、 对股票的每列按照月为单位求平均值 # 7、将股票表格按照adj close股价进行由高到低排序 # 练习提示: # 1. 使用pandas_datareader读取苹果公司的2020年上半年的股票指数 # 2. 求股票的涨跌率return:当天的收盘指数/前一天的收盘指数的对数 # 3. 股价的移动平均值:#求移动平均值rolling(42).mean() # 4. 股价的波动率:return(股票的涨跌率).rolling(42).std() 乘以窗口期的均方根 import datetime import yfinance as yf #雅虎api import pandas_datareader.data as web import numpy as np import pandas as pd import cmath import matplotlib.pyplot as plt #1 yf.pdr_override() start = datetime.datetime(2020, 1, 2)#开始日期 end = datetime.datetime(2020, 7, 1)#结束日期 apple = web.get_data_yahoo(\'AAPL\', start, end) print(apple.columns.values) # # #2 # apple[\'Close\'].plot(kind=\'bar\') # plt.show() # # # 3 # # 涨跌率 # apple[\'Return\'] = np.log(apple[\'Close\']/apple[\'Close\'].shift(1)) # apple[\'Return\'].plot(kind=\'bar\') # plt.show() # #股价移动平均 # apple[\'42d\']=apple[\'Close\'].rolling(42).mean() # apple[\'42d\'].plot(kind=\'bar\') # plt.show() # #波动率 # apple[\'Mov_Vol\']=apple[\'Close\'].rolling(42).std()*cmath.sqrt(42) # apple[\'Mov_Vol\'].plot(kind=\'bar\') # plt.show() # # 4 # apple=apple.sort_values(by=\'Open\') # print("OpenMax:",apple.index[-1]) # apple=apple.sort_values(by=\'Close\') # print("CloseMax:",apple.index[-1]) # apple=apple.sort_values(by=\'Close\') # # 5 # print("行数:",len(apple.index)) # print("列数:",len(list(apple))) # # 6 # apple[\'index\'] = apple.index # print(apple.groupby([apple[\'index\'].dt.year, apple[\'index\'].dt.month]).mean()[\'Close\']) # 7 print(apple.sort_values(by="Adj Close",ascending=False))