コード例 #1
0
def simulateOrders(start_date, end_date, orders_file, start_val, title="Portfolio Value"):
    # Process orders
    portvals = compute_portvals(start_date, end_date, orders_file, start_val)
    if isinstance(portvals, pd.DataFrame):
        portvals = portvals[portvals.columns[0]]  # if a DataFrame is returned select the first column to get a Series

    # Get portfolio stats
    cum_ret, avg_daily_ret, std_daily_ret, sharpe_ratio = get_portfolio_stats(portvals)

    # Simulate a $SPX-only reference portfolio to get stats
    prices_SPX = get_data(['$SPX'], pd.date_range(start_date, end_date))
    prices_SPX = prices_SPX[['$SPX']]  # remove SPY
    portvals_SPX = get_portfolio_value(prices_SPX, [1.0])
    cum_ret_SPX, avg_daily_ret_SPX, std_daily_ret_SPX, sharpe_ratio_SPX = get_portfolio_stats(portvals_SPX)

    # Compare portfolio against $SPX
    print "Data Range: {} to {}".format(start_date, end_date)
    print
    print "Sharpe Ratio of Fund: {}".format(sharpe_ratio)
    print "Sharpe Ratio of $SPX: {}".format(sharpe_ratio_SPX)
    print
    print "Cumulative Return of Fund: {}".format(cum_ret)
    print "Cumulative Return of $SPX: {}".format(cum_ret_SPX)
    print
    print "Standard Deviation of Fund: {}".format(std_daily_ret)
    print "Standard Deviation of $SPX: {}".format(std_daily_ret_SPX)
    print
    print "Average Daily Return of Fund: {}".format(avg_daily_ret)
    print "Average Daily Return of $SPX: {}".format(avg_daily_ret_SPX)
    print
    print "Final Portfolio Value: {}".format(portvals[-1])

    # Plot computed daily portfolio value
    df_temp = pd.concat([portvals, prices_SPX['$SPX']], keys=['Portfolio', 'SPY'], axis=1)
    plot_normalized_data(df_temp, title)
コード例 #2
0
ファイル: marketsim.py プロジェクト: kesamet/ML4T
def test_run():
    """Driver function."""
    # Define input parameters
    # Test 1
#    start_date = '2011-01-05'
#    end_date = '2011-01-20'
#    orders_file = os.path.join(".\orders", "orders-short.csv")
#    start_val = 1000000
    
    # Test 2
#    start_date = '2011-01-10'
#    end_date = '2011-12-20'
#    orders_file = os.path.join(".\orders", "orders.csv")
#    start_val = 1000000
    
    # Test 3
    start_date = '2011-01-14'
    end_date = '2011-12-14'
    orders_file = os.path.join(".\orders", "orders2.csv")
    start_val = 1000000
    
    
    # Process orders
    portvals = compute_portvals(start_date, end_date, orders_file, start_val)
    if isinstance(portvals, pd.DataFrame):
        portvals = portvals[portvals.columns[0]]  # if a DataFrame is returned select the first column to get a Series
    
    # Get portfolio stats
    cum_ret, avg_daily_ret, std_daily_ret, sharpe_ratio = get_portfolio_stats(portvals)

    # Simulate a $SPX-only reference portfolio to get stats
    prices_SPX = get_data(['$SPX'], pd.date_range(start_date, end_date))
    prices_SPX = prices_SPX[['$SPX']]  # remove SPY
    portvals_SPX = get_portfolio_value(prices_SPX, [1.0])
    cum_ret_SPX, avg_daily_ret_SPX, std_daily_ret_SPX, sharpe_ratio_SPX = get_portfolio_stats(portvals_SPX)

    # Compare portfolio against $SPX
    print "Data Range: {} to {}".format(start_date, end_date)
    print
    print "Sharpe Ratio of Fund: {}".format(sharpe_ratio)
    print "Sharpe Ratio of $SPX: {}".format(sharpe_ratio_SPX)
    print
    print "Cumulative Return of Fund: {}".format(cum_ret)
    print "Cumulative Return of $SPX: {}".format(cum_ret_SPX)
    print
    print "Standard Deviation of Fund: {}".format(std_daily_ret)
    print "Standard Deviation of $SPX: {}".format(std_daily_ret_SPX)
    print
    print "Average Daily Return of Fund: {}".format(avg_daily_ret)
    print "Average Daily Return of $SPX: {}".format(avg_daily_ret_SPX)
    print
    print "Final Portfolio Value: {}".format(portvals[-1])

    # Plot computed daily portfolio value
    df_temp = pd.concat([portvals, prices_SPX['$SPX']], keys=['Portfolio', '$SPX'], axis=1)
    plot_normalized_data(df_temp, title="Daily portfolio value and $SPX")
