我们也许想要把不合的金融商品涌如今一张图上:如许我们可以比较不合的股票,比较股票跟市场的关系,或者可以看其他证券,例如交易所交易基金(ETFs)。在后面的内容中,我们将会学到若何画金融证券跟一些指数(移动平均)的关系。届时你须要应用线段图而不是烛状图。(试想你若何重叠不合的烛状图而让图表保持整洁?)
下面我展示了不合技巧公司股票的数据,以及若何调剂数据让数据线聚在一路。
- microsoft = web.DataReader("MSFT", "yahoo", start, end)
- google = web.DataReader("GOOG", "yahoo", start, end)
- # Below I create a DataFrame consisting of the adjusted closing price of these stocks, first by making a list of these objects and using the join method
- stocks = pd.DataFrame({"AAPL": apple["Adj Close"],
- "MSFT": microsoft["Adj Close"],
- "GOOG": google["Adj Close"]})
- stocks.head()
- stocks.plot(grid = True)

这张图表的问题在哪里呢?固然价格的绝对值很重要(昂贵的股票很难购得,这不仅会影响它们的波动性,也会影响你交易它们的难易度),然则在交易中,我们更存眷每支股票价格的变更而不是它的价格。Google的股票价格比苹不雅微软的都高,这个差别让苹不雅和微软的股票显得波动性很低,而事实并不是那样。
一个解决办法就是用两个不合的标度来作图。一个标度用于苹不雅和微软的数据;另一个标度用来表示Google的数据。
- stocks.plot(secondary_y = ["AAPL", "MSFT"], grid = True)


这须要转化stock对象中的数据,操作如下:
- # df.apply(arg) will apply the function arg to each column in df, and return a DataFrame with the result
- # Recall that lambda x is an anonymous function accepting parameter x; in this case, x will be a pandas Series object
- stock_return = stocks.apply(lambda x: x / x[0])
- stock_return.head()
推荐阅读
【51CTO.com原创稿件】由51CTO主办的WOTA2017全球立异技巧峰会将于2017年7月21日拉开帷幕。本次峰会,将环绕机>>>详细阅读
本文标题:用Python做股市数据分析(一)
地址:http://www.17bianji.com/lsqh/36070.html
1/2 1

网友点评
精彩导读
科技快报
品牌展示