def strategy(ctx, group, name, path, low_latency, replay, session_id): pass_ctx_from_parent(ctx) ctx.group = group ctx.name = name ctx.path = path ctx.low_latency = low_latency if not replay else True ctx.replay = replay ctx.category = 'strategy' mode = pyyjj.mode.REPLAY if ctx.replay else pyyjj.mode.LIVE ctx.mode = pyyjj.get_mode_name(mode) ctx.location = pyyjj.location(mode, pyyjj.category.STRATEGY, group, name, ctx.locator) ctx.logger = create_logger(name, ctx.log_level, ctx.location) if path.endswith('.py'): ctx.strategy = Strategy(ctx) # keep strategy alive for pybind11 else: spec = util.spec_from_file_location( os.path.basename(path).split('.')[0], path) cpp = util.module_from_spec(spec) spec.loader.exec_module(cpp) ctx.strategy = cpp.Strategy(ctx.location) runner = Runner(ctx, mode) runner.add_strategy(ctx.strategy) if replay: ctx.session_id = session_id replay_setup.setup(ctx, session_id, strategy, runner) runner.run()
def __init__(self, ctx): pywingchun.AlgoService.__init__(self, ctx.locator, ctx.mode, ctx.low_latency) self.ctx = ctx self.ctx.logger = create_logger("algo", ctx.log_level, self.io_device.home) self.ctx.db = AlgoDB(self.io_device.home, ctx.name) self.orders = {}
def strategy(ctx, group, name, path, low_latency, replay, session_id, backtest): pass_ctx_from_parent(ctx) ctx.group = group ctx.name = name ctx.path = path ctx.low_latency = low_latency if not replay else True ctx.md_path = None assert not (replay and backtest ), "Replay mode and BackTest mode cannot be selected together" ctx.replay = replay ctx.backtest = backtest mode = pyyjj.mode.REPLAY if ctx.replay else pyyjj.mode.BACKTEST if ctx.backtest else pyyjj.mode.LIVE ctx.logger = create_logger( name, ctx.log_level, pyyjj.location(mode, pyyjj.category.STRATEGY, group, name, ctx.locator)) ctx.strategy = Strategy(ctx) # keep strategy alive for pybind11 runner = Runner(ctx, mode) runner.add_strategy(ctx.strategy) ctx.category = 'strategy' if replay: ctx.session_id = session_id replay_setup.setup(ctx, session_id, strategy, runner) if backtest: #ctx.md_path = os.path.join(ctx.home, 'md', group, name, 'journal', 'backtest', '00000000.*.journal') ctx.category = 'md' ctx.mode = pyyjj.get_mode_name(mode) ctx.session_id = session_id backtest_setup.setup(ctx, session_id, strategy, runner) runner.run()
def strategy(ctx, group, name): pass_ctx_from_root(ctx) ctx.group = group ctx.name = name ctx.category = 'strategy' ctx.mode = "live" ctx.low_latency = False ctx.logger = create_logger( ctx.name, ctx.log_level, pyyjj.location(pyyjj.get_mode_by_name(ctx.mode), pyyjj.category.STRATEGY, ctx.group, ctx.name, ctx.locator)) ctx.path = None ctx.runner = Runner(ctx, pyyjj.get_mode_by_name(ctx.mode)) def init_strategy_patch(self, path): pass with patch.object(Strategy, '_Strategy__init_strategy', init_strategy_patch): ctx.strategy = Strategy(ctx) ctx.strategy._pre_start = lambda ctx: None ctx.strategy._post_start = lambda ctx: None ctx.strategy._pre_stop = lambda ctx: None ctx.strategy._post_stop = lambda ctx: None ctx.strategy._on_trading_day = lambda ctx, daytime: None ctx.strategy._on_entrust = lambda ctx, entrust: None ctx.strategy._on_transaction = lambda ctx, transaction: None ctx.strategy._on_quote = lambda ctx, quote: ctx.logger.info( "quote received: {}".format(quote)) ctx.strategy._on_order = lambda ctx, order: ctx.logger.info( "order received: {}".format(order)) ctx.strategy._on_trade = lambda ctx, trade: ctx.logger.info( "trade received: {}".format(trade))
def ext(ctx): pass_ctx_from_root(ctx) ctx.journal_util_location = pyyjj.location(pyyjj.mode.LIVE, pyyjj.category.SYSTEM, 'util', 'extension', ctx.locator) ctx.logger = create_logger('ext', ctx.log_level, ctx.journal_util_location) if not os.getenv('KF_NO_EXT'): pass else: print('Extension disabled by KF_NO_EXT') ctx.logger.warning('Trying to manage extension while disallowed by KF_NO_EXT')
def md(ctx, source, low_latency): pass_ctx_from_parent(ctx) ctx.name = 'md_' + source ctx.source = source ctx.low_latency = low_latency ctx.logger = create_logger( source, ctx.log_level, pyyjj.location(pyyjj.mode.LIVE, pyyjj.category.MD, source, source, ctx.locator)) run_extension(ctx, EXTENSION_REGISTRY_MD)
def journal(ctx, mode, category, group, name): pass_ctx_from_root(ctx) ctx.low_latency = False ctx.mode = mode ctx.category = category ctx.group = group ctx.name = name ctx.location = pyyjj.location(kfj.MODES[mode], kfj.CATEGORIES[category], group, name, ctx.locator) ctx.journal_util_location = pyyjj.location(pyyjj.mode.LIVE, pyyjj.category.SYSTEM, 'util', 'journal', ctx.locator) ctx.logger = create_logger('journal', ctx.log_level, ctx.journal_util_location) pyyjj.setup_log(ctx.journal_util_location, 'journal')
def td(ctx, source, account, low_latency): pass_ctx_from_parent(ctx) ctx.name = 'td_' + source + '_' + account ctx.source = source ctx.account = account ctx.low_latency = low_latency ctx.logger = create_logger( source, ctx.log_level, pyyjj.location(pyyjj.mode.LIVE, pyyjj.category.TD, source, account, ctx.locator)) run_extension(ctx, EXTENSION_REGISTRY_TD)
def __init__(self, low_latency, locator, config_json): pywingchun.MarketData.__init__(self, low_latency, locator, "sim") self.config = MakerConfig(base=200.0, bound=1000, samples=1000, variation=4, randseed=6) self.orderbooks = {} self.logger = create_logger( "sim_md", "info", pyyjj.location(pyyjj.mode.LIVE, pyyjj.category.MD, "sim", "sim", locator))
def __init__(self, ctx): pywingchun.Ledger.__init__(self, ctx.locator, ctx.mode, ctx.low_latency) self.ctx = ctx self.ctx.ledger = self self.ctx.logger = create_logger("ledger", ctx.log_level, self.io_device.home) self.ctx.calendar = Calendar(ctx) self.ctx.db = LedgerDB(self.io_device.home, ctx.name) self.ctx.inst_infos = {} self.ctx.orders = {} self.ctx.trading_day = None self.ctx.ledgers = {} self.ctx.get_inst_info = self.get_inst_info
def __init__(self, ctx): self.ctx = ctx self.ctx.converter = self self.dest_id = 0 # dest_id 0 should be configurable TODO self.location = pyyjj.location(kfj.MODES['backtest'], kfj.CATEGORIES['md'], ctx.group, ctx.name, ctx.locator) self.io_device = pyyjj.io_device(self.location, False, True) self.writer = self.io_device.open_writer(self.dest_id) self.reader = self.io_device.open_reader_to_subscribe() self.reader.join(self.location, self.dest_id, 0) self.logger = create_logger("converter", ctx.log_level, self.location)
def ledger(ctx): pass_ctx_from_parent(ctx) commander_location = pyyjj.location(pyyjj.mode.LIVE, pyyjj.category.SYSTEM, 'service', 'ledger', ctx.locator) ctx.logger = create_logger("ledger_test", ctx.log_level, commander_location) io_device = pyyjj.io_device(commander_location) cmd_sock = io_device.connect_socket(commander_location, pyyjj.protocol.REQUEST, 10000) calendar.calendar_request(cmd_sock, commander_location) # order.new_order_single(cmd_sock, commander_location, test_account) # ctx.logger.info('done') order.cancel_all_order_for_account(cmd_sock, commander_location, test_account) ctx.logger.info('done')
def run(ctx, path, session_id): pass_ctx_from_parent(ctx) ctx.path = path ctx.low_latency = False ctx.logger = create_logger( ctx.name, ctx.log_level, pyyjj.location(pyyjj.mode.BACKTEST, pyyjj.category.STRATEGY, ctx.group, ctx.name, ctx.locator)) ctx.strategy = Strategy(ctx) runner = Runner(ctx, pyyjj.mode.BACKTEST) runner.add_strategy(ctx.strategy) ctx.session_id = session_id setup(ctx, session_id, run, runner) runner.run()
def __init__(self, ctx): pywingchun.Ledger.__init__(self, ctx.locator, ctx.mode, ctx.low_latency) self.ctx = ctx self.ctx.logger = create_logger("ledger", ctx.log_level, self.io_device.home) self.ctx.calendar = Calendar(ctx) self.ctx.db = LedgerDB(self.io_device.home, ctx.name) self.ctx.orders = {} self.trading_day = None self.accounts = {} self.subportfolios = {} self.portfolios = {}
def backtest(ctx, group, name): print('entering history') pass_ctx_from_root(ctx) ctx.category = 'md' ctx.group = group ctx.name = name ctx.mode = 'backtest' #ctx.location = pyyjj.location(kfj.MODES[ctx.mode], kfj.CATEGORIES[ctx.category], group, name, ctx.locator) ctx.journal_util_location = pyyjj.location(pyyjj.mode.LIVE, pyyjj.category.SYSTEM, 'util', 'journal', ctx.locator) ctx.logger = create_logger('journal', ctx.log_level, ctx.journal_util_location) pyyjj.setup_log(ctx.journal_util_location, 'journal')
def __init__(self, ctx): pyyjj.master.__init__( self, pyyjj.location(kfj.MODES['live'], kfj.CATEGORIES['system'], 'master', 'master', ctx.locator), ctx.low_latency) self.ctx = ctx self.ctx.master = self self.ctx.logger = create_logger("watcher", ctx.log_level, self.io_device.home) self.ctx.apprentices = {} ctx.calendar = Calendar(ctx) ctx.trading_day = ctx.calendar.trading_day self.publish_time(yjj_msg.TradingDay, ctx.calendar.trading_day_ns) ctx.master = self
def strategy(ctx, group, name, path, low_latency, replay, session_id): pass_ctx_from_parent(ctx) ctx.group = group ctx.name = name ctx.path = path ctx.low_latency = low_latency if not replay else True ctx.replay = replay ctx.session_id = session_id mode = pyyjj.mode.REPLAY if ctx.replay else pyyjj.mode.LIVE ctx.logger = create_logger(name, ctx.log_level, pyyjj.location(mode, pyyjj.category.STRATEGY, group, name, ctx.locator)) ctx.strategy = Strategy(ctx) # keep strategy alive for pybind11 runner = Runner(ctx, mode) runner.add_strategy(ctx.strategy) if replay: ctx.category = 'strategy' replay_setup.setup(ctx, session_id, strategy, runner) runner.run()
def __init__(self, ctx): mode = pyyjj.mode.REPLAY if ctx.replay else pyyjj.mode.LIVE pywingchun.Watcher.__init__(self, ctx.locator, mode, ctx.low_latency) self.ctx = ctx self.ctx.logger = create_logger("watcher", ctx.log_level, self.io_device.home) self.trading_day = None location = pyyjj.location(mode, pyyjj.category.SYSTEM, 'watcher', 'watcher', ctx.locator) url = make_url(ctx.locator, location, ctx.name) self.data_proxy = DataProxy(url) self.ledger_holder = LedgerHolder(url) self.accounts = {} self.subportfolios = {} self.portfolios = {}
def __init__(self, ctx): self.ctx = ctx self.ctx.converter = self self.filepath = ctx.filepath self.from_time = int(ctx.time) self.data = None # dest_id 0 should be configurable TODO self.dest_id = 0 self.location = pyyjj.location(kfj.MODES['backtest'], kfj.CATEGORIES['md'], ctx.group, ctx.name, ctx.locator) self.io_device = pyyjj.io_device(self.location, False, True) self.writer = self.io_device.open_writer(self.dest_id) self.reader = self.io_device.open_reader_to_subscribe() self.reader.join(self.location, self.dest_id, self.from_time) #self.reader.join(pyyjj.location(kfj.MODES['live'], kfj.CATEGORIES['md'], 'xtp', 'xtp', ctx.locator) , 0, self.from_time) self.logger = create_logger("converter", ctx.log_level, self.location) self.logger.info("converter creation success!")
def strategy(ctx, group, name): pass_ctx_from_root(ctx) ctx.group = group ctx.name = name ctx.category = 'strategy' ctx.mode = "live" ctx.low_latency = False ctx.logger = create_logger( ctx.name, ctx.log_level, pyyjj.location(pyyjj.get_mode_by_name(ctx.mode), pyyjj.category.STRATEGY, ctx.group, ctx.name, ctx.locator)) ctx.path = None ctx.runner = Runner(ctx, pyyjj.get_mode_by_name(ctx.mode)) def init_strategy_patch(self, path): pass with patch.object(Strategy, '_Strategy__init_strategy', init_strategy_patch): ctx.strategy = Strategy(ctx)
def ledger(ctx, protocol, msg_type): pass_ctx_from_parent(ctx) commander_location = pyyjj.location(pyyjj.mode.LIVE, pyyjj.category.SYSTEM, 'service', 'ledger', ctx.locator) ctx.logger = create_logger("ledger_test", ctx.log_level, commander_location) io_device = pyyjj.io_device(commander_location) cmd_sock = io_device.connect_socket( commander_location, pyyjj.protocol.SUBSCRIBE if protocol == 'sub' else pyyjj.protocol.REQUEST, 10000) if protocol == "sub": cmd_sock.setsockopt(int(pyyjj.protocol.SUBSCRIBE), 1, "") while True: cmd_sock.recv() print(cmd_sock.last_message()) calendar.calendar_request(cmd_sock, commander_location) broker.broker_state_request(cmd_sock, commander_location) order.cancel_all_order_for_account(cmd_sock, commander_location, test_account) asset.asset_request(cmd_sock, commander_location) asset.all_asset_info_request(cmd_sock, commander_location) ctx.logger.info('done')
import kungfu.yijinjing.journal as kfj from kungfu.yijinjing.log import create_logger kf_home = os.getenv('KF_HOME') kfext_log_level = os.getenv('KF_LOG_LEVEL') if not kfext_log_level: kfext_log_level = 'error' kfext_logger = None if kf_home: kfext_log_locator = kfj.Locator(kf_home) kfext_log_location = pyyjj.location(pyyjj.mode.LIVE, pyyjj.category.SYSTEM, 'extensions', 'extensions', kfext_log_locator) kfext_logger = create_logger('extensions', kfext_log_level, kfext_log_location) else: kfext_logger = create_logger('extensions', kfext_log_level, None) class ExtensionRegistry: def __init__(self, ext_type): self._registry = {} self.ext_type = ext_type def register_extension(self, name, extension): kfext_logger.debug('Registering %s extension %s', self.ext_type, name) self._registry[name] = extension def has_extension(self, name): return name in self._registry
def package(ctx): pass_ctx_from_root(ctx) ctx.location = pyyjj.location(pyyjj.mode.LIVE, pyyjj.category.SYSTEM, 'util', 'package', ctx.locator) ctx.logger = create_logger('package', ctx.log_level, ctx.location)