<s> from qstrader . order . order import SuggestedOrder <EOL> from qstrader . portfolio . portfolio import Portfolio <EOL> class PortfolioHandler ( object ) : <EOL> def __init__ ( <EOL> self , initial_cash , events_queue , <EOL> price_handler , position_sizer , risk_manager <EOL> ) : <EOL> """<STR_LIT>""" <EOL> self . initial_cash = initial_cash <EOL> self . events_queue = events_queue <EOL> self . price_handler = price_handler <EOL> self . position_sizer = position_sizer <EOL> self . risk_manager = risk_manager <EOL> self . portfolio = Portfolio ( price_handler , initial_cash ) <EOL> def _create_order_from_signal ( self , signal_event ) : <EOL> """<STR_LIT>""" <EOL> order = SuggestedOrder ( <EOL> signal_event . ticker , signal_event . action <EOL> ) <EOL> return order <EOL> def _place_orders_onto_queue ( self , order_list ) : <EOL> """<STR_LIT>""" <EOL> for order_event in order_list : <EOL> self . events_queue . put ( order_event ) <EOL> def _convert_fill_to_portfolio_update ( self , fill_event ) : <EOL> """<STR_LIT>""" <EOL> action = fill_event . action <EOL> ticker = fill_event . ticker <EOL> quantity = fill_event . quantity <EOL> price = fill_event . price <EOL> commission = fill_event . commission <EOL> self . portfolio . transact_position ( <EOL> action , ticker , quantity , <EOL> price , commission <EOL> ) <EOL> def on_signal ( self , signal_event ) : <EOL> """<STR_LIT>""" <EOL> initial_order = self . _create_order_from_signal ( signal_event ) <EOL> sized_order = self . position_sizer . size_order ( <EOL> self . portfolio , initial_order <EOL> ) <EOL> order_events = self . risk_manager . refine_orders ( <EOL> self . portfolio , sized_order <EOL> ) <EOL> self . _place_orders_onto_queue ( order_events ) <EOL> def on_fill ( self , fill_event ) : <EOL> """<STR_LIT>""" <EOL> self . _convert_fill_to_portfolio_update ( fill_event ) <EOL> def update_portfolio_value ( self ) : <EOL> """<STR_LIT>""" <EOL> self . portfolio . _update_portfolio ( ) </s>
