def main(self):
        logging.info("Initiating data notification script")

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

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

        # TODO: replace DB name after release
        mongo_db_name = 'tmldb_test'
        tmp_mongo_connstr = 'mongodb://*****:*****@10.0.1.2/tmldb_test?authMechanism=SCRAM-SHA-1'
        client = MongoClient(tmp_mongo_connstr)
        self.db = client[mongo_db_name]
        # Creating index for 'bartime'
        self.db['futurebarcol'].create_index([('bartime', pymongo.DESCENDING)],
                                             background=True)

        status_client = MongoClient(MONGO_CONNSTR)
        self.status_db = status_client[MONGO_EXO_DB]
        self.status_db[STATUS_QUOTES_COLLECTION].create_index(
            [('instrument', pymongo.DESCENDING)], background=True)

        last_minute = 0
        while True:
            # Getting last bar time from DB
            last_bar_time = self.get_last_bar_time()
            self.process_quote(last_bar_time)
            time.sleep(15)
    def on_new_quote(self, appclass, appname, data):
        if data.mtype != MsgQuoteNotification.mtype:
            return

        # Check data integrity
        if not self.check_quote_data(appname, appclass, data):
            return

        exec_time, decision_time = AssetIndexMongo.get_exec_time(
            datetime.now(), self.asset_info)
        start_time = time.time()

        quote_date = data['date']
        symbol = appname

        if quote_date > decision_time:
            # TODO: Check to avoid dupe launch
            # Run first EXO calculation for this day
            self.logger.info(
                "Run EXO calculation, at decision time: {0}".format(
                    decision_time))

            assetindex = AssetIndexMongo(MONGO_CONNSTR, MONGO_EXO_DB)
            exostorage = EXOStorage(MONGO_CONNSTR, MONGO_EXO_DB)

            futures_limit = 3
            options_limit = 10

            #datasource = DataSourceMongo(mongo_connstr, mongo_db_name, assetindex, futures_limit, options_limit, exostorage)
            #datasource = DataSourceSQL(SQL_HOST, SQL_USER, SQL_PASS, assetindex, futures_limit, options_limit, exostorage)
            #
            # Test DB temporary credentials
            #
            tmp_mongo_connstr = 'mongodb://*****:*****@10.0.1.2/tmldb_test?authMechanism=SCRAM-SHA-1'
            tmp_mongo_db = 'tmldb_test'
            datasource = DataSourceHybrid(SQL_HOST, SQL_USER, SQL_PASS,
                                          assetindex, tmp_mongo_connstr,
                                          tmp_mongo_db, futures_limit,
                                          options_limit, exostorage)

            # Run EXO calculation
            self.run_exo_calc(datasource,
                              decision_time,
                              symbol,
                              backfill_dict=None)

            end_time = time.time()
            self.signalapp.send(
                MsgStatus('OK',
                          'EXO processed for {0} at {1}'.format(
                              symbol, quote_date),
                          context={
                              'instrument': symbol,
                              'date': quote_date,
                              'exec_time': end_time - start_time
                          },
                          notify=True))

        else:
            self.logger.debug("Waiting next decision time")
    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 do_backfill(self):
        #
        self.logger.info("Run EXO backfill from {0}".format(
            self.args.backfill))

        assetindex = AssetIndexMongo(MONGO_CONNSTR, MONGO_EXO_DB)
        exostorage = EXOStorage(MONGO_CONNSTR, MONGO_EXO_DB)

        futures_limit = 3
        options_limit = 20
        # datasource = DataSourceMongo(mongo_connstr, mongo_db_name, assetindex, futures_limit, options_limit, exostorage)
        datasource = DataSourceSQL(SQL_HOST, SQL_USER, SQL_PASS, assetindex,
                                   futures_limit, options_limit, exostorage)

        exos = exostorage.exo_list(exo_filter=self.args.instrument + '_',
                                   return_names=True)

        exo_start_dates = {}
        exec_time, decision_time = AssetIndexMongo.get_exec_time(
            self.args.backfill, self.asset_info)

        current_time = decision_time

        if len(exos) > 0:
            for exo_name in exos:
                series = exostorage.load_series(exo_name)[0]
                if series is not None:
                    last_date = series.index[-1] + timedelta(days=1)
                    exec_time, decision_time = AssetIndexMongo.get_exec_time(
                        last_date, self.asset_info)
                    self.logger.info(
                        'Updating existing {0} series from: {1}'.format(
                            exo_name, decision_time))
                    exo_start_dates[exo_name] = decision_time

        else:
            self.logger.info('Updating new EXO series from: {0}'.format(
                self.args.backfill))
            exec_time, decision_time = AssetIndexMongo.get_exec_time(
                self.args.backfill, self.asset_info)

        exec_time_end, decision_time_end = AssetIndexMongo.get_exec_time(
            datetime.now(), self.asset_info)

        while current_time <= decision_time_end:
            self.logger.info("Backfilling: {0}".format(current_time))

            self.run_exo_calc(datasource,
                              current_time,
                              args.instrument,
                              backfill_dict=exo_start_dates)

            current_time += timedelta(days=1)
            exec_time += timedelta(days=1)
