def main(self):
        self.logger.info("Initiating EXO building engine for {0}".format(
            self.args.instrument))

        # Initialize EXO engine SignalApp (report first status)
        self.signalapp = SignalApp(self.args.instrument, APPCLASS_EXO,
                                   RABBIT_HOST, RABBIT_USER, RABBIT_PASSW)
        self.signalapp.send(MsgStatus('INIT', 'Initiating EXO engine'))

        # Get information about decision and execution time
        assetindex = AssetIndexMongo(MONGO_CONNSTR, MONGO_EXO_DB)
        self.asset_info = assetindex.get_instrument_info(args.instrument)

        if self.args.backfill is not None:
            # Backfill mode enabled
            self.do_backfill()
            self.signalapp.send(
                MsgStatus("OK",
                          "EXO backfill for {0} has been finished.".format(
                              self.args.instrument),
                          notify=True))
        else:
            # Online mode

            # Subscribe to datafeed signal app
            self.logger.debug('Subscribing datafeed for: ' +
                              self.args.instrument)
            datafeed = SignalApp(self.args.instrument, APPCLASS_DATA,
                                 RABBIT_HOST, RABBIT_USER, RABBIT_PASSW)
            # Listening datafeed loop
            datafeed.listen(self.on_new_quote)
    def listen_apps(self):
        # Waiting slack bot to connect first
        time.sleep(2)
        self.log.info("Listening for events")

        def callback(appclass, appname, msg):
            try:
                if msg.mtype == MsgStatus.mtype:
                    if msg.notify:
                        self.log.info("Notification: {0}.{1}: {2}".format(
                            appclass, appname, msg))
                        self.send_notification(appname, appclass, msg)
                    self.log.info("Incoming status: {0}.{1}: {2}".format(
                        appclass, appname, msg))
            except Exception:
                self.log.error("Failed to process: {0}.{1}: {2}".format(
                    appclass, appname, msg))
                self.log.exception('Error while processing message')

        try:
            app = SignalApp('*', '*')
            app.listen(callback)
        except Exception:
            self.log.exception('Error while listening RabbitMQ')
            self.send_message(
                "Exception occurred while processing framework messages, look into logs for information"
            )