コード例 #3
0
def testcode_marketsim(symbol = 'ML_based', base_dir = './orders/', \
                       sv = 100000, leverLimit = True, verbose = True):
    ###    Use one of the order folders below    ###
    #    of = "./orders/benchmark.csv"
    #    of = "./orders/bestPossibleStrategy.csv"
    #    of = "./orders/rule_based.csv"
    #    of = "./orders/ML_based.csv"
    of = symbol_to_path(symbol, base_dir)

    #    sv = 100000 # starting value of portfolio, i.e. initial cash available

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

    start_date = portVals.index[0]
    end_date = portVals.index[-1]
    pricesSPX = get_data(['$SPX'], pd.date_range(start_date, end_date))
    pricesSPX = pricesSPX['$SPX']

    cum_ret, avg_daily_ret, std_daily_ret, sharpe_ratio = get_portfolio_stats(portVals, \
                                                daily_rf = 0, samples_per_year = 252)
    cum_ret_SPY, avg_daily_ret_SPY, std_daily_ret_SPY, sharpe_ratio_SPY = \
            get_portfolio_stats(pricesSPX, daily_rf = 0, samples_per_year = 252)

    # Compare portfolio against $SPX
    if verbose == True:
        dfTemp = pd.concat([portVals, pricesSPX],
                           axis=1,
                           keys=['portfolio', '$SPX'])
        plot_normalized_data(dfTemp, '', '', '')

        print "\nDate Range: {} to {}".format(start_date.date(),
                                              end_date.date())
        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])

    return cum_ret, portVals
コード例 #4
0
def test_code():
    ###    Use one of the order folders below    ###
    ###    of = "./orders/orders-leverage-3.csv" # verify from wiki
    of = "./orders_mc2p1_spr2016/orders-12-modified.csv"  #verify from saved pdf
    sv = 1000000  # starting value of portfolio, i.e. initial cash available

    # 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 as a Series
    else:
        "warning, code did not return a DataFrame"

    start_date = portVals.index[0]
    end_date = portVals.index[-1]
    pricesSPX = get_data(['$SPX'], pd.date_range(start_date, end_date))
    pricesSPX = pricesSPX['$SPX']

    dfTemp = pd.concat([portVals, pricesSPX],
                       axis=1,
                       keys=['portfolio', '$SPX'])
    plot_normalized_data(dfTemp, '', '', '')

    cum_ret, avg_daily_ret, std_daily_ret, sharpe_ratio = get_portfolio_stats(portVals, \
                                                daily_rf = 0, samples_per_year = 252)
    cum_ret_SPY, avg_daily_ret_SPY, std_daily_ret_SPY, sharpe_ratio_SPY = \
            get_portfolio_stats(pricesSPX, daily_rf = 0, samples_per_year = 252)

    # Compare portfolio against $SPX
    print "\nDate Range: {} to {}".format(start_date.date(), end_date.date())
    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])
コード例 #5
0
ファイル: marketsim.py プロジェクト: bryrmeg/stocks
def test_run(start_date, end_date, orders, start_val,plot=True):
    # Process orders
    portvals = compute_portvals(start_date, end_date, orders, start_val)
    if isinstance(portvals, pd.DataFrame):
        portvals = portvals[portvals.columns[0]]  # if a DataFrame is returned select the first column to get a Series
    
    # Get portfolio stats
    cum_ret, avg_daily_ret, std_daily_ret, sharpe_ratio = get_portfolio_stats(portvals)

    # Simulate a SPY-only reference portfolio to get stats
    prices_SPX = get_data(['SPY'], pd.date_range(start_date, end_date))
    prices_SPX = prices_SPX[['SPY']]  # remove SPY
    portvals_SPX = get_portfolio_value(prices_SPX, [1.0])
    cum_ret_SPX, avg_daily_ret_SPX, std_daily_ret_SPX, sharpe_ratio_SPX = get_portfolio_stats(portvals_SPX)

    # Compare portfolio against $SPX
    print "Data Range: {} to {}".format(start_date, end_date)
    print
    print "Sharpe Ratio of Fund: {}".format(sharpe_ratio)
    print "Sharpe Ratio of $SPX: {}".format(sharpe_ratio_SPX)
    print
    print "Cumulative Return of Fund: {}".format(cum_ret)
    print "Cumulative Return of $SPX: {}".format(cum_ret_SPX)
    print
    print "Standard Deviation of Fund: {}".format(std_daily_ret)
    print "Standard Deviation of $SPX: {}".format(std_daily_ret_SPX)
    print
    print "Average Daily Return of Fund: {}".format(avg_daily_ret)
    print "Average Daily Return of $SPX: {}".format(avg_daily_ret_SPX)
    print
    print "Final Portfolio Value: {}".format(portvals[-1])
    print "Final SPY Value: {}".format(portvals_SPX[-1]*start_val)
    
    # Plot computed daily portfolio value
    if plot == True:
        df_temp = pd.concat([portvals, prices_SPX['SPY']], keys=['Portfolio', 'SPY'], axis=1)
        plot_normalized_data(df_temp, title="Daily portfolio value", ylabel="Normalized Price")
