def stratMark( dtStart, dtEnd, dFuncArgs ): """ @summary Markovitz strategy, generates a curve and then chooses a point on it. @param dtStart: Start date for portfolio @param dtEnd: End date for portfolio @param dFuncArgs: Dict of function args passed to the function @return DataFrame corresponding to the portfolio allocations """ if not dFuncArgs.has_key('dmPrice'): print 'Error:', stratMark.__name__, 'requires dmPrice information' return if not dFuncArgs.has_key('sPeriod'): print 'Error:', stratMark.__name__, 'requires rebalancing period' return if not dFuncArgs.has_key('lLookback'): print 'Error:', stratMark.__name__, 'requires lookback' return if not dFuncArgs.has_key('sMarkPoint'): print 'Error:', stratMark.__name__, 'requires markowitz point to choose' return ''' Optional variables ''' if not dFuncArgs.has_key('bAddAlpha'): bAddAlpha = False else: bAddAlpha = dFuncArgs['bAddAlpha'] dmPrice = dFuncArgs['dmPrice'] sPeriod = dFuncArgs['sPeriod'] lLookback = dFuncArgs['lLookback'] sMarkPoint = dFuncArgs['sMarkPoint'] ''' Select rebalancing dates ''' drNewRange = pand.DateRange(dtStart, dtEnd, timeRule=sPeriod) + pand.DateOffset(hours=16) dfAlloc = pand.DataMatrix() ''' Go through each rebalance date and calculate an efficient frontier for each ''' for i, dtDate in enumerate(drNewRange): dtStart = dtDate - pand.DateOffset(days=lLookback) if( dtStart < dmPrice.index[0] ): print 'Error, not enough data to rebalance' continue naRets = dmPrice.ix[ dtStart:dtDate ].values.copy() tsu.returnize1(naRets) tsu.fillforward(naRets) tsu.fillbackward(naRets) ''' Add alpha to returns ''' if bAddAlpha: if i < len(drNewRange) - 1: naFutureRets = dmPrice.ix[ dtDate:drNewRange[i+1] ].values.copy() tsu.returnize1(naFutureRets) tsu.fillforward(naFutureRets) tsu.fillbackward(naFutureRets) naAvg = np.mean( naFutureRets, axis=0 ) ''' make a mix of past/future rets ''' for i in range( naRets.shape[0] ): naRets[i,:] = (naRets[i,:] + (naAvg*0.05)) / 1.05 ''' Generate the efficient frontier ''' (lfReturn, lfStd, lnaPortfolios) = getFrontier( naRets, fUpper=0.2, fLower=0.01 ) lInd = 0 ''' plt.clf() plt.plot( lfStd, lfReturn)''' if( sMarkPoint == 'Sharpe'): ''' Find portfolio with max sharpe ''' fMax = -1E300 for i in range( len(lfReturn) ): fShrp = (lfReturn[i]-1) / (lfStd[i]) if fShrp > fMax: fMax = fShrp lInd = i ''' plt.plot( [lfStd[lInd]], [lfReturn[lInd]], 'ro') plt.draw() time.sleep(2) plt.show()''' elif( sMarkPoint == 'MinVar'): ''' use portfolio with minimum variance ''' fMin = 1E300 for i in range( len(lfReturn) ): if lfStd[i] < fMin: fMin = lfStd[i] lInd = i elif( sMarkPoint == 'MaxRet'): ''' use Portfolio with max returns (not really markovitz) ''' lInd = len(lfReturn)-1 elif( sMarkPoint == 'MinRet'): ''' use Portfolio with min returns (not really markovitz) ''' lInd = 0 else: print 'Warning: invalid sMarkPoint''' return ''' Generate allocation based on selected portfolio ''' naAlloc = (np.array( lnaPortfolios[lInd] ).reshape(1,-1) ) dmNew = pand.DataMatrix( index=[dtDate], data=naAlloc, columns=(dmPrice.columns) ) dfAlloc = dfAlloc.append( dmNew ) dfAlloc['_CASH'] = 0.0 return dfAlloc
dtEnd = dt.datetime(lYear + 1, 1, 1) dtStart = dtEnd - dt.timedelta(days=365) dtTest = dtEnd + dt.timedelta(days=365) timeofday = dt.timedelta(hours=16) ldtTimestamps = du.getNYSEdays(dtStart, dtEnd, timeofday) ldtTimestampTest = du.getNYSEdays(dtEnd, dtTest, timeofday) dmClose = norgateObj.get_data(ldtTimestamps, lsSymbols, "close") dmTest = norgateObj.get_data(ldtTimestampTest, lsSymbols, "close") naData = dmClose.values.copy() naDataTest = dmTest.values.copy() tsu.fillforward(naData) tsu.fillbackward(naData) tsu.returnize1(naData) tsu.fillforward(naDataTest) tsu.fillbackward(naDataTest) tsu.returnize1(naDataTest) lPeriod = 21 ''' Get efficient frontiers ''' (lfReturn, lfStd, lnaPortfolios, naAvgRets, naStd) = getFrontier(naData, lPeriod) (lfReturnTest, lfStdTest, unused, unused, unused) = getFrontier(naDataTest, lPeriod) plt.clf() fig = plt.figure()
dtEnd = dt.datetime(lYear+1,1,1) dtStart = dtEnd - dt.timedelta(days=365) dtTest = dtEnd + dt.timedelta(days=365) timeofday=dt.timedelta(hours=16) ldtTimestamps = du.getNYSEdays( dtStart, dtEnd, timeofday ) ldtTimestampTest = du.getNYSEdays( dtEnd, dtTest, timeofday ) dmClose = norgateObj.get_data(ldtTimestamps, lsSymbols, "close") dmTest = norgateObj.get_data(ldtTimestampTest, lsSymbols, "close") naData = dmClose.values.copy() naDataTest = dmTest.values.copy() tsu.fillforward(naData) tsu.fillbackward(naData) tsu.returnize1(naData) tsu.fillforward(naDataTest) tsu.fillbackward(naDataTest) tsu.returnize1(naDataTest) lPeriod = 21 ''' Get efficient frontiers ''' (lfReturn, lfStd, lnaPortfolios, naAvgRets, naStd) = getFrontier( naData, lPeriod ) (lfReturnTest, lfStdTest, unused, unused, unused) = getFrontier( naDataTest, lPeriod ) plt.clf() fig = plt.figure()
def stratMark(dtStart, dtEnd, dFuncArgs): """ @summary Markovitz strategy, generates a curve and then chooses a point on it. @param dtStart: Start date for portfolio @param dtEnd: End date for portfolio @param dFuncArgs: Dict of function args passed to the function @return DataFrame corresponding to the portfolio allocations """ if not dFuncArgs.has_key('dmPrice'): print 'Error:', stratMark.__name__, 'requires dmPrice information' return if not dFuncArgs.has_key('sPeriod'): print 'Error:', stratMark.__name__, 'requires rebalancing period' return if not dFuncArgs.has_key('lLookback'): print 'Error:', stratMark.__name__, 'requires lookback' return if not dFuncArgs.has_key('sMarkPoint'): print 'Error:', stratMark.__name__, 'requires markowitz point to choose' return ''' Optional variables ''' if not dFuncArgs.has_key('bAddAlpha'): bAddAlpha = False else: bAddAlpha = dFuncArgs['bAddAlpha'] dmPrice = dFuncArgs['dmPrice'] sPeriod = dFuncArgs['sPeriod'] lLookback = dFuncArgs['lLookback'] sMarkPoint = dFuncArgs['sMarkPoint'] ''' Select rebalancing dates ''' drNewRange = pand.DateRange(dtStart, dtEnd, timeRule=sPeriod) + pand.DateOffset(hours=16) dfAlloc = pand.DataMatrix() ''' Go through each rebalance date and calculate an efficient frontier for each ''' for i, dtDate in enumerate(drNewRange): dtStart = dtDate - pand.DateOffset(days=lLookback) if (dtStart < dmPrice.index[0]): print 'Error, not enough data to rebalance' continue naRets = dmPrice.ix[dtStart:dtDate].values.copy() tsu.returnize1(naRets) tsu.fillforward(naRets) tsu.fillbackward(naRets) ''' Add alpha to returns ''' if bAddAlpha: if i < len(drNewRange) - 1: naFutureRets = dmPrice.ix[dtDate:drNewRange[i + 1]].values.copy() tsu.returnize1(naFutureRets) tsu.fillforward(naFutureRets) tsu.fillbackward(naFutureRets) naAvg = np.mean(naFutureRets, axis=0) ''' make a mix of past/future rets ''' for i in range(naRets.shape[0]): naRets[i, :] = (naRets[i, :] + (naAvg * 0.05)) / 1.05 ''' Generate the efficient frontier ''' (lfReturn, lfStd, lnaPortfolios) = getFrontier(naRets, fUpper=0.2, fLower=0.01) lInd = 0 ''' plt.clf() plt.plot( lfStd, lfReturn)''' if (sMarkPoint == 'Sharpe'): ''' Find portfolio with max sharpe ''' fMax = -1E300 for i in range(len(lfReturn)): fShrp = (lfReturn[i] - 1) / (lfStd[i]) if fShrp > fMax: fMax = fShrp lInd = i ''' plt.plot( [lfStd[lInd]], [lfReturn[lInd]], 'ro') plt.draw() time.sleep(2) plt.show()''' elif (sMarkPoint == 'MinVar'): ''' use portfolio with minimum variance ''' fMin = 1E300 for i in range(len(lfReturn)): if lfStd[i] < fMin: fMin = lfStd[i] lInd = i elif (sMarkPoint == 'MaxRet'): ''' use Portfolio with max returns (not really markovitz) ''' lInd = len(lfReturn) - 1 elif (sMarkPoint == 'MinRet'): ''' use Portfolio with min returns (not really markovitz) ''' lInd = 0 else: print 'Warning: invalid sMarkPoint' '' return ''' Generate allocation based on selected portfolio ''' naAlloc = (np.array(lnaPortfolios[lInd]).reshape(1, -1)) dmNew = pand.DataMatrix(index=[dtDate], data=naAlloc, columns=(dmPrice.columns)) dfAlloc = dfAlloc.append(dmNew) dfAlloc['_CASH'] = 0.0 return dfAlloc
import matplotlib.pyplot as plt from pylab import * import pandas print pandas.__version__ # # Prepare to read the data startday = dt.datetime(2011, 1, 1) endday = dt.datetime(2011, 12, 31) timeofday=dt.timedelta(hours=16) timestamps = du.getNYSEdays(startday, endday, timeofday) # #Read close data from symbols list dataobj = da.DataAccess('Yahoo') symbols = dataobj.get_all_symbols() close = dataobj.get_data(timestamps, symbols, "close", verbose=True) # #Calculate daily_return trading_date = close.index daily_price = close.values.copy() daily_rets = close.values.copy() tsu.fillforward(daily_rets) tsu.fillbackward(daily_rets) tsu.returnize0(daily_rets) sharpe_list = sorted(zip(tsu.get_sharpe_ratio(daily_rets), daily_price[-1, :] / daily_price[0, :] - 1, symbols), reverse=True) #Filter NaN value sharpe_list = [f for f in sharpe_list if not math.isnan(f[0]) and not math.isnan(f[1])] print sharpe_list