def main(): # Read Data dates = pd.date_range('2009-01-01', '2012-12-31') symbols = ['SPY'] df = get_data(symbols, dates) plot_data(df) # Compute Daily returns daily_returns = compute_daily_returns(df) plot_data(daily_returns, title="Daily Returns", y_label="Daily returns", ) # Plot histogram daily_returns.hist(bins=20) mean = daily_returns['SPY'].mean() # print "mean: ", mean std = daily_returns['SPY'].std() # print "std: ", std # plot the mean on the histogram plt.axvline(mean, color="w", linestyle='dashed', linewidth=2) plt.axvline(std, color="r", linestyle='dashed', linewidth=2) plt.axvline(-std, color="r", linestyle='dashed', linewidth=2) plt.show() # Compute kurtosis print daily_returns.kurtosis()
def main(): # Read SPY 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 using a 20 day window 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 SPY data, retain matplotlib axis object ax = df['SPY'].plot(title="SPY Bollinger Bands", label="SPY") # Add rolling mean to same plot rm_SPY.plot(label="Rolling mean", ax=ax) # Add the upper and lower bands to the plot upper_band.plot(label="upper band", ax=ax) lower_band.plot(label="lower band", ax=ax) # Add legend ax.legend(loc='upper left') # Show the plot plt.show()
def main(): # Read data dates = pd.date_range("2009-01-01", "2012-12-31") symbols = ['SPY', 'IBM', 'GLD'] df = get_data(symbols, dates) # Compute daily returns daily_returns = compute_daily_returns(df) # Scatterplot SPY vs IBM daily_returns.plot(kind="scatter", x="SPY", y="IBM") beta_IBM, alpha_IBM = np.polyfit(daily_returns['SPY'], daily_returns['IBM'], 1) print "beta_IBM: ", beta_IBM print "alpha_IBM: ", alpha_IBM # for every point calculate the line: ma + b plt.plot(daily_returns['SPY'], beta_IBM *daily_returns['SPY'] + alpha_IBM, "-", color="r") plt.show() # Scatterplot SPY vs GLD daily_returns.plot(kind="scatter", x="SPY", y="GLD") beta_GLD, alpha_GLD = np.polyfit(daily_returns['SPY'], daily_returns['GLD'], 1) plt.plot(daily_returns['SPY'], beta_GLD *daily_returns['SPY'] + alpha_GLD, "-", color="r") plt.show() # Calculate correlation coefficient print daily_returns.corr(method="pearson")
def main(): # Read a month of data dates = pd.date_range('2012-07-01', '2012-07-31') symbols = ['SPY', 'GLD'] df = get_data(symbols, dates) plot_data(df) # Compute daily returns daily_returns = compute_daily_returns(df) plot_data(daily_returns, title="Daily returns", y_label="Daily returns")
def main(): # Read Data dates = pd.date_range('2009-01-01', '2012-12-31') symbols = ['SPY', 'IBM'] df = get_data(symbols, dates) # plot_data(df) # Compute Daily returns daily_returns = compute_daily_returns(df) plot_data(daily_returns, title="Daily Returns", y_label="Daily returns") daily_returns['SPY'].hist(bins=20, label="SPY") daily_returns['IBM'].hist(bins=20, label="IBM") plt.legend(loc="upper right") plt.show()