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 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 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 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])
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)
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 = '******'
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)
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 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)
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)
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 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)
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 = '******'
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.")