コード例 #6
0
def optimize_portfolio(sd=dt.datetime(2008,1,1), ed=dt.datetime(2009,1,1), \
    syms=['GOOG','AAPL','GLD','XOM'], gen_plot=False):
    start_val = 1000000
    daily_rf = 0
    samples_per_year = 252
    # 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
    allocGuess = np.ones(len(syms), dtype='float32') / len(syms)
    setBnds = tuple([(0, 1) for x, y in enumerate(allocGuess)
                     ])  #create tuple of (0,1) tuples
    # 'constraints' below constrains allocations to sum to 1
    # and 'setBnds' forces each allocation to lie in (0,1)
    srMax = spo.minimize(cost, allocGuess, bounds = setBnds, \
            constraints = ({ 'type': 'eq', 'fun': lambda inputs: 1.0 - np.sum(inputs) }), \
            args = (prices,start_val,daily_rf,samples_per_year,), \
            method = 'SLSQP', options = {'disp': True})
    allocs = srMax.x

    portVal = get_portfolio_value(prices, allocs, start_val)
    cr, adr, sddr, sr = get_portfolio_stats(portVal, daily_rf,
                                            samples_per_year)

    # Compare daily portfolio value with SPY using a normalized plot
    if gen_plot:
        df_temp = pd.concat([portVal, prices_SPY],
                            keys=['Portfolio', 'SPY'],
                            axis=1)
        plot_normalized_data(df_temp, 'Optimized portfolio values', 'date', \
                             'normalized price')

    return allocs, cr, adr, sddr, sr
コード例 #7
0
def market_simulator(orders_file,
                     start_val=1000000,
                     daily_rf=0.0,
                     samples_per_year=252.0,
                     save_fig=False,
                     fig_name="plot.png"):
    """
    This function takes in an orders file and execute trades based on the file

    Parameters:
    orders_file: The file whose orders will be execute
    start_val: The starting cash in dollars
    daily_rf: Daily risk-free rate, assuming it does not change
    samples_per_year: Sampling frequency per year

    Returns:
    Print out final portfolio value of the portfolio, as well as Sharpe ratio, 
    cumulative return, average daily return and standard deviation of the portfolio and $SPX.
    Plot a chart of the portfolio and $SPX performances

    """

    # Process orders
    portvals = compute_portvals(orders_file=orders_file, start_val=start_val)
    if not isinstance(portvals, pd.DataFrame):
        print("warning, code did not return a DataFrame")

    # Get portfolio stats
    cum_ret, avg_daily_ret, std_daily_ret, sharpe_ratio = get_portfolio_stats(
        portvals, daily_rf=daily_rf, samples_per_year=samples_per_year)

    # Get the stats for $SPX for the same date range for comparison
    start_date = portvals.index.min()
    end_date = portvals.index.max()
    SPX_prices = get_data(["$SPX"],
                          pd.date_range(start_date, end_date),
                          addSPY=False).dropna()
    cum_ret_SPX, avg_daily_ret_SPX, std_daily_ret_SPX, sharpe_ratio_SPX = \
    get_portfolio_stats(SPX_prices, daily_rf=daily_rf, samples_per_year=samples_per_year)

    # Compare portfolio against $SPX
    print("Date Range: {} to {}".format(start_date, end_date))
    print()
    print("Sharpe Ratio of Fund: {}".format(sharpe_ratio))
    print("Sharpe Ratio of $SPX : {}".format(sharpe_ratio_SPX))
    print()
    print("Cumulative Return of Fund: {}".format(cum_ret))
    print("Cumulative Return of $SPX : {}".format(cum_ret_SPX))
    print()
    print("Standard Deviation of Fund: {}".format(std_daily_ret))
    print("Standard Deviation of $SPX : {}".format(std_daily_ret_SPX))
    print()
    print("Average Daily Return of Fund: {}".format(avg_daily_ret))
    print("Average Daily Return of $SPX : {}".format(avg_daily_ret_SPX))
    print()
    print("Final Portfolio Value: {}".format(portvals.iloc[-1, -1]))

    # Plot the data
    plot_normalized_data(SPX_prices.join(portvals),
                         "Portfolio vs. SPX",
                         "Date",
                         "Normalized prices",
                         save_fig=save_fig,
                         fig_name=fig_name)
