<s> '''<STR_LIT>''' <EOL> import argparse <EOL> import datetime as dt <EOL> import time <EOL> import os <EOL> import sys <EOL> from ib . ext . Contract import Contract <EOL> from ib . opt import ibConnection , message <EOL> priceTicks = { <NUM_LIT:1> : '<STR_LIT>' , <NUM_LIT:2> : '<STR_LIT>' , <NUM_LIT:4> : '<STR_LIT>' , <NUM_LIT:6> : '<STR_LIT>' , <NUM_LIT:7> : '<STR_LIT>' , <NUM_LIT:9> : '<STR_LIT>' , <NUM_LIT> : '<STR_LIT>' } <EOL> sizeTicks = { <NUM_LIT:0> : '<STR_LIT>' , <NUM_LIT:3> : '<STR_LIT>' , <NUM_LIT:5> : '<STR_LIT>' , <NUM_LIT:8> : '<STR_LIT>' } <EOL> class TickLogger ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , tws , subscriptions ) : <EOL> '''<STR_LIT>''' <EOL> tws . register ( self . _priceHandler , message . TickPrice ) <EOL> tws . register ( self . _sizeHandler , message . TickSize ) <EOL> self . subscriptions = subscriptions <EOL> self . _startTime = time . time ( ) <EOL> if not os . path . exists ( '<STR_LIT>' ) : os . mkdir ( '<STR_LIT>' ) <EOL> fileName = '<STR_LIT>' % dt . datetime . now ( ) . strftime ( '<STR_LIT>' ) <EOL> print '<STR_LIT>' , fileName <EOL> self . dataFile = open ( fileName , '<STR_LIT:w>' ) <EOL> def _priceHandler ( self , msg ) : <EOL> '''<STR_LIT>''' <EOL> data = [ self . subscriptions [ msg . tickerId ] . m_symbol , '<STR_LIT>' , priceTicks [ msg . field ] , msg . price ] <EOL> self . _writeData ( data ) <EOL> def _sizeHandler ( self , msg ) : <EOL> '''<STR_LIT>''' <EOL> data = [ self . subscriptions [ msg . tickerId ] . m_symbol , '<STR_LIT:size>' , sizeTicks [ msg . field ] , msg . size ] <EOL> self . _writeData ( data ) <EOL> def _writeData ( self , data ) : <EOL> '''<STR_LIT>''' <EOL> timestamp = '<STR_LIT>' % ( time . time ( ) - self . _startTime ) <EOL> dataLine = '<STR_LIT:U+002C>' . join ( str ( bit ) for bit in [ timestamp ] + data ) + '<STR_LIT:\n>' <EOL> self . dataFile . write ( dataLine ) <EOL> def flush ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . dataFile . flush ( ) <EOL> def close ( self ) : <EOL> '''<STR_LIT>''' <EOL> print '<STR_LIT>' <EOL> self . dataFile . close ( ) <EOL> def printMessage ( msg ) : <EOL> '''<STR_LIT>''' <EOL> print '<STR_LIT>' , msg <EOL> def createContract ( symbol ) : <EOL> '''<STR_LIT>''' <EOL> c = Contract ( ) <EOL> c . m_symbol = symbol <EOL> c . m_secType = "<STR_LIT>" <EOL> c . m_exchange = "<STR_LIT>" <EOL> c . m_currency = "<STR_LIT>" <EOL> return c <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> parser = argparse . ArgumentParser ( description = '<STR_LIT>' ) <EOL> parser . add_argument ( "<STR_LIT>" , help = '<STR_LIT>' ) <EOL> parser . add_argument ( "<STR_LIT>" , "<STR_LIT>" , help = "<STR_LIT>" , <EOL> action = "<STR_LIT:store_true>" ) <EOL> args = parser . parse_args ( ) <EOL> symbols = args . symbols . strip ( ) . split ( '<STR_LIT:U+002C>' ) <EOL> print '<STR_LIT>' , symbols <EOL> subscriptions = { } <EOL> for idx , symbol in enumerate ( symbols ) : <EOL> subscriptions [ idx + <NUM_LIT:1> ] = createContract ( symbol ) <EOL> tws = ibConnection ( ) <EOL> logger = TickLogger ( tws , subscriptions ) <EOL> if args . verbose : <EOL> print '<STR_LIT>' <EOL> tws . registerAll ( printMessage ) <EOL> tws . connect ( ) <EOL> for subId , c in subscriptions . iteritems ( ) : <EOL> tws . reqMktData ( subId , c , "<STR_LIT>" , False ) <EOL> print '<STR_LIT>' <EOL> try : <EOL> while True : <EOL> time . sleep ( <NUM_LIT:2> ) <EOL> logger . flush ( ) <EOL> sys . stdout . write ( '<STR_LIT:.>' ) <EOL> except KeyboardInterrupt : <EOL> print '<STR_LIT>' <EOL> logger . close ( ) <EOL> tws . disconnect ( ) <EOL> print '<STR_LIT>' </s>
