<s> '''<STR_LIT>''' <EOL> import random <EOL> import pandas as pand <EOL> import numpy as np <EOL> import datetime as dt <EOL> import QSTK . qstkutil . tsutil as tsu <EOL> from QSTK . qstkutil import DataAccess as da <EOL> import QSTK . qstkutil . qsdateutil as du <EOL> def featMomentum ( dData , lLookback = <NUM_LIT:20> , b_human = False ) : <EOL> '''<STR_LIT>''' <EOL> if b_human : <EOL> for sym in dData [ '<STR_LIT>' ] : <EOL> x = <NUM_LIT:1000> / dData [ '<STR_LIT>' ] [ sym ] [ <NUM_LIT:0> ] <EOL> dData [ '<STR_LIT>' ] [ sym ] = dData [ '<STR_LIT>' ] [ sym ] * x <EOL> return dData [ '<STR_LIT>' ] <EOL> dfPrice = dData [ '<STR_LIT>' ] . copy ( ) <EOL> tsu . returnize0 ( dfPrice . values ) <EOL> dfRet = pand . rolling_sum ( dfPrice , lLookback ) <EOL> return dfRet <EOL> def featHiLow ( dData , lLookback = <NUM_LIT:20> , b_human = False ) : <EOL> '''<STR_LIT>''' <EOL> if b_human : <EOL> for sym in dData [ '<STR_LIT>' ] : <EOL> x = <NUM_LIT:1000> / dData [ '<STR_LIT>' ] [ sym ] [ <NUM_LIT:0> ] <EOL> dData [ '<STR_LIT>' ] [ sym ] = dData [ '<STR_LIT>' ] [ sym ] * x <EOL> return dData [ '<STR_LIT>' ] <EOL> dfPrice = dData [ '<STR_LIT>' ] <EOL> maxes = pand . rolling_max ( dfPrice , lLookback , <NUM_LIT:1> ) <EOL> mins = pand . rolling_min ( dfPrice , lLookback , <NUM_LIT:1> ) <EOL> ranges = maxes - mins <EOL> dfRet = ( ( ( dfPrice - mins ) * <NUM_LIT:2> ) / ranges ) - <NUM_LIT:1> <EOL> return dfRet <EOL> def featDate ( dData , b_human = False ) : <EOL> '''<STR_LIT>''' <EOL> if b_human : <EOL> for sym in dData [ '<STR_LIT>' ] : <EOL> x = <NUM_LIT:1000> / dData [ '<STR_LIT>' ] [ sym ] [ <NUM_LIT:0> ] <EOL> dData [ '<STR_LIT>' ] [ sym ] = dData [ '<STR_LIT>' ] [ sym ] * x <EOL> return dData [ '<STR_LIT>' ] <EOL> dfPrice = dData [ '<STR_LIT>' ] <EOL> dfRet = pand . DataFrame ( index = dfPrice . index , columns = dfPrice . columns , data = np . zeros ( dfPrice . shape ) ) <EOL> for sStock in dfPrice . columns : <EOL> tsPrice = dfPrice [ sStock ] <EOL> tsRet = dfRet [ sStock ] <EOL> for i in range ( len ( tsPrice . index ) ) : <EOL> today = tsPrice . index [ i ] <EOL> days = today - dt . datetime ( today . year , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> tsRet [ i ] = float ( days . days * <NUM_LIT:2> ) / <NUM_LIT> - <NUM_LIT:1> <EOL> return dfRet <EOL> def featOption ( dData , b_human = False ) : <EOL> '''<STR_LIT>''' <EOL> if b_human : <EOL> for sym in dData [ '<STR_LIT>' ] : <EOL> x = <NUM_LIT:1000> / dData [ '<STR_LIT>' ] [ sym ] [ <NUM_LIT:0> ] <EOL> dData [ '<STR_LIT>' ] [ sym ] = dData [ '<STR_LIT>' ] [ sym ] * x <EOL> return dData [ '<STR_LIT>' ] <EOL> dfPrice = dData [ '<STR_LIT>' ] <EOL> dfRet = pand . DataFrame ( index = dfPrice . index , columns = dfPrice . columns , data = np . zeros ( dfPrice . shape ) ) <EOL> for sStock in dfPrice . columns : <EOL> tsPrice = dfPrice [ sStock ] <EOL> tsRet = dfRet [ sStock ] <EOL> for i in range ( len ( tsPrice . index ) ) : <EOL> today = tsPrice . index [ i ] <EOL> last_close = du . getLastOptionClose ( today , tsPrice . index ) <EOL> next_close = du . getNextOptionClose ( today , tsPrice . index ) <EOL> days_between = next_close - last_close <EOL> days = today - last_close <EOL> tsRet [ i ] = float ( days . days * <NUM_LIT:2> ) / days_between . days - <NUM_LIT:1> <EOL> return dfRet <EOL> def featMA ( dData , lLookback = <NUM_LIT:30> , bRel = True , b_human = False ) : <EOL> '''<STR_LIT>''' <EOL> dfPrice = dData [ '<STR_LIT>' ] <EOL> dfRet = pand . rolling_mean ( dfPrice , lLookback ) <EOL> if bRel : <EOL> dfRet = dfRet / dfPrice <EOL> if b_human : <EOL> data2 = dfRet * dData [ '<STR_LIT>' ] <EOL> data3 = pand . DataFrame ( { "<STR_LIT>" : data2 [ data2 . columns [ <NUM_LIT:0> ] ] } ) <EOL> for sym in dfRet . columns : <EOL> if sym != '<STR_LIT>' and sym != '<STR_LIT>' : <EOL> data3 [ sym + "<STR_LIT>" ] = data2 [ sym ] <EOL> data3 [ sym ] = dData [ '<STR_LIT>' ] [ sym ] <EOL> del data3 [ '<STR_LIT>' ] <EOL> return data3 <EOL> return dfRet <EOL> def featEMA ( dData , lLookback = <NUM_LIT:20> , bRel = True , b_human = False ) : <EOL> '''<STR_LIT>''' <EOL> dfPrice = dData [ '<STR_LIT>' ] <EOL> dfRet = pand . ewma ( dfPrice , span = lLookback ) <EOL> if bRel : <EOL> dfRet = dfRet / dfPrice ; <EOL> if b_human : <EOL> data2 = dfRet * dData [ '<STR_LIT>' ] <EOL> data3 = pand . DataFrame ( { "<STR_LIT>" : data2 [ data2 . columns [ <NUM_LIT:0> ] ] } ) <EOL> for sym in dfRet . columns : <EOL> if sym != '<STR_LIT>' and sym != '<STR_LIT>' : <EOL> data3 [ sym + "<STR_LIT>" ] = data2 [ sym ] <EOL> data3 [ sym ] = dData [ '<STR_LIT>' ] [ sym ] <EOL> del data3 [ '<STR_LIT>' ] <EOL> return data3 <EOL> return dfRet <EOL> def featSTD ( dData , lLookback = <NUM_LIT:20> , bRel = True , b_human = False ) : <EOL> '''<STR_LIT>''' <EOL> dfPrice = dData [ '<STR_LIT>' ] . copy ( ) <EOL> tsu . returnize1 ( dfPrice . values ) <EOL> dfRet = pand . rolling_std ( dfPrice , lLookback ) <EOL> if bRel : <EOL> dfRet = dfRet / dfPrice <EOL> if b_human : <EOL> for sym in dData [ '<STR_LIT>' ] : <EOL> x = <NUM_LIT:1000> / dData [ '<STR_LIT>' ] [ sym ] [ <NUM_LIT:0> ] <EOL> dData [ '<STR_LIT>' ] [ sym ] = dData [ '<STR_LIT>' ] [ sym ] * x <EOL> return dData [ '<STR_LIT>' ] <EOL> return dfRet <EOL> def featRSI ( dData , lLookback = <NUM_LIT> , b_human = False ) : <EOL> '''<STR_LIT>''' <EOL> dfDelta = dData [ '<STR_LIT>' ] . copy ( ) <EOL> dfDelta . ix [ <NUM_LIT:1> : , : ] -= dfDelta . ix [ : - <NUM_LIT:1> , : ] . values <EOL> dfDelta . ix [ <NUM_LIT:0> , : ] = np . NAN <EOL> dfDeltaUp = dfDelta <EOL> dfDeltaDown = dfDelta . copy ( ) <EOL> for sColumn in dfDeltaUp . columns : <EOL> tsColDown = dfDeltaDown [ sColumn ] <EOL> tsColDown [ tsColDown >= <NUM_LIT:0> ] = <NUM_LIT:0> <EOL> tsColUp = dfDeltaUp [ sColumn ] <EOL> tsColUp [ tsColUp <= <NUM_LIT:0> ] = <NUM_LIT:0> <EOL> dfRolUp = pand . rolling_mean ( dfDeltaUp , lLookback , min_periods = <NUM_LIT:1> ) <EOL> dfRolDown = pand . rolling_mean ( dfDeltaDown , lLookback , min_periods = <NUM_LIT:1> ) . abs ( ) <EOL> dfRS = dfRolUp / dfRolDown <EOL> dfRSI = <NUM_LIT> - ( <NUM_LIT> / ( <NUM_LIT:1.0> + dfRS ) ) <EOL> if b_human : <EOL> for sym in dData [ '<STR_LIT>' ] : <EOL> x = <NUM_LIT:1000> / dData [ '<STR_LIT>' ] [ sym ] [ <NUM_LIT:0> ] <EOL> dData [ '<STR_LIT>' ] [ sym ] = dData [ '<STR_LIT>' ] [ sym ] * x <EOL> return dData [ '<STR_LIT>' ] <EOL> return dfRSI <EOL> def featDrawDown ( dData , lLookback = <NUM_LIT:30> , b_human = False ) : <EOL> '''<STR_LIT>''' <EOL> dfPrice = dData [ '<STR_LIT>' ] <EOL> dfRet = pand . DataFrame ( index = dfPrice . index , columns = dfPrice . columns , data = np . zeros ( dfPrice . shape ) ) <EOL> dfMax = pand . rolling_max ( dfPrice , lLookback ) <EOL> return ( dfMax - dfPrice ) / dfMax ; <EOL> if b_human : <EOL> for sym in dData [ '<STR_LIT>' ] : <EOL> x = <NUM_LIT:1000> / dData [ '<STR_LIT>' ] [ sym ] [ <NUM_LIT:0> ] <EOL> dData [ '<STR_LIT>' ] [ sym ] = dData [ '<STR_LIT>' ] [ sym ] * x <EOL> return dData [ '<STR_LIT>' ] <EOL> return dfRet <EOL> def featRunUp ( dData , lLookback = <NUM_LIT:30> , b_human = False ) : <EOL> '''<STR_LIT>''' <EOL> dfPrice = dData [ '<STR_LIT>' ] <EOL> dfMax = pand . rolling_min ( dfPrice , lLookback ) <EOL> return dfPrice / dfMax ; <EOL> if b_human : <EOL> for sym in dData [ '<STR_LIT>' ] : <EOL> x = <NUM_LIT:1000> / dData [ '<STR_LIT>' ] [ sym ] [ <NUM_LIT:0> ] <EOL> dData [ '<STR_LIT>' ] [ sym ] = dData [ '<STR_LIT>' ] [ sym ] * x <EOL> return dData [ '<STR_LIT>' ] <EOL> return dfRet <EOL> def featVolumeDelta ( dData , lLookback = <NUM_LIT:30> , b_human = False ) : <EOL> '''<STR_LIT>''' <EOL> dfVolume = dData [ '<STR_LIT>' ] <EOL> dfRet = pand . rolling_mean ( dfVolume , lLookback ) <EOL> dfRet /= dfVolume <EOL> if b_human : <EOL> for sym in dData [ '<STR_LIT>' ] : <EOL> x = <NUM_LIT:1000> / dData [ '<STR_LIT>' ] [ sym ] [ <NUM_LIT:0> ] <EOL> dData [ '<STR_LIT>' ] [ sym ] = dData [ '<STR_LIT>' ] [ sym ] * x <EOL> return dData [ '<STR_LIT>' ] <EOL> return dfRet <EOL> def featAroon ( dData , bDown = False , lLookback = <NUM_LIT> , b_human = False ) : <EOL> '''<STR_LIT>''' <EOL> dfPrice = dData [ '<STR_LIT>' ] <EOL> dfRet = pand . DataFrame ( index = dfPrice . index , columns = dfPrice . columns , <EOL> data = np . zeros ( dfPrice . shape ) ) <EOL> for i in range ( dfPrice . shape [ <NUM_LIT:0> ] ) : <EOL> if ( ( i - lLookback ) < <NUM_LIT:0> ) : <EOL> dfRet . ix [ i , : ] = np . NAN <EOL> else : <EOL> if bDown : <EOL> dfRet . ix [ i , : ] = dfPrice . values [ i : ( i - lLookback ) : - <NUM_LIT:1> , : ] . argmin ( <EOL> axis = <NUM_LIT:0> ) <EOL> else : <EOL> dfRet . ix [ i , : ] = dfPrice . values [ i : ( i - lLookback ) : - <NUM_LIT:1> , : ] . argmax ( <EOL> axis = <NUM_LIT:0> ) <EOL> dfRet = ( ( lLookback - <NUM_LIT:1.> ) - dfRet ) / ( lLookback - <NUM_LIT:1.> ) * <NUM_LIT> <EOL> if b_human : <EOL> for sym in dData [ '<STR_LIT>' ] : <EOL> x = <NUM_LIT:1000> / dData [ '<STR_LIT>' ] [ sym ] [ <NUM_LIT:0> ] <EOL> dData [ '<STR_LIT>' ] [ sym ] = dData [ '<STR_LIT>' ] [ sym ] * x <EOL> return dData [ '<STR_LIT>' ] <EOL> return dfRet <EOL> def featAroonDown ( dData , lLookback = <NUM_LIT> , b_human = False ) : <EOL> '''<STR_LIT>''' <EOL> return featAroon ( dData , bDown = True , lLookback = lLookback , b_human = b_human ) <EOL> def featStochastic ( dData , lLookback = <NUM_LIT> , bFast = True , lMA = <NUM_LIT:3> , b_human = False ) : <EOL> '''<STR_LIT>''' <EOL> dfLow = dData [ '<STR_LIT>' ] <EOL> dfHigh = dData [ '<STR_LIT>' ] <EOL> dfPrice = dData [ '<STR_LIT>' ] <EOL> dfLows = pand . rolling_min ( dfLow , lLookback ) <EOL> dfHighs = pand . rolling_max ( dfHigh , lLookback ) <EOL> dfStoch = ( dfPrice - dfLows ) / ( dfHighs - dfLows ) <EOL> if not bFast : <EOL> dfStoch = pand . rolling_mean ( dfStoch , lMA ) <EOL> if b_human : <EOL> for sym in dData [ '<STR_LIT>' ] : <EOL> x = <NUM_LIT:1000> / dData [ '<STR_LIT>' ] [ sym ] [ <NUM_LIT:0> ] <EOL> dData [ '<STR_LIT>' ] [ sym ] = dData [ '<STR_LIT>' ] [ sym ] * x <EOL> return dData [ '<STR_LIT>' ] <EOL> return dfStoch <EOL> def featBeta ( dData , lLookback = <NUM_LIT> , sMarket = '<STR_LIT>' , b_human = False ) : <EOL> '''<STR_LIT>''' <EOL> dfPrice = dData [ '<STR_LIT>' ] <EOL> dfRets = dfPrice . copy ( ) <EOL> tsu . returnize1 ( dfRets . values ) <EOL> tsMarket = dfRets [ sMarket ] <EOL> dfRet = pand . rolling_cov ( tsMarket , dfRets , lLookback ) <EOL> dfRet /= dfRet [ sMarket ] <EOL> if b_human : <EOL> for sym in dData [ '<STR_LIT>' ] : <EOL> x = <NUM_LIT:1000> / dData [ '<STR_LIT>' ] [ sym ] [ <NUM_LIT:0> ] <EOL> dData [ '<STR_LIT>' ] [ sym ] = dData [ '<STR_LIT>' ] [ sym ] * x <EOL> return dData [ '<STR_LIT>' ] <EOL> return dfRet <EOL> def featBollinger ( dData , lLookback = <NUM_LIT:20> , b_human = False ) : <EOL> '''<STR_LIT>''' <EOL> if b_human : <EOL> dfPrice = dData [ '<STR_LIT>' ] <EOL> nstdsRet = pand . DataFrame ( index = dfPrice . index , columns = dfPrice . columns , data = np . zeros ( dfPrice . shape ) ) <EOL> pstdsRet = pand . DataFrame ( index = dfPrice . index , columns = dfPrice . columns , data = np . zeros ( dfPrice . shape ) ) <EOL> data3 = pand . DataFrame ( { "<STR_LIT>" : dfPrice [ dfPrice . columns [ <NUM_LIT:0> ] ] } ) <EOL> for sym in dfPrice . columns : <EOL> if sym != '<STR_LIT>' and sym != '<STR_LIT>' : <EOL> tsPrice = dfPrice [ sym ] <EOL> nstdRet = nstdsRet [ sym ] <EOL> pstdRet = pstdsRet [ sym ] <EOL> for i in range ( len ( tsPrice . index ) ) : <EOL> if i < lLookback - <NUM_LIT:1> : <EOL> nstdRet [ i ] = float ( '<STR_LIT>' ) <EOL> pstdRet [ i ] = float ( '<STR_LIT>' ) <EOL> continue <EOL> fAvg = np . average ( tsPrice [ i - ( lLookback - <NUM_LIT:1> ) : i + <NUM_LIT:1> ] ) <EOL> fStd = np . std ( tsPrice [ i - ( lLookback - <NUM_LIT:1> ) : i + <NUM_LIT:1> ] ) <EOL> pstdRet [ i ] = fAvg + <NUM_LIT> * fStd <EOL> nstdRet [ i ] = fAvg - <NUM_LIT> * fStd <EOL> data3 [ sym ] = dfPrice [ sym ] <EOL> data3 [ sym + "<STR_LIT>" ] = nstdsRet [ sym ] <EOL> data3 [ sym + "<STR_LIT>" ] = pstdsRet [ sym ] <EOL> del data3 [ '<STR_LIT>' ] <EOL> return data3 <EOL> else : <EOL> dfPrice = dData [ '<STR_LIT>' ] <EOL> dfRet = pand . DataFrame ( index = dfPrice . index , columns = dfPrice . columns , data = np . zeros ( dfPrice . shape ) ) <EOL> dfAvg = pand . rolling_mean ( dfPrice , lLookback ) <EOL> dfStd = pand . rolling_std ( dfPrice , lLookback ) <EOL> return ( dfPrice - dfAvg ) / ( <NUM_LIT> * dfStd ) <EOL> def featCorrelation ( dData , lLookback = <NUM_LIT:20> , sRel = '<STR_LIT>' , b_human = False ) : <EOL> '''<STR_LIT>''' <EOL> dfPrice = dData [ '<STR_LIT>' ] <EOL> if sRel not in dfPrice . columns : <EOL> raise KeyError ( "<STR_LIT>" % sRel ) <EOL> naRets = dfPrice . values . copy ( ) <EOL> tsu . returnize1 ( naRets ) <EOL> dfHistReturns = pand . DataFrame ( index = dfPrice . index , columns = dfPrice . columns , data = naRets ) <EOL> dfRet = pand . DataFrame ( index = dfPrice . index , columns = dfPrice . columns , data = np . zeros ( dfPrice . shape ) ) <EOL> for sStock in dfHistReturns . columns : <EOL> tsHistReturns = dfHistReturns [ sStock ] <EOL> tsRelativeReturns = dfHistReturns [ sRel ] <EOL> tsRet = dfRet [ sStock ] <EOL> for i in range ( len ( tsHistReturns . index ) ) : <EOL> if i < lLookback - <NUM_LIT:1> : <EOL> tsRet [ i ] = float ( '<STR_LIT>' ) <EOL> continue <EOL> naCorr = np . corrcoef ( tsHistReturns [ i - ( lLookback - <NUM_LIT:1> ) : i + <NUM_LIT:1> ] , tsRelativeReturns [ i - ( lLookback - <NUM_LIT:1> ) : i + <NUM_LIT:1> ] ) <EOL> tsRet [ i ] = naCorr [ <NUM_LIT:0> , <NUM_LIT:1> ] <EOL> if b_human : <EOL> for sym in dData [ '<STR_LIT>' ] : <EOL> x = <NUM_LIT:1000> / dData [ '<STR_LIT>' ] [ sym ] [ <NUM_LIT:0> ] <EOL> dData [ '<STR_LIT>' ] [ sym ] = dData [ '<STR_LIT>' ] [ sym ] * x <EOL> return dData [ '<STR_LIT>' ] <EOL> return dfRet <EOL> def featPrice ( dData , b_human = False ) : <EOL> '''<STR_LIT>''' <EOL> if b_human : <EOL> for sym in dData [ '<STR_LIT>' ] : <EOL> x = <NUM_LIT:1000> / dData [ '<STR_LIT>' ] [ sym ] [ <NUM_LIT:0> ] <EOL> dData [ '<STR_LIT>' ] [ sym ] = dData [ '<STR_LIT>' ] [ sym ] * x <EOL> return dData [ '<STR_LIT>' ] <EOL> return dData [ '<STR_LIT>' ] <EOL> def featVolume ( dData , b_human = False ) : <EOL> '''<STR_LIT>''' <EOL> if b_human : <EOL> for sym in dData [ '<STR_LIT>' ] : <EOL> x = <NUM_LIT:1000> / dData [ '<STR_LIT>' ] [ sym ] [ <NUM_LIT:0> ] <EOL> dData [ '<STR_LIT>' ] [ sym ] = dData [ '<STR_LIT>' ] [ sym ] * x <EOL> return dData [ '<STR_LIT>' ] <EOL> return dData [ '<STR_LIT>' ] <EOL> def featRand ( dData , b_human = False ) : <EOL> '''<STR_LIT>''' <EOL> dfPrice = dData [ '<STR_LIT>' ] <EOL> dfRet = pand . DataFrame ( index = dfPrice . index , columns = dfPrice . columns , <EOL> data = np . random . randn ( * dfPrice . shape ) ) <EOL> if b_human : <EOL> for sym in dData [ '<STR_LIT>' ] : <EOL> x = <NUM_LIT:1000> / dData [ '<STR_LIT>' ] [ sym ] [ <NUM_LIT:0> ] <EOL> dData [ '<STR_LIT>' ] [ sym ] = dData [ '<STR_LIT>' ] [ sym ] * x <EOL> return dData [ '<STR_LIT>' ] <EOL> return dfRet <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> pass </s>
