<s> """<STR_LIT>""" <EOL> import numpy as np <EOL> import matplotlib . pyplot as plt <EOL> from pyalgotrade . technical import roc <EOL> from pyalgotrade import dispatcher <EOL> class Results ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , eventsDict , lookBack , lookForward ) : <EOL> assert ( lookBack > <NUM_LIT:0> ) <EOL> assert ( lookForward > <NUM_LIT:0> ) <EOL> self . __lookBack = lookBack <EOL> self . __lookForward = lookForward <EOL> self . __values = [ [ ] for i in xrange ( lookBack + lookForward + <NUM_LIT:1> ) ] <EOL> self . __eventCount = <NUM_LIT:0> <EOL> for instrument , events in eventsDict . items ( ) : <EOL> for event in events : <EOL> if event . isComplete ( ) : <EOL> self . __eventCount += <NUM_LIT:1> <EOL> values = np . cumprod ( event . getValues ( ) + <NUM_LIT:1> ) <EOL> values = values / values [ event . getLookBack ( ) ] <EOL> for t in range ( event . getLookBack ( ) * - <NUM_LIT:1> , event . getLookForward ( ) + <NUM_LIT:1> ) : <EOL> self . setValue ( t , values [ t + event . getLookBack ( ) ] ) <EOL> def __mapPos ( self , t ) : <EOL> assert ( t >= - <NUM_LIT:1> * self . __lookBack and t <= self . __lookForward ) <EOL> return t + self . __lookBack <EOL> def setValue ( self , t , value ) : <EOL> if value is None : <EOL> raise Exception ( "<STR_LIT>" % ( t ) ) <EOL> pos = self . __mapPos ( t ) <EOL> self . __values [ pos ] . append ( value ) <EOL> def getValues ( self , t ) : <EOL> pos = self . __mapPos ( t ) <EOL> return self . __values [ pos ] <EOL> def getLookBack ( self ) : <EOL> return self . __lookBack <EOL> def getLookForward ( self ) : <EOL> return self . __lookForward <EOL> def getEventCount ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __eventCount <EOL> class Predicate ( object ) : <EOL> """<STR_LIT>""" <EOL> def eventOccurred ( self , instrument , bards ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> class Event ( object ) : <EOL> def __init__ ( self , lookBack , lookForward ) : <EOL> assert ( lookBack > <NUM_LIT:0> ) <EOL> assert ( lookForward > <NUM_LIT:0> ) <EOL> self . __lookBack = lookBack <EOL> self . __lookForward = lookForward <EOL> self . __values = np . empty ( ( lookBack + lookForward + <NUM_LIT:1> ) ) <EOL> self . __values [ : ] = np . NAN <EOL> def __mapPos ( self , t ) : <EOL> assert ( t >= - <NUM_LIT:1> * self . __lookBack and t <= self . __lookForward ) <EOL> return t + self . __lookBack <EOL> def isComplete ( self ) : <EOL> return not any ( np . isnan ( self . __values ) ) <EOL> def getLookBack ( self ) : <EOL> return self . __lookBack <EOL> def getLookForward ( self ) : <EOL> return self . __lookForward <EOL> def setValue ( self , t , value ) : <EOL> if value is not None : <EOL> pos = self . __mapPos ( t ) <EOL> self . __values [ pos ] = value <EOL> def getValue ( self , t ) : <EOL> pos = self . __mapPos ( t ) <EOL> return self . __values [ pos ] <EOL> def getValues ( self ) : <EOL> return self . __values <EOL> class Profiler ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , predicate , lookBack , lookForward ) : <EOL> assert ( lookBack > <NUM_LIT:0> ) <EOL> assert ( lookForward > <NUM_LIT:0> ) <EOL> self . __predicate = predicate <EOL> self . __lookBack = lookBack <EOL> self . __lookForward = lookForward <EOL> self . __feed = None <EOL> self . __rets = { } <EOL> self . __futureRets = { } <EOL> self . __events = { } <EOL> def __addPastReturns ( self , instrument , event ) : <EOL> begin = ( event . getLookBack ( ) + <NUM_LIT:1> ) * - <NUM_LIT:1> <EOL> for t in xrange ( begin , <NUM_LIT:0> ) : <EOL> try : <EOL> ret = self . __rets [ instrument ] [ t ] <EOL> if ret is not None : <EOL> event . setValue ( t + <NUM_LIT:1> , ret ) <EOL> except IndexError : <EOL> pass <EOL> def __addCurrentReturns ( self , instrument ) : <EOL> nextTs = [ ] <EOL> for event , t in self . __futureRets [ instrument ] : <EOL> event . setValue ( t , self . __rets [ instrument ] [ - <NUM_LIT:1> ] ) <EOL> if t < event . getLookForward ( ) : <EOL> t += <NUM_LIT:1> <EOL> nextTs . append ( ( event , t ) ) <EOL> self . __futureRets [ instrument ] = nextTs <EOL> def __onBars ( self , dateTime , bars ) : <EOL> for instrument in bars . getInstruments ( ) : <EOL> self . __addCurrentReturns ( instrument ) <EOL> eventOccurred = self . __predicate . eventOccurred ( instrument , self . __feed [ instrument ] ) <EOL> if eventOccurred : <EOL> event = Event ( self . __lookBack , self . __lookForward ) <EOL> self . __events [ instrument ] . append ( event ) <EOL> self . __addPastReturns ( instrument , event ) <EOL> self . __futureRets [ instrument ] . append ( ( event , <NUM_LIT:1> ) ) <EOL> def getResults ( self ) : <EOL> """<STR_LIT>""" <EOL> return Results ( self . __events , self . __lookBack , self . __lookForward ) <EOL> def run ( self , feed , useAdjustedCloseForReturns = True ) : <EOL> """<STR_LIT>""" <EOL> if useAdjustedCloseForReturns : <EOL> assert feed . barsHaveAdjClose ( ) , "<STR_LIT>" <EOL> try : <EOL> self . __feed = feed <EOL> self . __rets = { } <EOL> self . __futureRets = { } <EOL> for instrument in feed . getRegisteredInstruments ( ) : <EOL> self . __events . setdefault ( instrument , [ ] ) <EOL> self . __futureRets [ instrument ] = [ ] <EOL> if useAdjustedCloseForReturns : <EOL> ds = feed [ instrument ] . getAdjCloseDataSeries ( ) <EOL> else : <EOL> ds = feed [ instrument ] . getCloseDataSeries ( ) <EOL> self . __rets [ instrument ] = roc . RateOfChange ( ds , <NUM_LIT:1> ) <EOL> feed . getNewValuesEvent ( ) . subscribe ( self . __onBars ) <EOL> disp = dispatcher . Dispatcher ( ) <EOL> disp . addSubject ( feed ) <EOL> disp . run ( ) <EOL> finally : <EOL> feed . getNewValuesEvent ( ) . unsubscribe ( self . __onBars ) <EOL> def build_plot ( profilerResults ) : <EOL> x = [ ] <EOL> y = [ ] <EOL> std = [ ] <EOL> for t in xrange ( profilerResults . getLookBack ( ) * - <NUM_LIT:1> , profilerResults . getLookForward ( ) + <NUM_LIT:1> ) : <EOL> x . append ( t ) <EOL> values = np . asarray ( profilerResults . getValues ( t ) ) <EOL> y . append ( values . mean ( ) ) <EOL> std . append ( values . std ( ) ) <EOL> plt . clf ( ) <EOL> plt . plot ( x , y , color = '<STR_LIT>' ) <EOL> eventT = profilerResults . getLookBack ( ) <EOL> plt . errorbar ( x [ eventT + <NUM_LIT:1> : ] , y [ eventT + <NUM_LIT:1> : ] , std [ eventT + <NUM_LIT:1> : ] , alpha = <NUM_LIT:0> , ecolor = '<STR_LIT>' ) <EOL> plt . axhline ( y = y [ eventT ] , xmin = - <NUM_LIT:1> * profilerResults . getLookBack ( ) , xmax = profilerResults . getLookForward ( ) , color = '<STR_LIT>' ) <EOL> plt . xlim ( profilerResults . getLookBack ( ) * - <NUM_LIT:1> - <NUM_LIT:0.5> , profilerResults . getLookForward ( ) + <NUM_LIT:0.5> ) <EOL> plt . xlabel ( '<STR_LIT>' ) <EOL> plt . ylabel ( '<STR_LIT>' ) <EOL> def plot ( profilerResults ) : <EOL> """<STR_LIT>""" <EOL> build_plot ( profilerResults ) <EOL> plt . show ( ) </s>
