示例#1
0
def scatterplots():
    #Read data
    dates = pd.date_range('2009-01-01', '2012-12-31')
    symbols = ['SPY', 'XOM', 'GLD']
    df = get_data(symbols, dates)

    #Compute daily returns
    daily_returns = compute_daily_returns(df)

    # Scatterplot SPY vs XOM
    daily_returns.plot(kind='scatter', x='SPY', y='XOM')
    beta_XOM, alpha_XOM = np.polyfit(daily_returns['SPY'],
                                     daily_returns['XOM'], 1)
    print("beta_XOM =", beta_XOM)
    print("alpha_XOM =", alpha_XOM)
    plt.plot(daily_returns['SPY'],
             beta_XOM * daily_returns['SPY'] + alpha_XOM,
             '-',
             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)
    print("beta_GLD =", beta_GLD)
    print("alpha_GLD =", alpha_GLD)
    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'))
示例#2
0
def histogram_statistics():
    #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", ylabel="Daily returns")

    #Plot a histogram
    daily_returns.hist(bins=20)  #changing no. of bins to 20
    # plt.show()

    #Get mean and standard deviation
    mean = daily_returns['SPY'].mean()
    print("mean =", mean)
    std = daily_returns['SPY'].std()
    print("std =", std)

    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())
示例#3
0
def bollinger_bands():
    """Bollinger Bands."""
    # 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()
示例#4
0
def compute_daily_returns_run():
    # Read data
    dates = pd.date_range('2012-07-01', '2012-07-31')  # one month only
    symbols = ['SPY', 'XOM']
    df = get_data(symbols, dates)
    plot_data(df)

    # Compute daily returns
    daily_returns = compute_daily_returns(df)
    plot_data(daily_returns, title="Daily returns", ylabel="Daily returns")
示例#5
0
def compute_statistics():
    # Read data
    dates = pd.date_range('2010-01-01', '2012-12-31')
    symbols = ['SPY', 'XOM', 'GOOG', 'GLD']
    df = get_data(symbols, dates)
    plot_data(df)

    # Compute global statistics for each stock
    print(df.mean())
    print(df.median())
    print(df.std())
示例#6
0
def plot_multiple_histograms():
    #Read data
    dates = pd.date_range('2009-01-01', '2012-12-31')
    symbols = ['SPY', 'XOM']
    df = get_data(symbols, dates)
    plot_data(df)

    #Compute daily returns
    daily_returns = compute_daily_returns(df)
    plot_data(daily_returns, title="Daily returns", ylabel="Daily returns")

    #Plot histogram directly from dataframe
    # daily_returns.hist(bins=20)
    # plt.show()

    #Compute and plot both histograms on the same chart
    daily_returns['SPY'].hist(bins=20, label="SPY")
    daily_returns['XOM'].hist(bins=20, label="XOM")
    plt.legend(loc='upper right')
    plt.show()
示例#7
0
def rolling_mean():
    # Read data
    dates = pd.date_range('2012-01-01', '2012-12-31')
    symbols = ['SPY']
    df = get_data(symbols, dates)

    # Plot SPY data, retain matplotlib axis object
    ax = df['SPY'].plot(title="SPY rolling mean", label='SPY')

    # Compute rolling mean using a 20-day window
    # deprecated -> rm_SPY = pd.rolling_mean(df['SPY'], window=20)
    rm_SPY = df['SPY'].rolling(window=20, center=False).mean()

    # Add rolling mean to same plot
    rm_SPY.plot(label='Rolling mean', ax=ax)

    # Add acis labels and legend
    ax.set_xlabel("Date")
    ax.set_ylabel("Price")
    ax.legend(loc='upper left')
    plt.show()
示例#8
0
import matplotlib.pyplot as plt


def plot(df_data):
    ax = df_data.plot(title="Incomplete Data", fontsize=2)
    ax.set_xlabel("Date")
    ax.set_ylabel("Price")
    plt.show()


def fill_missing_values(df_data):
    """Fill missing values in data frame, in place."""
    df_data.fillna(method="ffill", inplace=True)
    df_data.fillna(method="bfill", inplace=True)


if __name__ == '__main__':
    #list of symbols
    symbollist = ["PSX", "FAKE1", "FAKE2"]
    #symbollist=["FAKE2"]
    #create date range
    dates = pd.date_range('2005-12-31', '2014-12-07')
    #get adjusted close of each symbol
    df_data = get_data(symbollist, dates)

    # Fill missing values
    fill_missing_values(df_data)

    # Plot
    plot(df_data)