<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 , message <EOL> import time <EOL> import pandas as pd <EOL> import datetime as dt <EOL> class AlgoSystem : <EOL> def __init__ ( self , symbol , qty , resample_interval , <EOL> averaging_period = <NUM_LIT:5> , port = <NUM_LIT> ) : <EOL> self . client_id = <NUM_LIT:1> <EOL> self . order_id = <NUM_LIT:1> <EOL> self . qty = qty <EOL> self . symbol_id , self . symbol = <NUM_LIT:0> , symbol <EOL> self . resample_interval = resample_interval <EOL> self . averaging_period = averaging_period <EOL> self . port = port <EOL> self . tws_conn = None <EOL> self . bid_price , self . ask_price = <NUM_LIT:0> , <NUM_LIT:0> <EOL> self . last_prices = pd . DataFrame ( columns = [ self . symbol_id ] ) <EOL> self . average_price = <NUM_LIT:0> <EOL> self . is_position_opened = False <EOL> self . account_code = None <EOL> self . unrealized_pnl , self . realized_pnl = <NUM_LIT:0> , <NUM_LIT:0> <EOL> self . position = <NUM_LIT:0> <EOL> def error_handler ( self , msg ) : <EOL> if msg . typeName == "<STR_LIT:error>" and msg . id != - <NUM_LIT:1> : <EOL> print "<STR_LIT>" , msg <EOL> def server_handler ( self , msg ) : <EOL> if msg . typeName == "<STR_LIT>" : <EOL> self . order_id = msg . orderId <EOL> elif msg . typeName == "<STR_LIT>" : <EOL> self . account_code = msg . accountsList <EOL> elif msg . typeName == "<STR_LIT>" and msg . contract . m_symbol == self . symbol : <EOL> self . unrealized_pnl = msg . unrealizedPNL <EOL> self . realized_pnl = msg . realizedPNL <EOL> self . position = msg . position <EOL> elif msg . typeName == "<STR_LIT:error>" and msg . id != - <NUM_LIT:1> : <EOL> return <EOL> def tick_event ( self , msg ) : <EOL> if msg . field == <NUM_LIT:1> : <EOL> self . bid_price = msg . price <EOL> elif msg . field == <NUM_LIT:2> : <EOL> self . ask_price = msg . price <EOL> elif msg . field == <NUM_LIT:4> : <EOL> self . last_prices . loc [ dt . datetime . now ( ) ] = msg . price <EOL> resampled_prices = self . last_prices . resample ( self . resample_interval , <EOL> how = '<STR_LIT>' , <EOL> fill_method = "<STR_LIT>" ) <EOL> self . average_price = resampled_prices . tail ( <EOL> self . averaging_period ) . mean ( ) [ <NUM_LIT:0> ] <EOL> self . perform_trade_logic ( ) <EOL> def perform_trade_logic ( self ) : <EOL> is_buy_signal = self . ask_price < self . average_price <EOL> is_sell_signal = self . bid_price > self . average_price <EOL> print dt . datetime . now ( ) , "<STR_LIT>" , is_buy_signal , "<STR_LIT:/>" , is_sell_signal , "<STR_LIT>" , self . average_price <EOL> if self . average_price != <NUM_LIT:0> and self . bid_price != <NUM_LIT:0> and self . ask_price != <NUM_LIT:0> and self . position == <NUM_LIT:0> and not self . is_position_opened : <EOL> if is_sell_signal : <EOL> self . place_market_order ( self . symbol , self . qty , False ) <EOL> self . is_position_opened = True <EOL> elif is_buy_signal : <EOL> self . place_market_order ( self . symbol , self . qty , True ) <EOL> self . is_position_opened = True <EOL> elif self . is_position_opened : <EOL> if self . position > <NUM_LIT:0> and is_sell_signal : <EOL> self . place_market_order ( self . symbol , self . qty , False ) <EOL> self . is_position_opened = False <EOL> elif self . position < <NUM_LIT:0> and is_buy_signal : <EOL> self . place_market_order ( self . symbol , self . qty , True ) <EOL> self . is_position_opened = False <EOL> self . monitor_position ( ) <EOL> def monitor_position ( self ) : <EOL> print '<STR_LIT>' % ( self . position , <EOL> self . unrealized_pnl , <EOL> self . realized_pnl ) <EOL> def place_market_order ( self , symbol , qty , is_buy ) : <EOL> contract = self . create_contract ( symbol , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> buysell = '<STR_LIT>' if is_buy else '<STR_LIT>' <EOL> order = self . create_order ( '<STR_LIT>' , qty , buysell ) <EOL> self . tws_conn . placeOrder ( self . order_id , contract , order ) <EOL> self . order_id += <NUM_LIT:1> <EOL> print "<STR_LIT>" , qty , "<STR_LIT>" , symbol , "<STR_LIT:to>" , buysell <EOL> def create_contract ( self , 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 ( self , 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> def request_market_data ( self , symbol_id , symbol ) : <EOL> contract = self . create_contract ( symbol , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> self . tws_conn . reqMktData ( symbol_id , contract , '<STR_LIT>' , False ) <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> def cancel_market_data ( self , symbol ) : <EOL> self . tws_conn . cancelMktData ( symbol ) <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> def request_account_updates ( self , account_code ) : <EOL> self . tws_conn . reqAccountUpdates ( True , account_code ) <EOL> def connect_to_tws ( self ) : <EOL> self . tws_conn = Connection . create ( port = self . port , <EOL> clientId = self . client_id ) <EOL> self . tws_conn . connect ( ) <EOL> def disconnect_from_tws ( self ) : <EOL> if self . tws_conn is not None : <EOL> self . tws_conn . disconnect ( ) <EOL> def register_callback_functions ( self ) : <EOL> self . tws_conn . registerAll ( self . server_handler ) <EOL> self . tws_conn . register ( self . error_handler , '<STR_LIT>' ) <EOL> self . tws_conn . register ( self . tick_event , <EOL> message . tickPrice , <EOL> message . tickSize ) <EOL> def start ( self ) : <EOL> try : <EOL> self . connect_to_tws ( ) <EOL> self . register_callback_functions ( ) <EOL> self . request_market_data ( self . symbol_id , self . symbol ) <EOL> self . request_account_updates ( self . account_code ) <EOL> while True : <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> except Exception , e : <EOL> print "<STR_LIT>" , e <EOL> self . cancel_market_data ( self . symbol ) <EOL> finally : <EOL> print "<STR_LIT>" <EOL> self . disconnect_from_tws ( ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> system = AlgoSystem ( "<STR_LIT>" , <NUM_LIT:100> , "<STR_LIT>" , <NUM_LIT:5> ) <EOL> system . start ( ) </s>