コード例 #8
0
def test_run():

    """Driver function."""
    # Define input parameters
    start_date = '2007-12-31'
    end_date = '2009-12-31'
    orders_file = os.path.join("orders", "current_order.csv")
    dates = pd.date_range(start_date, end_date)
    symbols = ['IBM']
    df = get_data(symbols, dates, True)

    window_size = 20

    sma_band, upper_band, lower_band = get_bands(df['IBM'],window=window_size)

    #+1 going out, -1 coming in
    transition_array_upper = np.pad(np.diff(np.array(df['IBM'] > upper_band).astype(int)),
                    (1,0), 'constant', constant_values = (0,))

    #+2 stock price going up,-2 stock price going down
    transition_array_sma = np.pad(np.diff(np.array(df['IBM'] > sma_band).astype(int)*2),
                    (1,0), 'constant', constant_values = (0,))

    #+3 stock price going up from lower_band,-3 stock price going down from lower_band
    transition_array_lower = np.pad(np.diff(np.array(df['IBM'] > lower_band).astype(int)*3),
                    (1,0), 'constant', constant_values = (0,))

    write_orders(df,transition_array_upper,transition_array_sma,transition_array_lower, orders_file)

    ax = df['IBM'].plot(title="Bollinger Bands", label='IBM')
    sma_band.plot(label='SMA', ax=ax, color = 'Goldenrod')
    upper_band.plot(label='Upper Bollinger Band', ax=ax, color = 'Turquoise')
    lower_band.plot(label='Lower Bollinger Band', ax=ax, color = 'Turquoise')
    ax.set_xlabel("Date")
    ax.set_ylabel("Price")
    ax.legend(loc='upper left')
    plt.show()

    portvals = compute_portvals(start_date, end_date, orders_file, 10000)

    # Get portfolio stats
    cum_ret, avg_daily_ret, std_daily_ret, sharpe_ratio = get_portfolio_stats(portvals)

    # Simulate a $SPX-only reference portfolio to get stats
    prices_SPX = get_data(['$SPX'], pd.date_range(start_date, end_date))
    prices_SPX = prices_SPX[['$SPX']]  # remove SPY
    portvals_SPX = get_portfolio_value(prices_SPX, [1.0])
    cum_ret_SPX, avg_daily_ret_SPX, std_daily_ret_SPX, sharpe_ratio_SPX = get_portfolio_stats(portvals_SPX)

    # Compare portfolio against $SPX
    print "Data Range: {} to {}".format(start_date, end_date)
    print
    print "Sharpe Ratio of Fund: {}".format(sharpe_ratio)
    print "Sharpe Ratio of $SPX: {}".format(sharpe_ratio_SPX)
    print
    print "Cumulative Return of Fund: {}".format(cum_ret)
    print "Cumulative Return of $SPX: {}".format(cum_ret_SPX)
    print
    print "Standard Deviation of Fund: {}".format(std_daily_ret)
    print "Standard Deviation of $SPX: {}".format(std_daily_ret_SPX)
    print
    print "Average Daily Return of Fund: {}".format(avg_daily_ret)
    print "Average Daily Return of $SPX: {}".format(avg_daily_ret_SPX)
    print
    print "Final Portfolio Value: {}".format(portvals[-1])

    # Plot computed daily portfolio value
    df_temp = pd.concat([portvals, prices_SPX['$SPX']], keys=['Portfolio', 'SPY'], axis=1)
    plot_normalized_data(df_temp, title="Daily portfolio value")