def get_data(assets, start_date): prices = {} for asset, ticker in assets.items(): prices[asset] = get_close_price(ticker, start_date) df = DataFrame(prices) return df
def Econ_env(YYYY, m, dd): start_date = datetime.datetime(YYYY, m, dd) GDP = DataReader('GDP', "fred", start=start_date) sp500 = DataReader('^GSPC', "yahoo", start=start_date) Array = DataFrame({'S&P':sp500["Adj Close"]}) return Array
def main(): # define the desired portfolio characteristics std_max = 100 # maximum standard deviation MAX_ITERS = 100 # max number of iterations lam = .94 # exponential decay number assets = (['GOOGLE', 'APPLE', 'CAT', 'SPDR_GOLD', 'OIL', 'NATURAL_GAS', 'USD', 'GOLDMANSACHS', 'DOMINION']) # Pull data from Yahoo! Finance GOOG= Stock_Close('GOOG', 2010, 1, 1) AAPL = Stock_Close('AAPL', 2010,1, 1) SP500 = Stock_Close('^GSPC', 2010, 1, 1) CAT = Stock_Close('CAT', 2010, 1, 1) GOLD = Stock_Close('GLD', 2010, 1, 1) GAS = Stock_Close('GAZ', 2010, 1, 1) OIL = Stock_Close('OIL', 2010, 1, 1) GS = Stock_Close('GS', 2010, 1, 1) DOM = Stock_Close('D', 2010, 1, 1) # FX currency USD = Stock_Close('UUP', 2010, 1, 1) # create a dataframe housing the above X = DataFrame({'GOOGLE':GOOG, 'APPLE':AAPL, 'CAT':CAT, 'SPDR_GOLD':GOLD, 'OIL':OIL, 'NATURAL_GAS':GAS, 'USD':USD, 'GOLDMANSACHS': GS, 'DOMINION':DOM}) # define weights of each asset held # weights = {'GOOGLE':.2, # 'APPLE':.1, # 'CAT':.1, # 'SPDR GOLD':.05, # 'OIL':.1, # 'NATURAL GAS':.1, # 'USD':.05, # 'DOMINION':.1, # 'GOLDMANSACHS':.2} best = zeros(((2+len(assets)), MAX_ITERS)) for i in range(1, MAX_ITERS): # check to make sure sum weights = 1 numWeight = DirichletDistro(len(assets),1) if int(sum(numWeight)) < 1.01: #create dictionary of weights for each of the assets weights = dict(zip(assets, numWeight)) profit, STD, EWMA = Stock_stats(X, weights, assets, lam) # store trial profit, stdev in column of best best[0:2, i] = [ profit, STD] best[2::, i] = numWeight else: print('Sum of weights does not equal 1') # now that we have the monte carlo simulation setup, eliminate # all trials that do not meet critera for i in range(1, MAX_ITERS): if best[1, i] >std_max: best[:, i] = zeros(( (2+len(assets)), 1)) # drop the trial # add more critera here... else: pass print( max(sum(best, 0)) ) # print the maximum portfolio return X, weights, STD, profit, numWeight, best, EWMA
def main(): # define the desired portfolio characteristics std_max = .2 # maximum standard deviation MAX_ITERS = 200 # max number of iterations lam = .94 # exponential decay number exit_date = 12 # when you sell stocks (in months) window_begin = 6 # how far back you want to window reg. (in months) beta = .6 # personal risk adversion level delta = .99 # discount factor assets = (['GOOGLE', 'APPLE', 'CAT', 'SPDR_GOLD', 'OIL', 'NATURAL_GAS', 'USD', 'GOLDMANSACHS', 'DOMINION']) print('Pulling data from Yahoo! Finance') # Pull data from Yahoo! Finance GOOG= Stock_Close('GOOG', 2010, 1, 1) AAPL = Stock_Close('AAPL', 2010,1, 1) SP500 = Stock_Close('^GSPC', 2010, 1, 1) CAT = Stock_Close('CAT', 2010, 1, 1) GOLD = Stock_Close('GLD', 2010, 1, 1) GAS = Stock_Close('GAZ', 2010, 1, 1) OIL = Stock_Close('OIL', 2010, 1, 1) GS = Stock_Close('GS', 2010, 1, 1) DOM = Stock_Close('D', 2010, 1, 1) # FX currency USD = Stock_Close('UUP', 2010, 1, 1) # create a dataframe housing the above X = DataFrame({'GOOGLE':GOOG, 'APPLE':AAPL, 'CAT':CAT, 'SPDR_GOLD':GOLD, 'OIL':OIL, 'NATURAL_GAS':GAS, 'USD':USD, 'GOLDMANSACHS': GS, 'DOMINION':DOM}) best = zeros(((4+len(assets)), MAX_ITERS)) print('Running monte carlo simulation') for i in range(1, MAX_ITERS): print('Percent Done: \t' + str(float(i)/float(MAX_ITERS)*100)+' %') numWeight = DirichletDistro(len(assets),1) EU = {} # check to make sure sum weights = 1 if int(sum(numWeight)) < 1.01: # account for floating point err #create dictionary of weights for each of the assets weights = dict(zip(assets, numWeight)) STD, EWMA = Stock_stats(X, weights, assets, lam) # calculate price at future period regr, window, projection, profit = regression(X, assets, window_begin, exit_date) # calculate expected utility for ass in assets: profit[ass] = profit[ass] * weights[ass] EU[ass] = utl(profit[ass], EWMA[ass], beta, delta, exit_date*30) # calculate the variance of the portfolio var_portfolio = var(X, weights, assets) # # check if the EWMA is above the specified limit if var_portfolio == std_max: best[:, i] = zeros(( (4+len(assets)) )) # drop the trial # add more critera here... else: # store trial profit, EWMA, STD in column of best best[0:4, i] = [sum(EU.values()), var_portfolio, sum(EWMA.values()), sum(profit.values())] sorted_assest = sorted(weights.keys()) print(sum(profit.values())) j = 4 for ass in sorted_assest: best[j, i] = weights[ass] j = j+1 else: print('Sum of weights does not equal 1') maxP = max(best[0,:]) print( "The maximum profit to be made is: %f") % maxP # find the column where the sum is equal to the max opt = where(best[0,:] == maxP) # OptimalAllocation = dict(zip(assets, [float(xx) for xx in best[2:,opt]])) print('\n The optimal asset allocation in the portfolio is:') # print(OptimalAllocation) return X, best, assets, EU, regr, profit