<s> """<STR_LIT>""" <EOL> import os <EOL> from pyalgotrade import dispatcher <EOL> from pyalgotrade . dataseries import resampled <EOL> datetime_format = "<STR_LIT>" <EOL> class CSVFileWriter ( object ) : <EOL> def __init__ ( self , csvFile ) : <EOL> self . __file = open ( csvFile , "<STR_LIT:w>" ) <EOL> self . __writeLine ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> def __writeLine ( self , * values ) : <EOL> line = "<STR_LIT:U+002C>" . join ( [ str ( value ) for value in values ] ) <EOL> self . __file . write ( line ) <EOL> self . __file . write ( os . linesep ) <EOL> def writeBar ( self , bar_ ) : <EOL> adjClose = bar_ . getAdjClose ( ) <EOL> if adjClose is None : <EOL> adjClose = "<STR_LIT>" <EOL> dateTime = bar_ . getDateTime ( ) . strftime ( datetime_format ) <EOL> self . __writeLine ( <EOL> dateTime , <EOL> bar_ . getOpen ( ) , <EOL> bar_ . getHigh ( ) , <EOL> bar_ . getLow ( ) , <EOL> bar_ . getClose ( ) , <EOL> bar_ . getVolume ( ) , <EOL> adjClose <EOL> ) <EOL> def close ( self ) : <EOL> self . __file . close ( ) <EOL> def resample_impl ( barFeed , frequency , csvFile ) : <EOL> instruments = barFeed . getRegisteredInstruments ( ) <EOL> if len ( instruments ) != <NUM_LIT:1> : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> csvWriter = CSVFileWriter ( csvFile ) <EOL> def on_bar ( ds , dateTime , value ) : <EOL> csvWriter . writeBar ( value ) <EOL> insrumentDS = barFeed [ instruments [ <NUM_LIT:0> ] ] <EOL> resampledDS = resampled . ResampledBarDataSeries ( insrumentDS , frequency ) <EOL> resampledDS . getNewValueEvent ( ) . subscribe ( on_bar ) <EOL> disp = dispatcher . Dispatcher ( ) <EOL> disp . addSubject ( barFeed ) <EOL> disp . run ( ) <EOL> resampledDS . pushLast ( ) <EOL> def resample_to_csv ( barFeed , frequency , csvFile ) : <EOL> """<STR_LIT>""" <EOL> assert frequency > <NUM_LIT:0> , "<STR_LIT>" <EOL> resample_impl ( barFeed , frequency , csvFile ) </s>
