예제 #1
0
파일: hw1.py 프로젝트: Will-NU/AptanaStudio
def simulate(startdate, enddate, ls_symbols, ls_alloc):
    dt_timeofday = dt.timedelta(hours=16)

    ldt_timestamps = du.getNYSEdays(startdate, enddate, dt_timeofday)

    c_dataobj = da.DataAccess('Yahoo', cachestalltime=0)

    ls_keys = ['open', 'high', 'low', 'close', 'volume', 'actual_close']
    ldf_data = c_dataobj.get_data(ldt_timestamps, ls_symbols, ls_keys)
    d_data = dict(zip(ls_keys, ldf_data))

    for s_key in ls_keys:
        d_data[s_key] = d_data[s_key].fillna(method='ffill')
        d_data[s_key] = d_data[s_key].fillna(method='bfill')
        d_data[s_key] = d_data[s_key].fillna(1.0)

    na_price = d_data['close'].values
    na_normalized_price = na_price / na_price[0, :]
    na_port_price = np.sum(ls_alloc * na_normalized_price, 1)

    na_daily_rets = na_port_price.copy()
    tsu.returnize0(na_daily_rets)

    vol = np.std(na_daily_rets)
    daily_ret = np.average(na_daily_rets)

    sharpe_ratio = np.sqrt(252) * daily_ret / vol
    cum_ret = na_port_price[-1]

    return vol, daily_ret, sharpe_ratio, cum_ret
예제 #2
0
def simulate(dt_start, dt_end, ls_symbols, ls_allocation):
    '''
    Simulate function
    '''

    # Get closing prices (hours=16)
    dt_timeofday = dt.timedelta(hours=16)
    # Get a list of trading days.
    ldt_timestamps = du.getNYSEdays(dt_start, dt_end, dt_timeofday)

    # Open Yahoo data set and read (adjusted) closing price
    ls_keys = ['close']
    c_dataobj = da.DataAccess('Yahoo')
    ldf_data = c_dataobj.get_data(ldt_timestamps, ls_symbols, ls_keys)
    d_data = dict(zip(ls_keys, ldf_data))

    # Compute portfolio value
    tmp = d_data['close'].values.copy()
    d_normal = tmp / tmp[0, :]
    alloc = np.array(ls_allocation).reshape(4, 1)
    portVal = np.dot(d_normal, alloc)

    # Compute daily returns
    dailyVal = portVal.copy()
    tsu.returnize0(dailyVal)

    # Compute statistics
    daily_ret = np.mean(dailyVal)
    vol = np.std(dailyVal)
    sharpe = np.sqrt(252) * daily_ret / vol
    cum_ret = portVal[portVal.shape[0] - 1][0]

    # return
    return vol, daily_ret, sharpe, cum_ret
예제 #3
0
def compute_stats(ts_daily_value):
    # Normalize the values to start at 1.0 and see
    # the daily values relative to the initial value
    ts_norm_value = ts_daily_value / ts_daily_value[0]
    total_norm_return = ts_norm_value[len(ts_norm_value) - 1]

    # calculate the daily returns
    # ret(t) = (price(t)/price(t-1)) - 1
    # returnize works on numpy arrays and not in dataframes
    na_daily_return = ts_norm_value.values
    tsu.returnize0(na_daily_return)

    mean_rets = np.mean(na_daily_return)
    volatility = np.std(na_daily_return)

    # hardcode 252 instead of using actual year daycount to keep it comparable
    sharpe = math.sqrt(252) * mean_rets / volatility

    return {
        "avg_day_ret": mean_rets,
        "volatility": volatility,
        "sharpe": sharpe,
        "total_return": total_norm_return,
        "day_ret": na_daily_return,
    }
예제 #4
0
def getPortfolioStats(na_price, allocation):
    # Getting the numpy ndarray or pandas dataframe of close prices.
    
    #Normalize the prices according to the first day. The first row for each stock should have a value of 1.0 at this point.
    # Normalizing the prices to start at 1 and see relative returns
    if type(na_price) == type(pd.DataFrame()):
        na_price = na_price.values
        na_normalized_price = na_price / na_price[0,:]
    else:
        na_normalized_price = na_price / na_price[0]
   
    #Multiply each colunpmn by the allocation to the corresponding equity. 
    allocatedprice =  na_normalized_price * allocation
    
    #Sum each row for each day. That is your cumulative daily portfolio value.
    cum_daily_port_value = allocatedprice
    cum_ret = cum_daily_port_value[-1]
    
    #daily return
    daily_port_returns = cum_daily_port_value.copy()
    tsu.returnize0(daily_port_returns)
    
    vol = np.std(daily_port_returns)
    daily_ret = np.average(daily_port_returns)
    
    sharpe = tsu.get_sharpe_ratio(daily_port_returns)
    #tsu.sharpeRatio(cum_daily_port_value)
    return cum_ret, vol, daily_ret, sharpe, na_normalized_price
예제 #5
0
def simulate(dt_start, dt_end, ls_symbols, ls_allocation):
    # Formatting the date timestamps
    dt_timeofday = dt.timedelta(hours=16)
    ldt_timestamps = du.getNYSEdays(dt_start, dt_end, dt_timeofday)

    # Open the dataset and read in the closing price
    ls_keys = ['close']
    c_dataobj = da.DataAccess('Yahoo')
    ldf_data = c_dataobj.get_data(ldt_timestamps, ls_symbols, ls_keys)
    d_data = dict(zip(ls_keys, ldf_data))

    # Calculate the portfolio value
    temp = d_data['close'].values.copy()
    d_normal = temp / temp[0, :]
    alloc = np.array(ls_allocation).reshape(4, 1)
    portVal = np.dot(d_normal, alloc)

    # Caluclate the daily returns
    dailyVal = portVal.copy()
    tsu.returnize0(dailyVal)

    # Calculate statistics
    daily_ret = np.mean(dailyVal)
    vol = np.std(dailyVal)
    sharpe = np.sqrt(NUM_TRADING_DAYS) * daily_ret / vol
    cum_ret = portVal[portVal.shape[0] - 1][0]

    return vol, daily_ret, sharpe, cum_ret
def calculateMetrics(prices, verbosity=2):
    """ Compute metrics of a series of prices
     @param prices: a series of prices. the dates are assumed consecutive and in increasing order
     @return stdDailyRets, avgDailyRets, sharpeRatio, cumRet
    """

    dailyReturns = copy(prices)
    returnize0(dailyReturns)

    stdDailyRets = np.std(dailyReturns)
    avgDailyRets = np.mean(dailyReturns)
    sharpeRatio = avgDailyRets / stdDailyRets * np.sqrt(252)
    cumRet = prices[-1] / prices[0]

    if verbosity >= 2:
        print '-------- Portfolio Prices ------------\n', prices
        print '\n-------- Portfolio Returns ------------\n', dailyReturns
    if verbosity >= 1:
        print '\n--------- Metrics -------------'
        print 'Final portfolio value =', prices[-1]
        print 'Date Range:', prices.index[0], 'to', prices.index[-1]
        print 'Sharpe Ratio =', sharpeRatio
        print 'Total return =', cumRet
        print 'Standard deviation of daily returns =', stdDailyRets
        print 'Average daily returns =', avgDailyRets

    return stdDailyRets, avgDailyRets, sharpeRatio, cumRet
예제 #7
0
파일: hw_01.py 프로젝트: jsucsy/learning
def simulate_failed2(date_start, date_end, symbols, allocations):
    '''rebalances daily'''
    
    date_timeofday = dt.timedelta(hours=16)
    ldt_timestamps = du.getNYSEdays(date_start, date_end, date_timeofday)
    c_dataobj = da.DataAccess('Yahoo')
    ls_keys = ['open', 'high', 'low', 'close', 'volume', 'actual_close']
    ldf_data = c_dataobj.get_data(ldt_timestamps, symbols, ls_keys)
    d_data = dict(zip(ls_keys, ldf_data))
    na_price = d_data['close'].values
    na_normalized_price = na_price / na_price[0, :]
    na_rets = na_normalized_price.copy()
    
    print na_rets
    
    seclist = {}
    for x in range(len(allocations)):
        seclist[x] = na_rets[:,x] * allocations[x]
        
    print seclist
    
    for sec in seclist.iterkeys():
        tsu.returnize0(seclist[sec])
    
#     tsu.returnize0(na_alloc)
#     
#     print na_alloc
    
    print seclist
    portrets = []
    
    for index in range(len(seclist[0])):
        tempsum = 0.0
        print "Index %s" % index
        for sec in seclist.iterkeys():
            print "sec %s" % sec
            if np.isnan(seclist[sec][index]):
                print "Passing: [%s] [%s]" % (sec, index)
                pass
            else:
                tempsum += seclist[sec][index]   
        portrets.append(tempsum)
        
    na_portrets = np.array(portrets)

    na_port_total = np.prod(na_portrets + 1)
    
    print na_portrets

    #na_portrets = np.sum(na_rets, axis = 1)
    #na_port_total = np.cumprod(portrets + 1)
    
    
    rf_rate = 0
    vol = np.std(na_portrets)
    daily_ret = np.average(na_portrets)
    cum_ret = na_port_total
    sharpe = np.sqrt(252)*((daily_ret - rf_rate)/vol)

    return vol, daily_ret, sharpe, cum_ret
