예제 #1
0
def test_run():
    # Read data
    dates = pd.date_range('2010-07-01', '2010-12-31')
    symbols = ['GOOG', 'AAPL', 'GLD', 'XOM']
    df1 = get_data(symbols, dates, addSPY=True)
    spy = get_data([], dates)
    df1 = fill_missing_values(df1)
    rel1 = df1 / df1.iloc[0]
    plt.plot(rel1)
    plt.legend(rel1)
    plt.show()

    df = get_data(symbols, dates, addSPY=False)
    df = fill_missing_values(df)

    start_val = 1000000
    alloc = [0.0, 0.3, 0.7, 0.0]
    if sum(alloc) != 1.0:
        print "allocations must sum to 1"
        raise
    portfolio = compute_portfolio_value(df, alloc, start_val)
    daily = compute_daily_returns(portfolio)
    cumulative, avg, risk, sharpe = compute_portfolio_stats(portfolio, daily)
    print "cumulative return ", cumulative
    print "average daily return ", avg
    print "risk (std of daily return) ", risk
    print "sharpe ratio ", sharpe
    # plot relative performance of portfolio vs SPY
    spy['portfolio'] = portfolio
    rel = spy / spy.iloc[0]
    plt.plot(rel)
    plt.title('portfolio return vs. SPY')
    plt.legend(rel)
    plt.show()
예제 #2
0
def test_run():
    # Read data
    dates = pd.date_range('2000-01-01', '2012-12-31', freq='BM')
    symbols = ['SPY']
    df = get_data(symbols, dates)
    df = fill_missing_values(df)
    monthly = compute_daily_returns(df)  #its monthly
    plt.plot(monthly)
    #monthly.groupby(monthly.index.month).boxplot()

    monthlyWithMonth = monthly.copy()
    monthlyWithMonth['Month'] = monthly.index.month
    monthlyWithMonth.boxplot(by='Month')
    plt.suptitle("")
    plt.show()
    #avg = monthly.groupby(monthly.index.month).mean()
    #std = monthly.groupby(monthly.index.month).std()
    #tot = monthly.groupby(monthly.index.month).sum()
    midtermYearsMonthly = monthlyWithMonth.copy()
    midtermYearsMonthly['Year'] = monthly.index.year
    midtermYearsMonthly = midtermYearsMonthly.loc[
        midtermYearsMonthly['Year'].isin([2002, 2006, 2010])]
    midtermYearsMonthly[['SPY', 'Month']].boxplot(by='Month')
    plt.title("boxplot of midterm years")
    plt.suptitle("")
    plt.show()

    electionYearsMonthly = monthlyWithMonth.copy()
    electionYearsMonthly['Year'] = monthly.index.year
    electionYearsMonthly = electionYearsMonthly.loc[
        electionYearsMonthly['Year'].isin([2000, 2004, 2008, 2012])]
    electionYearsMonthly[['SPY', 'Month']].boxplot(by='Month')
    plt.title("boxplot of election years")
    plt.suptitle("")
    plt.show()
예제 #3
0
def test_run():
    # Read data
    dates = pd.date_range('2009-01-01', '2012-12-31')
    #symbols = ['SPY','XOM']
    symbols = ['SPY', 'XOM']
    df = get_data(symbols, dates)
    #df.fillna(method='ffill', inplace=True)
    #df.fillna(method='bfill', inplace=True)
    daily = compute_daily_returns(df)
    plot_data(df)
    plot_data(daily)
    daily['SPY'].hist(bins=20, label="SPY")
    daily['XOM'].hist(bins=20, label="XOM")
    plt.legend(loc='upper right')
    mean = daily.mean()
    std = daily.std()
    k = daily.kurtosis()

    plt.axvline(mean['SPY'], color='w', linestyle='dashed', linewidth=2)
    plt.axvline(mean['XOM'], color='w', linestyle='dashed', linewidth=2)
    plt.axvline(std['SPY'], color='r', linestyle='dashed', linewidth=2)
    plt.axvline(-std['SPY'], color='r', linestyle='dashed', linewidth=2)
    plt.axvline(std['XOM'], color='r', linestyle='dashed', linewidth=2)
    plt.axvline(-std['XOM'], color='r', linestyle='dashed', linewidth=2)
    plt.show()
    print "mean is ", mean, " and std is ", std
    print "kurtosos is ", k
예제 #4
0
def test_run():
    dates = pd.date_range('2010-01-01', '2010-01-30')
    symbols = ['GOOG', 'IBM', 'GLD']
    dataframe = util.get_data(symbols, dates)

    util.plot_selected(dataframe, ['GOOG', 'IBM'], '2010-01-30', '2010-01-01')
    util.plot_selected(util.normalize_data(dataframe), ['GOOG', 'IBM'],
                       '2010-01-30', '2010-01-01')
