<s> """<STR_LIT>""" <EOL> from pyalgotrade import dataseries <EOL> from pyalgotrade . dataseries import bards <EOL> class Line ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , low , high , dateTime , white ) : <EOL> self . __low = low <EOL> self . __high = high <EOL> self . __dateTime = dateTime <EOL> self . __white = white <EOL> def getDateTime ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __dateTime <EOL> def getLow ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __low <EOL> def getHigh ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __high <EOL> def isWhite ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __white <EOL> def isBlack ( self ) : <EOL> """<STR_LIT>""" <EOL> return not self . __white <EOL> class LineBreak ( dataseries . SequenceDataSeries ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , barDataSeries , reversalLines , useAdjustedValues = False , maxLen = None ) : <EOL> if not isinstance ( barDataSeries , bards . BarDataSeries ) : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> if reversalLines < <NUM_LIT:2> : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> if dataseries . get_checked_max_len ( maxLen ) < reversalLines : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> super ( LineBreak , self ) . __init__ ( maxLen ) <EOL> self . __reversalLines = reversalLines <EOL> self . __useAdjustedValues = useAdjustedValues <EOL> barDataSeries . getNewValueEvent ( ) . subscribe ( self . __onNewBar ) <EOL> def __onNewBar ( self , dataSeries , dateTime , value ) : <EOL> line = self . __getNextLine ( value ) <EOL> if line is not None : <EOL> self . appendWithDateTime ( dateTime , line ) <EOL> def __isReversal ( self , value , breakUp ) : <EOL> assert ( len ( self ) ) <EOL> lines = self [ self . __reversalLines * - <NUM_LIT:1> : ] <EOL> if breakUp : <EOL> breakPoint = max ( [ line . getHigh ( ) for line in lines ] ) <EOL> ret = value > breakPoint <EOL> else : <EOL> breakPoint = min ( [ line . getLow ( ) for line in lines ] ) <EOL> ret = value < breakPoint <EOL> return ret <EOL> def __getNextLine ( self , bar ) : <EOL> ret = None <EOL> if len ( self ) > <NUM_LIT:0> : <EOL> lastLine = self [ - <NUM_LIT:1> ] <EOL> close = bar . getClose ( self . __useAdjustedValues ) <EOL> if lastLine . isWhite ( ) : <EOL> if close > lastLine . getHigh ( ) : <EOL> ret = Line ( lastLine . getHigh ( ) , close , bar . getDateTime ( ) , True ) <EOL> elif self . __isReversal ( close , False ) : <EOL> ret = Line ( close , lastLine . getLow ( ) , bar . getDateTime ( ) , False ) <EOL> else : <EOL> if close < lastLine . getLow ( ) : <EOL> ret = Line ( close , lastLine . getLow ( ) , bar . getDateTime ( ) , False ) <EOL> elif self . __isReversal ( close , True ) : <EOL> ret = Line ( lastLine . getHigh ( ) , close , bar . getDateTime ( ) , True ) <EOL> else : <EOL> white = False <EOL> if bar . getClose ( self . __useAdjustedValues ) >= bar . getOpen ( self . __useAdjustedValues ) : <EOL> white = True <EOL> ret = Line ( bar . getLow ( self . __useAdjustedValues ) , bar . getHigh ( self . __useAdjustedValues ) , bar . getDateTime ( ) , white ) <EOL> return ret <EOL> def setMaxLen ( self , maxLen ) : <EOL> if maxLen < self . __reversalLines : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> super ( LineBreak , self ) . setMaxLen ( maxLen ) </s>
