全局统计(global statistics)
例如我们平常接触到的平均值,中位数,标准差等等
在获得数据的基础上,我们才能进行数据的统计
import pandas as pd
def get_data(symbols,dates):
df = pd.DataFrame(index = dates)
if 'SPY' not in symbols: #为了防止失去参考
symbols.insert(0,'SPY')
for symbol in symbols:
#1读取数据
df_temp = pd.read_csv("data/{}.csv".format(symbol),index_col = 'Date',
parse_dates=True,usecols=['Date','Adj Close'],
na_values = ['nan'])
#2改名字
df_temp = df_temp.rename(columns={'Adj Close':symbol})
#3吧数据合并到一个表上
df=df.join(df_temp,how = 'inner')
return df
def test_run():
# Define a date range
dates = pd.date_range('2010-01-01', '2012-12-31')
# Choose stock symbols to read
symbols = ['SPY','XOM','GOOG', 'IBM', 'GLD']
# Get stock data
df = get_data(symbols, dates)
#compute global statistics for each stock
print 'mean:\n',df.mean()
print '\nmeadian:\n',df.median()
print '\nstd:\n',df.std()
if __name__ == '__main__':
test_run()
借助python,我们可以轻易地进行全局统计了!
2.滚动平均值
import pandas as pd
import matplotlib.pyplot as plt
def get_data(symbols,dates):
df = pd.DataFrame(index = dates)
if 'SPY' not in symbols: #为了防止失去参考
symbols.insert(0,'SPY')
for symbol in symbols:
#1读取数据
df_temp = pd.read_csv("data/{}.csv".format(symbol),index_col = 'Date',
parse_dates=True,usecols=['Date','Adj Close'],
na_values = ['nan'])
#2改名字
df_temp = df_temp.rename(columns={'Adj Close':symbol})
#3吧数据合并到一个表上
df=df.join(df_temp,how = 'inner')
return df
def test_run():
# Define a date range
dates = pd.date_range('2012-01-01', '2012-12-31')
# Choose stock symbols to read
symbols = ['SPY']
# Get stock data
df = get_data(symbols, dates)
ax = df['SPY'].plot(title = 'SPY rolling mean',label = 'SPY')
#先算出滚动平均值
rm_SPY=pd.rolling_mean(df['SPY'],window = 20)
#对滚动平均值画图,随后加到之前画的图像中
rm_SPY.plot(label = "Rolling mean",ax=ax)
#加上坐标轴的名字和图例子
ax.set_xlabel("Date")
ax.set_ylabel("price")
ax.legend(loc = "upper left")
plt.show()
if __name__ == '__main__':
test_run()
import pandas as pd
import matplotlib.pyplot as plt
def get_data(symbols,dates):
df = pd.DataFrame(index = dates)
if 'SPY' not in symbols: #为了防止失去参考
symbols.insert(0,'SPY')
for symbol in symbols:
#1读取数据
df_temp = pd.read_csv("data/{}.csv".format(symbol),index_col = 'Date',
parse_dates=True,usecols=['Date','Adj Close'],
na_values = ['nan'])
#2改名字
df_temp = df_temp.rename(columns={'Adj Close':symbol})
#3吧数据合并到一个表上
df=df.join(df_temp,how = 'inner')
return df
def plot_data(df, title="Stock prices"):
"""Plot stock prices with a custom title and meaningful axis labels."""
ax = df.plot(title=title, fontsize=12)
ax.set_xlabel("Date")
ax.set_ylabel("Price")
plt.show()
def get_rolling_mean(values, window):
"""Return rolling mean of given values, using specified window size."""
return pd.rolling_mean(values, window=window)
def get_rolling_std(values, window):
"""Return rolling standard deviation of given values, using specified window size."""
# TODO: Compute and return rolling standard deviation
return pd.rolling_std(values,window=window)
def get_bollinger_bands(rm, rstd):
"""Return upper and lower Bollinger Bands."""
# TODO: Compute upper_band and lower_band
upper_band = rm+2*rstd
lower_band = rm-2*rstd
return upper_band, lower_band
def test_run():
# Read data
dates = pd.date_range('2012-01-01', '2012-12-31')
symbols = ['SPY']
df = get_data(symbols, dates)
# Compute Bollinger Bands
# 1. Compute rolling mean
rm_SPY = get_rolling_mean(df['SPY'], window=20)
# 2. Compute rolling standard deviation
rstd_SPY = get_rolling_std(df['SPY'], window=20)
# 3. Compute upper and lower bands
upper_band, lower_band = get_bollinger_bands(rm_SPY, rstd_SPY)
# Plot raw SPY values, rolling mean and Bollinger Bands
ax = df['SPY'].plot(title="Bollinger Bands", label='SPY')
rm_SPY.plot(label='Rolling mean', ax=ax)
upper_band.plot(label='upper band', ax=ax)
lower_band.plot(label='lower band', ax=ax)
# Add axis labels and legend
ax.set_xlabel("Date")
ax.set_ylabel("Price")
ax.legend(loc='upper left')
plt.show()
if __name__ == "__main__":
test_run()
这个时候,看到比较好的买点和卖点了