예제 #5
0
def optimize_portfolio(sd, ed, syms, opt, gen_plot=False):
    dates = pd.date_range(sd, ed)
    df = get_data(syms, dates, addSPY=False)
    df = fill_missing_values(df)
    spy = get_data([], dates)
    alloc = fit_portfolio_alloc(df, error_calc, optimize=opt)
    portfolio = compute_portfolio_value(df, alloc, 1)
    daily = compute_daily_returns(portfolio)
    cumulative, avg, risk, sharpe = compute_portfolio_stats(portfolio, daily)
    if gen_plot:
        spy['portfolio'] = portfolio
        rel = spy / spy.iloc[0]
        plt.plot(rel)
        tit = "portfolio return optimizing " + opt + " vs. SPY"
        plt.title(tit)
        plt.legend(rel)
        plt.show()
    return alloc, cumulative, avg, risk, sharpe
예제 #6
0
def test_run():
	dates = pd.date_range('2010-01-01', '2010-01-30')
	symbols = ['IBM', 'GOOG', 'GLD']
	dataframe = util.get_data(symbols, dates)
	print dataframe
	
	ndarray = dataframe.values
	print "[0,0]: " + str(ndarray[0,0])
	print "[3,2]: " + str(ndarray[3,2])
	print "[0:3,1:3]: " + str(ndarray[0:3,1:3])
	print "[:,3]: " + str(ndarray[:,3])
	print "[-1,1:3]: " + str(ndarray[-1,1:3])
def compute_returns_from_allocs(allocDf):
    riskFreeRate = 0.0
    dfAll = allocDf.copy()
    syms = dfAll.columns
    monthly = []
    for index, row in dfAll.iterrows():
        dates = pd.date_range(dtm.mkFirstOfMonth(index),
                              dtm.mkLastOfMonth(index))
        df = pu.get_data(syms, dates, addSPY=False)
        df = pu.fill_missing_values(df)
        portfolio = pu.compute_portfolio_value(df, row, 1)
        daily = pu.compute_daily_returns(portfolio)
        monthly.append(compute_cr(portfolio, daily, riskFreeRate))
    dfAll['monthly'] = monthly
    dfAll['cumulative'] = (dfAll['monthly'] + 1).cumprod()
    return dfAll
def test_run():
    #opts = ['risk','cumulative','sharpe']
    opt = 'cumulative'
    #syms=['GOOG','AAPL','GLD','XOM']
    syms = ['$DJI', '$SPX', 'GLD']
    monthsList = [(-3, -1), (-14, -12)]
    #dates = pd.date_range('2002-01-01', '2012-08-31', freq='BM')
    dates = pd.date_range('2006-01-01', '2012-08-31', freq='BM')
    allocsByMonth = []
    for curMon in dates:
        allocsList = []
        for tup in monthsList:
            #print tup
            #curMon = dt.datetime(2008,1,1)
            sd, ed = create_date_range(curMon, tup)
            #print sd, ed
            allocs, cr, adr, sddr, sr = \
            pu.optimize_portfolio(sd=sd, ed=ed, \
                                  syms=syms, opt=opt, gen_plot=False)
            dr = pd.DataFrame(allocs).T
            dr.columns = syms
            #print "allocations for opt",opt,"\n", dr.to_string(index=False)
            #print "sanity sum", sum(allocs)
            #print "cumulative return", cr
            #print "average daily return", adr
            #print "risk",sddr
            #print "sharpe ratio", sr
            allocsList.append(allocs)
        allocArr = pd.DataFrame(allocsList)
        allocArr.columns = syms
        #print curMon
        #print allocArr
        #print allocArr.mean()
        allocsByMonth.append(allocArr.mean())
    #print allocsByMonth
    allocDf = pd.DataFrame(allocsByMonth, index=dates)
    res = compute_returns_from_allocs(allocDf)
    print res.round(3)
    spy = pu.get_data([], dates)
    spy['portfolio'] = res.cumulative
    spy = pu.normalize_data(spy)
    pu.plot_data(spy)
예제 #9
0
def test_run():
    # Read data
    dates = pd.date_range('2009-01-01', '2012-12-31')
    #symbols = ['SPY','XOM']
    symbols = ['SPY', 'XOM', 'GLD']
    df = get_data(symbols, dates)
    #df.fillna(method='ffill', inplace=True)
    #df.fillna(method='bfill', inplace=True)
    daily = compute_daily_returns(df)
    plot_data(df)
    plot_data(daily)
    daily.plot(kind='scatter', x='SPY', y='XOM')
    beta_XOM, alpha_XOM = np.polyfit(daily['SPY'], daily['XOM'], 1)
    print "XOM b ", beta_XOM, " alph ", alpha_XOM
    plt.plot(daily['SPY'], beta_XOM * daily['SPY'] + alpha_XOM, '-', color='r')
    plt.show()
    daily.plot(kind='scatter', x='SPY', y='GLD')
    beta_GLD, alpha_GLD = np.polyfit(daily['SPY'], daily['GLD'], 1)
    print "GLD b ", beta_GLD, " alph ", alpha_GLD
    plt.plot(daily['SPY'], beta_GLD * daily['SPY'] + alpha_GLD, '-', color='r')
    plt.show()
    print daily.corr(method='pearson')
예제 #10
0
def test_run():
    dates = pd.date_range('2010-01-01', '2010-12-31')
    symbols = ['GOOG', 'IBM', 'GLD']
    dataframe = util.get_data(symbols, dates)

    print dataframe / dataframe.ix[0]