def ReadData(DataFile, TimeTag, IDTag, FactorTag): #Reading the timestamps from a text file. timestamps=[] file = open(TimeTag, 'r') for onedate in file.readlines(): timestamps.append(dt.datetime.strptime(onedate, "%Y-%m-%d\n")) file.close() symbols=[] file = open(IDTag, 'r') for f in file.readlines(): j = f[:-1] symbols.append(j) file.close() # Reading the Data Values Numpyarray=pickle.load(open( DataFile, 'rb' )) for i in range(0,len(Numpyarray)): tsu.fillforward(Numpyarray[i]) tsu.fillbackward(Numpyarray[i]) featureslist=[] file = open(FactorTag, 'r') for f in file.readlines(): j = f[:-1] featureslist.append(j) file.close() PandasObject= Panel(Numpyarray, items=featureslist, major_axis=timestamps, minor_axis=symbols) featureslist.sort() return (PandasObject, featureslist, symbols, timestamps)
sharperatios[sym]['sr'] = 0 sharperatios = sharperatios.T # # Plot the adjusted close data # plt.clf() newtimestamps = close.index pricedat = close.values # pull the 2D ndarray out of the pandas object #normalize nans for sym in close.columns: sharperatios['NaNs'][sym] = sharperatios['NaNs'][sym] / len(timestamps) tsu.fillforward(pricedat) tsu.fillbackward(pricedat) dailyrets = (pricedat[1:,:]/pricedat[0:-1,:]) - 1 #print dailyrets dailyrets = np.insert(dailyrets, 0, np.zeros(dailyrets.shape[1]), 0) pp = PdfPages('bse-summary2013.pdf') plt.plot(newtimestamps, dailyrets) plt.legend(close.columns) plt.ylabel('dailyrets') plt.xlabel('Date') pp.savefig() for sym in range (0, len(close.columns)): averageret = np.average(dailyrets[:,sym])
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.returnize0(naData) tsu.fillforward(naDataTest) tsu.fillbackward(naDataTest) tsu.returnize0(naDataTest) ''' Get efficient frontiers ''' (lfReturn, lfStd, lnaPortfolios, naAvgRets, naStd) = getFrontier( naData) (lfReturnTest, lfStdTest, unused, unused, unused) = getFrontier( naDataTest) plt.clf() fig = plt.figure() ''' Plot efficient frontiers ''' plt.plot(lfStd,lfReturn, 'b')
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
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