Ejemplo n.º 1
0
def optimize_portfolio(sd=dt.datetime(2008,1,1), ed=dt.datetime(2009,1,1), \
    syms=['GOOG','AAPL','GLD','XOM'], gen_plot=False):

    # Read in adjusted closing prices for given symbols, date range
    dates = pd.date_range(sd, ed)
    prices_all = get_data(syms, dates)  # automatically adds SPY
    prices_all = prices_all.fillna(method='backfill')
    prices_all = prices_all.fillna(method='ffill')
    prices = prices_all[syms]  # only portfolio symbols
    prices_SPY = prices_all['SPY']  # only SPY, for comparison later
    rfr = 0.0
    sf = 252.0
    # find the allocations for the optimal portfolio
    # note that the values here ARE NOT meant to be correct for a test case
    arg_num = len(prices.columns)
    allocs = np.asarray(
        arg_num *
        [1 / float(arg_num)])  # add code here to find the allocations

    #cr, adr, sddr, sr = [0.25, 0.001, 0.0005, 2.1] # add code here to compute stats
    #arg_num=len(prices.columns)
    def con(x):
        return float(sum(x)) - 1.0

    cons = ({'type': 'eq', 'fun': con})
    bnds = arg_num * [(0.0, 1.0)]

    min_result = spo.minimize(lambda x: prices.div(prices.iloc[0]).multiply(x).
                              sum(axis=1).pct_change().std(),
                              allocs,
                              method='SLSQP',
                              bounds=bnds,
                              constraints=cons)
    allocs = min_result.x
    prices_test = prices.div(prices.iloc[0])
    prices_test1 = prices_test.multiply(allocs)
    port_val = prices_test1.sum(axis=1)
    cr = port_val.iloc[-1] / port_val.iloc[0] - 1
    daily_ret = port_val.pct_change()
    adr = daily_ret.mean()
    sddr = daily_ret.std()
    daily_rfr = rfr
    sharp_ratio = (daily_ret.sub(daily_rfr)).mean() / (
        (daily_ret.sub(daily_rfr)).std())
    sr = sharp_ratio * np.power(sf, 1.0 / 2.0)
    # Get daily portfolio value
    #port_val = prices_SPY # add code here to compute daily portfolio values

    # Compare daily portfolio value with SPY using a normalized plot
    if gen_plot:
        # add code to plot here
        #df_temp = pd.concat([port_val, prices_SPY], keys=['Portfolio', 'SPY'], axis=1)
        prices_SPY_test = prices_SPY.div(prices_SPY.iloc[0])
        plot_prices = pd.DataFrame(
            prices_test.multiply(allocs).sum(axis=1)).join(
                pd.DataFrame(prices_SPY_test)).rename(columns={0: 'PORTFOLIO'})
        plot_data(plot_prices)
        pass

    return allocs, cr, adr, sddr, sr
Ejemplo n.º 2
0
def BestPossible():
    # Read data
    dates = pd.date_range('2008-1-1', '2009-12-31')
    start_val = 100000
    syms = ['AAPL']
    prices_all = get_data(syms, dates)
    prices_port = prices_all[syms]  # only price of each stock in portfolio
    prices_SPY = prices_all['SPY']  # only SPY, for comparison later
    dfprices = pd.DataFrame(prices_port)
    dfprices['Cash'] = start_val - 200 * dfprices.ix[0, 'AAPL']
    dfprices['Holding'] = 200
    dfprices['Stock values'] = dfprices['Holding'] * dfprices['AAPL']
    dfprices['Port_val'] = dfprices['Cash'] + dfprices['Stock values']
    dfprices['Benchmark'] = dfprices['Port_val'] / dfprices['Port_val'].ix[
        0, :]
    dfprices['Best'] = Best_strategy(prices_all[syms])
    dfprices['Best possible portfolio'] = dfprices['Best'] / dfprices[
        'Best'].ix[0, :]
    df = pd.concat(
        [dfprices['Benchmark'], dfprices['Best possible portfolio']],
        keys=['Benchmark', 'Best possible portfolio'],
        axis=1)
    plot_data(df,
              title="Benchmark vs. Best possible portfolio",
              ylabel="Normalized price",
              xlabel="Date")
    crBM, adrBM, sddrBM = compute_portfolio_stats(dfprices['Benchmark'])
    crBest, adrBest, sddrBest = compute_portfolio_stats(
        dfprices['Best possible portfolio'])
    print "statistics of Benchmark", crBM, adrBM, sddrBM
    print "statistics of Best Stategy", crBest, adrBest, sddrBest
    return df