Exemplo n.º 5
0
    def test_chain_handle_missing_data(self):
        assetindex = AssetIndexMongo(MONGO_CONNSTR, MONGO_EXO_DB)



        futures_limit = 3
        options_limit = 20

        datasource = DataSourceSQL(SQL_HOST, SQL_USER, SQL_PASS, assetindex, futures_limit, options_limit)

        base_date = datetime(2014, 2, 18, 11, 10, 0)
        instr = datasource.get("CL", base_date)
        rh = RolloverHelper(instr)
        fut, opt_chain = rh.get_active_chains()
        # opt_chain.get_by_delta(0.05) on the CL on this date is has absent data
        # opt_chain selects next available date
        self.assertEqual(opt_chain.get_by_delta(0.05), opt_chain.get_by_delta(0.04))

        base_date = datetime(2016, 2, 17, 11, 10, 0)
        instr = datasource.get("ZN", base_date)
        rh = RolloverHelper(instr)
        fut, opt_chain = rh.get_active_chains()
        # opt_chain[11] is an absent strike inside DB, if it absent opt_chain selects next available strike
        self.assertEqual(opt_chain[11], opt_chain[12])
        self.assertEqual(opt_chain[-11], opt_chain[-12])
Exemplo n.º 6
0
    def __init__(self, smartexo_class, **kwargs):
        self.verbosive_logging = kwargs.get('verbosive_logging', False)
        self.futures_limit = kwargs.get('futures_limit', 3)
        self.options_limit = kwargs.get('options_limit', 20)
        self.smartexo_class = smartexo_class

        if self.verbosive_logging:
            logging.basicConfig(format='%(message)s', level=logging.DEBUG)
        else:
            logging.basicConfig(format='%(message)s', level=logging.INFO)

        self.assetindex = AssetIndexMongo(MONGO_CONNSTR, MONGO_EXO_DB)
        self.exostorage = EXOStorage(MONGO_CONNSTR, MONGO_EXO_DB)

        self.datasource = DataSourceSQL(SQL_HOST, SQL_USER, SQL_PASS,
                                        self.assetindex, self.futures_limit,
                                        self.options_limit, self.exostorage)
    def setUp(self):
        mongo_connstr = 'mongodb://localhost:27017/'
        mongo_db_name = 'tmldb'
        assetindex = AssetIndexMongo(mongo_connstr, mongo_db_name)
        self.date = datetime(2014, 1, 6, 10, 15, 0)
        futures_limit = 3
        options_limit = 10
        self.datasource = DataSourceMongo(mongo_connstr, mongo_db_name, assetindex, self.date, futures_limit, options_limit)

        server = 'h9ggwlagd1.database.windows.net'
        user = '******'
        password = '******'
