<s> """<STR_LIT>""" <EOL> """<STR_LIT>""" <EOL> from ib . ext . Contract import Contract <EOL> from ib . ext . Order import Order <EOL> from ib . opt import Connection <EOL> def error_handler ( msg ) : <EOL> print "<STR_LIT>" , msg <EOL> def server_handler ( msg ) : <EOL> print "<STR_LIT>" , msg . typeName , "<STR_LIT:->" , msg <EOL> def create_contract ( symbol , sec_type , exch , prim_exch , curr ) : <EOL> contract = Contract ( ) <EOL> contract . m_symbol = symbol <EOL> contract . m_secType = sec_type <EOL> contract . m_exchange = exch <EOL> contract . m_primaryExch = prim_exch <EOL> contract . m_currency = curr <EOL> return contract <EOL> def create_order ( order_type , quantity , action ) : <EOL> order = Order ( ) <EOL> order . m_orderType = order_type <EOL> order . m_totalQuantity = quantity <EOL> order . m_action = action <EOL> return order <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> client_id = <NUM_LIT:1> <EOL> order_id = <NUM_LIT> <EOL> port = <NUM_LIT> <EOL> tws_conn = None <EOL> try : <EOL> tws_conn = Connection . create ( port = port , <EOL> clientId = client_id ) <EOL> tws_conn . connect ( ) <EOL> tws_conn . register ( error_handler , '<STR_LIT>' ) <EOL> tws_conn . registerAll ( server_handler ) <EOL> aapl_contract = create_contract ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> aapl_order = create_order ( '<STR_LIT>' , <NUM_LIT:100> , '<STR_LIT>' ) <EOL> tws_conn . placeOrder ( order_id , aapl_contract , aapl_order ) <EOL> finally : <EOL> if tws_conn is not None : <EOL> tws_conn . disconnect ( ) </s>
