<s> """<STR_LIT>""" <EOL> from pyalgotrade . utils import dt <EOL> from pyalgotrade . utils import csvutils <EOL> from pyalgotrade . barfeed import membf <EOL> from pyalgotrade import bar <EOL> import datetime <EOL> import pytz <EOL> class RowParser ( object ) : <EOL> def parseBar ( self , csvRowDict ) : <EOL> raise NotImplementedError ( ) <EOL> def getFieldNames ( self ) : <EOL> raise NotImplementedError ( ) <EOL> def getDelimiter ( self ) : <EOL> raise NotImplementedError ( ) <EOL> class BarFilter ( object ) : <EOL> def includeBar ( self , bar_ ) : <EOL> raise NotImplementedError ( ) <EOL> class DateRangeFilter ( BarFilter ) : <EOL> def __init__ ( self , fromDate = None , toDate = None ) : <EOL> self . __fromDate = fromDate <EOL> self . __toDate = toDate <EOL> def includeBar ( self , bar_ ) : <EOL> if self . __toDate and bar_ . getDateTime ( ) > self . __toDate : <EOL> return False <EOL> if self . __fromDate and bar_ . getDateTime ( ) < self . __fromDate : <EOL> return False <EOL> return True <EOL> class USEquitiesRTH ( DateRangeFilter ) : <EOL> timezone = pytz . timezone ( "<STR_LIT>" ) <EOL> def __init__ ( self , fromDate = None , toDate = None ) : <EOL> super ( USEquitiesRTH , self ) . __init__ ( fromDate , toDate ) <EOL> self . __fromTime = datetime . time ( <NUM_LIT:9> , <NUM_LIT:30> , <NUM_LIT:0> ) <EOL> self . __toTime = datetime . time ( <NUM_LIT:16> , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> def includeBar ( self , bar_ ) : <EOL> ret = super ( USEquitiesRTH , self ) . includeBar ( bar_ ) <EOL> if ret : <EOL> barDay = bar_ . getDateTime ( ) . weekday ( ) <EOL> if barDay > <NUM_LIT:4> : <EOL> return False <EOL> barTime = dt . localize ( bar_ . getDateTime ( ) , USEquitiesRTH . timezone ) . time ( ) <EOL> if barTime < self . __fromTime : <EOL> return False <EOL> if barTime > self . __toTime : <EOL> return False <EOL> return ret <EOL> class BarFeed ( membf . BarFeed ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , frequency , maxLen = None ) : <EOL> super ( BarFeed , self ) . __init__ ( frequency , maxLen ) <EOL> self . __barFilter = None <EOL> self . __dailyTime = datetime . time ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> def getDailyBarTime ( self ) : <EOL> return self . __dailyTime <EOL> def setDailyBarTime ( self , time ) : <EOL> self . __dailyTime = time <EOL> def getBarFilter ( self ) : <EOL> return self . __barFilter <EOL> def setBarFilter ( self , barFilter ) : <EOL> self . __barFilter = barFilter <EOL> def addBarsFromCSV ( self , instrument , path , rowParser ) : <EOL> loadedBars = [ ] <EOL> reader = csvutils . FastDictReader ( open ( path , "<STR_LIT:r>" ) , fieldnames = rowParser . getFieldNames ( ) , delimiter = rowParser . getDelimiter ( ) ) <EOL> for row in reader : <EOL> bar_ = rowParser . parseBar ( row ) <EOL> if bar_ is not None and ( self . __barFilter is None or self . __barFilter . includeBar ( bar_ ) ) : <EOL> loadedBars . append ( bar_ ) <EOL> self . addBarsFromSequence ( instrument , loadedBars ) <EOL> class GenericRowParser ( RowParser ) : <EOL> def __init__ ( self , columnNames , dateTimeFormat , dailyBarTime , frequency , timezone ) : <EOL> self . __dateTimeFormat = dateTimeFormat <EOL> self . __dailyBarTime = dailyBarTime <EOL> self . __frequency = frequency <EOL> self . __timezone = timezone <EOL> self . __haveAdjClose = False <EOL> self . __dateTimeColName = columnNames [ "<STR_LIT>" ] <EOL> self . __openColName = columnNames [ "<STR_LIT>" ] <EOL> self . __highColName = columnNames [ "<STR_LIT>" ] <EOL> self . __lowColName = columnNames [ "<STR_LIT>" ] <EOL> self . __closeColName = columnNames [ "<STR_LIT>" ] <EOL> self . __volumeColName = columnNames [ "<STR_LIT>" ] <EOL> self . __adjCloseColName = columnNames [ "<STR_LIT>" ] <EOL> self . __columnNames = columnNames <EOL> def _parseDate ( self , dateString ) : <EOL> ret = datetime . datetime . strptime ( dateString , self . __dateTimeFormat ) <EOL> if self . __dailyBarTime is not None : <EOL> ret = datetime . datetime . combine ( ret , self . __dailyBarTime ) <EOL> if self . __timezone : <EOL> ret = dt . localize ( ret , self . __timezone ) <EOL> return ret <EOL> def barsHaveAdjClose ( self ) : <EOL> return self . __haveAdjClose <EOL> def getFieldNames ( self ) : <EOL> return None <EOL> def getDelimiter ( self ) : <EOL> return "<STR_LIT:U+002C>" <EOL> def parseBar ( self , csvRowDict ) : <EOL> dateTime = self . _parseDate ( csvRowDict [ self . __dateTimeColName ] ) <EOL> open_ = float ( csvRowDict [ self . __openColName ] ) <EOL> high = float ( csvRowDict [ self . __highColName ] ) <EOL> low = float ( csvRowDict [ self . __lowColName ] ) <EOL> close = float ( csvRowDict [ self . __closeColName ] ) <EOL> volume = float ( csvRowDict [ self . __volumeColName ] ) <EOL> adjClose = None <EOL> if self . __adjCloseColName is not None : <EOL> adjCloseValue = csvRowDict . get ( self . __adjCloseColName , "<STR_LIT>" ) <EOL> if len ( adjCloseValue ) > <NUM_LIT:0> : <EOL> adjClose = float ( adjCloseValue ) <EOL> self . __haveAdjClose = True <EOL> extra = { } <EOL> for k , v in csvRowDict . iteritems ( ) : <EOL> if k not in self . __columnNames : <EOL> extra [ k ] = csvutils . float_or_string ( v ) <EOL> return bar . BasicBar ( <EOL> dateTime , open_ , high , low , close , volume , adjClose , self . __frequency , extra = extra <EOL> ) <EOL> class GenericBarFeed ( BarFeed ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , frequency , timezone = None , maxLen = None ) : <EOL> super ( GenericBarFeed , self ) . __init__ ( frequency , maxLen ) <EOL> self . __timezone = timezone <EOL> self . __haveAdjClose = False <EOL> self . __dateTimeFormat = "<STR_LIT>" <EOL> self . __columnNames = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } <EOL> self . setDailyBarTime ( None ) <EOL> def barsHaveAdjClose ( self ) : <EOL> return self . __haveAdjClose <EOL> def setNoAdjClose ( self ) : <EOL> self . __columnNames [ "<STR_LIT>" ] = None <EOL> self . __haveAdjClose = False <EOL> def setColumnName ( self , col , name ) : <EOL> self . __columnNames [ col ] = name <EOL> def setDateTimeFormat ( self , dateTimeFormat ) : <EOL> self . __dateTimeFormat = dateTimeFormat <EOL> def addBarsFromCSV ( self , instrument , path , timezone = None ) : <EOL> """<STR_LIT>""" <EOL> if timezone is None : <EOL> timezone = self . __timezone <EOL> rowParser = GenericRowParser ( self . __columnNames , self . __dateTimeFormat , self . getDailyBarTime ( ) , self . getFrequency ( ) , timezone ) <EOL> super ( GenericBarFeed , self ) . addBarsFromCSV ( instrument , path , rowParser ) <EOL> if rowParser . barsHaveAdjClose ( ) : <EOL> self . __haveAdjClose = True <EOL> elif self . __haveAdjClose : <EOL> raise Exception ( "<STR_LIT>" ) </s>