Exemplo n.º 8
0
    def build_smartexo(self, start_date, **smartexo_kwargs):
        self.clear_smartexo()

        logging.info(
            "Starting EXO calculation process from: {0}".format(start_date))

        if self.smartexo_class.ASSET_LIST is None:
            warnings.warn(
                "You must define ASSET_LIST inside SmartEXO class. Aborting..."
            )
            return

        for ticker in self.smartexo_class.ASSET_LIST:
            logging.info("Processing: " + ticker)
            currdate = start_date
            enddate = datetime.combine(datetime.now().date(), dttime(0, 0, 0))

            while currdate <= enddate:
                start_time = time.time()
                date = currdate

                asset_info = self.assetindex.get_instrument_info(ticker)
                exec_time_end, decision_time_end = AssetIndexMongo.get_exec_time(
                    date, asset_info)

                logging.info("\t\tRun on {0}".format(decision_time_end))

                with self.smartexo_class(ticker, 0, decision_time_end,
                                         self.datasource,
                                         **smartexo_kwargs) as exo_engine:
                    try:
                        asset_list = exo_engine.ASSET_LIST
                        # Checking if current symbol is present in EXO class ASSET_LIST
                        if asset_list is not None:
                            if ticker not in asset_list:
                                # Skipping assets which are not in the list
                                continue
                    except AttributeError:
                        warnings.warn(
                            "EXO class {0} doesn't contain ASSET_LIST attribute filter, calculating all assets"
                            .format(self.smartexo_class))

                    # Load EXO information from mongo
                    exo_engine.load()
                    exo_engine.calculate()

                end_time = time.time()
                currdate += timedelta(days=1)
                logging.debug("Elapsed: {0}".format(end_time - start_time))
        logging.info('Done')
Exemplo n.º 9
0
    def test_real_world_debug(self):
        return

        assetindex = AssetIndexMongo(MONGO_CONNSTR, MONGO_EXO_DB)
        storage = EXOStorage(MONGO_CONNSTR, MONGO_EXO_DB)
        # datasource = DataSourceSQL(SQL_HOST, SQL_USER, SQL_PASS, assetindex, futures_limit=10, options_limit=10)
        datasource = DataSourceMongo(MONGO_CONNSTR,
                                     MONGO_EXO_DB,
                                     assetindex,
                                     futures_limit=10,
                                     options_limit=10,
                                     exostorage=storage)
        exmgr = ExecutionManager(MONGO_CONNSTR,
                                 datasource,
                                 dbname=MONGO_EXO_DB)

        positions = exmgr.account_positions_process(write_to_db=False)
Exemplo n.º 10
0
    def setUpClass(self):
        mongo_connstr = 'mongodb://*****:*****@10.0.1.2/tmldb?authMechanism=SCRAM-SHA-1'
        mongo_db_name = 'tmldb'
        assetindex = AssetIndexMongo(mongo_connstr, mongo_db_name)
        exostorage = EXOStorage(mongo_connstr, mongo_db_name)

        # base_date = datetime(2011, 6, 13, 12, 45, 0)

        futures_limit = 3
        options_limit = 10

        server = 'h9ggwlagd1.database.windows.net'
        user = '******'
        password = '******'
        self.datasource = DataSourceSQL(server, user, password, assetindex,
                                        futures_limit, options_limit,
                                        exostorage)
Exemplo n.º 11
0
    def test_max_min_offset(self):
        assetindex = AssetIndexMongo(MONGO_CONNSTR, MONGO_EXO_DB)

        base_date = datetime(2015, 6, 13, 12, 45, 0)

        futures_limit = 3
        options_limit = 20

        datasource = DataSourceSQL(SQL_HOST, SQL_USER, SQL_PASS, assetindex, futures_limit, options_limit)

        instr = datasource.get("ES", base_date)
        rh = RolloverHelper(instr)
        fut, opt_chain = rh.get_active_chains()

        offset = opt_chain.maxoffset
        self.assertEqual(20, offset)

        offset = opt_chain.minoffset
        self.assertEqual(-20, offset)
    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)