예제 #8
0
def compare(start_day, start_month, start_year, end_day, end_month, end_year,
            ls_symbols):

    # Start and End date of the charts
    dt_start = dt.datetime(start_year, start_month, start_day)
    dt_end = dt.datetime(end_year, end_month, end_day)

    # We need closing prices so the timestamp should be hours=16.
    dt_timeofday = dt.timedelta(hours=16)

    # Get a list of trading days between the start and the end.
    ldt_timestamps = du.getNYSEdays(dt_start, dt_end, dt_timeofday)

    # Creating an object of the dataaccess class with Yahoo as the source.
    c_dataobj = da.DataAccess('Yahoo')

    # Keys to be read from the data, it is good to read everything in one go.
    ls_keys = ['open', 'high', 'low', 'close', 'volume', 'actual_close']

    # Reading the data, now d_data is a dictionary with the keys above.
    # Timestamps and symbols are the ones that were specified before.
    ldf_data = c_dataobj.get_data(ldt_timestamps, ls_symbols, ls_keys)
    d_data = dict(zip(ls_keys, ldf_data))

    # Filling the data for NAN
    for s_key in ls_keys:
        d_data[s_key] = d_data[s_key].fillna(method='ffill')
        d_data[s_key] = d_data[s_key].fillna(method='bfill')
        d_data[s_key] = d_data[s_key].fillna(1.0)

    # Getting the numpy ndarray of close prices.
    na_price = d_data['close'].values

    # Normalizing the prices to start at 1 and see relative returns
    na_normalized_price = na_price / na_price[0, :]

    # Plotting the prices with x-axis=timestamps
    plt.clf()
    fig1 = plt.figure()
    plt.plot(ldt_timestamps, na_normalized_price)
    plt.legend(ls_symbols)
    plt.ylabel('Normalized Close')
    plt.xlabel('Date')
    fig1.autofmt_xdate(rotation=45)
    plt.savefig('normalized_returns.jpg', format='jpg')

    # Copy the normalized prices to a new ndarry to find returns.
    na_rets = na_normalized_price.copy()

    # Calculate the daily returns of the prices. (Inplace calculation)
    # returnize0 works on ndarray and not dataframes.
    tsu.returnize0(na_rets)

    # Plotting the scatter plot of daily returns between Company-1 & Company-2
    plt.clf()
    fig2 = plt.figure()
    plt.scatter(na_rets[:, 0], na_rets[:, 1], c='blue')
    plt.ylabel(ls_symbols[0])
    plt.xlabel(ls_symbols[1])
    plt.savefig('scatter1v2.jpg', format='jpg')
예제 #9
0
def portfolio_analyzer(na_port_vals):
    # Normalizing the prices to start at 1 and see relative returns
#    na_vals = ls_port_vals.values
    na_normalized_vals = na_port_vals / na_port_vals[0]
    print(na_normalized_vals)


    # Numpy matrix of filled data values
    na_portrets = na_normalized_vals.copy()

    tsu.returnize0(na_portrets)
    port_length = len(na_portrets)
    print("Data Points: ")
    print(port_length)
    port_length = 252
    port_avg = np.average(na_portrets)
    port_div = np.std(na_portrets)
    port_sharp = math.sqrt(port_length)*port_avg/port_div

    # Estimate portfolio returns
    print("Days: ")
    print(port_length)
    print("Sharp Ratio:")
    print(port_sharp)

    print("Stdev:")
    print(port_div)
    print("Avg Ret:") 
    print(port_avg)
예제 #10
0
def calcStats(na_normalized_price, lf_allocations):
    #Calculate cumulative daily portfolio value
    #row-wise multiplication by weights
    na_weighted_price = na_normalized_price * lf_allocations;
    #row-wise sum
    na_portf_value = na_weighted_price.copy().sum(axis=1);

    #Calculate daily returns on portfolio
    na_portf_rets = na_portf_value.copy()
    tsu.returnize0(na_portf_rets);

    #Calculate volatility (stdev) of daily returns of portfolio
    f_portf_volatility = np.std(na_portf_rets);

    #Calculate average daily returns of portfolio
    f_portf_avgret = np.mean(na_portf_rets);

    #Calculate portfolio sharpe ratio (avg portfolio return / portfolio stdev) * sqrt(252)
    f_portf_sharpe = (f_portf_avgret / f_portf_volatility) * np.sqrt(250);

    #Calculate cumulative daily return
    #...using recursive function
    def cumret(t, lf_returns):
        #base-case
        if t==0:
            return (1 + lf_returns[0]);
        #continuation
        return (cumret(t-1, lf_returns) * (1 + lf_returns[t]));
    f_portf_cumrets = cumret(na_portf_rets.size - 1, na_portf_rets);

    return [f_portf_volatility, f_portf_avgret, f_portf_sharpe, f_portf_cumrets, na_portf_value];
예제 #11
0
def simulate(startDate, endDate, tickers, allocations):
    tradingDays = getTradingDays(startDate, endDate)
    numberOfTradingDays = len(tradingDays)
    # Keys to be read from the data, it is good to read everything in one go.
    keys = ['close']
    dataObject = da.DataAccess('Yahoo')
    data = dataObject.get_data(tradingDays, tickers, keys)

    # assuming there is at least one ticker
    finalNormalizedData = np.zeros(numberOfTradingDays)
    for x in range(0, len(tickers)):
        # get the data for the ticker
        normalizedData = data[0].values[:,x]
        # normalize the data
        normalizedData = normalizedData / normalizedData[0]
        # allocate the appropriate percentage
        normalizedData = normalizedData * allocations[x]
        # add the normalizedData to the finalized array
        finalNormalizedData = finalNormalizedData + normalizedData

    cum_ret = finalNormalizedData[numberOfTradingDays - 1]\
    # calculate average daily return
    tsu.returnize0(finalNormalizedData)
    volatility = np.std(finalNormalizedData)
    avg_daily_return = np.mean(finalNormalizedData)
    sharpe = (avg_daily_return * 252 - 0) / ( volatility * sqrt(252) ) 
    
    return(volatility, avg_daily_return, sharpe, cum_ret)
예제 #12
0
def do_benchmark_calculations(start_date, end_date, ls_symbols):
    # We need closing prices so the timestamp should be hours=16.
    dt_timeofday = dt.timedelta(hours=16)
    ldt_timestamps = du.getNYSEdays(start_date, end_date, dt_timeofday)
    c_dataobj = da.DataAccess('Yahoo', cachestalltime=0)
    ls_keys = ['open', 'high', 'low', 'close', 'volume', 'actual_close']
    ldf_data = c_dataobj.get_data(ldt_timestamps, ls_symbols, ls_keys)
    d_data = dict(zip(ls_keys, ldf_data))
    df_rets = d_data['close']
    df_rets = df_rets.fillna(method='ffill')
    df_rets = df_rets.fillna(method='bfill')
    df_rets = df_rets.fillna(1.0)

    na_price = df_rets.values.copy()

    normalized_price = na_price / na_price[0:1]
    tsu.returnize0(normalized_price)
    cummulative_return = np.cumprod(normalized_price + 1)[-1]

    avg = normalized_price.mean()
    std = normalized_price.std()

    print "benchmark sharpe_ratio:", avg / std * math.sqrt(252)
    print "benchmark total return:", cummulative_return
    print "benchmark standard deviation", std
    print "benchmark average return", avg
예제 #13
0
def process_benchmark(dt_start, dt_end, benchmark_symbol):
    ldt_timestamps = du.getNYSEdays(dt_start, dt_end + dt.timedelta(days=1),
                                    dt.timedelta(hours=16))
    dt_start = ldt_timestamps[0]
    dt_end = ldt_timestamps[-1]
    dataobj = da.DataAccess('Yahoo')
    ls_keys = ['close']
    ldf_data = dataobj.get_data(ldt_timestamps, [benchmark_symbol], ls_keys)
    d_data = dict(zip(ls_keys, ldf_data))

    for s_key in ls_keys:
        d_data[s_key] = d_data[s_key].fillna(method='ffill')
        d_data[s_key] = d_data[s_key].fillna(method='bfill')
        d_data[s_key] = d_data[s_key].fillna(1.0)

    # Extract the data/values
    na_prices = d_data['close'].values
    # Normalizing the prices to start at 1 and see relative returns
    na_normalized_price = na_prices / na_prices[0, :]

    na_benchmark_returns = na_normalized_price.copy()
    tsu.returnize0(na_benchmark_returns)

    # Calculate values statistics
    f_benchmark_avg_return = np.mean(na_benchmark_returns)
    total_ret_benchmark = np.prod(na_benchmark_returns + 1.0)
    stddev_benchmark = np.std(na_benchmark_returns)
    sharpe_benchmark = math.sqrt(
        252.0) * f_benchmark_avg_return / stddev_benchmark

    return dt_start, dt_end, sharpe_benchmark, total_ret_benchmark, stddev_benchmark, f_benchmark_avg_return
예제 #14
0
def simulate(begindate, enddate, tickers, weightings):
    dt_delta = date.timedelta(hours=16)
    ldt_timestamps = dateUtil.getNYSEdays(begindate, enddate, dt_delta)

    ls_keys = ['close']
    c_dataobj = dataAccess.DataAccess('Yahoo')
    ldf_data = c_dataobj.get_data(ldt_timestamps, tickers, ls_keys)
    d_data = dict(zip(ls_keys, ldf_data))

    # Grab the closing values of every stock
    temp = d_data['close'].values.copy()
    # Normalize those values with respect to the initial value
    d_normal = temp / temp[0, :]
    alloc = numpy.array(weightings).reshape(4, 1)
    # Multiply the normalized daily changes by the allocation
    # This gives us an array reflecting the amount allocated to each stock
    portVal = numpy.dot(d_normal, alloc)

    dailyVal = portVal.copy()
    tsUtil.returnize0(dailyVal)

    daily_ret = numpy.mean(dailyVal)
    volatility = numpy.std(dailyVal)
    sharpe = numpy.sqrt(252) * daily_ret / volatility
    total_returns = portVal[portVal.shape[0] - 1][0]

    return volatility, daily_ret, sharpe, total_returns
def analyse(na_close, ls_allocations):

    # Normalize the close price
    na_norm_close = na_close / na_close[0, :]

    # Get weighted daily returns
    weighted_daily_close = np.dot(na_norm_close, ls_allocations)

    # Copy the weighted daily close to a new ndarray to find returns
    port_daily_ret = weighted_daily_close.copy()

    # Calculate daily returns of the portfolio close price
    tsu.returnize0(port_daily_ret)

    # Get average portfolio daily returns
    daily_ret = np.average(port_daily_ret)

    # Calculate volatility of average weighted daily returns
    vol = np.std(port_daily_ret)

    # Calculate Sharpe Ratio
    k = math.sqrt(252)
    sharpe = k * (daily_ret/vol)

    # Calculate Cumulative Return
    cum_ret = np.dot((na_close[-1]/na_close[0]),ls_allocations)

    return vol, daily_ret, sharpe, cum_ret