class EventLoggerScript:
    def __init__(self):
        loglevel = logging.DEBUG
        logging.getLogger("pika").setLevel(logging.WARNING)
        logger = logging.getLogger('AlphaCustomOnlineScript')
        logger.setLevel(loglevel)

        # create console handler with a higher log level
        ch = logging.StreamHandler(sys.stdout)
        ch.setLevel(loglevel)

        # create formatter and add it to the handlers
        formatter = logging.Formatter(
            '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
        ch.setFormatter(formatter)
        logger.addHandler(ch)

        self.log = logger

        self.log.info('Init Event logger script')
        self.signal_app = SignalApp('*', '*', RABBIT_HOST, RABBIT_USER,
                                    RABBIT_PASSW)

        client = MongoClient(MONGO_CONNSTR)
        self.db = client[MONGO_EXO_DB]
        self.prepare_db()

    def prepare_db(self):
        self.db[EVENTS_STATUS].create_index([('appclass', pymongo.ASCENDING),
                                             ('appname', pymongo.ASCENDING)],
                                            background=True)
        self.db[EVENTS_LOG].create_index([('date', pymongo.DESCENDING)],
                                         background=True)

    def log_event(self, appclass, appname, date, msgtype, msgtext):
        msg_dict = {
            'appclass': appclass,
            'appname': appname,
            'msgtype': msgtype,
            'date': date,
            'text': msgtext,
        }
        self.db[EVENTS_LOG].insert_one(msg_dict)

    def process_message_status(self, appclass, appname, msg):
        msg_dict = {
            'appclass': appclass,
            'appname': appname,
            'status': msg.status,
            'date': msg.date,
            'text': msg.message,
        }
        self.db[EVENTS_STATUS].replace_one(
            {
                'appclass': appclass,
                'appname': appname
            }, msg_dict, upsert=True)
        self.log_event(appclass, appname, msg.date, msg.mtype,
                       '{0}: {1}'.format(msg.status, msg.message))

    def process_message_quotenotification(self, appclass, appname, msg):
        msg_text = 'Quote: {0} at {1}'.format(msg.instrument, msg.date)
        self.log_event(appclass, appname, datetime.now(), msg.mtype, msg_text)

    def process_message_exoquote(self, appclass, appname, msg):
        msg_text = 'EXOQuote: {0} at {1}'.format(msg.exo_name, msg.exo_date)
        self.log_event(appclass, appname, datetime.now(), msg.mtype, msg_text)

    def process_message_alphasignal(self, appclass, appname, msg):
        msg_text = 'AlphaSignal: {0} Exposure {1} AccountsCount: {2}'.format(
            msg.swarm_name, msg.exposure, len(msg.positions))
        self.log_event(appclass, appname, datetime.now(), msg.mtype, msg_text)

    def process_message_alphastate(self, appclass, appname, msg):
        msg_text = 'AlphaState: {0} Exposure {1} LastDate: {2}'.format(
            msg.swarm_name, msg.exposure, msg.last_date)
        self.log_event(appclass, appname, datetime.now(), msg.mtype, msg_text)

    def process_event_callback(self, appclass, appname, msg):
        self.log.info("New message {0}.{1}: {2}".format(
            appclass, appname, msg))

        if msg.mtype == MsgStatus.mtype:
            self.process_message_status(appclass, appname, msg)
        elif msg.mtype == MsgQuoteNotification.mtype:
            self.process_message_quotenotification(appclass, appname, msg)
        elif msg.mtype == MsgEXOQuote.mtype:
            self.process_message_exoquote(appclass, appname, msg)
        elif msg.mtype == MsgAlphaSignal.mtype:
            self.process_message_alphasignal(appclass, appname, msg)
        elif msg.mtype == MsgAlphaState.mtype:
            self.process_message_alphastate(appclass, appname, msg)

    def main(self):
        """
        Application main()
        :return:
        """
        # Subscribe to rabbit MQ EXO feed
        self.signal_app.listen(self.process_event_callback)
Exemple #4
0
from tradingcore.signalapp import SignalApp

app = SignalApp('*', 'testclass')


def callback(appname, appclass, data):
    print(appname + '.' + appclass)

    print(data)


print("Listening")

app.listen(callback)
class AlphaOnlineScript:
    def __init__(self, args, loglevel):
        self.args = args
        self.loglevel = loglevel
        self.alpha_name = args.alphaname
        logging.getLogger("pika").setLevel(logging.WARNING)
        logger = logging.getLogger('AlphaOnlineScript')
        logger.setLevel(loglevel)

        # create console handler with a higher log level
        ch = logging.StreamHandler(sys.stdout)
        ch.setLevel(loglevel)

        # create formatter and add it to the handlers
        formatter = logging.Formatter(
            '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
        ch.setFormatter(formatter)
        logger.addHandler(ch)

        self.log = logger

        self.log.info('Init AlphaOnlineScript Alpha: {0}'.format(
            self.alpha_name))

        self.signal_app = SignalApp(self.alpha_name, APPCLASS_ALPHA,
                                    RABBIT_HOST, RABBIT_USER, RABBIT_PASSW)
        self.signal_app.send(
            MsgStatus(
                "INIT",
                'Initiating online alpha engine {0}'.format(self.alpha_name)))
        self.exo_app = SignalApp('*', APPCLASS_EXO, RABBIT_HOST, RABBIT_USER,
                                 RABBIT_PASSW)

    def swarm_updated_callback(self, swm):
        # Logging swarm structure information
        if self.loglevel == logging.DEBUG:
            self.log.debug(
                'swarm_updated_callback: Swarm processed: {0}'.format(
                    swm.name))
            last_state = swm.laststate_to_dict()
            del last_state['swarm_series']
            pp = pprint.PrettyPrinter(indent=4)

            self.log.debug('Swarm last state: \n {0}'.format(
                pp.pformat(last_state)))

        self.signal_app.send(MsgAlphaState(swm))

    def on_exo_quote_callback(self, appclass, appname, msg):

        # Make sure that is valid EXO quote message
        if msg.mtype == MsgEXOQuote.mtype:
            self.log.debug('on_exo_quote_callback: {0}.{1} Data: {2}'.format(
                appname, appclass, msg))

            self.log.info('Processing EXO quote: {0} at {1}'.format(
                msg.exo_name, msg.exo_date))
            try:
                # Load strategy_context
                m = importlib.import_module('scripts.alphas.alpha_{0}'.format(
                    self.alpha_name.replace('alpha_', '').replace('.py', '')))

                # Initiate swarm from Mongo DB
                exo_name = msg.exo_name

                context = m.STRATEGY_CONTEXT
                context['strategy']['suffix'] = m.STRATEGY_SUFFIX

                swmonline = SwarmOnlineManager(MONGO_CONNSTR, MONGO_EXO_DB,
                                               context)
                # Update and save swarm with new day data (and run callback)
                swmonline.process(exo_name,
                                  swm_callback=self.swarm_updated_callback)
                self.signal_app.send(
                    MsgStatus("RUN", 'Processing generic alpha'))
            except:
                self.log.exception("Error in processing EXO quote: {0}".format(
                    msg.exo_name))
                self.signal_app.send(
                    MsgStatus(
                        "ERROR",
                        "Error while processing EXO quote: {0} for alpha {1}".
                        format(msg.exo_name, self.alpha_name),
                        notify=True,
                    ))

    def main(self):
        """
        Application main()
        :return:
        """
        # Subscribe to rabbit MQ EXO feed
        self.exo_app.listen(self.on_exo_quote_callback)
class TradingOnlineScript:
    def __init__(self, args, loglevel):
        self.args = args
        self.loglevel = loglevel
        logging.getLogger("pika").setLevel(logging.WARNING)
        logger = logging.getLogger('TradingOnlineScript')
        logger.setLevel(loglevel)

        # create console handler with a higher log level
        ch = logging.StreamHandler(sys.stdout)
        ch.setLevel(loglevel)

        # create formatter and add it to the handlers
        formatter = logging.Formatter(
            '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
        ch.setFormatter(formatter)
        logger.addHandler(ch)

        self.log = logger

        self.log.info('Init TradingOnlineScript')

        self.signal_app = SignalApp('TradingOnlineScript', APPCLASS_SIGNALS,
                                    RABBIT_HOST, RABBIT_USER, RABBIT_PASSW)
        self.signal_app.send(
            MsgStatus("INIT", 'Initiating trading online engine'))
        self.alpha_app = SignalApp('*', APPCLASS_ALPHA, RABBIT_HOST,
                                   RABBIT_USER, RABBIT_PASSW)

        #
        # Init EXO engine datasource
        #
        assetindex = AssetIndexMongo(MONGO_CONNSTR, MONGO_EXO_DB)
        exostorage = EXOStorage(MONGO_CONNSTR, MONGO_EXO_DB)

        futures_limit = 3
        options_limit = 10
        self.datasource = DataSourceBase(assetindex, futures_limit,
                                         options_limit, exostorage)

        self.exmgr = ExecutionManager(MONGO_CONNSTR, self.datasource,
                                      MONGO_EXO_DB)

    def on_alpha_state_callback(self, appclass, appname, msg):

        # Make sure that is valid EXO quote message
        if msg.mtype == MsgAlphaState.mtype:
            self.signal_app.send(
                MsgStatus("RUN", "Processing account positions"))

            self.log.debug(
                'on_alpha_signal_callback: {0}.{1} Data: {2}'.format(
                    appname, appclass, msg))
            self.log.info('Processing Alpha state of: {0} at {1}'.format(
                msg.swarm_name, msg.last_date))
            try:
                # Processing positions for each campaign/account
                pos_list = self.exmgr.account_positions_process(
                    write_to_db=True)
                pp = pprint.PrettyPrinter(indent=4)
                self.log.debug(pp.pformat(pos_list))

                # Send position information to real-time software via RabbitMQ
                self.signal_app.send(MsgAlphaSignal(msg, pos_list))
            except:
                self.log.exception("Error in processing account positions")
                self.signal_app.send(
                    MsgStatus("ERROR",
                              "Error while processing account positions",
                              notify=True))

    def main(self):
        """
        Application main()
        :return:
        """
        # Subscribe to rabbit MQ EXO feed
        self.alpha_app.listen(self.on_alpha_state_callback)