Exemplo n.º 13
0
    try:
        from .settings import *
    except SystemError:
        from scripts.settings import *

    try:
        from .settings_local import *
    except SystemError:
        try:
            from scripts.settings_local import *
        except ImportError:
            pass
        pass

    mongo_db_name = 'tmldb'
    assetindex = AssetIndexMongo(MONGO_CONNSTR, mongo_db_name)
    exostorage = EXOStorage(MONGO_CONNSTR, mongo_db_name)

    base_date = datetime(2011, 3, 1, 10, 15, 0)

    futures_limit = 3
    options_limit = 10

    DEBUG = '.'

    datasource = DataSourceMongo(MONGO_CONNSTR, mongo_db_name, assetindex,
                                 futures_limit, options_limit, exostorage)

    server = 'h9ggwlagd1.database.windows.net'
    user = '******'
    password = '******'
            rh = RolloverHelper(instr)
            fut, opt_chain = rh.get_active_chains()

            trans_list += [
                Transaction(fut, self.date, 5.0, fut.price,
                            leg_name='neutral'),
            ]
            return trans_list

        return []


if __name__ == "__main__":
    mongo_connstr = 'mongodb://*****:*****@10.0.1.2/tmldb?authMechanism=SCRAM-SHA-1'
    mongo_db_name = 'tmldb'
    assetindex = AssetIndexMongo(mongo_connstr, mongo_db_name)
    exostorage = EXOStorage(mongo_connstr, mongo_db_name)

    base_date = datetime(2011, 6, 13, 12, 45, 0)
    futures_limit = 3
    options_limit = 10

    DEBUG = '.'

    datasource = DataSourceMongo(mongo_connstr, mongo_db_name, assetindex,
                                 futures_limit, options_limit, exostorage)

    server = 'h9ggwlagd1.database.windows.net'
    user = '******'
    password = '******'
    datasource = DataSourceSQL(server, user, password, assetindex,
    def process_quote(self, last_bar_time):
        dtnow = self.date_now()

        exec_time, decision_time = AssetIndexMongo.get_exec_time(
            dtnow, self.asset_info)
        if self.last_quote_date is None:
            self.last_quote_date = self.get_last_quote_date()

        quote_status = 'IDLE'

        if self.is_quote_delayed(last_bar_time):
            if self.last_minute != dtnow.minute:
                logging.info('Quote delayed more than {0} minutes '
                             'for {1} LastBarTimeDB: {2} Now: {3}'.format(
                                 self.args.delay, self.args.instrument,
                                 last_bar_time, dtnow))

                self.signalapp.send(
                    MsgStatus('DELAY',
                              'Quote delayed more than {0} minutes '
                              'for {1} LastBarTimeDB: {2} Now: {3}'.format(
                                  self.args.delay, self.args.instrument,
                                  last_bar_time, dtnow),
                              notify=True))
                quote_status = 'DELAY'

        # Fire new quote notification if last_bar_time > decision_time
        if self.last_quote_date.date() != last_bar_time.date(
        ) and last_bar_time > decision_time:
            if quote_status != 'DELAY':
                quote_status = 'RUN'
            # Reporting current status
            self.signalapp.send(
                MsgStatus('RUN',
                          'Processing new bar {0}'.format(last_bar_time)))
            logging.info(
                'Running new bar. Bar time: {0}'.format(last_bar_time))
            self.last_quote_date = last_bar_time
            context = {
                'last_bar_time': last_bar_time,
                'now': dtnow,
                'last_run_date': self.last_quote_date,
                'decision_time': decision_time,
                'execution_time': exec_time,
                'instrument': self.args.instrument,
                'quote_status': quote_status,
            }
            logging.debug('Current context:\n {0}'.format(
                self.pprinter.pformat(context)))
            self.signalapp.send(
                MsgQuoteNotification(self.args.instrument, last_bar_time,
                                     context))
            self.set_last_quote_state(context, update=False)

        else:
            context = {
                'last_bar_time': last_bar_time,
                'now': dtnow,
                'last_run_date': self.last_quote_date,
                'decision_time': decision_time,
                'execution_time': exec_time,
                'instrument': self.args.instrument,
                'quote_status': quote_status,
            }

            if self.last_quote_date == NULL_DATE:
                # If quote is absent is status_quotes collection, insert new
                self.set_last_quote_state(context, update=False)
            else:
                self.set_last_quote_state(context, update=True)

            # Log initial information:
            if self.last_minute == -1:
                logging.debug('Current context:\n {0}'.format(
                    self.pprinter.pformat(context)))
            elif self.last_minute != dtnow.minute:
                logging.debug('Last bar time {0}'.format(last_bar_time))
                #self.signalapp.send(MsgStatus('IDLE', 'Last bar time {0}'.format(last_bar_time), context))

            self.last_minute = dtnow.minute
Exemplo n.º 16
0
    def test_chain_get_by_delta(self):
        assetindex = AssetIndexMongo(MONGO_CONNSTR, MONGO_EXO_DB)

        base_date = datetime(2015, 6, 13, 12, 45, 0)

        futures_limit = 3
        options_limit = 20

        datasource = DataSourceSQL(SQL_HOST, SQL_USER, SQL_PASS, assetindex, futures_limit, options_limit)

        instr = datasource.get("ES", base_date)
        rh = RolloverHelper(instr)
        fut, opt_chain = rh.get_active_chains()

        atm_strike = opt_chain.atmstrike

        opt = opt_chain.get_by_delta(0.5)
        self.assertEqual(opt.strike, atm_strike)
        self.assertEqual(opt.putorcall, 'C')

        opt = opt_chain.get_by_delta(-0.5)
        self.assertEqual(opt.strike, atm_strike)
        self.assertEqual(opt.putorcall, 'P')

        self.assertRaises(ValueError, opt_chain.get_by_delta, 0)
        self.assertRaises(ValueError, opt_chain.get_by_delta, float('nan'))
        self.assertRaises(ValueError, opt_chain.get_by_delta, 1)
        self.assertRaises(ValueError, opt_chain.get_by_delta, 2)
        self.assertRaises(ValueError, opt_chain.get_by_delta, -2)
        self.assertRaises(ValueError, opt_chain.get_by_delta, -1)

        # ITM Put
        opt = opt_chain.get_by_delta(-0.7)
        self.assertEqual(opt.strike, 2115)
        self.assertEqual(opt.putorcall, 'P')
        self.assertAlmostEqual(opt.delta, -0.75, 2)

        opt = opt_chain.get_by_delta(-0.999999)
        self.assertEqual(opt.strike, 2195.0)
        self.assertEqual(opt.putorcall, 'P')
        self.assertAlmostEqual(opt.delta, -0.989, 3)

        # OTM Put
        opt = opt_chain.get_by_delta(-0.3)
        self.assertEqual(opt.strike, 2070)
        self.assertEqual(opt.putorcall, 'P')
        self.assertAlmostEqual(opt.delta, -0.27, 2)

        opt = opt_chain.get_by_delta(-0.00001)
        self.assertEqual(opt, opt_chain[-20].P)

        # ITM Call
        opt = opt_chain.get_by_delta(0.7)
        self.assertEqual(opt.strike, 2070)
        self.assertEqual(opt.putorcall, 'C')
        self.assertAlmostEqual(opt.delta, 0.73, 2)

        opt = opt_chain.get_by_delta(0.9999999)
        self.assertEqual(opt, opt_chain[-20].C)

        # OTM Call
        opt = opt_chain.get_by_delta(0.3)
        self.assertEqual(opt.strike, 2115)
        self.assertEqual(opt.putorcall, 'C')
        self.assertAlmostEqual(opt.delta, 0.24, 2)

        opt = opt_chain.get_by_delta(0.000001)
        self.assertEqual(opt, opt_chain[20].C)
Exemplo n.º 17
0
class SmartEXOUtils:
    def __init__(self, smartexo_class, **kwargs):
        self.verbosive_logging = kwargs.get('verbosive_logging', False)
        self.futures_limit = kwargs.get('futures_limit', 3)
        self.options_limit = kwargs.get('options_limit', 20)
        self.smartexo_class = smartexo_class

        if self.verbosive_logging:
            logging.basicConfig(format='%(message)s', level=logging.DEBUG)
        else:
            logging.basicConfig(format='%(message)s', level=logging.INFO)

        self.assetindex = AssetIndexMongo(MONGO_CONNSTR, MONGO_EXO_DB)
        self.exostorage = EXOStorage(MONGO_CONNSTR, MONGO_EXO_DB)

        self.datasource = DataSourceSQL(SQL_HOST, SQL_USER, SQL_PASS,
                                        self.assetindex, self.futures_limit,
                                        self.options_limit, self.exostorage)

    def plot_transactions_payoff(self, smart_exo_position_func, analysis_date,
                                 analysis_instrument, **whatif_kwargs):

        payoff = PayoffAnalyzer(self.datasource)
        instr = self.datasource.get(analysis_instrument, analysis_date)
        rh = RolloverHelper(instr)
        fut, opt_chain = rh.get_active_chains()

        strikes_on_graph = whatif_kwargs.get('strikes_on_graph', 30)
        whatif_iv_change = whatif_kwargs.get('whatif_iv_change', 0)
        whatif_days_to_expiration = whatif_kwargs.get(
            'whatif_days_to_expiration', int(opt_chain.to_expiration_days / 2))

        payoff.load_transactions(
            smart_exo_position_func(analysis_date, fut, opt_chain),
            analysis_date)
        payoff.plot(strikes_on_graph, whatif_iv_change,
                    whatif_days_to_expiration)

    def clear_smartexo(self):
        logging.info("Deleting all SmartEXO of :" +
                     self.smartexo_class.EXO_NAME)
        client = MongoClient(MONGO_CONNSTR)
        db = client[MONGO_EXO_DB]
        db['exo_data'].delete_many({
            'name': {
                '$regex': '.*{0}*.'.format(self.smartexo_class.EXO_NAME)
            }
        })

    def build_smartexo(self, start_date, **smartexo_kwargs):
        self.clear_smartexo()

        logging.info(
            "Starting EXO calculation process from: {0}".format(start_date))

        if self.smartexo_class.ASSET_LIST is None:
            warnings.warn(
                "You must define ASSET_LIST inside SmartEXO class. Aborting..."
            )
            return

        for ticker in self.smartexo_class.ASSET_LIST:
            logging.info("Processing: " + ticker)
            currdate = start_date
            enddate = datetime.combine(datetime.now().date(), dttime(0, 0, 0))

            while currdate <= enddate:
                start_time = time.time()
                date = currdate

                asset_info = self.assetindex.get_instrument_info(ticker)
                exec_time_end, decision_time_end = AssetIndexMongo.get_exec_time(
                    date, asset_info)

                logging.info("\t\tRun on {0}".format(decision_time_end))

                with self.smartexo_class(ticker, 0, decision_time_end,
                                         self.datasource,
                                         **smartexo_kwargs) as exo_engine:
                    try:
                        asset_list = exo_engine.ASSET_LIST
                        # Checking if current symbol is present in EXO class ASSET_LIST
                        if asset_list is not None:
                            if ticker not in asset_list:
                                # Skipping assets which are not in the list
                                continue
                    except AttributeError:
                        warnings.warn(
                            "EXO class {0} doesn't contain ASSET_LIST attribute filter, calculating all assets"
                            .format(self.smartexo_class))

                    # Load EXO information from mongo
                    exo_engine.load()
                    exo_engine.calculate()

                end_time = time.time()
                currdate += timedelta(days=1)
                logging.debug("Elapsed: {0}".format(end_time - start_time))
        logging.info('Done')

    def plot_smartexo_price(self):
        if self.smartexo_class.ASSET_LIST is None:
            warnings.warn(
                "You must define ASSET_LIST inside SmartEXO class. Aborting..."
            )
            return

        for ticker in self.smartexo_class.ASSET_LIST:
            exo_df, exo_info = self.exostorage.load_series('{0}_{1}'.format(
                ticker, self.smartexo_class.EXO_NAME))

            f, (ax1, ax2) = plt.subplots(2,
                                         gridspec_kw={'height_ratios': [3, 1]})

            exo_df['exo'].plot(ax=ax1,
                               title='{0}_{1}'.format(
                                   ticker, self.smartexo_class.EXO_NAME))
            ax = exo_df['regime'].plot(ax=ax1, secondary_y=True)
            ax.set_ylim(-2, 2)

            exo_df['delta'].plot(ax=ax2)
            ax2.set_title('Delta')
            plt.show()
Exemplo n.º 18
0
                                self.date,
                                1.0,
                                itm_put.price,
                                leg_name='opt_itm_leg'),
                    Transaction(otm_put,
                                self.date,
                                -1.0,
                                otm_put.price,
                                leg_name='opt_otm_leg'),
                ]