예제 #16
0
 def computestats(self):          
        #nothing fancy, just use library functions to compute daily returns, standard deviation and average of daily returns
        self.daily_portfolio_val /= self.daily_portfolio_val[0]
        daily_portfolio_return = tsu.returnize0(self.daily_portfolio_val)
        avg = np.mean(daily_portfolio_return) 
        stdev = np.std(daily_portfolio_return)
        SR = avg / stdev * math.sqrt(252)   
        total_return = 1
        for ret in range(1,len(daily_portfolio_return)):
            
            total_return = total_return * (1 + daily_portfolio_return[ret])
        
        spy_ldf_data = self.dataobj.get_data(self.ldt_timestamps, ['SPY'], ls_keys)
            #ldf_data_2008 = dataobj.get_data(ldt_timestamps, ls_symbols_2008, ls_keys)
        spy_d_data = dict(zip(ls_keys, spy_ldf_data))               
        close_prices = spy_d_data['close'].copy()
        close_prices = close_prices.fillna(method='ffill')
        close_prices = close_prices.fillna(method='bfill') 
        na_rets_spy = close_prices['SPY'].values
        daily_spy_return = tsu.returnize0(na_rets_spy)
        
        print "AVG return: ",avg, "stdev of return: ", stdev, "sharp ratio: ", SR , "total cumulative return", total_return      
        plt.clf()
        plt.plot(self.ldt_timestamps, daily_portfolio_return)
        plt.plot(self.ldt_timestamps, daily_spy_return)
        plt.legend(self.ls_symbols)
        plt.ylabel('Return comparison')
        plt.xlabel('Date')             
def calculateMetrics(prices, verbosity=2):
    """ Compute metrics of a series of prices
     @param prices: a series of prices. the dates are assumed consecutive and in increasing order
     @return stdDailyRets, avgDailyRets, sharpeRatio, cumRet
    """

    dailyReturns = copy(prices)
    returnize0(dailyReturns)

    stdDailyRets = np.std(dailyReturns)
    avgDailyRets = np.mean(dailyReturns)
    sharpeRatio = avgDailyRets / stdDailyRets * np.sqrt(252)
    cumRet = prices[-1] / prices[0]

    if verbosity >= 2:
        print '-------- Portfolio Prices ------------\n', prices
        print '\n-------- Portfolio Returns ------------\n', dailyReturns
    if verbosity >= 1:
        print '\n--------- Metrics -------------'
        print 'Final portfolio value =', prices[-1]
        print 'Date Range:', prices.index[0], 'to', prices.index[-1]
        print 'Sharpe Ratio =', sharpeRatio
        print 'Total return =', cumRet
        print 'Standard deviation of daily returns =', stdDailyRets
        print 'Average daily returns =', avgDailyRets

    return stdDailyRets, avgDailyRets, sharpeRatio, cumRet
def simulate(dt_start, dt_end, ls_symbols, lf_weights):
    # We need closing prices so the timestamp should be hours=16.
    dt_timeofday = dt.timedelta(hours=16)

    # Get a list of trading days between the start and the end.
    ldt_timestamps = du.getNYSEdays(dt_start, dt_end, dt_timeofday)

    # Creating an object of the dataaccess class with Yahoo as the source.
    c_dataobj = da.DataAccess('Yahoo')

    # Keys to be read from the data, it is good to read everything in one go.
    ls_keys = ['open', 'high', 'low', 'close', 'volume', 'actual_close']

    # Reading the data, now d_data is a dictionary with the keys above.
    # Timestamps and symbols are the ones that were specified before.
    ldf_data = c_dataobj.get_data(ldt_timestamps, ls_symbols, ls_keys)
    d_data = dict(zip(ls_keys, ldf_data))
    
    # Filling the data for NAN
    for s_key in ls_keys:
        d_data[s_key] = d_data[s_key].fillna(method='ffill')
        d_data[s_key] = d_data[s_key].fillna(method='bfill')
        d_data[s_key] = d_data[s_key].fillna(1.0)

    # Getting the numpy ndarray of close prices.
    na_price = d_data['close'].values
    #print "Close prices:\n", na_price
    
    # Normalizing the prices to start at 1 and see relative returns
    na_normalized_price = na_price / na_price[0, :]

    # Copy the normalized prices to a new ndarry to find returns.
    na_rets = na_normalized_price.copy()
    #print "normalized returns:\n", na_rets
    
    # Calculate value of portfolio
    portfolio_daily_value = np.sum(na_rets * lf_weights, axis=1)
    #print "Portfolio daily value: ", portfolio_daily_value
    
    # Cumulative return is the last day's value
    cum_ret = portfolio_daily_value[-1]

    # Calculate the daily returns of the prices. (Inplace calculation)
    # returnize0 works on ndarray and not dataframes.
    tsu.returnize0(portfolio_daily_value)
    #print "daily returns \n", portfolio_daily_value

    # Calculate volatility as portfolio's standart deviation
    vol = np.std(portfolio_daily_value)

    # Calculate daily return
    daily_ret = np.mean(portfolio_daily_value, axis=0)

    # Calculate Sharpe ratio with assumption that risk free rate is 0
    # and year has 252 trading days 
    risk_free_rate=0
    sharpe = (daily_ret - risk_free_rate)/vol*np.sqrt(252)#len(portfolio_daily_value))

    # Return calculated values
    return vol, daily_ret, sharpe, cum_ret
예제 #19
0
def simulate(dt_start, dt_end, ls_symbols, ratio):
    dt_timeofday = dt.timedelta(hours=16)
    ldt_timestamps = du.getNYSEdays(dt_start, dt_end, dt_timeofday)
    c_dataobj = da.DataAccess('Yahoo', cachestalltime=0)
    ls_keys = ['open','high','low','close','volume','actual_close']
    ldf_data = c_dataobj.get_data(ldt_timestamps, ls_symbols, ls_keys)
    d_data = dict(zip(ls_keys, ldf_data))
    
    #for l in ls_symbols:
        

    na_price = d_data['close'].values
    na_normalized_price = na_price / na_price[0,:]
    na_normalized_price_ratio = np.multiply(na_normalized_price, ratio)
    all_price_ratio = np.sum(na_normalized_price_ratio, axis=1)    
    
    """ calculate Volatility """
    vol = np.std(tsu.returnize0(all_price_ratio))    
    
    """ daily return """
    daily_ret = np.mean(tsu.returnize0(all_price_ratio))
    """ cumulative daily return"""
    cum_ret = 0
    sharpe = 0
    
    return vol, daily_ret, sharpe, cum_ret
예제 #20
0
def simulate(start, end, equities, allocs):
    vol, daily_ret, sharpe, cum_ret = 0,0,0,0
    
    # setup, get matrix of normalized closing prices
    dt_timeofday = dt.timedelta(hours=16)
    ldt_timestamps = du.getNYSEdays(start, end, dt_timeofday)
    c_dataobj = da.DataAccess('Yahoo', cachestalltime=0)
    ls_keys = ['open', 'high', 'low', 'close', 'volume', 'actual_close']
    ldf_data = c_dataobj.get_data(ldt_timestamps, equities, ls_keys)
    d_data = dict(zip(ls_keys, ldf_data))
    na_price = d_data['close'].values
    na_normalized_price = na_price / na_price[0,:]
    
    # first, compute (normalized) daily returns
    na_weighted_cumulative_ret = np.sum(na_normalized_price * allocs, axis=1)
    na_normalized_weighted_daily_ret = na_weighted_cumulative_ret.copy()
    tsu.returnize0(na_normalized_weighted_daily_ret)    
    
    # compute volatility of daily returns of total portfolio
    vol = np.std(na_normalized_weighted_daily_ret)
    
    # compute average daily return
    daily_ret = np.mean(na_normalized_weighted_daily_ret)
    
    #compute Sharpe ratio
    sharpe = np.sqrt(252) * daily_ret / vol
    
    # compute cumulative return of portfolio
    cum_ret = na_weighted_cumulative_ret[len(na_weighted_cumulative_ret)-1]
    
    return vol, daily_ret, sharpe, cum_ret
예제 #21
0
def print_results(cumm_port, mkt_benchmark):
    norm_port = cumm_port['VAL'].copy()
    norm_port = norm_port/norm_port[0]
    norm_bench = mkt_benchmark[mkt_benchmark.columns[0]].copy()
    norm_bench = norm_bench/norm_bench[0]

    norm_port_ret = norm_port.copy()
    norm_bench_ret = norm_bench.copy()
    tsu.returnize0(norm_port_ret)
    tsu.returnize0(norm_bench_ret)
    msg = """Data Range :  {0}  to  {1}

Sharpe Ratio of Fund : {2}
Sharpe Ratio of Benchmark : {3}

Total Return of Fund :  {4}
Total Return of Benchmark : {5}

Standard Deviation of Fund : {6}
Standard Deviation of Benchmark : {7}

Average Daily Return of Fund : {8}
Average Daily Return of Benchmark : {9}""".format(cumm_port.index[0],
        cumm_port.index[-1],
        calc_sharpeRatio(norm_port),
        calc_sharpeRatio(norm_bench),
        norm_port[-1],
        norm_bench[-1],
        np.std(norm_port_ret),
        np.std(norm_bench_ret),
        np.mean(norm_port_ret),
        np.mean(norm_bench_ret))
    print(msg)
예제 #22
0
파일: hw1.py 프로젝트: fs2013/study
def get_returns(symbols, beg, end, time_of_day, price_type):

    # Trading days between the beg and the end.
    ldt_timestamps = du.getNYSEdays(beg, end, time_of_day)

    # Data Access class with Yahoo as the source.
    data_access = da.DataAccess('Yahoo')

    # Keys to be read from the data, it is good to read everything in one go.
    keys = ['open', 'high', 'low', 'close', 'volume', 'actual_close']

    # Reading the data, now data is a dictionary with the keys above.
    # Timestamps and symbols are the ones that were specified before.
    data_df = data_access.get_data(ldt_timestamps, symbols, keys)
    data = dict(zip(keys, data_df))

    # Filling the data for NAN
    for s_key in keys:
        data[s_key] = data[s_key].fillna(method='ffill')
        data[s_key] = data[s_key].fillna(method='bfill')
        data[s_key] = data[s_key].fillna(1.0)

    # Getting the numpy ndarray of close prices.
    na_price = data[price_type].values

    # Normalizing the prices to start at 1 and see relative returns
    na_normalized_price = na_price / na_price[0, :]
    # Copy the normalized prices to a new ndarry to find returns.
    na_rets = na_normalized_price.copy()
    # Calculate the daily returns of the prices. (Inplace calculation)
    # returnize0 works on ndarray and not dataframes.
    tsu.returnize0(na_rets)

    return ldt_timestamps, na_price, na_rets
