Package QSTK :: Package qstkfeat :: Module classes
[hide private]
[frames] | no frames]

Source Code for Module QSTK.qstkfeat.classes

 1  ''' 
 2  Created on Nov 7, 2011 
 3   
 4  @author: John Cornwell 
 5  @contact: JohnWCornwellV@gmail.com 
 6  @summary: File containing various classification functions 
 7   
 8  ''' 
 9   
10  ''' 3rd Party Imports ''' 
11  import pandas as pand 
12  import numpy as np 
13   
14 -def classFutRet( dData, lLookforward=21, sRel=None, dfOpen=pand.DataFrame() ):
15 ''' 16 @summary: Calculate classification, uses future returns 17 @param dData: Dictionary of data to use 18 @param lLookforward: Number of days to look in the future 19 @param sRel: Stock symbol that this should be relative to, ususally $SPX. 20 @patam dfOpen: If supplied, stock will be purchased at i+1 open. 21 @return: DataFrame containing values 22 ''' 23 24 dfPrice = dData['close'] 25 26 ''' Class DataFrame will be 1:1, we can use the price as a template, need to copy values ''' 27 dfRet = pand.DataFrame( index=dfPrice.index, columns=dfPrice.columns, data=np.copy(dfPrice.values) ) 28 29 ''' If we want market relative, calculate those values now ''' 30 if not sRel == None: 31 lLen = len(dfPrice[sRel].index) 32 ''' Loop over time ''' 33 for i in range(lLen): 34 35 if i + lLookforward >= lLen: 36 dfRet[sRel][i] = float('nan') 37 continue 38 39 ''' We either buy on todays close or tomorrows open ''' 40 if len( dfOpen.index ) == 0: 41 fBuy = dfRet[sRel][i] 42 else: 43 fBuy = dfOpen[sRel][i+1] 44 45 dfRet[sRel][i] = (dfRet[sRel][i+lLookforward] - fBuy) / fBuy 46 47 ''' Loop through stocks ''' 48 for sStock in dfPrice.columns: 49 50 ''' We have already done this stock ''' 51 if sStock == sRel: 52 continue 53 54 lLen = len(dfPrice[sStock].index) 55 ''' Loop over time ''' 56 for i in range(lLen): 57 58 if i + lLookforward >= lLen: 59 dfRet[sStock][i] = float('nan') 60 continue 61 62 ''' We either buy on todays close or tomorrows open ''' 63 if len( dfOpen.index ) == 0: 64 fBuy = dfRet[sStock][i] 65 else: 66 fBuy = dfOpen[sStock][i+1] 67 68 dfRet[sStock][i] = (dfRet[sStock][i+lLookforward] - fBuy) / fBuy 69 70 ''' Make market relative ''' 71 if not sRel == None: 72 dfRet[sStock][i] -= dfRet[sRel][i] 73 74 return dfRet
75 76 77 if __name__ == '__main__': 78 pass 79