if __name__ == "__main__":
    mongo_connstr = 'mongodb://*****:*****@10.0.1.2/tmldb?authMechanism=SCRAM-SHA-1'
    mongo_db_name = 'tmldb'
    assetindex = AssetIndexMongo(mongo_connstr, mongo_db_name)
    exostorage = EXOStorage(mongo_connstr, mongo_db_name)

    base_date = datetime(2014, 1, 13, 12, 45, 0)
    futures_limit = 3
    options_limit = 10

    DEBUG = '.'

    datasource = DataSourceMongo(mongo_connstr, mongo_db_name, assetindex,
                                 futures_limit, options_limit, exostorage)

    server = 'h9ggwlagd1.database.windows.net'
    user = '******'
    password = '******'
    datasource = DataSourceSQL(server, user, password, assetindex,
def main(args, loglevel):
    if args.logfile == '':
        logging.basicConfig(
            stream=sys.stdout,
            format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
            level=loglevel)
    else:
        logging.basicConfig(
            filename=args.logfile,
            format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
            level=loglevel)
    signalapp = SignalApp("AlphaRebalancer", APPCLASS_ALPHA, RABBIT_HOST,
                          RABBIT_USER, RABBIT_PASSW)
    signalapp.send(MsgStatus('INIT', 'Initiating alpha rebalancer script'))

    #exo_names = get_exo_names_mat()
    logging.getLogger("pika").setLevel(logging.WARNING)
    logging.info("Starting...")

    exo_storage = EXOStorage(MONGO_CONNSTR, MONGO_EXO_DB)
    exo_names = exo_storage.exo_list()

    for exo in exo_names:
        logging.info("Processing EXO: " + exo)
        # Load alpha modules to process
        for module in os.listdir('alphas'):
            #
            #  Custom EXO folder found
            #
            swm = None
            context = None

            if module.lower() == exo.lower() and os.path.isdir(
                    os.path.join('alphas', module)):
                for custom_file in os.listdir(os.path.join('alphas', module)):
                    if 'alpha_' in custom_file and '.py' in custom_file:
                        logging.debug(
                            'Processing custom module: ' +
                            os.path.join('alphas', module, custom_file))
                        try:
                            m = importlib.import_module(
                                'scripts.alphas.{0}.{1}'.format(
                                    module, custom_file.replace('.py', '')))

                            context = m.STRATEGY_CONTEXT
                            context['strategy']['exo_name'] = exo
                            context['strategy'][
                                'suffix'] = m.STRATEGY_SUFFIX + 'custom'
                            context['strategy']['exo_storage'] = exo_storage

                            logging.info(
                                'Running CUSTOM alpha: ' + Swarm.get_name(
                                    m.STRATEGY_CONTEXT, m.STRATEGY_SUFFIX))

                            if 'exo_name' in context['strategy'] and context[
                                    'strategy']['exo_name'] != exo:
                                logging.error(
                                    "Custom strategy context exo_name != current EXO name (folder mismatch?)"
                                )
                                raise ValueError(
                                    "Custom strategy context exo_name != current EXO name (folder mismatch?)"
                                )

                            swm = Swarm(context)
                            swm.run_swarm()
                            swm.pick()

                            #
                            # Saving last EXO state to online DB
                            #
                            swmonline = SwarmOnlineManager(
                                MONGO_CONNSTR, MONGO_EXO_DB,
                                m.STRATEGY_CONTEXT)
                            logging.debug('Saving: {0}'.format(swm.name))
                            swmonline.save(swm)
                        except:
                            logging.exception('Exception occurred:')
                            signalapp.send(
                                MsgStatus('ERROR',
                                          'Exception in {0}'.format(
                                              Swarm.get_name(
                                                  m.STRATEGY_CONTEXT,
                                                  m.STRATEGY_SUFFIX)),
                                          notify=True))

            elif 'alpha_' in module and '.py' in module:
                logging.debug('Processing generic module: ' + module)
                try:
                    m = importlib.import_module('scripts.alphas.{0}'.format(
                        module.replace('.py', '')))
                    for direction in [-1, 1]:
                        context = m.STRATEGY_CONTEXT
                        context['strategy']['exo_name'] = exo
                        context['strategy']['opt_params'][0] = OptParamArray(
                            'Direction', [direction])
                        context['strategy']['suffix'] = m.STRATEGY_SUFFIX
                        context['strategy']['exo_storage'] = exo_storage

                        logging.info('Running alpha: ' +
                                     Swarm.get_name(m.STRATEGY_CONTEXT) +
                                     ' Direction: {0}'.format(direction))

                        swm = Swarm(context)
                        swm.run_swarm()
                        swm.pick()
                        #
                        # Saving last EXO state to online DB
                        #
                        swmonline = SwarmOnlineManager(MONGO_CONNSTR,
                                                       MONGO_EXO_DB,
                                                       m.STRATEGY_CONTEXT)
                        logging.debug('Saving: {0}'.format(swm.name))
                        swmonline.save(swm)
                except:
                    logging.exception('Exception occurred:')
                    signalapp.send(
                        MsgStatus('ERROR',
                                  'Exception in {0}'.format(
                                      Swarm.get_name(m.STRATEGY_CONTEXT,
                                                     m.STRATEGY_SUFFIX)),
                                  notify=True))

    logging.info("Processing accounts positions")
    assetindex = AssetIndexMongo(MONGO_CONNSTR, MONGO_EXO_DB)
    datasource = DataSourceMongo(MONGO_CONNSTR,
                                 MONGO_EXO_DB,
                                 assetindex,
                                 futures_limit=10,
                                 options_limit=10,
                                 exostorage=exo_storage)
    exmgr = ExecutionManager(MONGO_CONNSTR, datasource, dbname=MONGO_EXO_DB)
    exmgr.account_positions_process(write_to_db=True)

    signalapp.send(MsgStatus('DONE', 'Alpha rebalancer script', notify=True))
    logging.info("Done.")