def simulate(dt_start, dt_end, ls_symbols, allocations):
    dt_timeofday = dt.timedelta(hours=16)
    ldt_timestamps = du.getNYSEdays(dt_start, dt_end, dt_timeofday)

    c_dataobj = da.DataAccess('Yahoo', cachestalltime=0)
    ls_keys = ['open', 'high', 'low', 'close', 'volume', 'actual_close']
    ldf_data = c_dataobj.get_data(ldt_timestamps, ls_symbols, ls_keys)
    d_data = dict(zip(ls_keys, ldf_data))

    na_price = d_data['close'].values
    na_normalized_price = na_price / na_price[0, :]
    na_normalized_price_alloc = na_normalized_price * allocations

    sum_daily = np.zeros( len(na_normalized_price_alloc) )
    for index, item in enumerate(na_normalized_price_alloc):
        for value in item:
            sum_daily[index] += value

    na_rets = sum_daily.copy()
    tsu.returnize0(na_rets)
        
    daily_cum_ret = np.ones( len(na_rets) )
    for i in range (1, len(na_rets)):
        daily_cum_ret[i] = daily_cum_ret[i - 1] * (1 + na_rets[i])
    cum_ret = daily_cum_ret[len(daily_cum_ret) - 1]

    vol = np.std(na_rets)
    daily_ret = np.average(na_rets)
    sharpe = math.sqrt(252) * (daily_ret/vol)

    return vol, daily_ret, sharpe, cum_ret
예제 #24
0
def simulate(dt_start, dt_end, ls_symbols, allocations):
  dt_timeofday = dt.timedelta(hours=16)
  # Get a list of trading days between the start and the end.
  ldt_timestamps = du.getNYSEdays(dt_start, dt_end, dt_timeofday)
  c_dataobj = da.DataAccess('Yahoo')
  # Keys to be read from the data, it is good to read everything in one go.
  ls_keys = ['open', 'high', 'low', 'close', 'volume', 'actual_close']

  # Reading the data, now d_data is a dictionary with the keys above.
  # Timestamps and symbols are the ones that were specified before.
  ldf_data = c_dataobj.get_data(ldt_timestamps, ls_symbols, ls_keys)
  d_data = dict(zip(ls_keys, ldf_data))

  # Getting the numpy ndarray of close prices.
  na_price = d_data['close'].values
  # Normalizing the prices to start at 1 and see relative returns
  na_normalized_price = na_price / na_price[0, :]
  na_rets = na_normalized_price.copy()

  # Calculate the daily returns of the prices. (Inplace calculation)
  # returnize0 works on ndarray and not dataframes.
  tsu.returnize0(na_rets)

  #print na_normalized_price
  daily = np.zeros(len(na_normalized_price))
  i = 0
  for row in na_normalized_price:
    daily[i] = row[0]*allocations[0]+row[1]*allocations[1]+row[2]*allocations[2]+row[3]*allocations[3]
    i += 1
  #print daily
  daily_returns = daily.copy()
  tsu.returnize0(daily_returns)
  #print daily_returns
  return (tsu.get_sharpe_ratio(daily_returns), np.std(daily_returns), np.average(daily_returns), (daily[-1]/daily[0]))
def simulate(dt_start, dt_end, ls_symbols, ls_allocation):

        #date timestamps
        dt_timeofday = dt.timedelta(hours=16)
        ldt_timestamps = du.getNYSEdays(dt_start, dt_end, dt_timeofday)

        # read closing values
        ls_keys = ['close']
        c_dataobj = da.DataAccess('Yahoo')
        ldf_data = c_dataobj.get_data(ldt_timestamps, ls_symbols, ls_keys)
        d_data = dict(zip(ls_keys, ldf_data))
        
        #agg portfolio value
        temp = d_data['close'].values.copy()
        d_normal = temp / temp[0,:]
        alloc = np.array(ls_allocation).reshape(4,1)
        portfolio = np.dot(d_normal, alloc)

        #daily returns
        dailyVal = portfolio.copy()
        tsu.returnize0(dailyVal) # who named this function?

        # calc. daily return, trading vol., sharpe ratio, and total returns
        daily_return = np.mean(dailyVal)
        vol = np.std(dailyVal)
        sharpe = np.sqrt(trading_days) * daily_return / vol
        cum_return = portfolio[portfolio.shape[0] -1][0]
        
        return vol, daily_return, sharpe, cum_return
def simulate(dt_start, dt_end, ls_symbols, allocations):
    dt_timeofday = dt.timedelta(hours=16)
    ldt_timestamps = du.getNYSEdays(dt_start, dt_end, dt_timeofday)

    c_dataobj = da.DataAccess("Yahoo")
    ls_keys = ["open", "high", "low", "close", "volume", "actual_close"]
    ldf_data = c_dataobj.get_data(ldt_timestamps, ls_symbols, ls_keys)
    d_data = dict(zip(ls_keys, ldf_data))

    na_price = d_data["close"].values
    na_normalized_price = na_price / na_price[0, :]

    na_rets = na_normalized_price.copy()
    tsu.returnize0(na_rets)

    daily_cum_ret = np.ones((len(na_rets), len(ls_symbols)))

    for i in range(1, len(na_rets)):
        daily_cum_ret[i] = daily_cum_ret[i - 1] * (1 + na_rets[i])
    accum_ret = daily_cum_ret[len(daily_cum_ret) - 1]

    cum_ret = 0
    for index, item in enumerate(allocations):
        cum_ret += item * accum_ret[index]

    return cum_ret
예제 #27
0
def simulateNoCapital (start_date, end_date, symbol_list, allocation):
    market_close_time = dt.timedelta(hours=16)
    trade_day_list = du.getNYSEdays(start_date, end_date, market_close_time)
    data_access_service = da.DataAccess('Yahoo')
    column_list = ['open', 'high', 'low', 'close', 'volume', 'actual_close']
    trade_data = data_access_service.get_data(trade_day_list, symbol_list, column_list)
    trade_dictionary = dict(zip(column_list, trade_data))
    price_list = trade_dictionary['close'].values
    daily_returns = price_list.copy()
#    init_allocation = np.array([item * initial_investment for item in allocation], np.float); 
#    start_price = price_list[0, :]
#    number_of_stocks = (init_allocation//start_price).astype(int)
    
#    portfolio_daily_values = (number_of_stocks * price_list)[:, :].sum(1)
     
#    portfolio_daily_returns = portfolio_daily_values.copy()

    # Calculate the daily returns of the prices. (Inplace calculation)
    # returnize0 works on ndarray and not dataframes.
    tsu.returnize0(daily_returns)
    
    print daily_returns
    portfolio_daily_returns = (daily_returns * allocation)[:, :].sum(1) 
    
    average_daily_return = portfolio_daily_returns[:].mean()
    daily_return_stddev = portfolio_daily_returns[:].std()
    sharpe_ratio = math.sqrt(portfolio_daily_returns.size) * average_daily_return / daily_return_stddev;
    cum_return = portfolio_daily_returns[:].sum();  
    return (daily_return_stddev, average_daily_return, sharpe_ratio, cum_return)
예제 #28
0
    def simulate(start_date, end_date, symbols_list, allocation_list):
        timeofday_date = dt.timedelta(hours=16)
        timestamps_list = du.getNYSEdays(start_date, end_date, timeofday_date)

        # Let's use dynamic trading days count based on data
        trading_days = len(timestamps_list)

        keys_list = ['close']
        data_object = da.DataAccess('Yahoo', cachestalltime=0)
        data_list = data_object.get_data(timestamps_list,
                                         symbols_list,
                                         keys_list)
        data_dictionary = dict(zip(keys_list, data_list))

        price_array_raw = data_dictionary['close'].values.copy()
        price_array_normalized = Helpers.normalize_price_array(price_array_raw)
        allocation_vector = np.array(allocation_list).reshape(4, 1)
        portfolio_values = np.dot(price_array_normalized, allocation_vector)

        daily_returns = portfolio_values.copy()
        tsu.returnize0(daily_returns)

        # Compute volatility and mean (daily return) values of portfolio
        volatility = Helpers.get_volatility(daily_returns)
        daily_return = np.mean(daily_returns)
        # Compute Shrape ratio of portfolio
        shrape_ratio = Helpers.get_shrape_ratio(daily_return,
                                                volatility,
                                                trading_days)
        # Compute Cumulative Return of portfolio
        daily_cum_return = Helpers.get_cumulative_return(portfolio_values)

        return volatility, daily_return, shrape_ratio, daily_cum_return
예제 #29
0
def simulate(dt_start, dt_end, ls_symbols, ls_allocation):

	assert len(ls_symbols) == len(ls_allocation)
	
	# 4:00 PM timestamp for closing prices
	dt_timeofday = dt.timedelta(hours=16)

	# get trading dates
	ldt_timestamps = du.getNYSEdays(dt_start, dt_end, dt_timeofday)

	# access data from Yahoo
	c_dataobj = da.DataAccess('Yahoo')

	# data to read
	ls_keys = ['open', 'high', 'low', 'close', 'volume', 'actual_close']

	# read the data and create a dictionary
	ldf_data = c_dataobj.get_data(ldt_timestamps, ls_symbols, ls_keys)
	d_data = dict(zip(ls_keys, ldf_data))

	# get closing prices
	na_price = d_data['close'].values.copy()
	na_normal_price = na_price / na_price[0, :]
	portfolio_value = np.dot(na_normal_price, np.array(ls_allocation).reshape(len(ls_symbols), 1))

	daily_returns = portfolio_value.copy()
	tsu.returnize0(daily_returns)

	avg_daily_returns = np.mean(daily_returns)
	volatility = np.std(daily_returns)
	sharpe_ratio = np.sqrt(len(ldt_timestamps)) * avg_daily_returns / volatility
	cumulative_returns = portfolio_value[portfolio_value.shape[0] - 1][0]

	return volatility, avg_daily_returns, sharpe_ratio, cumulative_returns
