<s> from pyalgotrade import barfeed <EOL> from pyalgotrade . dataseries import resampled <EOL> from pyalgotrade import resamplebase <EOL> from pyalgotrade import bar <EOL> class BarsGrouper ( resamplebase . Grouper ) : <EOL> def __init__ ( self , groupDateTime , bars , frequency ) : <EOL> resamplebase . Grouper . __init__ ( self , groupDateTime ) <EOL> self . __barGroupers = { } <EOL> self . __frequency = frequency <EOL> for instrument , bar_ in bars . items ( ) : <EOL> barGrouper = resampled . BarGrouper ( groupDateTime , bar_ , frequency ) <EOL> self . __barGroupers [ instrument ] = barGrouper <EOL> def addValue ( self , value ) : <EOL> for instrument , bar_ in value . items ( ) : <EOL> barGrouper = self . __barGroupers . get ( instrument ) <EOL> if barGrouper : <EOL> barGrouper . addValue ( bar_ ) <EOL> else : <EOL> barGrouper = resampled . BarGrouper ( self . getDateTime ( ) , bar_ , self . __frequency ) <EOL> self . __barGroupers [ instrument ] = barGrouper <EOL> def getGrouped ( self ) : <EOL> bar_dict = { } <EOL> for instrument , grouper in self . __barGroupers . items ( ) : <EOL> bar_dict [ instrument ] = grouper . getGrouped ( ) <EOL> return bar . Bars ( bar_dict ) <EOL> class ResampledBarFeed ( barfeed . BaseBarFeed ) : <EOL> def __init__ ( self , barFeed , frequency , maxLen = None ) : <EOL> super ( ResampledBarFeed , self ) . __init__ ( frequency , maxLen ) <EOL> if not isinstance ( barFeed , barfeed . BaseBarFeed ) : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> if not resamplebase . is_valid_frequency ( frequency ) : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> for instrument in barFeed . getRegisteredInstruments ( ) : <EOL> self . registerInstrument ( instrument ) <EOL> self . __values = [ ] <EOL> self . __barFeed = barFeed <EOL> self . __grouper = None <EOL> self . __range = None <EOL> barFeed . getNewValuesEvent ( ) . subscribe ( self . __onNewValues ) <EOL> def __onNewValues ( self , dateTime , value ) : <EOL> if self . __range is None : <EOL> self . __range = resamplebase . build_range ( dateTime , self . getFrequency ( ) ) <EOL> self . __grouper = BarsGrouper ( self . __range . getBeginning ( ) , value , self . getFrequency ( ) ) <EOL> elif self . __range . belongs ( dateTime ) : <EOL> self . __grouper . addValue ( value ) <EOL> else : <EOL> self . __values . append ( self . __grouper . getGrouped ( ) ) <EOL> self . __range = resamplebase . build_range ( dateTime , self . getFrequency ( ) ) <EOL> self . __grouper = BarsGrouper ( self . __range . getBeginning ( ) , value , self . getFrequency ( ) ) <EOL> def getCurrentDateTime ( self ) : <EOL> return self . __barFeed . getCurrentDateTime ( ) <EOL> def barsHaveAdjClose ( self ) : <EOL> return self . __barFeed . barsHaveAdjClose ( ) <EOL> def getNextBars ( self ) : <EOL> ret = None <EOL> if len ( self . __values ) : <EOL> ret = self . __values . pop ( <NUM_LIT:0> ) <EOL> return ret <EOL> def eof ( self ) : <EOL> return len ( self . __values ) == <NUM_LIT:0> <EOL> def join ( self ) : <EOL> pass <EOL> def peekDateTime ( self ) : <EOL> return None <EOL> def start ( self ) : <EOL> super ( ResampledBarFeed , self ) . start ( ) <EOL> def stop ( self ) : <EOL> pass <EOL> def checkNow ( self , dateTime ) : <EOL> if self . __range is not None and not self . __range . belongs ( dateTime ) : <EOL> self . __values . append ( self . __grouper . getGrouped ( ) ) <EOL> self . __grouper = None <EOL> self . __range = None </s>
