<s> import multiprocessing <EOL> import os <EOL> import threading <EOL> import signal <EOL> import logging <EOL> from datetime import datetime , timedelta <EOL> from time import sleep <EOL> import click <EOL> log = logging . getLogger ( '<STR_LIT>' ) <EOL> class IntervalClock ( object ) : <EOL> def __init__ ( self , interval ) : <EOL> self . interval = interval <EOL> def __iter__ ( self ) : <EOL> while True : <EOL> yield datetime . utcnow ( ) <EOL> sleep ( self . interval ) <EOL> class SimulatedClock ( object ) : <EOL> def __init__ ( self , start , stop , interval ) : <EOL> from app_conf import settings <EOL> interval = settings . CLOCK_INTERVAL <EOL> self . start = start <EOL> self . stop = stop <EOL> self . interval = timedelta ( seconds = interval ) <EOL> def __iter__ ( self ) : <EOL> current = self . start <EOL> while current < self . stop : <EOL> yield current <EOL> current += self . interval <EOL> class ControllerBase ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , clock , broker , portfolio , strategies ) : <EOL> self . _clock = clock <EOL> self . _broker = broker <EOL> self . _strategies = strategies <EOL> self . _portfolio = portfolio <EOL> def initialize ( self , tick ) : <EOL> for strategy in self . _strategies : <EOL> strategy . start ( self . _broker , tick ) <EOL> def run ( self ) : <EOL> raise NotImplementedError ( ) <EOL> def run_until_stopped ( self ) : <EOL> raise NotImplementedError ( ) <EOL> def is_running ( self ) : <EOL> raise NotImplementedError ( ) <EOL> def stop ( self ) : <EOL> raise NotImplementedError ( ) <EOL> def execute_tick ( self , tick ) : <EOL> raise NotImplementedError ( ) <EOL> class ThreadedControllerMixin ( object ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( ThreadedControllerMixin , self ) . __init__ ( * args , ** kwargs ) <EOL> self . _stop_requested = False <EOL> self . _main_loop = None <EOL> self . _is_running = False <EOL> self . _thread_lock = threading . Condition ( ) <EOL> def run ( self ) : <EOL> assert self . _main_loop is None <EOL> self . _main_loop = threading . Thread ( target = self . _run ) <EOL> self . _main_loop . start ( ) <EOL> def run_until_stopped ( self ) : <EOL> self . run ( ) <EOL> while self . is_running ( ) : <EOL> try : <EOL> sleep ( <NUM_LIT> ) <EOL> except KeyboardInterrupt : <EOL> if self . is_running ( ) : <EOL> self . stop ( ) <EOL> break <EOL> else : <EOL> click . secho ( '<STR_LIT>' , fg = '<STR_LIT>' ) <EOL> def is_running ( self ) : <EOL> return self . _is_running <EOL> def _run ( self ) : <EOL> self . _is_running = True <EOL> try : <EOL> self . _thread_lock . acquire ( ) <EOL> clock = iter ( self . _clock ) <EOL> self . initialize ( next ( clock ) ) <EOL> for tick in clock : <EOL> if self . _stop_requested : <EOL> break <EOL> self . execute_tick ( tick ) <EOL> if self . _stop_requested : <EOL> break <EOL> else : <EOL> self . _is_running = False <EOL> os . kill ( os . getpid ( ) , signal . SIGINT ) <EOL> finally : <EOL> self . _is_running = False <EOL> self . _thread_lock . notify ( ) <EOL> def stop ( self ) : <EOL> click . secho ( '<STR_LIT>' , fg = '<STR_LIT>' ) <EOL> self . _stop_requested = True <EOL> self . _main_loop . join ( ) <EOL> class SingleThreadedControllerMixin ( object ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( SingleThreadedControllerMixin , self ) . __init__ ( * args , ** kwargs ) <EOL> self . _stop_requested = False <EOL> self . _is_running = False <EOL> def run ( self ) : <EOL> raise NotImplementedError ( ) <EOL> def run_until_stopped ( self ) : <EOL> def stop ( signal , frame ) : <EOL> self . stop ( ) <EOL> signal . signal ( signal . SIGINT , lambda signal , frame : self . stop ( ) ) <EOL> self . _is_running = True <EOL> try : <EOL> clock = iter ( self . _clock ) <EOL> self . initialize ( next ( clock ) ) <EOL> for tick in clock : <EOL> if self . _stop_requested : <EOL> break <EOL> self . execute_tick ( tick ) <EOL> if self . _stop_requested : <EOL> break <EOL> else : <EOL> click . secho ( '<STR_LIT>' , fg = '<STR_LIT>' ) <EOL> finally : <EOL> self . _is_running = False <EOL> def is_running ( self ) : <EOL> return self . _is_running <EOL> def stop ( self ) : <EOL> click . secho ( '<STR_LIT>' , fg = '<STR_LIT>' ) <EOL> self . _stop_requested = True <EOL> class Controller ( SingleThreadedControllerMixin , ControllerBase ) : <EOL> def execute_tick ( self , tick ) : <EOL> self . _broker . set_current_tick ( tick ) <EOL> operations = [ strategy . tick ( tick ) for strategy in self . _strategies ] <EOL> operations = [ op for op in operations if op ] <EOL> self . _portfolio . run_operations ( operations , self . _strategies ) </s>