예제 #30
0
def calcStats(na_normalized_price, lf_allocations):
    #Calculate cumulative daily portfolio value
    #row-wise multiplication by weights
    na_weighted_price = na_normalized_price * lf_allocations;
    #row-wise sum
    na_portf_value = na_weighted_price.copy().sum(axis=1);

    #Calculate daily returns on portfolio
    na_portf_rets = na_portf_value.copy()
    tsu.returnize0(na_portf_rets);

    #Calculate volatility (stdev) of daily returns of portfolio
    f_portf_volatility = np.std(na_portf_rets); 

    #Calculate average daily returns of portfolio
    f_portf_avgret = np.mean(na_portf_rets);

    #Calculate portfolio sharpe ratio (avg portfolio return / portfolio stdev) * sqrt(252)
    f_portf_sharpe = (f_portf_avgret / f_portf_volatility) * np.sqrt(250);

    #Calculate cumulative daily return
    #...using recursive function
    def cumret(t, lf_returns):
        #base-case
        if t==0:
            return (1 + lf_returns[0]);
        #continuation
        return (cumret(t-1, lf_returns) * (1 + lf_returns[t]));
    f_portf_cumrets = cumret(na_portf_rets.size - 1, na_portf_rets);

    return [f_portf_volatility, f_portf_avgret, f_portf_sharpe, f_portf_cumrets, na_portf_value];
예제 #31
0
def simulate(startDate, endDate, symbolsEq, allocationEq) :
    
    dt_timeofday = dt.timedelta(hours=16)
    ldt_timestamps = du.getNYSEdays(dt_start, dt_end, dt_timeofday)
    c_dataobj = da.DataAccess('Yahoo')
    ls_keys = ['open', 'high', 'low', 'close', 'volume', 'actual_close']
    ldf_data = c_dataobj.get_data(ldt_timestamps, ls_symbols, ls_keys)
    d_data = dict(zip(ls_keys, ldf_data))
    na_price = d_data['close'].values
    na_normalized_price = na_price / na_price[0, :]

    symbolSP = ["$SPX"]
    sp_data = c_dataobj.get_data(ldt_timestamps, symbolSP, ls_keys)
    sp_d_data = dict(zip(ls_keys, sp_data))
    sp_price = sp_d_data['close'].values
    sp_price_normalized = sp_price / sp_price[0, :]
    dailyReturnSP = sp_price_normalized.copy()
    tsu.returnize0(dailyReturnSP)
    
    na_normalizedPriceAllocation = na_normalized_price*allocationEq
    na_sumRows = na_normalizedPriceAllocation.sum(axis=1)
    dailyReturn = na_sumRows.copy()
    tsu.returnize0(dailyReturn)
    avgDailyReturn = np.average(dailyReturn)    
    dailyReturnStdDev = np.std(dailyReturn)

    sharpeRatio = np.sqrt(252)*avgDailyReturn/dailyReturnStdDev
    excessReturn = dailyReturn - dailyReturnSP
    
    avgExcessReturn = np.average(excessReturn)
    excessReturnStdDev = np.std(excessReturn)
    cumulativeReturn = na_sumRows[-1]

    return dailyReturnStdDev, avgDailyReturn, sharpeRatio, cumulativeReturn  
예제 #32
0
def simulate(start_date, end_date, symbols, allocations):
	ldt_timestamps = du.getNYSEdays(start_date, end_date, dt_timeofday)
	ldf_data = c_dataobj.get_data(ldt_timestamps, symbols, ls_keys)
	d_data = dict(zip(ls_keys, ldf_data))
	na_price = d_data['close'].values
	na_normalized_price = na_price / na_price[0, :]


	na_rets = na_normalized_price.copy()
	
	
	na_portrets = np.sum(na_rets * allocations, axis=1)
	cum_ret = na_portrets[-1]

	tsu.returnize0(na_portrets)

	avg_daily_return = np.mean(na_portrets)
	std_dev = np.std(na_portrets)

	k = np.sqrt(250)
	sharpe_ratio = k * avg_daily_return/std_dev




	## sqStdDev = np.std(squareArray)
	return std_dev, avg_daily_return, sharpe_ratio, cum_ret
예제 #33
0
파일: hw1.py 프로젝트: Will-NU/AptanaStudio
def simulate(startdate, enddate, ls_symbols, ls_alloc):
    dt_timeofday = dt.timedelta(hours = 16)
    
    ldt_timestamps = du.getNYSEdays(startdate, enddate, dt_timeofday)
    
    c_dataobj = da.DataAccess('Yahoo', cachestalltime = 0)
    
    ls_keys = ['open', 'high', 'low', 'close', 'volume', 'actual_close']
    ldf_data = c_dataobj.get_data(ldt_timestamps, ls_symbols, ls_keys)
    d_data = dict(zip(ls_keys, ldf_data))
    
    for s_key in ls_keys:
        d_data[s_key] = d_data[s_key].fillna(method='ffill')
        d_data[s_key] = d_data[s_key].fillna(method='bfill')
        d_data[s_key] = d_data[s_key].fillna(1.0)
        
    na_price = d_data['close'].values
    na_normalized_price = na_price / na_price[0,:]
    na_port_price = np.sum(ls_alloc * na_normalized_price, 1)
    
    na_daily_rets = na_port_price.copy()
    tsu.returnize0(na_daily_rets)
    
    vol = np.std(na_daily_rets)
    daily_ret = np.average(na_daily_rets)
    
    sharpe_ratio = np.sqrt(252) * daily_ret / vol
    cum_ret = na_port_price[-1]
    
    return vol, daily_ret, sharpe_ratio, cum_ret
예제 #34
0
def analyse(na_close, ls_allocations):

    # Normalize the close price
    na_norm_close = na_close / na_close[0, :]

    # Get weighted daily returns
    weighted_daily_close = np.dot(na_norm_close, ls_allocations)

    # Copy the weighted daily close to a new ndarray to find returns
    port_daily_ret = weighted_daily_close.copy()

    # Calculate daily returns of the portfolio close price
    tsu.returnize0(port_daily_ret)

    # Get average portfolio daily returns
    daily_ret = np.average(port_daily_ret)

    # Calculate volatility of average weighted daily returns
    vol = np.std(port_daily_ret)

    # Calculate Sharpe Ratio
    k = math.sqrt(252)
    sharpe = k * (daily_ret / vol)

    # Calculate Cumulative Return
    cum_ret = np.dot((na_close[-1] / na_close[0]), ls_allocations)

    return vol, daily_ret, sharpe, cum_ret
예제 #35
0
def simulate(startdate, enddate, equities, allocations):

    # Date timestamps
    ldt_timestamps = du.getNYSEdays(startdate, enddate, dt.timedelta(hours=16))

    # Data access
    c_dataobj = da.DataAccess('Yahoo')
    ls_keys = ['open', 'high', 'low', 'close', 'volume', 'actual_close']
    ldf_data = c_dataobj.get_data(ldt_timestamps, equities, ls_keys)
    d_data = dict(zip(ls_keys, ldf_data))

    # Calculate normalized close data
    close_data = d_data['close'].values
    normalized_close_data = close_data / close_data[0, :]

    # Make sure allocations is a column vector:
    allocations = np.array(allocations).reshape(len(allocations), 1)

    # Calculate portfolio close data
    portfolio_close_data = np.dot(normalized_close_data, allocations)

    # Calculate total return
    portfolio_close_data_copy = portfolio_close_data.copy()
    portfolio_normalized_cumulative_daily_return = np.sum(
        portfolio_close_data_copy, axis=1)
    cum_ret = portfolio_normalized_cumulative_daily_return[-1]

    # Calculate volatility, average daily return and sharpe ratio
    portfolio_close_data_copy = portfolio_close_data.copy()
    tsu.returnize0(portfolio_close_data_copy)
    avg_daily_ret = np.mean(portfolio_close_data_copy)
    std_dev = np.std(portfolio_close_data_copy)
    sharpe = np.sqrt(252) * avg_daily_ret / std_dev

    return std_dev, avg_daily_ret, sharpe, cum_ret
예제 #36
0
def simulate(dt_start, dt_end, ls_symbols, allocation):

    dt_timeofday = dt.timedelta(hours=16)
    ldt_timestamps = du.getNYSEdays(dt_start, dt_end, dt_timeofday)

    c_dataobj = da.DataAccess('Yahoo')
    ls_keys = ['open', 'high', 'low', 'close', 'volume', 'actual_close']
    ldf_data = c_dataobj.get_data(ldt_timestamps, ls_symbols, ls_keys)
    d_data = dict(zip(ls_keys, ldf_data))

    #noramlize and accumulate
    values = d_data['close'].values
    values = values / values[0, :]
    values = values * allocation

    # sum per day
    cum_day = values.sum(axis=1)

    na_rets = cum_day.copy()

    # diff between current & perv date
    tsu.returnize0(na_rets)

    std_dev = np.std(na_rets)
    mean = np.mean(na_rets)
    sharpe = math.sqrt(252) * mean / std_dev
    # perv val * current val
    cum_daily = np.cumprod(1 + na_rets, axis=0, dtype=float)


    return std_dev, mean, sharpe, cum_daily[len(cum_daily) -1]
예제 #37
0
def portfolio_analyzer(na_port_vals):
    # Normalizing the prices to start at 1 and see relative returns
#    na_vals = ls_port_vals.values
    na_normalized_vals = na_port_vals / na_port_vals[0]
    print(na_normalized_vals)


    # Numpy matrix of filled data values
    na_portrets = na_normalized_vals.copy()

    tsu.returnize0(na_portrets)
    port_length = len(na_portrets)
    print("Data Points: ")
    print(port_length)
    port_length = 252
    port_avg = np.average(na_portrets)
    port_div = np.std(na_portrets)
    port_sharp = math.sqrt(port_length)*port_avg/port_div

    # Estimate portfolio returns
    print("Days: ")
    print(port_length)
    print("Sharpe Ratio:")
    print(port_sharp)

    print("Stdev:")
    print(port_div)
    print("Avg Ret:") 
    print(port_avg)
예제 #38
0
 def computestats(self):          
        #nothing fancy, just use library functions to compute daily returns, standard deviation and average of daily returns
        self.daily_portfolio_val /= self.daily_portfolio_val[0]
        daily_portfolio_return = tsu.returnize0(self.daily_portfolio_val)
        avg = np.mean(daily_portfolio_return) 
        stdev = np.std(daily_portfolio_return)
        SR = avg / stdev * math.sqrt(252)   
        total_return = 1
        for ret in range(1,len(daily_portfolio_return)):
            
            total_return = total_return * (1 + daily_portfolio_return[ret])
        
        spy_ldf_data = self.dataobj.get_data(self.ldt_timestamps, ['SPY'], ls_keys)
            #ldf_data_2008 = dataobj.get_data(ldt_timestamps, ls_symbols_2008, ls_keys)
        spy_d_data = dict(zip(ls_keys, spy_ldf_data ))               
        close_prices = spy_d_data['close'].copy()
        close_prices = close_prices.fillna(method='ffill')
        close_prices = close_prices.fillna(method='bfill') 
        na_rets_spy = close_prices['SPY'].values
        daily_spy_return = tsu.returnize0(na_rets_spy)
        
        print "AVG return: ",avg, "stdev of return: ", stdev, "sharp ratio: ", SR , "total cumulative return", total_return      
        plt.clf()
        plt.plot(self.ldt_timestamps, daily_portfolio_return)
        plt.plot(self.ldt_timestamps, daily_spy_return)
        plt.legend(self.ls_symbols)
        plt.ylabel('Return comparison')
        plt.xlabel('Date')             