Ejemplo n.º 3
0
def get_RSI(prices, gen_plot=False):
    prices_RSI = prices.copy()
    prices_RSI = prices_RSI / prices_RSI.iloc[0]
    prices_RSI['gain/loss'] = prices_RSI['JPM'] - prices_RSI['JPM'].shift(1)
    prices_RSI['gain'] = prices_RSI['gain/loss'].apply(
        lambda x: x > 0 and x or 0)
    prices_RSI['loss'] = prices_RSI['gain/loss'].apply(
        lambda x: x < 0 and x or 0)
    list_gain = prices_RSI['gain'].tolist()
    list_loss = prices_RSI['loss'].tolist()
    t = 7
    list_RS = []
    for i in range(0, t):
        list_RS.append(np.NAN)
    for i in range(t, prices.shape[0]):
        RS = (np.mean(list_gain[0:i]) *
              (t - 1) + list_gain[i]) / (np.mean(list_loss[0:i]) *
                                         (t - 1) + list_loss[i]) * (-1)
        list_RS.append(RS)
    prices_RSI['RS'] = list_RS
    prices_RSI['RSI'] = prices_RSI['RS'].apply(lambda x: 100 - 100 / (1 + x))
    #plot graph
    if gen_plot:
        print("Technical Analysis by RSI: ")
        plot_data(prices_RSI[['RSI']],
                  title="Method-4 RSI",
                  xlabel="Date",
                  ylabel="Price")
        plt.savefig('indicator_RSI')
    """
Ejemplo n.º 4
0
def assess_portfolio(sd=dt.datetime(2008, 1, 1), ed=dt.datetime(2009, 1, 1),
                     syms=['GOOG', 'AAPL', 'GLD', 'XOM'],
                     allocs=[0.1, 0.2, 0.3, 0.4],
                     sv=1000000, rfr=0.0, sf=252.0,
                     gen_plot=False):
    if sum(allocs) != 1:
        raise Exception('Allocations to the equities should sum to 1')

    dates = pd.date_range(sd, ed)
    prices_all = get_data(syms, dates)
    prices = prices_all[syms]
    prices_SPY = prices_all['SPY']

    normed = prices / prices.iloc[0]
    alloced = normed * allocs
    pos_vals = alloced * sv
    port_val = pos_vals.sum(axis=1)

    cr, adr, sddr, sr = compute_portfolio_stats(port_val=port_val, rfr=rfr, sf=sf)

    if gen_plot:
        df_temp = pd.concat([port_val, prices_SPY], keys=['Portfolio', 'SPY'], axis=1)
        df_temp = df_temp / df_temp.iloc[0]
        plot_data(df_temp)

    ev = port_val[-1]

    return cr, adr, sddr, sr, ev
Ejemplo n.º 5
0
def optimize_portfolio(sd=dt.datetime(2008,1,1), ed=dt.datetime(2009,1,1), \
    syms=['GOOG','AAPL','GLD','XOM'], gen_plot=False):

    # Read in adjusted closing prices for given symbols, date range
    dates = pd.date_range(sd, ed)
    prices_all = get_data(syms, dates)  # automatically adds SPY
    prices = prices_all[syms]  # only portfolio symbols
    prices_SPY = prices_all['SPY']  # only SPY, for comparison later

    # find the allocations for the optimal portfolio
    # note that the values here ARE NOT meant to be correct for a test case
    def r_sharpratio(allocs):
        return analysis(prices, allocs)[3] * (-1)
    
    xGuess = np.ones(len(syms)) / float(len(syms))
    
    bs = [(0, 1) for i in range(len(syms))]
    cs = ({ 'type': 'eq', 'fun': lambda x: 1.0 - np.sum(x) })
          
    allocs = spo.minimize(r_sharpratio, xGuess, method = 'SLSQP', bounds = bs, constraints = cs)
    
    cr, adr, sddr, sr = analysis(prices, allocs.x)

    # Get daily portfolio value
    port_val = prices_SPY / prices_SPY.ix[0] # add code here to compute daily portfolio values
    port_val2 = (prices / prices.ix[0] * allocs.x).sum(axis = 1)
    # Compare daily portfolio value with SPY using a normalized plot
    if gen_plot:
        # add code to plot here
        df = pd.concat([port_val2, port_val], keys=['Portfolio', 'SPY'], axis=1)
        plot_data(df, title="Daily Portfolio Value and SPY", xlabel="Date", ylabel="Price")

    return allocs.x, cr, adr, sddr, sr
Ejemplo n.º 6
0
def test_code1():

    # this is a helper function you can use to test your code
    # note that during autograding his function will not be called.
    # Define input parameters

    of = "/home/mdonaher/PycharmProjects/mc2_pc2/orders.csv"
    sv = 10000
    start_date = dt.datetime(2007, 12, 31)
    end_date = dt.datetime(2009, 12, 31)

    # Process orders
    portvals = compute_portvals(
        orders_file=of,
        start_val=sv,
        sd=start_date,
        ed=end_date,
    )
    print type(portvals)

    if isinstance(portvals, pd.DataFrame):
        portvals = portvals[portvals.columns[0]]  # just get the first column
    else:
        print "warning, Your code did not return a DataFrame"
    plot_data(portvals)

    # Get portfolio stats
    # Here we just fake the data. you should use your code from previous assignments.
    start_date = portvals.index.min()
    end_date = portvals.index.max()
    start_date1 = ((portvals.index.min()).to_pydatetime()).strftime('%Y-%m-%d')
    end_date1 = ((portvals.index.max()).to_pydatetime()).strftime('%Y-%m-%d')
    calc_cum_ret = (portvals.ix[end_date, ['Cummulative_Value']] /
                    portvals.ix[start_date, ['Cummulative_Value']]) - 1
    calc_adr = ((portvals / portvals.shift()) - 1).mean()
    calc_sddr = ((portvals / portvals.shift()) - 1).std()
    cum_ret, avg_daily_ret, std_daily_ret, sharpe_ratio = [
        calc_cum_ret, calc_adr, calc_sddr, 1.5
    ]
    cum_ret_SPY, avg_daily_ret_SPY, std_daily_ret_SPY, sharpe_ratio_SPY = [
        0.2, 0.01, 0.02, 1.5
    ]

    # Compare portfolio against $SPX

    print "Date Range: {} to {}".format(start_date1, end_date1)
    print
    print "Sharpe Ratio of Fund: {}".format(sharpe_ratio)
    print "Sharpe Ratio of SPY : {}".format(sharpe_ratio_SPY)
    print
    print "Cumulative Return of Fund: {}".format(cum_ret)
    print "Cumulative Return of SPY : {}".format(cum_ret_SPY)
    print
    print "Standard Deviation of Fund: {}".format(std_daily_ret)
    print "Standard Deviation of SPY : {}".format(std_daily_ret_SPY)
    print
    print "Average Daily Return of Fund: {}".format(avg_daily_ret)
    print "Average Daily Return of SPY : {}".format(avg_daily_ret_SPY)
    print
    print "Final Portfolio Value: {}".format(portvals[-1])
Ejemplo n.º 7
0
def optimize_portfolio(sd, ed, syms, gen_plot=True):
    dates = pd.date_range(sd, ed)
    prices_all = get_data(syms, dates)  # automatically adds SPY

    prices_all.fillna(method="ffill", inplace=True)
    prices_all.fillna(method="bfill", inplace=True)

    prices = prices_all[syms]  # only portfolio symbols
    prices_SPY = prices_all['SPY']  # only SPY, for comparison later

    allocs = find_optimum_allocs(prices)
    allocs = allocs / np.sum(allocs)

    port_val = get_portfolio_value(prices, allocs)
    cum_ret, avg_daily_ret, std_daily_ret, sharp_ratio = get_portfolio_status(
        port_val)

    normed_SPY = prices_SPY / prices_SPY.ix[0, :]
    if gen_plot:
        df_temp = pd.concat([port_val, normed_SPY],
                            keys=['Portfolio', 'SPY'],
                            axis=1)
        plot_data(df_temp, title='Daily Portfolio Value and SPY')
        pass

    return allocs, cum_ret, avg_daily_ret, std_daily_ret, sharp_ratio
Ejemplo n.º 8
0
def main():
	dates = pd.date_range('2009-01-01', '2012-12-31')
	symbols = ['SPY']
	df = get_data(symbols, dates)
	plot_data(df)

	daily_returns = compute_daily_returns(df)

	# histogram
	daily_returns.hist(bins=20)
	
	'''	
	call this twice if wanting to plot 2+ charts on same chart:
	daily_returns['SPY'].hist(bins=20, label="SPY")
	daily_returns['XOM'].hist(bins=20, label="XOM")
	'''

	mean = daily_returns['SPY'].mean()
	std = daily_returns['SPY'].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()

	print daily_returns.kurtosis()
Ejemplo n.º 9
0
def test_run():
    start_date = '2009-01-01'
    end_date = '2012-12-31'
    dates = pd.date_range(start_date, end_date)
    symbols = ['SPY']

    df = get_data(symbols, dates)
    daily_returns = compute_daily_returns(df)

    plot_data(daily_returns, title="Daily returns", ylabel="Daily returns")

    daily_returns.hist(bins=20)

    mean = daily_returns['SPY'].mean()
    print("mean:\t\t\t{}".format(mean))
    std = daily_returns['SPY'].std()
    print("std. deviation:\t{}".format(std))

    plt.axvline(mean, color='red', linestyle='dashed', linewidth=1)
    plt.axvline(std, color='black', linestyle='dashed', linewidth=1)
    plt.axvline(-std, color='black', linestyle='dashed', linewidth=1)

    plt.show()

    kurtosis = daily_returns.kurtosis()
    print("kurtosis:\t{}".format(kurtosis))
Ejemplo n.º 10
0
def assess_portfolio(start_date, end_date, symbols, allocs, start_val=1):
    """Simulate and assess the performance of a stock portfolio."""
    # Read in adjusted closing prices for given symbols, date range
    dates = pd.date_range(start_date, end_date)
    prices_all = get_data(symbols, dates)  # automatically adds SPY
    prices = prices_all[symbols]  # only portfolio symbols
    prices_SPY = prices_all['SPY']  # only SPY, for comparison later

    # Get daily portfolio value
    port_val = get_portfolio_value(prices, allocs, start_val)
    plot_data(port_val, title="Daily Portfolio Value")

    # Get portfolio statistics (note: std_daily_ret = volatility)
    cum_ret, avg_daily_ret, std_daily_ret, sharpe_ratio = get_portfolio_stats(port_val)

    # Print statistics
    print "Start Date:", start_date
    print "End Date:", end_date
    print "Symbols:", symbols
    print "Allocations:", allocs
    print "Sharpe Ratio:", sharpe_ratio
    print "Volatility (stdev of daily returns):", std_daily_ret
    print "Average Daily Return:", avg_daily_ret
    print "Cumulative Return:", cum_ret

    # Compare daily portfolio value with SPY using a normalized plot
    df_temp = pd.concat([port_val, prices_SPY], keys=['Portfolio', 'SPY'], axis=1)
    plot_normalized_data(df_temp, title="Daily portfolio value and SPY")
Ejemplo n.º 11
0
def optimize_portfolio(sd=dt.datetime(2008, 1, 1), ed=dt.datetime(2009, 1, 1),
                       syms=['GOOG', 'AAPL', 'GLD', 'XOM'], gen_plot=False):

    # Read in adjusted closing prices for given symbols, date range
    dates = pd.date_range(sd, ed)
    prices_all = get_data(syms, dates)  # automatically adds SPY
    prices = prices_all[syms]  # only portfolio symbols
    prices_SPY = prices_all['SPY']  # only SPY, for comparison later

    # find the allocations for the optimal portfolio
    allocs = optimize_sharpe_ratio(prices)
    # compute stats
    cr, adr, sddr, sr = compute_portfolio_stats(prices, allocs)

    # Get daily portfolio value
    normalized_prices = prices / prices.ix[0]
    alloced = normalized_prices * allocs
    port_val = alloced.sum(axis=1)

    # Compare daily portfolio value with SPY using a normalized plot
    if gen_plot:
        # add code to plot here
        df_temp = pd.concat([port_val, prices_SPY],
                            keys=['Portfolio', 'SPY'], axis=1)
        df_temp = df_temp / df_temp.ix[0]
        plot_data(df_temp, "Daily portfolio value and SPY",
                  'Normalized price', 'Time')

    return allocs, cr, adr, sddr, sr
Ejemplo n.º 12
0
def assess_portfolio(sd = dt.datetime(2008,1,1), ed = dt.datetime(2009,1,1), \
    syms = ['GOOG','AAPL','GLD','XOM'], \
    allocs=[0.1,0.2,0.3,0.4], \
    sv=1000000, rfr=0.0, sf=252.0, \
    gen_plot=False):

    # Read in adjusted closing prices for given symbols, date range
    dates = pd.date_range(sd, ed)
    prices_all = get_data(syms, dates)  # automatically adds SPY
    prices = prices_all[syms]  # only portfolio symbols
    prices_SPY = prices_all['SPY']  # only SPY, for comparison later

    # compute daily portfolio values
    port_val = compute_portfolio_value(prices, allocs, start_val=1000000)

    # compute portfolio statistics (note: std_daily_ret = volatility)
    cr, adr, sddr, sr = compute_portfolio_statistics(port_val, freq_sample=252)

    # Compare daily portfolio value with SPY using a normalized plot
    if gen_plot:
        # add code to plot here
        df_temp = pd.concat([port_val, prices_SPY],
                            keys=['Portfolio', 'SPY'],
                            axis=1)
        plot_data(df_temp)
        pass

    return cr, adr, sddr, sr
def test_code():

    # see format of [order].csv in orders directory
    of = "./orders/orders2.csv"
    sv = 1000000

    # Process orders
    portvals = compute_portvals(orders_file=of, start_val=sv)
    print portvals
    if isinstance(portvals, pd.DataFrame):
        portvals = portvals[portvals.columns[0]]  # just get the first column
    else:
        "warning, code did not return a DataFrame"

    # plot with spy to compare
    start_date = portvals.index[0]
    end_date = portvals.index[-1]

    dates = pd.date_range(start_date, end_date)
    temp = get_data(['GOOG'], dates)
    SPY = temp['SPY']

    normalized_prices_SPY = SPY.copy()
    normalized_prices_SPY = (normalized_prices_SPY) / SPY[0]

    normalized_portval = portvals.copy()
    normalized_portval = (normalized_portval) / portvals[0]

    df_temp = pd.concat([normalized_portval, normalized_prices_SPY],
                        keys=['Portfolio', 'SPY'],
                        axis=1)
    plot_data(df_temp)
Ejemplo n.º 14
0
def assess_portfolio(sd = dt.datetime(2008,1,1), ed = dt.datetime(2009,1,1), \
    syms = ['GOOG','AAPL','GLD','XOM'], \
    allocs=[0.1,0.2,0.3,0.4], \
    sv=1000000, rfr=0.0, sf=252.0, \
    gen_plot=False):

    # Read in adjusted closing prices for given symbols, date range
    dates = pd.date_range(sd, ed)
    prices_all = get_data(syms, dates)  # automatically adds SPY
    prices = prices_all[syms]  # only portfolio symbols
    prices_SPY = prices_all['SPY']  # only SPY, for comparison later
    prices_SPY = (prices_SPY / prices_SPY.iloc[0]) * sv

    # Get daily portfolio value
    port_val = get_portfolio_value(prices, allocs, sv)

    # Get portfolio statistics (note: std_daily_ret = volatility)
    cr, adr, sddr, sr = get_portfolio_stats(port_val, rfr, sf)

    # Compare daily portfolio value with SPY using a normalized plot
    if gen_plot:
        # Plot normalized portfolio value.
        df_temp = pd.concat([port_val / sv, prices_SPY / sv],
                            keys=['Portfolio', 'SPY'],
                            axis=1)
        plot_data(df_temp,
                  title="Daily portfolio value and SPY",
                  ylabel="Normalized price")

    # Compute end value
    ev = port_val[-1]

    return cr, adr, sddr, sr, ev
Ejemplo n.º 15
0
def optimize_portfolio(start_date, end_date, symbols):
    """Simulate and optimize portfolio allocations."""
    # Read in adjusted closing prices for given symbols, date range
    dates = pd.date_range(start_date, end_date)
    prices_all = get_data(symbols, dates)  # automatically adds SPY
    prices = prices_all[symbols]  # only portfolio symbols
    prices_SPY = prices_all['SPY']  # only SPY, for comparison later

    # Get optimal allocations
    allocs = find_optimal_allocations(prices)
    allocs = allocs / np.sum(allocs)  # normalize allocations, if they don't sum to 1.0

    # Get daily portfolio value (already normalized since we use default start_val=1.0)
    port_val = get_portfolio_value(prices, allocs)

    # Get portfolio statistics (note: std_daily_ret = volatility)
    cum_ret, avg_daily_ret, std_daily_ret, sharpe_ratio = get_portfolio_stats(port_val)

    # Print statistics
    print "Start Date:", start_date
    print "End Date:", end_date
    print "Symbols:", symbols
    print "Optimal allocations:", allocs
    print "Sharpe Ratio:", sharpe_ratio
    print "Volatility (stdev of daily returns):", std_daily_ret
    print "Average Daily Return:", avg_daily_ret
    print "Cumulative Return:", cum_ret

    # Compare daily portfolio value with normalized SPY
    normed_SPY = prices_SPY / prices_SPY.ix[0, :]
    df_temp = pd.concat([port_val, normed_SPY], keys=['Portfolio', 'SPY'], axis=1)
    plot_data(df_temp, title="Daily Portfolio Value and SPY")
Ejemplo n.º 16
0
def assess_portfolio(sd = dt.datetime(2008,1,1), ed = dt.datetime(2009,1,1), \
    syms = ['GOOG','AAPL','GLD','XOM'], \
    allocs=[0.1,0.2,0.3,0.4], \
    sv=1000000, rfr=0.0, sf=252.0, \
    gen_plot=False):

    # Read in adjusted closing prices for given symbols, date range
    dates = pd.date_range(sd, ed)
    prices_all = get_data(syms, dates)  # automatically adds SPY
    prices = prices_all[syms]  # only portfolio symbols
    prices_SPY = prices_all['SPY']  # only SPY, for comparison later

    # Get daily portfolio value

    # Get portfolio statistics (note: std_daily_ret = volatility)
    cr, adr, sddr, sr = [0.25, 0.001, 0.0005,
                         2.1]  # add code here to compute stats

    port_val = prices_SPY  # add code here to compute daily portfolio values

    normed = prices / prices.iloc[0]

    #print (allocs)
    alloced = normed * allocs

    pos_vals = alloced * sv
    port_val = pos_vals.sum(axis=1)

    normed_SPY = prices_SPY / prices_SPY.iloc[0]

    daily_rets = port_val / port_val.shift(
        1) - 1  #by 1 row, subtracted by 1 to view change

    ev = sv
    ev = port_val[len(pos_vals.index) - 1]
    cr = ev / port_val[0] - 1  #portval.length?
    adr = daily_rets.mean()  #check that avg. daily return is correct
    sddr = daily_rets.std()
    sr_annualized = math.sqrt(sf)
    sr = sr_annualized * (adr - rfr) / (sddr - rfr)
    # Compare daily portfolio value with SPY using a normalized plot
    if gen_plot:
        # add code to plot here
        #plot_data( normed, title="Normalized Stock Prices", ylabel="Normalized price")
        #df_temp = pd.concat([port_val, prices_SPY], keys=['Portfolio', 'SPY'], axis=1)
        df_temp = pd.concat([port_val / sv, normed_SPY],
                            keys=['Portfolio', 'SPY'],
                            axis=1)
        #df_temp = pd.concat([normed_SPY], keys=['Portfolio', 'SPY'], axis=1)

        #df_temp = pd.concat([port_val], keys=['Portfolio', 'SPY'], axis=1)

        #plot_data([port_val, prices_SPY], title="Daily portfolio value and SPY", ylabel="Normalized price")
        plot_data(df_temp,
                  title='Daily portfolio value and SPY',
                  ylabel='Normalized price')
        pass

    # Add code here to properly compute end value
    return cr, adr, sddr, sr, ev
Ejemplo n.º 17
0
def test_run():

	# 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 # of bins to 20

	# 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()
Ejemplo n.º 18
0
 def SMA(self, sd, ed, stock, SMA_Nday):
     dates = pd.date_range(sd, ed)
     prices1 = ut.get_data([stock], dates)
     nsd = sd - dt.timedelta(days=100)
     ndates = pd.date_range(nsd, ed)
     prices = ut.get_data([stock], ndates)
     prices_apple = prices[[stock]]
     # plot_data(prices)
     SMA_apple = pd.rolling_mean(prices_apple,
                                 SMA_Nday,
                                 min_periods=1,
                                 center=False)
     price_SMA = prices_apple / SMA_apple
     price_SMA_norm = (price_SMA - price_SMA.mean()) / price_SMA.std()
     #    price_SMA = price_SMA/price_SMA.ix[SMA_Nday]
     if False:
         SMA_apple = SMA_apple / prices_apple.ix[0]
         prices_n = prices_apple / prices_apple.ix[0]
         SMA_all = pd.concat([prices_n, SMA_apple, price_SMA],
                             keys=['Price', 'SMA', 'Price/SMA'],
                             axis=1)
         ut.plot_data(SMA_all,
                      title='Technical indicator: SMA',
                      ylabel='Relative Value')
     return pd.DataFrame(price_SMA_norm, index=prices1.index).fillna(0)
Ejemplo n.º 19
0
def test_run():

    # 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 # of bins to 20

    # 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()
Ejemplo n.º 20
0
def assess_portfolio(sd = dt.datetime(2008,1,1), ed = dt.datetime(2009,1,1), \
    syms = ['GOOG','AAPL','GLD','XOM'], \
    allocs=[0.1,0.2,0.3,0.4], \
    sv=1000000, rfr=0.0, sf=252.0, \
    gen_plot=False):

    # Read in adjusted closing prices for given symbols, date range
    dates = pd.date_range(sd, ed)
    prices_all = get_data(syms, dates)  # automatically adds SPY
    prices = prices_all[syms]  # only portfolio symbols
    prices_SPY = prices_all['SPY']  # only SPY, for comparison later
    prices_SPY = (prices_SPY/prices_SPY.iloc[0])*sv

    # Get daily portfolio value
    port_val = get_portfolio_value(prices, allocs, sv)
    
    # Get portfolio statistics (note: std_daily_ret = volatility)
    cr, adr, sddr, sr = get_portfolio_stats(port_val, rfr, sf)
    
    # Compare daily portfolio value with SPY using a normalized plot
    if gen_plot:
        # Plot normalized portfolio value.
        df_temp = pd.concat([port_val/sv, prices_SPY/sv], keys=['Portfolio', 'SPY'], axis=1)
        plot_data(df_temp,
                  title="Daily portfolio value and SPY",
                  ylabel="Normalized price")
        
    # Compute end value
    ev = port_val[-1]

    return cr, adr, sddr, sr, ev
def assess_portfolio(portfolio=None,sd = dt.datetime(2008,1,1), ed = dt.datetime(2009,1,1), \
    syms = ['GOOG','AAPL','GLD','XOM'], \
    allocs=[0.1,0.2,0.3,0.4], \
    sv=1000000, rfr=0.0, sf=252.0,precios=0, \
    gen_plot=False):

    # Read in adjusted closing prices for given symbols, date range
    dates = pd.date_range(sd, ed)
    if(type(portfolio)==type(None)):
        prices_all = get_data(syms, dates)  # automatically adds SPY
        prices = prices_all[syms]  # only portfolio symbols
        prices_SPY = prices_all['SPY']  # only SPY, for comparison later
    else:
        #pdb.set_trace()
        prices_all = get_data(syms,dates)
        prices = portfolio# pd.concat([portfolio,prices_all[syms]],axis=1)
        prices_SPY = prices_all[syms]
    # Get daily portfolio value

    
    
    
    if( (isinstance(precios,pd.core.series.Series))):
        pdb.set_trace()
        precios = precios.to_frame()
        normalized_values = normalize_data(precios)
        
    else:
        normalized_values = normalize_data(prices)

    
    alloced_values    = normalized_values*allocs
    pos_val           = alloced_values*sv
    port_val          = pos_val.sum(axis=1) # add code here to compute daily portfolio values

    d_returns         = daily_returns(port_val)
    cr                = port_val.iloc[-1]/port_val.iloc[0] -1
    adr               = d_returns.mean()
    sddr              = d_returns.std()
    sr                = (adr/sddr)*np.sqrt(252)
    prices_SPY = normalize_data(prices_SPY)
    port_val   = normalize_data(port_val)
    port_val   = pd.DataFrame(data=port_val,index=port_val.index,columns=['Portfolio'])
    # Get portfolio statistics (note: std_daily_ret = volatility)
    #cr, adr, sddr, sr = [0.25, 0.001, 0.0005, 2.1] # add code here to compute stats

    # Compare daily portfolio value with SPY using a normalized plot
    if gen_plot:
        # add code to plot here
        if(type(portfolio)==type(None)):
            df_temp = pd.concat([port_val, prices_SPY], keys=['Portfolio', 'SPY'], axis=1)
        else:
            #pdb.set_trace()
            df_temp = pd.concat([port_val, prices_SPY], axis=1)
        plot_data(df_temp)

    # Add code here to properly compute end value
    ev = sv

    return(cr, adr, sddr, sr, ev)
Ejemplo n.º 22
0
 def RSI(self, sd, ed, stock, SMA_Nday):
     dates = pd.date_range(sd, ed)
     prices1 = ut.get_data([stock], dates)
     nsd = sd - dt.timedelta(days=100)
     ndates = pd.date_range(nsd, ed)
     prices = ut.get_data([stock], ndates)
     prices_apple = prices[[stock]]
     price_delta = prices_apple.diff()
     dUp = price_delta.copy()
     dDown = price_delta.copy()
     dUp[dUp < 0] = 0
     dDown[dDown > 0] = 0
     rUp = pd.rolling_mean(dDown, SMA_Nday, min_periods=1,
                           center=False).abs()
     rDown = pd.rolling_mean(dUp, SMA_Nday, min_periods=1,
                             center=False).abs()
     RS = rUp / rDown
     RSI = 100 - 100 / (1 + RS)
     RSI_norm = (RSI - RSI.mean()) / RSI.std()
     if False:
         prices_1 = prices_apple / prices_apple.ix[0]
         RSI_1 = RSI / RSI.ix[SMA_Nday]
         RSI_all = pd.concat([prices_1, RSI_1],
                             keys=['Price', 'RSI'],
                             axis=1)
         plt.figure(3)
         ut.plot_data(RSI,
                      title='Technical indicator: Relative Strength Index',
                      ylabel='RSI')
     return pd.DataFrame(RSI_norm, index=prices1.index).fillna(0)
def optimize_portfolio(sd=dt.datetime(2008,1,1), ed=dt.datetime(2009,1,1), \
    syms=['GOOG','AAPL','GLD','XOM'], gen_plot=False):

    # Read in adjusted closing prices for given symbols, date range
    dates = pd.date_range(sd, ed)
    prices_all = get_data(syms, dates)  # automatically adds SPY
    prices = prices_all[syms]  # only portfolio symbols
    prices_SPY = prices_all['SPY']  # only SPY, for comparison later

    # find the allocations for the optimal portfolio
    # note that the values here ARE NOT meant to be correct for a test case
     # add code here to find the allocations
    
    x0 = np.random.random(len(syms))
    x0 /= x0.sum()
    # x0=np.asarray([0.2, 0.2, 0.3, 0.3, 0.0])
    fun = lambda x: -sharp_ratio(prices.values,x)
    cons = ({ 'type': 'eq', 'fun': lambda inputs: 1 - np.sum(inputs) })
    bnds = tuple((0,None) for i in range(len(syms)))
    res = minimize(fun, x0 , method='SLSQP', bounds=bnds, constraints=cons)

    allocs = res.x

    cr, adr, sddr, sr = [0.25, 0.001, 0.0005, 2.1] # add code here to compute stats

    priceSPY=prices_SPY.values
    priceSPY /= priceSPY[0]

    price_stocks = prices.values
    price_stocks /= price_stocks[0]
    price_stocks *= allocs

    port_val = pd.DataFrame(price_stocks.sum(axis=1),index=prices.index)
    prices_SPY = pd.DataFrame(priceSPY,index=prices.index)



    # Get daily portfolio value
    # port_val = prices_SPY # add code here to compute daily portfolio values

    cr = port_val.values[-1] -1
    dr = port_val.values
    drShift = np.vstack([dr[0],dr[0:(len(dr)-1)]])
    dr = dr/drShift -1

    adr = dr.mean()
    sddr=dr.std()

    k = math.sqrt(252)

    sr = k*np.mean(dr)/np.std(dr)

    # Compare daily portfolio value with SPY using a normalized plot
    if gen_plot:
        # add code to plot here
        df_temp = pd.concat([port_val, prices_SPY], keys=['Portfolio', 'SPY'], axis=1)
        df_temp.columns=df_temp.columns.get_level_values(0)
        plot_data(df_temp, "Daily portfolio value and SPY", "Date", "Normalized prices")

    return allocs, cr, adr, sddr, sr
def assess_portfolio(sd = dt.datetime(2008,1,1), ed = dt.datetime(2009,1,1),
    syms = ['GOOG','AAPL','GLD','XOM'],
    allocs=[0.1,0.2,0.3,0.4],
    sv=1000000, rfr=0.0, sf=252.0,
    gen_plot=False):

    # Read in adjusted closing prices for given symbols, date range
    dates = pd.date_range(sd, ed)
    prices_all = get_data(syms, dates)  # automatically adds SPY
    prices = prices_all[syms]  # only portfolio symbols
    prices_SPY = prices_all['SPY']  # only SPY, for comparison later

    # Get portfolio statistics (note: std_daily_ret = volatility)
    port_val = get_portfolio_value(prices, allocs, sv)
    cr, adr, sddr, sr = compute_portfolio_stats(port_val, rfr, sf) # add code here to compute stats

    # Compare daily portfolio value with SPY using a normalized plot
    if gen_plot:
        # add code to plot here
        port_val_norm = port_val / port_val.iloc[0]
        prices_SPY_norm = prices_SPY / prices_SPY.iloc[0]
        df_temp = pd.concat([port_val_norm, prices_SPY_norm], keys=['Portfolio', 'SPY'], axis=1)
        plot_data(df_temp)
        pass

    # Add code here to properly compute end value
    ev = port_val[-1]

    return cr, adr, sddr, sr, ev
def optimize_portfolio(sd=dt.datetime(2008,1,1), ed=dt.datetime(2009,1,1), \
    syms=['GOOG','AAPL','GLD','XOM'], gen_plot=False):

    # Read in adjusted closing prices for given symbols, date range
    dates = pd.date_range(sd, ed)
    prices_all = get_data(syms, dates)  # automatically adds SPY
    prices = prices_all[syms]  # only portfolio symbols
    prices_SPY = prices_all['SPY']  # only SPY, for comparison later
    normed_SPY = prices_SPY/prices_SPY.ix[0]
    # find the allocations for the optimal portfolio
    # note that the values here ARE NOT meant to be correct for a test case
    noa = len(syms)
    noa = (noa * [1. / noa,])

    cons = ({'type': 'eq', 'fun': lambda x: np.sum(x) - 1})
    bnds = tuple((0,1) for x in range(len(syms)))
    min = co.minimize(negative_sharpe, noa, args=(prices,), method='SLSQP', bounds=bnds, constraints=cons)
    allocs = np.asarray(min.x)

    cr, adr, sddr, sr, normed_daily_portfolio = assess_portfolio(sd,ed,syms, allocs.tolist())

    # Compare daily portfolio value with SPY using a normalized plot
    if gen_plot:
        df_temp = pd.concat([normed_daily_portfolio, normed_SPY], keys=['Portfolio', 'SPY'], axis=1)
        plot_data(df_temp, title="Daily Portfolio Value and SPY", ylabel="Price")
        plt.show()
        pass

    return allocs, cr, adr, sddr, sr
Ejemplo n.º 26
0
def assess_portfolio(start_date, end_date, symbols, allocs, start_val=1):
    """Simulate and assess the performance of a stock portfolio."""
    # Read in adjusted closing prices for given symbols, date range
    dates = pd.date_range(start_date, end_date)
    prices_all = get_data(symbols, dates)  # automatically adds SPY
    prices = prices_all[symbols]  # only portfolio symbols
    prices_SPY = prices_all['SPY']  # only SPY, for comparison later

    # Get daily portfolio value
    port_val = get_portfolio_value(prices, allocs, start_val)
    plot_data(port_val, title="Daily Portfolio Value")

    # Get portfolio statistics (note: std_daily_ret = volatility)
    cum_ret, avg_daily_ret, std_daily_ret, sharpe_ratio = get_portfolio_stats(port_val)

    # Print statistics
    print "Start Date:", start_date
    print "End Date:", end_date
    print "Symbols:", symbols
    print "Allocations:", allocs
    print "Sharpe Ratio:", sharpe_ratio
    print "Volatility (stdev of daily returns):", std_daily_ret
    print "Average Daily Return:", avg_daily_ret
    print "Cumulative Return:", cum_ret

    # Compare daily portfolio value with SPY using a normalized plot
    df_temp = pd.concat([port_val, prices_SPY], keys=['Portfolio', 'SPY'], axis=1)
    plot_normalized_data(df_temp, title="Daily portfolio value and SPY")
Ejemplo n.º 27
0
def optimize_portfolio(sd = dt.datetime(2008, 1, 1), ed = dt.datetime(2009, 1, 1), \
    syms=['GOOG', 'AAPL', 'GLD', 'XOM'], gen_plot = False):

    startValue = 1000000
    tradeFrequency = 252.0

    # Read in adjusted closing prices for given symbols, date range
    dates = pd.date_range(sd, ed)
    prices_all = get_data(syms, dates)  # automatically adds SPY

    # Fill in missing data
    prices_all.fillna(method="ffill")
    prices_all.fillna(method="bfill")

    prices = prices_all[syms]  # only portfolio symbols
    prices_SPY = prices_all['SPY']  # only SPY, for comparison later

    # Find the allocations for the optimal portfolio

    # Initialize allocations array with same allocation for each stock (initial guess) & normalize prices to first entries
    allocs = np.asarray([(1.0 / len(syms)) for i in range(len(syms))])
    normalized = prices / prices.values[0]

    # Optimize allocation to minimize negative of Sharpe ratio equation (maximize Sharpe ratio)
    bounds = [(0.0, 1.0) for i in range(len(syms))]
    constraints = ({'type': 'eq', 'fun': lambda inputs: 1.0 - np.sum(inputs)})
    minimized = opt.minimize(negSharpe,
                             allocs,
                             args=(normalized, startValue),
                             method='SLSQP',
                             constraints=constraints,
                             bounds=bounds,
                             options={'disp': True})

    # Calculate portfolio value to use in calculation of return values
    new_allocs = minimized.x
    new_sddr = minimized.fun
    allocations = normalized.multiply(new_allocs)
    position_vals = allocations.multiply(startValue)
    portfolio_val = position_vals.sum(axis=1)

    # Get daily portfolio values, cumulative value, and Sharpe ratio
    daily = (portfolio_val / portfolio_val.shift(1)) - 1
    cumulative = (portfolio_val[-1] / portfolio_val[0]) - 1
    sharpe = np.sqrt(tradeFrequency) * daily.mean() / new_sddr

    # Compare daily portfolio value with SPY using a normalized plot
    if gen_plot:
        portfolio_val /= portfolio_val[0]
        prices_SPY /= prices_SPY[0]
        df_temp = pd.concat([portfolio_val, prices_SPY],
                            keys=['Portfolio', 'SPY'],
                            axis=1)
        plot_data(df_temp,
                  title="Optimal Portfolio vs. SPY Daily Comparison",
                  ylabel="Normalized Price")
        plt.savefig('plot.png')
        pass

    return new_allocs, cumulative, daily.mean(), new_sddr, sharpe
def test_run():
    # Get initial data
    start_date = '2004-08-31'
    end_date = '2016-01-01'
    dates = pd.date_range(start_date, end_date)
    symbols = ['SPY', 'VOO', 'GLD', 'XOM', 'AAPL']
    original_allocation = [0.2, 0.2, 0.2, 0.2, 0.2]
    start_val = 20000  # one million dollars!!!

    # plot the original data
    original_data = util.portfolio_daily_values(start_val, dates, symbols,
                                                original_allocation)

    util.plot_data(original_data, "Original", label='Original')

    # Plot the new data
    new_allocation = fit_line(original_data, negative_sharpe, start_val, dates,
                              symbols, original_allocation)
    new_data = util.portfolio_daily_values(start_val, dates, symbols,
                                           new_allocation)
    util.plot_data(new_data, "Optimized", label='Optimized')
    plt.legend(loc='upper right')

    print("Original Allocation=", original_allocation)
    print("New allocation=", new_allocation)

    plt.show()
Ejemplo n.º 29
0
def test_run():
    symbols = ['SPY', 'XOM', 'GOOG', 'GLD']
    dates = pd.date_range('2010-01-01', '2010-12-31')
    df =  get_data(symbols, dates)
    plot_data(df)
    #plot_selected(df, ['SPY', 'GOOG', 'IBM', 'GLD'], '2010-03-01', '2010-04-01')
    print(df.mean())
Ejemplo n.º 30
0
def test_run():
	# Read data
	dates = pd.date_range('2009-01-01', '2012-12-31')
	symbols = ['SPY', 'XOM']
	df = get_data(symbols, dates)
	plot_data(df)
	
	""" Two separate histograms ==========="""
	# 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) 
	plt.show()

	""" Histograms on the same graph ======"""
	# Compute daily returns
	daily_returns = compute_daily_returns(df)
	
	# 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()
    def generateMACDChart(price, a, b, c):
        #emas with price
        macd_line, signal_line, macd_histogram, ema_upper, ema_lower = macd(
            price, a, b, c)
        macd_data = pd.merge(price,
                             ema_upper,
                             left_index=True,
                             right_index=True)
        macd_data = pd.merge(macd_data,
                             ema_lower,
                             left_index=True,
                             right_index=True)
        macd_data = macd_data.ix[20:]
        macd_data = macd_data / macd_data.ix[0]
        macd_data.columns = [
            'Price Normalized', '26 Day EMA Normalized Price',
            '12 Day EMA Normalized Price'
        ]

        #macd indicators
        macd_line = pd.merge(macd_line,
                             signal_line,
                             left_index=True,
                             right_index=True)
        #macd_line=pd.merge(macd_line, macd_histogram, left_index=True,right_index=True)
        macd_line.columns = ['Signal Line', 'MACD Line']
        plot_data(macd_data,
                  title="EMA Prices Over Time",
                  xlabel="Date",
                  ylabel="Price")
        plot_data(macd_line,
                  title="MACD and Signal Line Over Time",
                  xlabel="Date",
                  ylabel="MACD or Signal Value")
def test_run():
    # 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_return = compute_daily_returns(df)
    plot_data(daily_returns)

    # Plot a histogram
    daily_returns.hist() # Default number of bins is 10
    daily_returns.hist(bins=20) # 20 bins

# Computing Histogram Statistics
    mean = daily_returns['SPY'].mean()
    std = daily_returns['SPY'].std()

    plt.axvline(mean)
    plt.axvline(std)
    plt.axvline(-std)
    plt.show()

    print daily_returns['SPY'].kurtosis()
Ejemplo n.º 33
0
def assess_portfolio(sd = dt.datetime(2008,1,1), ed = dt.datetime(2009,1,1), \
    syms = ['GOOG','AAPL','GLD','XOM'], \
    allocs=[0.1,0.2,0.3,0.4], \
    sv=1000000, rfr=0.0, sf=252.0, \
    gen_plot=True):
    
    # Read in adjusted closing prices for given symbols, date range
    dates = pd.date_range(sd, ed)
    prices_all = get_data(syms, dates)  # automatically adds SPY
    prices = prices_all[syms]  # only portfolio symbols
    prices_SPY = prices_all['SPY']  # only SPY, for comparison later
    # Get daily portfolio value
    alloc_val = (prices/prices.iloc[0])*allocs*sv    
    port_val = alloc_val.sum(1)
    # Get portfolio statistics (note: std_daily_ret = volatility)
    cr = (port_val[-1]-port_val[0])/port_val[0]
    adr = cr/sf
    sddr = np.std(port_val/port_val[0])
    sr = (cr-rfr)/sddr# add code here to compute stats

    # Compare daily portfolio value with SPY using a normalized plot
    norm_SPY = (prices_SPY/prices_SPY.iloc[0])*sv
    if gen_plot:
        df_temp = pd.concat([port_val, norm_SPY], keys=['Portfolio', 'SPY'], axis=1)
        plot_data(df_temp)
        pass

    # Add code here to properly compute end value
    ev = port_val[-1]

    return cr, adr, sddr, sr, ev
Ejemplo n.º 34
0
def test_run():
    # Read data
    dates = pd.date_range('2009-01-01', '2012-12-31')  # one month only
    symbols = ['SPY','XOM','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", ylabel="Daily returns")

    # 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.grid()
    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.grid()
    plt.show()

    # Calculate correlation coefficient
    print daily_returns.corr(method='pearson')
Ejemplo n.º 35
0
 def save_plot_and_model(self,
                         benchmark_values,
                         portfolio_value,
                         trades,
                         save_plot=False):
     """
     save trained model and plot result if save_plot set to True
     """
     # save transaction and portfolio image and training records
     benchmark_values = benchmark_values.rename(
         columns={'SPY': "benchmark"})
     portfolio_value = portfolio_value.rename(
         columns={'SPY': "q-learn-trader"})
     if save_plot:
         p_value_all = portfolio_value.join(benchmark_values)
         ut.plot_data(trades,
                      title="transactions_train",
                      ylabel="amount",
                      save_image=True,
                      save_dir=self.current_working_dir)
         ut.plot_data(p_value_all,
                      title="portfolio value_train",
                      ylabel="USD",
                      save_image=True,
                      save_dir=self.current_working_dir)
     self.learner.save_model(table_name=self.current_working_dir +
                             dyna_q_trader_model_save_name)
Ejemplo n.º 36
0
def calculateMetrics(data):
    #accuracy = Mean Absolute % Error MAPE
    #efficiency = time taken for training
    #25 training data sets

    eff = np.zeros([25,2])
    accu = np.zeros([25,2])
    curr = 0

    #set 1-5 : split 90:10 , shuffle 4 times
    RunMetric(data,0.9,0,eff,accu)

     # set 6-10 : split 80:20 , shuffle 4 times
    RunMetric(data, 0.8, 5, eff, accu)

    # set 11-15 : split 70:30 , shuffle 4 times
    RunMetric(data, 0.7, 10, eff, accu)

    # set 16-20 : split 60:40 , shuffle 4 times
    RunMetric(data, 0.6, 15, eff, accu)

    # set 21-25 : split 50:50 , shuffle 4 times
    RunMetric(data, 0.8, 20, eff, accu)

    dfeff = pd.DataFrame(eff, columns=['DTLearner', 'RTLearner'], index=range(1,26))
    print (dfeff)
    util.plot_data(dfeff, title="Train Time - DTLearner vs RTLearner", xlabel="Training sets", ylabel="Time Taken in sec")

    dfacc = pd.DataFrame(accu, columns=['DTLearner', 'RTLearner'], index=range(1, 26))
    print (dfacc)
    util.plot_data(dfacc, title="MAPE - DTLearner vs RTLearner", xlabel="Training sets",
                   ylabel="MAPE")
def optimize_portfolio(start_date, end_date, symbols):
    """Simulate and optimize portfolio allocations."""
    # Read in adjusted closing prices for given symbols, date range
    dates = pd.date_range(start_date, end_date)
    prices_all = get_data(symbols, dates)  # automatically adds SPY
    prices = prices_all[symbols]  # only portfolio symbols
    prices_SPY = prices_all['SPY']  # only SPY, for comparison later

    # Get optimal allocations


    allocs = find_optimal_allocations(prices)

    allocs = allocs / np.sum(allocs)  # normalize allocations, if they don't sum to 1.0

    # Get daily portfolio value (already normalized since we use default start_val=1.0)
    port_val = get_portfolio_value(prices, allocs)

    # Get portfolio statistics (note: std_daily_ret = volatility)
    cum_ret, avg_daily_ret, std_daily_ret, sharpe_ratio = get_portfolio_stats(port_val)

    # Print statistics
    print "Start Date:", start_date
    print "End Date:", end_date
    print "Symbols:", symbols
    print "Optimal allocations:", allocs
    print "Sharpe Ratio:", sharpe_ratio
    print "Volatility (stdev of daily returns):", std_daily_ret
    print "Average Daily Return:", avg_daily_ret
    print "Cumulative Return:", cum_ret

    # Compare daily portfolio value with normalized SPY
    normed_SPY = prices_SPY / prices_SPY.ix[0, :]
    df_temp = pd.concat([port_val, normed_SPY], keys=['Portfolio', 'SPY'], axis=1)
    plot_data(df_temp, title="Daily Portfolio Value and SPY")
Ejemplo n.º 38
0
def assess_portfolio(sd = dt.datetime(2008,1,1), ed = dt.datetime(2009,1,1), \
    syms = ['GOOG','AAPL','GLD','XOM'], \
    allocs=[0.1,0.2,0.3,0.4], \
    sv=1000000, rfr=0.0, sf=252.0, \
    gen_plot=False):

    # Read in adjusted closing prices for given symbols, date range
    dates = pd.date_range(sd, ed)
    prices_all = get_data(syms, dates)  # automatically adds SPY
    prices = prices_all[syms]  # only portfolio symbols

    prices_SPY = prices_all['SPY']  # only SPY, for comparison later



    # Get daily portfolio value
    #port_val = prices_SPY # add code here to compute daily portfolio values


    priceSPY=prices_SPY.values
    priceSPY /= priceSPY[0]

    price_stocks = prices.values
    price_stocks /= price_stocks[0]
    price_stocks *= allocs

    port_val = pd.DataFrame(price_stocks.sum(axis=1),index=prices.index)
    prices_SPY = pd.DataFrame(priceSPY,index=prices.index)


    # Get portfolio statistics (note: std_daily_ret = volatility)
    cr, adr, sddr, sr = [0.25, 0.001, 0.0005, 2.1] # add code here to compute stats
    cr = port_val.values[-1] -1

    dr = port_val.values
    drShift = np.vstack([dr[0],dr[0:(len(dr)-1)]])
    dr = dr/drShift -1

    adr = dr.mean()
    sddr=dr.std()

    k = math.sqrt(sf)

    sr = k*np.mean(dr-rfr)/np.std(dr-rfr)

    # Compare daily portfolio value with SPY using a normalized plot

    if gen_plot:
        # add code to plot here
        df_temp = pd.concat([port_val, prices_SPY], keys=['Portfolio', 'SPY'], axis=1)
        df_temp.columns=df_temp.columns.get_level_values(0)
        plot_data(df_temp, "Daily portfolio value and SPY", "Date", "Normalized prices")


    # Add code here to properly compute end value

    ev = sv*port_val.values[-1]

    return cr, adr, sddr, sr, ev
Ejemplo n.º 39
0
def plot_normalized_data(df, title="Normalized prices", xlabel="Date", ylabel="Normalized price"):
    """Normalize given stock prices and plot for comparison.

    Parameters
    ----------
        df: DataFrame containing stock prices to plot (non-normalized)
        title: plot title
        xlabel: X-axis label
        ylabel: Y-axis label
    """
    normdf = df.divide(df.ix[0])
    plot_data(normdf, title=title, xlabel=xlabel, ylabel=ylabel)
Ejemplo n.º 40
0
def plot_normalized_data(df, title="Normalized prices", xlabel="Date", ylabel="Normalized price"):
    """Normalize given stock prices and plot for comparison.

    Parameters
    ----------
        df: DataFrame containing stock prices to plot (non-normalized)
        title: plot title
        xlabel: X-axis label
        ylabel: Y-axis label
    """
    #TODO: Your code here
    plot_data((df / df.ix[0,:]), title, xlabel,ylabel)
Ejemplo n.º 41
0
def main():
	dates = pd.date_range('2009-01-01', '2012-12-31')
	symbols = ['SPY', 'XOM', 'GLD']
	df = get_data(symbols, dates)
	plot_data(df)

	daily_returns = compute_daily_returns(df)

	daily_returns.plot(kind='scatter', x='SPY', y='XOM')
	beta_XOM, alpha_XOM = np.polyfit(daily_returns['SPY'], daily_returns['XOM'], 1)

	plt.show()

	daily_returns.plot(kind='scatter', x='SPY', y='GLD')
	plt.show()
Ejemplo n.º 42
0
def plot_normalized_data(df, title="Normalized prices", xlabel="Date", ylabel="Normalized price"):
    """Normalize given stock prices and plot for comparison.

    Parameters
    ----------
        df: DataFrame containing stock prices to plot (non-normalized)
        title: plot title
        xlabel: X-axis label
        ylabel: Y-axis label
    """
    #TODO: Your code here
    
    # Normalise the data frame
    df_temp = df / df.iloc[0]
    
    # plot the normalized data
    plot_data(df_temp)
Ejemplo n.º 43
0
def test_code():
    # this is a helper function you can use to test your code
    # note that during autograding his function will not be called.
    # Define input parameters

    of = "./orders/orders.csv"
    sv = 10000

    # Process orders
    portvals = compute_portvals(orders_file=of, start_val=sv)
    if isinstance(portvals, pd.DataFrame):
        portvals = portvals[portvals.columns[0]]  # just get the first column
    else:
        "warning, code did not return a DataFrame"

    # Get portfolio stats
    # Here we just fake the data. you should use your code from previous assignments.
    #start_dates = dt.datetime(2008, 1, 1)
    #end_dates = dt.datetime(2008, 6, 1)
    #cum_ret, avg_daily_ret, std_daily_ret, sharpe_ratio = [0.2, 0.01, 0.02, 1.5]
    #cum_ret_SPY, avg_daily_ret_SPY, std_daily_ret_SPY, sharpe_ratio_SPY = [0.2, 0.01, 0.02, 1.5]

    cum_ret, avg_daily_ret, std_daily_ret, sharpe_ratio, portfolio_value, start_dates, end_dates = compute_portfolio_stats(portvals)
    cum_ret_SPY, avg_daily_ret_SPY, std_daily_ret_SPY, sharpe_ratio_SPY, spy_value, start_dates, end_dates  = compute_portfolio_stats(get_data(['$SPX'], dates = pd.date_range(start_dates, end_dates)))
    portfolio_value = portfolio_value.to_frame()
    portfolio_value.columns=[['Portfolio']]
    vals = pd.concat([portfolio_value/portfolio_value.iloc[0], spy_value/spy_value.iloc[0]], axis = 1)
    plot_data(vals)

    # Compare portfolio against $SPX
    print
    print "Date Range: {} to {}".format(start_dates, end_dates)
    print
    print "Sharpe Ratio of Fund: {}".format(sharpe_ratio)
    print "Sharpe Ratio of $SPX : {}".format(sharpe_ratio_SPY)
    print
    print "Cumulative Return of Fund: {}".format(cum_ret)
    print "Cumulative Return of $SPX : {}".format(cum_ret_SPY)
    print
    print "Standard Deviation of Fund: {}".format(std_daily_ret)
    print "Standard Deviation of $SPX : {}".format(std_daily_ret_SPY)
    print
    print "Average Daily Return of Fund: {}".format(avg_daily_ret)
    print "Average Daily Return of $SPX : {}".format(avg_daily_ret_SPY)
    print
    print "Final Portfolio Value: {}".format(portvals[-1])
Ejemplo n.º 44
0
def test_run():

	# 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['SPY'].hist(bins=20, label="SPY")
	daily_returns['XOM'].hist(bins=20, label="XOM")
	plt.legend(loc='upper right')
	
	plt.show()
Ejemplo n.º 45
0
def test_run():
   
    
    #Testing
    testing_set = data_set.ix[test_dates]
    testX = testing_set.iloc[:, 1:4].as_matrix()
    testY = testing_set['Prediction'].as_matrix()
    predY = learner.query( testX ) # get the predictions
        
    #Build dataframe to show results    
    results = pd.DataFrame( predY, columns = ['PredictedY'], index = test_dates )
    results['RealizedY'] = testY
    results['Error'] = (testY - predY)
    rmse = np.sqrt(((testY - predY) ** 2).sum()/testY.shape[0])
    
    plot_data(results[ ['PredictedY', 'RealizedY'] ] , title="Realized vs Predicted", xlabel="Date", ylabel="Price", filename=None)
    plot_data( results['Error'], title="Prediction error", xlabel="Date", ylabel="Error", filename=None)
   
    print rmse
Ejemplo n.º 46
0
def assess_portfolio(sd = dt.datetime(2008,1,1), ed = dt.datetime(2009,1,1), \
    syms = ['GOOG','AAPL','GLD','XOM'], \
    allocs=[0.1,0.2,0.3,0.4], \
    sv=1000000, rfr=0.0, sf=252.0, \
    gen_plot=False):

    # Read in adjusted closing prices for given symbols, date range
    dates = pd.date_range(sd, ed)
    prices_all = get_data(syms, dates)  # automatically adds SPY
    prices = prices_all[syms]  # only portfolio symbols
    prices_SPY = prices_all['SPY']  # only SPY, for comparison later

    # Get daily portfolio value
    #port_val = prices_SPY # add code here to compute daily portfolio values
    norm=prices/prices.ix[0,:]
    norm_allocation=allocs*norm
    actual_val=sv*norm_allocation
    port_val=actual_val.sum(axis=1)

    # Get portfolio statistics (note: std_daily_ret = volatility)
    #cr, adr, sddr, sr = [0.25, 0.001, 0.0005, 2.1] # add code here to compute stats
    daily_returns = (port_val / port_val.shift(1)) - 1
    adr = daily_returns.mean()
    cr = (port_val[-1]/port_val[0]) - 1
    sddr = daily_returns.std()

    k = np.sqrt(sf)
    sr = k * np.mean(adr - rfr)/sddr
    
    # Compare daily portfolio value with SPY using a normalized plot
    if gen_plot:
        # add code to plot here
        df_temp = pd.concat([port_val, prices_SPY], keys=['Portfolio', 'SPY'], axis=1)
        plot_data(df_temp, title="Daily portfolio value and SPY", xlabel="Date", ylabel="Normalized price")
        pass

    # Add code here to properly compute end value
    ev = port_val[ed]

    return cr, adr, sddr, sr, ev
Ejemplo n.º 47
0
def assess_portfolio(sd = dt.datetime(2008,1,1), ed = dt.datetime(2009,1,1), \
    syms = ['GOOG','AAPL','GLD','XOM'], \
    allocs=[0.1,0.2,0.3,0.4], \
    sv=1000000, rfr=0.0, sf=252.0, \
    gen_plot=False):

    # Read in adjusted closing prices for given symbols, date range
    dates = pd.date_range(sd, ed)
    prices_all = get_data(syms, dates)  # automatically adds SPY
    prices = prices_all[syms]  # only portfolio symbols
    prices_SPY = prices_all['SPY']  # only SPY, for comparison later

    # Get portfolio statistics (note: std_daily_ret = volatility)
    cr, adr, sddr, sr, port_val = compute_portfolio_stats(prices, sv, allocs, rfr, sf)

    # Compare daily portfolio value with SPY using a normalized plot
    if gen_plot:
        # add code to plot here
        df_temp = pd.concat([port_val/port_val[0], prices_SPY/prices_SPY[0]], keys=['Portfolio', 'SPY'], axis=1)
        plot_data(df_temp, ylabel = "Normalized Price")
        pass

    return cr, adr, sddr, sr, sv
Ejemplo n.º 48
0
def assess_portfolio(sd = dt.datetime(2008,1,1), ed = dt.datetime(2009,1,1), \
    syms = ['GOOG','AAPL','GLD','XOM'], \
    allocs = [0.1,0.2,0.3,0.4], \
    sv = 1000000, rfr = 0.0, sf = 252.0, \
    gen_plot = True):

    # Read in adjusted closing prices for given symbols + date range
    dates = pd.date_range(sd, ed)
    prices_all = get_data(syms, dates)  # automatically adds SPY
    prices = prices_all[syms]  # only portfolio symbols
    prices_SPY = prices_all['SPY']  # only SPY, for comparison later

    # Get daily portfolio value
    prices_SPY = prices_SPY / prices_SPY[0]
    normed_prices = prices / prices.ix[0]
    alloced = normed_prices * allocs  # allocating weights accordingly
    print "Allocation to each stock in portfolio over each day is:"
    print (alloced)

    init_port_distrib = sv * alloced
    port_vals = init_port_distrib.sum(axis=1) 

    # Get portfolio statistics 
    cr, adr, sddr, sr = compute_portfolio_stats(prices, allocs, rfr, sf)

    # Compare daily portfolio value with SPY using a normalized plot
    if gen_plot:
        df_temp = pd.concat([port_vals/sv, prices_SPY], keys=['Portfolio', 'SPY'], axis=1)
        plot_data(df_temp, ylabel="Normalized Price")
        plt.show()
        pass

    # Compute end value
    ev = sv * (cr + 1)

    return cr, adr, sddr, sr, ev
Ejemplo n.º 49
0
    rmse = math.sqrt(((testY - testPredY) ** 2).sum() / testY.shape[0])
    print
    print "Out of sample results"
    print "RMSE: ", rmse
    c = np.corrcoef(testPredY, y=testY)
    print "corr: ", c[0,1]

    testPredY_pd = pd.DataFrame(data = testPredY, index=testY_pd.index, columns=[symbol])
    plotTestY =testPredY_pd*testPrices_pd + testPrices_pd
    plotTestPredY = testPredY_pd*testPrices_pd + testPrices_pd

    testorders, testlong_orders, testshort_orders, testexit_orders = find_orders(testPredY_pd, 0.025)
    testportval = compute_portvals(testorders, start_val=10000)
    print "Returns: ", testportval.iloc[-1]/testportval.iloc[0]

    plotTestPredData = pd.concat([testPrices_pd, plotTrainY, plotTrainPredY], axis=1)
    plotTestPredData.columns = ['Training Prices', 'TrainY', 'PredY']


    #TODO If you want to plot...
    # plot_data_2axes(plotTrainPredData, ['Predicted Y', 'Training Y'])
    plot_data(plotTrainPredData, title='Training Y/Price/Predicted Y:') #Prediction Plot
    # plot_data(plotTestPredData)
    # plot_corr(testY, testPredY)
    plot_orders(trainPrices_pd, trainlong_orders, trainshort_orders, trainexit_orders, title='In Sample Trading Data') #Entries/Exits In-Sample
    plot_orders(trainportval/trainportval[0], trainlong_orders, trainshort_orders, trainexit_orders, title='In Sample Backtest', ylabel="Cumulative Return") #In Sample Backtest
    plot_orders(testPrices_pd, testlong_orders, testshort_orders, testexit_orders, title='Out of Sample Trading Data') #Entries/Exits Out-Of-Sample
    plot_orders(testportval/testportval[0], testlong_orders, testshort_orders, testexit_orders, title='Out of Sample Backtest', ylabel="Cumulative Return") #Out of Sample Backtest


    print 'Debug Here'
def test_run():
    """Driver function."""
    # Define input parameters
    start_date = "2007-12-31"
    end_date = "2015-12-31"

    # Get quotations
    stock_symbol = ["IBM"]
    dates = pd.date_range(start_date, end_date)
    stock_prices = get_data(stock_symbol, dates, addSPY=False)
    stock_prices.dropna(inplace=True)

    # Learning and test set dates
    # Get only valid dates with non NaN values
    n_dates = stock_prices.shape[0]
    learning_dates = stock_prices.index.values[0 : int(n_dates * 0.60)]
    test_dates = stock_prices.index.values[int(n_dates * 0.60) :]

    # print stock_prices.ix[learning_dates,]
    print "Learning set from ", learning_dates[0], " to ", learning_dates[-1]
    print "Test set from ", test_dates[0], " to ", test_dates[-1]

    # Get indicators
    bollinger_ind = Bollinger.Bollinger()
    momentum_ind = Momentum.Momentum()
    volatility_ind = Volatility.Volatility()
    bollinger_ind.addPriceSeries(stock_prices)
    momentum_ind.addPriceSeries(stock_prices)
    volatility_ind.addPriceSeries(stock_prices)

    # Get data to be predicted
    future_return = stock_prices / stock_prices.shift(5) - 1
    future_return.columns = ["Prediction"]

    # Merge in a dataframe, combining the three predictors
    data_set = future_return.join(bollinger_ind.getIndicator(), how="inner")
    data_set = data_set.join(momentum_ind.getIndicator(), how="inner")
    data_set = data_set.join(volatility_ind.getIndicator(), how="inner")
    data_set.dropna(inplace=True)
    # print learning_set

    # Learning
    learner = BagLearner.BagLearner()
    learning_set = data_set.ix[learning_dates]
    trainX = learning_set.iloc[:, 1:4].as_matrix()
    trainY = learning_set["Prediction"].as_matrix()
    learner.addEvidence(trainX, trainY)

    # Testing
    testing_set = data_set.ix[test_dates]
    testX = testing_set.iloc[:, 1:4].as_matrix()
    testY = testing_set["Prediction"].as_matrix()
    predY = learner.query(testX)  # get the predictions

    # Build dataframe to show results
    results = pd.DataFrame(predY, columns=["PredictedY"], index=test_dates)
    results["RealizedY"] = testY
    results["Error"] = testY - predY
    rmse = np.sqrt(((testY - predY) ** 2).sum() / testY.shape[0])

    plot_data(
        results[["PredictedY", "RealizedY"]],
        title="Realized vs Predicted",
        xlabel="Date",
        ylabel="Price",
        filename=None,
    )
    plot_data(results["Error"], title="Prediction error", xlabel="Date", ylabel="Error", filename=None)

    print rmse
Ejemplo n.º 51
0
def plot_normalized_data(df, title="Daily portfolio value and SPY", xlabel="Date", ylabel="Normalized price"):
    """Plot stock prices with a custom title and meaningful axis labels."""
    df_temp = pd.concat([df, prices_SPY/sv], keys=['Portfolio', 'SPY'], axis=1)
    plot_data(df_temp, title, xlabel, ylabel)
Ejemplo n.º 52
0
def test_run():
    # Read data and plot
    dates = pd.date_range('2009-01-01', '2012-12-31')
    symbols = ['SPY', 'XOM', 'GLD', 'GOOG']
    initialize = 'n' # toggles whether or not to initialize with SPY data (must be 'n' if SPY is in symbols)
    df = get_data(symbols, dates, initialize)
    plot_data(df)

    # Compute daily returns
    daily_returns = compute_daily_returns(df)
    plot_data(daily_returns, title='Daily Returns', xlabel='Date', ylabel='Daily Returns')

    # Plot a histogram
    daily_returns.hist(bins=20)

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

    # Scatterplot SPY vs GOOG
    daily_returns.plot(kind='scatter', title='Correlation SPY vs GOOG', x='SPY', y='GOOG')
    # beta is the slope of the line in the scatterplot, and alpha is the intercept
    beta_GOOG, alpha_GOOG = np.polyfit(daily_returns['SPY'], daily_returns['GOOG'], 1) # this returns the alpha and beta for GOOG
    # below '-' indicates a line should be drawn
    plt.plot(daily_returns['SPY'], beta_GOOG*daily_returns['SPY'] + alpha_GOOG, '-', color='r') # plots a regression curve using y = mx + b
    print '\nGOOG beta = ', beta_GOOG
    print 'GOOG alpha = ', alpha_GOOG # higher alpha would mean the stock performed better than the SPY
    plt.show()

    # Scatterplot SPY vs XOM
    daily_returns.plot(kind='scatter', title='Correlation SPY vs XOM', x='SPY', y='XOM')
    # beta is the slope of the line in the scatterplot, and alpha is the intercept
    beta_XOM, alpha_XOM = np.polyfit(daily_returns['SPY'], daily_returns['XOM'], 1) # this returns the alpha and beta for XOM
    # below '-' indicates a line should be drawn
    plt.plot(daily_returns['SPY'], beta_GOOG*daily_returns['SPY'] + alpha_XOM, '-', color='r') # plots a regression curve using y = mx + b
    print '\nXOM beta = ', beta_XOM
    print 'XOM alpha = ', alpha_XOM # higher alpha would mean the stock performed better than the SPY
    plt.show()

    # Calculate Correlation Coefficient
    # this uses pearson correlation coeff method to estimate how closely the data points fit the regression line
    print '\nCorrelation Coefficients:\n', daily_returns.corr(method='pearson')

    # plot separate plots for each stock
    plt.axvline(mean, color='w', linestyle='dashed', linewidth=2) # adds vertical dashed line at the mean
    plt.axvline(std, color='r', linestyle='dashed', linewidth=2)
    plt.axvline(-std, color='r', linestyle='dashed', linewidth=2)
    plt.show()

    # plot overlayed plots
    daily_returns['SPY'].hist(bins=20, label='SPY')
    daily_returns['XOM'].hist(bins=20, label='XOM')
    daily_returns['GLD'].hist(bins=20, label='GLD')
    plt.legend(loc='upper right')
    plt.show()

    # Compute kurtosis
    # This is how closely a dataset fits a gaussian normal distribution +num has longer tails, -num shorter tails
    print '\nKurtosis:\n', daily_returns.kurtosis()