Пример #1
0
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)
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)
Пример #3
0
        sharperatios[sym]['NaNs'] = nans
        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)):
Пример #4
0
lYear = 2009
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 '''
Пример #5
0
# Read the historical data in from our data store
#
endday = dt.datetime(2011,1,1) 
startday = endday - dt.timedelta(days=1095) #three years back
timeofday=dt.timedelta(hours=16)
print "start getNYSEdays"
timestamps = du.getNYSEdays(startday,endday,timeofday)
print "start read"
close = dataobj.get_data(timestamps,portsyms,"close")
print "end read"

#
# Copy, prep, and compute daily returns
#
rets = close.values.copy()
tsu.fillforward(rets)
tsu.returnize0(rets)

#
# Estimate portfolio total returns
#
portrets = sum(rets*portalloc,axis=1)
porttot = cumprod(portrets+1)
componenttot = cumprod(rets+1,axis=0) # compute returns for components

#
# Plot the result
#
plt.clf()
fig = plt.figure()
fig.add_subplot(111)
Пример #6
0
    if llValid[i] == llTotal[i]:
        lHundred = lHundred + 1
    
ltBoth.sort()
ltBoth.reverse()

for tRes in ltBoth:
    print '%10s: %.02lf%%'%( tRes[1], tRes[0] )
    
print '\n%i out of %i elemenets non-zero.'%(lNonZero, len(lsItems))
print '%i out of %i elemenets have 100%% participation.'%(lHundred, len(lsItems))


''' Retrieve and plot quarterly earnings per share '''

dmEps = dmKeys = dmValues[ dLabel['EPSPIQ'] ]
naEps = dmEps.values
tsu.fillforward( naEps )

plt.clf()

for i, sStock in enumerate( symbols ):
    plt.plot( tsAll, naEps[:,i] )
    
plt.gcf().autofmt_xdate(rotation=45)
plt.legend( symbols, loc='upper left' )
plt.title('EPS of various stocks')

savefig('tutorial6.pdf',format='pdf')

Пример #7
0
lYear = 2009
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()
Пример #8
0
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
Пример #9
0
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