예제 #39
0
def simulate( start, end, symbols, allocations):
    #calculate daily returns of portfolio
    timeofday = dt.timedelta(hours=16)
    ldt_timestamps = du.getNYSEdays(start, end, timeofday)
    c_dataobj = da.DataAccess('Yahoo')
    ls_keys = ['open', 'high', 'low', 'close', 'volume', 'actual_close']
    ldf_data = c_dataobj.get_data(ldt_timestamps, symbols, ls_keys)
    #print("test")
    d_data = dict(zip(ls_keys, ldf_data))
    # Filling the data for NAN
    for s_key in ls_keys:
        d_data[s_key] = d_data[s_key].fillna(method='ffill')
        d_data[s_key] = d_data[s_key].fillna(method='bfill')
        d_data[s_key] = d_data[s_key].fillna(1.0)
    # Getting the numpy ndarray of close prices.
    na_price = d_data['close'].values
    na_normalized_price = na_price / na_price[0, :]
    na_portfolio_price = (allocations * na_normalized_price).sum(1)
    # Copy the normalized prices to a new ndarry to find returns.
    na_rets = na_portfolio_price.copy()
    # Calculate the daily returns of the prices. (Inplace calculation)
    # returnize0 works on ndarray and not dataframes.
    tsu.returnize0(na_rets)
    # Plotting the plot of daily returns
    dev = na_rets.std()
    #if dev==0:
    #  print na_rets[:5]
    avg = na_rets.mean()
    cum_ret=(na_portfolio_price[-1])
    sharpe = avg/dev*(252**0.5)
    return dev, avg, sharpe, cum_ret
예제 #40
0
def main(fund_tx_file, comparision_symbol):

	fund_txn = pd.read_csv(fund_tx_file, parse_dates=[[0, 1, 2]], header=None, index_col=[0])
	fund_txn.sort_index(inplace=True)
	sorted_dates = fund_txn.index
	start_date = sorted_dates[0]
	end_date = sorted_dates[-1] + dt.timedelta(days=1)

	total_daily_rets = fund_txn.iloc[:, -1].astype('f4')
	# print total_daily_rets
	daily_ret = tsu.returnize0(total_daily_rets.copy())
	avg_daily_ret = np.mean(daily_ret)
	std_dev = np.std(daily_ret)
	sharpe = np.sqrt(252) * avg_daily_ret/std_dev
	cum_ret = total_daily_rets[-1]/total_daily_rets[0]

	comp_sym_vol, comp_sym_daily_ret, comp_sym_sharpe, comp_sym_cum_ret = optimizer.simulate(
		start_date, end_date, [comparision_symbol], [1.0])

	print("Details of the Performance of the portfolio :")
	print("Data Range : {} to {}").format(str(start_date + dt.timedelta(hours=16)), 
		str(end_date + dt.timedelta(hours=16)))

	print("Sharpe Ratio of Fund : {}").format(sharpe)
	print("Sharpe Ratio of {} : {}").format(comparision_symbol,comp_sym_sharpe)

	print("Total Return of Fund : {}").format(cum_ret)
	print("Total Return of {} : {}").format(comparision_symbol, comp_sym_cum_ret)

	print("Standard Deviation of Fund : {}").format(std_dev)
	print("Standard Deviation of {} : {}").format(comparision_symbol, comp_sym_vol)

	print("Average Daily Return of Fund : {}").format(avg_daily_ret)
	print("Average Daily Return of {} : {}").format(comparision_symbol, comp_sym_daily_ret)

	# Plot Fund vs comparing symbol
	plt.clf()
	fig = plt.figure(1)
	ax = plt.subplot(111)
	daily_ret_cummulative = np.cumprod(daily_ret + 1, axis=0)

	# Calculate daily returns for comparing symbol
	ldt_timestamps, na_price = optimizer.get_close_price_for_symbols(start_date, 
		end_date, [comparision_symbol])
	na_normalized_price = na_price / na_price[0, :]
	all_sum_daily = np.sum(na_normalized_price, 1)
	comp_sym_daily_ret = tsu.returnize0(all_sum_daily.copy())
	comp_sym_cummulative = np.cumprod(comp_sym_daily_ret + 1, axis=0)

	plt.plot(sorted_dates, daily_ret_cummulative, label='Fund', alpha=0.4)
	plt.plot(sorted_dates, comp_sym_cummulative, label=comparision_symbol)
	plt.ylabel('Cumulative Returns')
	plt.xlabel('Date')
	fig.autofmt_xdate(rotation=45)
	
	box = ax.get_position()
	ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])
	# Put a legend to the right of the current axis
	ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))
	plt.show()
예제 #41
0
def simulate(startdate, enddate, symbols, alloc):
	ls_symbols = symbols
	lf_alloc = alloc
	dt_start = startdate
	dt_end = enddate
	dt_timeofday=dt.timedelta(hours=16)
	ldt_timestamps = du.getNYSEdays(dt_start, dt_end, dt_timeofday)

	c_dataobj = da.DataAccess('Yahoo', cachestalltime=0)
	ls_keys = ['open', 'close', 'high', 'low', 'volume']
	ldf_data = c_dataobj.get_data(ldt_timestamps, ls_symbols, ls_keys)
	d_data = dict(zip(ls_keys, ldf_data))
	for s_key in ls_keys:
        	d_data[s_key] = d_data[s_key].fillna(method='ffill')
        	d_data[s_key] = d_data[s_key].fillna(method='bfill')
        	d_data[s_key] = d_data[s_key].fillna(1.0)
	
	na_price = d_data['close'].values
	
	na_normalized_price = na_price/na_price[0, :]
	na_port = na_normalized_price*lf_alloc
	na_port_daily_totals = np.sum(na_port, axis = 1)
	na_rets = na_port_daily_totals.copy()
	tsu.returnize0(na_rets)

	vol = np.std(na_rets)
	daily_ret = np.mean(na_rets)
	sharpe = mt.sqrt(252)*daily_ret/vol
	cum_ret = na_port_daily_totals[-1]/na_port_daily_totals[0]

	return (vol, daily_ret, sharpe, cum_ret)
def plot():
    ls_alloc = optimal_allocation_4(dt_start, dt_end, ls_symbols)
    dt_timeofday = dt.timedelta(hours=16)
    ldt_timestamps = du.getNYSEdays(dt_start, dt_end, dt_timeofday)
    c_dataobj = da.DataAccess('Yahoo')
    ls_key = ['close']
    ldf_data = c_dataobj.get_data(ldt_timestamps, ls_symbols, ls_key)
    d_data = dict(zip(ls_key, ldf_data))
    df_rets = d_data['close'].copy()
    df_rets = df_rets.fillna(method='ffill')
    df_rets = df_rets.fillna(method='bfill')

    na_rets = df_rets.values

    tsu.returnize0(na_rets)
    na_portrets = np.sum(na_rets * ls_alloc, axis=1)
    na_port_total = np.cumprod(na_portrets + 1)
    na_component_total = np.cumprod(na_rets + 1, axis=0)
    plt.clf()
    fig = plt.figure()
    fig.add_subplot(111)
    plt.plot(ldt_timestamps, na_component_total, alpha=0.4)
    plt.plot(ldt_timestamps, na_port_total)
    ls_names = ls_symbols
    ls_names.append('Portfolio')
    plt.legend(ls_names)
    plt.ylabel('Cumulative Returns')
    plt.xlabel('Date')
    fig.autofmt_xdate(rotation=45)
예제 #43
0
파일: hw1.py 프로젝트: chailu2000/coursera
def simulate(na_price, allocations, trading_days):

    # Normalizing the prices to start at 1 and see relative returns
    na_normalized_price = na_price / na_price[0, :]

    # Copy the normalized prices to a new ndarry to find returns.
    na_rets = na_normalized_price.copy()

    # portifolio price
    port_rets = np.sum(na_rets * allocations, axis=1)

    port_price = port_rets.copy()

    # Calculate the daily returns of the prices. (Inplace calculation)
    # returnize0 works on ndarray and not dataframes.
    tsu.returnize0(port_rets)

    #print port_rets

    # portifolio cumulative return
    port_cum = port_price[-1] - 1

    # standard deviation
    vol = np.std(port_rets)
    daily_ret = np.mean(port_rets)
    sharpe = np.sqrt(trading_days) * daily_ret / vol
    cum_ret = port_cum

    return vol,daily_ret,sharpe,cum_ret
예제 #44
0
def compute_stats(na_price):

    ''' Compute Sharpe etc '''
    # Calculate cumulative returns
    na_cum = na_price / na_price[0]

    # Calculate total returns
    na_total = na_price[-1] - na_price[0]

    # Calculate daily returns
    # Copy the normalized prices to a new ndarry to find returns.
    na_rets = na_cum.copy()

    # Calculate the daily returns of the prices. (Inplace calculation)
    # returnize0 works on ndarray and not dataframes.
    tsu.returnize0(na_rets)
    
    # Calculate std and mean of returns
    na_std = np.std(na_rets)
    na_mean = np.mean(na_rets)

    # Calculate Sharpe ratio
    na_sharpe = na_mean / na_std
    
    return(na_sharpe, na_total, na_std, na_mean, na_cum)
