【发布时间】:2018-10-13 11:42:55
【问题描述】:
我有以下代码,其中包含 1995 年 1 月至 2000 年 12 月期间美国股票的回报。我希望使用 Python 中的 60 个月滚动回报来计算 2001 年 1 月的回报。有5年的回报,这怎么可能?
我想计算 2001 年 1 月至 2010 年 12 月期间每只股票的这个值。任何帮助都会很棒!
import quandl
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# Use Quandl to get adjusted close price
quandl.ApiConfig.api_key = 'Enter Your Key'
stocks = ['MSFT', 'AAPL', 'WMT', 'GE', 'KO', 'F', 'JNJ', 'BA', 'XOM']
stockdata = quandl.get_table('WIKI/PRICES', ticker = stocks, paginate=True,
qopts = { 'columns': ['date', 'ticker', 'adj_close'] },
date = { 'gte': '1995-1-1', 'lte': '2000-12-31' })
# Setting date as index with columns of tickers and adjusted closing
# price
data1 = stockdata.set_index('date')
table = data1.pivot(columns='ticker')
table.head()
# Daily and annual returns of the stocks
returns_daily = table.pct_change()
returns_daily.head()
【问题讨论】:
标签: python pandas finance quandl economics