예제 #45
0
def simulate(begindate, enddate, tickers, weightings):
    dt_delta = date.timedelta(hours=16)
    ldt_timestamps = dateUtil.getNYSEdays(begindate, enddate, dt_delta)

    ls_keys = ['close']
    c_dataobj = dataAccess.DataAccess('Yahoo')
    ldf_data = c_dataobj.get_data(ldt_timestamps, tickers, ls_keys)
    d_data = dict(zip(ls_keys, ldf_data))

    # Grab the closing values of every stock
    temp = d_data['close'].values.copy()
    # Normalize those values with respect to the initial value
    d_normal = temp / temp[0, :]
    alloc = numpy.array(weightings).reshape(4, 1)
    # Multiply the normalized daily changes by the allocation
    # This gives us an array reflecting the amount allocated to each stock
    portVal = numpy.dot(d_normal, alloc)

    dailyVal = portVal.copy()
    tsUtil.returnize0(dailyVal)

    daily_ret = numpy.mean(dailyVal)
    volatility = numpy.std(dailyVal)
    sharpe = numpy.sqrt(252) * daily_ret / volatility
    total_returns = portVal[portVal.shape[0] - 1][0]

    return volatility, daily_ret, sharpe, total_returns
예제 #46
0
파일: hw_01.py 프로젝트: jsucsy/learning
def simulate(date_start, date_end, symbols, allocations):
    date_timeofday = dt.timedelta(hours=16)
    ldt_timestamps = du.getNYSEdays(date_start, date_end, date_timeofday)
    c_dataobj = da.DataAccess('Yahoo')
    ls_keys = ['open', 'high', 'low', 'close', 'volume', 'actual_close']
    ldf_data = c_dataobj.get_data(ldt_timestamps, symbols, ls_keys)
    d_data = dict(zip(ls_keys, ldf_data))
    na_price = d_data['close'].values
    
    # Normalizing the prices to start at 1 and see relative returns
    na_normalized_price = na_price / na_price[0, :]
    
    # Copy the normalized prices to a new ndarry to find returns.
    na_rets = na_normalized_price.copy()
    na_portrets = np.sum(na_rets * allocations, axis = 1)
    na_port_total = np.cumprod(na_portrets + 1)
    
    tsu.returnize0(na_portrets)
    tsu.returnize0(na_port_total)
    
    rf_rate = 0
    vol = np.std(na_portrets)
    daily_ret = np.average(na_portrets)
    cum_ret = na_port_total[-1]
    #sharpe = np.sqrt(len(ldt_timestamps))*((cum_ret - rf_rate)/vol)
    sharpe = np.sqrt(252)*((daily_ret - rf_rate)/vol)
    
    return vol, daily_ret, sharpe, cum_ret
예제 #47
0
파일: Homework 1.py 프로젝트: AK88-RM/peque
def simulate(startdate, enddate, equities, allocations):

	# Date timestamps
	ldt_timestamps = du.getNYSEdays(startdate, enddate, dt.timedelta(hours = 16))

	# Data access
	c_dataobj = da.DataAccess('Yahoo')
	ls_keys = ['open', 'high', 'low', 'close', 'volume', 'actual_close']
	ldf_data = c_dataobj.get_data(ldt_timestamps, equities, ls_keys)
	d_data = dict(zip(ls_keys, ldf_data))

	# Calculate normalized close data
	close_data = d_data['close'].values
	normalized_close_data = close_data / close_data[0, :]

	# Make sure allocations is a column vector:
	allocations = np.array(allocations).reshape(len(allocations), 1)

	# Calculate portfolio close data
	portfolio_close_data = np.dot(normalized_close_data, allocations)

	# Calculate total return
	portfolio_close_data_copy = portfolio_close_data.copy()
	portfolio_normalized_cumulative_daily_return = np.sum(portfolio_close_data_copy, axis = 1)
	cum_ret = portfolio_normalized_cumulative_daily_return[-1]

	# Calculate volatility, average daily return and sharpe ratio
	portfolio_close_data_copy = portfolio_close_data.copy()
	tsu.returnize0(portfolio_close_data_copy)
	avg_daily_ret = np.mean(portfolio_close_data_copy)
	std_dev = np.std(portfolio_close_data_copy)
	sharpe = np.sqrt(252) * avg_daily_ret / std_dev

	return std_dev, avg_daily_ret, sharpe, cum_ret
예제 #48
0
def simulate(dt_start, dt_end, ls_symbols, lf_port_alloc):

    # Formatting timestamps
    dt_timeofday = dt.timedelta(hours=16)
    ldt_timestamps = du.getNYSEdays(dt_start, dt_end, dt_timeofday)

    # Creating dataset, read in closing price and create dictionary
    c_dataobj = da.DataAccess('Yahoo')
    ls_keys = ['close']
    ldf_data = c_dataobj.get_data(ldt_timestamps, ls_symbols, ls_keys)
    d_data = dict(zip(ls_keys, ldf_data))

    # Filling the data for NAN
    for s_key in ls_keys:
        d_data[s_key] = d_data[s_key].fillna(method='ffill')
        d_data[s_key] = d_data[s_key].fillna(method='bfill')
        d_data[s_key] = d_data[s_key].fillna(1.0)

    # Getting the numpy ndarray of close prices and calculate daily portfolio returns
    na_price = d_data['close'].values
    na_normalized_price = na_price / na_price[0, :]
    na_daily_rets = na_normalized_price.copy()
    tsu.returnize0(na_daily_rets)
    na_daily_portrets = np.sum(na_daily_rets * lf_port_alloc, axis=1)

    # Calculate portfolio returns and estimate statistics
    mean_portret = np.mean(na_daily_portrets)
    port_vol = np.std(na_daily_portrets)
    port_sharpe = np.sqrt(252) * mean_portret / port_vol
    cum_portrets = np.cumprod(na_daily_portrets + 1)
    cum_portret = cum_portrets[-1]

    return port_vol, mean_portret, port_sharpe, cum_portret
    def analyze(self):
        self._parse_value_file()
        self._read_benchmark()
        self.portfolio_values = self.values
        self.values = zip(self.portfolio_values, self.bm_values)
        self._plot()

        values = np.array(self.portfolio_values, float)
        tsu.returnize0(values)

        bench_values = np.array(self.bm_values, float)
        tsu.returnize0(bench_values)

        return {
            "portfolio": {
                'stdev': values.std(),
                'avg': values.mean(),
                'sharp': np.sqrt(252) * values.mean() / values.std(),
                'return':
                (self.portfolio_values[-1] / self.portfolio_values[0])
            },
            self.benchmark: {
                'stdev': bench_values.std(),
                'avg': bench_values.mean(),
                'sharp':
                np.sqrt(252) * bench_values.mean() / bench_values.std(),
                'return': (self.bm_values[-1] / self.bm_values[0])
            }
        }
예제 #50
0
def main(argv):
    input_file    = argv[0]
    output_dir    = argv[1]
    benchmark     = argv[2]

    dates, prices = read_orders(input_file)
    bm_prices     = read_prices(dates[0], dates[-1], benchmark)

    total         = prices[-1]
    returns       = prices[-1] / prices[0]
    bm_returns    = bm_prices[-1] / bm_prices[0]

    n_prices      = prices / prices[0]
    n_bm_prices   = bm_prices / bm_prices[0]

    plot_timeseries(
        dates, 
        n_prices, 
        n_bm_prices, 
        benchmark, 
        file_name(output_dir, dates[0], dates[-1]),
        ylabel = 'Normalized Close'
    )

    tsu.returnize0(prices)
    tsu.returnize0(bm_prices)
    print_results(
        dates, 
        benchmark, 
        prices, 
        bm_prices, 
        returns, 
        bm_returns, 
        total
    )
예제 #51
0
def eventprofiler(df_events, d_data, i_lookback=20, i_lookforward=20,
                s_filename='study', b_market_neutral=True, b_errorbars=True,
                s_market_sym='SPY'):
    ''' Event Profiler for an event matix'''
    df_close = d_data['close'].copy()
    df_rets = df_close.copy()
    tsu.returnize0(df_rets.values)

    if b_market_neutral == True:
        df_rets = df_rets - df_rets[s_market_sym]
        del df_rets[s_market_sym]
        del df_events[s_market_sym]

    df_close = df_close.reindex(columns=df_events.columns)

    # Removing the starting and the end events
    df_events.values[0:i_lookback, :] = np.NaN
    df_events.values[-i_lookforward:, :] = np.NaN

    # Number of events
    i_no_events = int(np.nansum(df_events.values))
    na_event_rets = "False"

    # Looking for the events and pushing them to a matrix
    for i, s_sym in enumerate(df_events.columns):
        for j, dt_date in enumerate(df_events.index):
            if df_events[s_sym][dt_date] == 1:
                na_ret = df_rets[s_sym][j - i_lookback:j + 1 + i_lookforward]
                if type(na_event_rets) == type(""):
                    na_event_rets = na_ret
                else:
                    na_event_rets = np.vstack((na_event_rets, na_ret))

    # Computing daily rets and retuns
    na_event_rets = np.cumprod(na_event_rets + 1, axis=1)
    na_event_rets = (na_event_rets.T / na_event_rets[:, i_lookback]).T

    # Study Params
    na_mean = np.mean(na_event_rets, axis=0)
    na_std = np.std(na_event_rets, axis=0)
    li_time = range(-i_lookback, i_lookforward + 1)

    # Plotting the chart
    plt.clf()
    plt.axhline(y=1.0, xmin=-i_lookback, xmax=i_lookforward, color='k')
    if b_errorbars == True:
        plt.errorbar(li_time[i_lookback:], na_mean[i_lookback:],
                    yerr=na_std[i_lookback:], ecolor='#AAAAFF',
                    alpha=0.1)
    plt.plot(li_time, na_mean, linewidth=3, label='mean', color='b')
    plt.xlim(-i_lookback - 1, i_lookforward + 1)
    if b_market_neutral == True:
        plt.title('Market Relative mean return of ' +\
                str(i_no_events) + ' events')
    else:
        plt.title('Mean return of ' + str(i_no_events) + ' events')
    plt.xlabel('Days')
    plt.ylabel('Cumulative Returns')
    plt.savefig(s_filename, format='pdf')
예제 #52
0
def calculate_weighted_daily_returns_from_close_data(
        data_dictionary_of_close_market_data, allocation):
    normalised_close_data = normalise_prices(
        data_dictionary_of_close_market_data)
    allocated_returns = create_weighted_portfolio_from(normalised_close_data,
                                                       allocation)
    tsu.returnize0(allocated_returns)
    return allocated_returns
예제 #53
0
def main():
    capital = int(sys.argv[1])
    order_file = sys.argv[2]
    value_output_file = sys.argv[3]

    ##symbols and dates are ordered
    dates_set, symbols_set = load_dates_and_symbols(order_file)

    start_date = dates_set[0]
    end_date = dates_set[len(dates_set) - 1] + dt.timedelta(days=1)

    df_close = getData(start_date, end_date, symbols_set)['close']
    df_close = df_close.fillna(method='ffill')
    df_close = df_close.fillna(method='bfill')
    df_close = df_close.fillna(1.0)

    df_trades = copy.deepcopy(df_close)
    df_trades = df_trades * 0

    ##fill trade matrix with actual trades
    fill_trade_matrix(df_trades, order_file)

    ##cash values
    df_cash_flows = df_close * df_trades
    ##time series with cash inflows and outflows
    cash_trades = np.sum(df_cash_flows.values.copy() * -1, axis=1)

    df_close['_CASH'] = 1.0
    df_trades['_CASH'] = cash_trades

    ##now we need a cummulative sum of df_trades to got holding matrix
    df_holding = df_trades.cumsum()

    df_portafolio_value = df_close * df_holding
    df_portafolio_value = df_portafolio_value.sum(axis=1) + capital

    for row_index in df_portafolio_value.index:
        print row_index, "--", df_portafolio_value[row_index]

    na_portafolio_value = df_portafolio_value.values

    tsu.returnize0(na_portafolio_value)

    cummulative_return = np.cumprod(na_portafolio_value + 1)[-1]
    avg = na_portafolio_value.mean()
    std = na_portafolio_value.std()

    print "start_date", start_date, " -- ", "end_date", end_date

    print "fund sharpe_ratio:", avg / std * math.sqrt(252)
    print "fund total return:", cummulative_return
    print "fund standard deviation", std
    print "fund average return", avg
    print "-----"
    do_benchmark_calculations(start_date, end_date, ["$SPX"])
예제 #54
0
def simulate(start_date, end_date, symbols, allocations):

  # List of symbols
  ls_symbols = symbols
  
  # Start and End date of the simualte function
  dt_start = start_date
  dt_end = end_date

  # We need closing prices so the timestamp should be hours=16. (time = 4PM)
  dt_timeofday = dt.timedelta(hours=16)

  # Get a list of trading days between the start and the end.
  ldt_timestamps = du.getNYSEdays(dt_start, dt_end, dt_timeofday)

  # Creating an object of the dataaccess class with Yahoo as the source.
  c_dataobj = da.DataAccess('Yahoo')

  # Keys to be read from the data, it is good to read everything in one go.
  ls_keys = ['open', 'high', 'low', 'close', 'volume', 'actual_close']

  # Reading the data, now d_data is a dictionary with the keys above.
  # Timestamps and symbols are the ones that were specified before.
  ldf_data = c_dataobj.get_data(ldt_timestamps, ls_symbols, ls_keys)
  d_data = dict(zip(ls_keys, ldf_data))

  # Copying close price into separate dataframe to find rets
  df_rets = d_data['close'].values.copy()

  # Normalizing the prices to start at 1 and see relative returns
  df_normal = df_rets / df_rets[0, :] 

  #reshape the list of allocations
  alloc = np.array(allocations).reshape(4,1) 

  #Calculate the portfolio value
  portVal = np.dot(df_normal, alloc)

  #Calculate the daily value
  dailyVal = portVal.copy()

  # returnize0 works on ndarray and not dataframes.
  tsu.returnize0(dailyVal)

  #Calculate daily returns
  daily_ret = np.mean(dailyVal)
  vol = np.std(dailyVal)

  #sharpe ratio = K * mean(daily return) / std(daily return)
  #K = sqrt(Number of trading days)
  sharpe_ratio = np.sqrt(Number_Trading_Days) * (daily_ret / vol)

  cum_ret = portVal[portVal.shape[0] -1][0]  #Taking the last cumulative return value in PortVal array

  return daily_ret,vol,sharpe_ratio,cum_ret 
def simulate(na_normalized_price, ls_alloc):

    # Calculate the daily returns of the prices.
    na_port_daily_returns = np.sum(na_normalized_price * ls_alloc, axis=1)
    tsu.returnize0(na_port_daily_returns)

    std_dev = na_port_daily_returns.std()
    daily_ret = na_port_daily_returns.mean()
    sharpe = sqrt(252) * daily_ret / std_dev
    cum_ret = 1 + na_port_daily_returns.sum()

    return std_dev, daily_ret, sharpe, cum_ret
예제 #56
0
def calcInfo(originalPrice, normalized, timeStamp, ratio):
    tempValue = np.sum(normalized * ratio, axis=1)

    #Calculate daily return
    dailyReturn = tempValue.copy()
    tsu.returnize0(dailyReturn)

    volatility = np.std(dailyReturn)
    average = np.mean(dailyReturn)
    sharpeRatio = (average / volatility) * np.sqrt(len(timeStamp))
    totalReturn = np.cumprod(dailyReturn + 1, axis=0)
    return sharpeRatio
예제 #57
0
파일: hmwk1.py 프로젝트: aifa/CompInvest
def simulate (startdate="January 1,2006", enddate="December 31, 2010", ls_symbols=["AAPL", "GLD", "GOOG", "$SPX", "XOM"], allocations=[0.2,0.3,0.4,0.1]):

    dt_start = dtParser.parse(startdate)
    #print dt_start
    dt_end=dtParser.parse(enddate)
    #print dt_end
    dt_timeofday=dt.timedelta(hours=16)

    # Get a list of trading days between the start and the end.
    ldt_timestamps = du.getNYSEdays(dt_start, dt_end, dt_timeofday)

    # Creating an object of the dataaccess class with Yahoo as the source.
    c_dataobj = da.DataAccess('Yahoo')

    # Keys to be read from the data, it is good to read everything in one go.
    ls_keys = ['open', 'high', 'low', 'close', 'volume', 'actual_close']

    #print ls_symbols
    # Reading the data, now d_data is a dictionary with the keys above.
    # Timestamps and symbols are the ones that were specified before.
    ldf_data = c_dataobj.get_data(ldt_timestamps, ls_symbols, ls_keys)
    #define dictionary (relate ls_keys to data columns)
    d_data = dict(zip(ls_keys, ldf_data))

    # Filling the data for NAN
    for s_key in ls_keys:
        d_data[s_key] = d_data[s_key].fillna(method='ffill')
        d_data[s_key] = d_data[s_key].fillna(method='bfill')
        d_data[s_key] = d_data[s_key].fillna(1.0)

    # Getting the numpy ndarray of close prices.
    na_price = d_data['close'].values

    # Normalizing the prices to start at 1 and see relative returns
    na_normalized_price = na_price / na_price[0, :]
    
    # Copy the normalized prices to a new ndarry to find returns.
    na_rets = na_normalized_price.copy()
    
    na_portrets = np.sum(na_rets * allocations, axis=1)

    # Calculate the daily returns of the prices. (Inplace calculation)
    # returnize0 works on ndarray and not dataframes.
    tsu.returnize0(na_portrets)
    
    na_port_total = np.cumprod(na_portrets+1)
    
    dailyReturnAverage = np.average(na_portrets)
    stdev = np.std(na_portrets)
    sharpe = sqrt(252)*np.mean(na_portrets)/stdev
    cumReturn = na_port_total[na_port_total.size-1]/na_port_total[0]

    return (sharpe,stdev,dailyReturnAverage,cumReturn, na_port_total)
예제 #58
0
def assessPortfolio(na_price, allocations):
    na_normalized = (na_price / na_price[0, :]) * allocations
    daily_ret = np.zeros([np.size(na_price, 0), 1])
    for i in range(np.size(daily_ret, 0)):
        daily_ret[[i]] = sum(na_normalized[i, :])
    norm_daily_ret = daily_ret.copy()
    tsu.returnize0(norm_daily_ret)
    daily_ret_avg = np.mean(norm_daily_ret)
    vol = np.std(norm_daily_ret)
    sharpe = np.sqrt(252) * daily_ret_avg / vol
    cum_ret = float(daily_ret[-1])
    return vol, daily_ret_avg, sharpe, cum_ret
예제 #59
0
def main():
    ''' Main Function'''

    # List of symbols
    ls_symbols = ["ETH-BTC", "DASH-BTC", "LTC-BTC"]
    # Start and End date of the charts
    dt_start = dt.datetime(2017, 1, 10)
    dt_end = dt.datetime(2017, 12, 30)
    # We need closing prices so the timestamp should be hours=16.
    dt_timeofday = dt.timedelta(hours=16)
    # Get a list of trading days between the start and the end.
    ldt_timestamps = du.getNYSEdays(dt_start, dt_end, dt_timeofday)
    # Creating an object of the dataaccess class with Yahoo as the source.
    c_dataobj = da.DataAccess('Yahoo')
    # Keys to be read from the data, it is good to read everything in one go.
    ls_keys = ['open', 'high', 'low', 'close', 'volume', 'actual_close']
    # Reading the data, now d_data is a dictionary with the keys above.
    # Timestamps and symbols are the ones that were specified before.
    ldf_data = c_dataobj.get_data(ldt_timestamps, ls_symbols, ls_keys)
    d_data = dict(zip(ls_keys, ldf_data))

    # Filling the data for NAN
    for s_key in ls_keys:
        d_data[s_key] = d_data[s_key].fillna(method='ffill')
        d_data[s_key] = d_data[s_key].fillna(method='bfill')
        d_data[s_key] = d_data[s_key].fillna(1.0)

    # Getting the numpy ndarray of close prices.
    na_price = d_data['close'].values

    # Plotting the prices with x-axis=timestamps
    plt.clf()
    plt.plot(ldt_timestamps, na_price)
    plt.legend(ls_symbols)
    plt.ylabel('Adjusted Close')
    plt.xlabel('Date')
    plt.savefig('adjustedclose.pdf', format='pdf')

    # Normalizing the prices to start at 1 and see relative returns
    na_normalized_price = na_price / na_price[0, :]
    na_rets = na_normalized_price.copy()
    tsu.returnize0(na_rets)
    dev = na_rets.std(axis=0)
    #if dev==0:
    #  print na_rets[:5]
    avg = na_rets.mean(axis=0)
    cum_ret=(na_normalized_price[-1])
    sharpe = avg/dev*(252**0.5)
    print  "Sharpe ratio of Symbol:", sharpe
    print  "Total returns of Symbol: ", cum_ret
    print "Standard deviation of Symbols:", dev
    print  "Daily returns of Symbols:", avg