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 unittest(ctx): pass_ctx_from_parent(ctx) import unittest loader = unittest.TestLoader() suite = loader.discover("python/kungfu/tests") runner = unittest.TextTestRunner() runner.run(suite)
def bar(ctx, source, frequency, low_latency): print(source, frequency, low_latency) pass_ctx_from_parent(ctx) ctx.low_latency = low_latency location = pyyjj.location(pyyjj.mode.LIVE, pyyjj.category.MD, source, str(frequency)+"_min", ctx.locator) server = kfext_bar.bar(location, source, frequency, low_latency) server.run()
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 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 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 td(ctx, source, account, low_latency): pass_ctx_from_parent(ctx) ctx.db = AccountsDB( pyyjj.location(pyyjj.mode.LIVE, pyyjj.category.SYSTEM, 'etc', 'kungfu', ctx.locator), 'accounts') account_config = ctx.db.get_td_account_config(source, account) ext = EXTENSION_REGISTRY_TD.get_extension(source)(low_latency, ctx.locator, account, account_config) ext.run()
def watcher(ctx, low_latency, replay, session_id): pass_ctx_from_parent(ctx) ctx.low_latency = low_latency if not replay else True ctx.replay = replay ctx.session_id = session_id watcher_instance = Watcher(ctx) if replay: ctx.category = 'system' ctx.group = 'watcher' ctx.name = 'watcher' replay_setup.setup(ctx, session_id, watcher, watcher_instance) watcher_instance.run()
def ledger(ctx, low_latency, replay, session_id): pass_ctx_from_parent(ctx) ctx.low_latency = low_latency if not replay else True ctx.replay = replay ctx.mode = pyyjj.mode.REPLAY if ctx.replay else pyyjj.mode.LIVE ctx.session_id = session_id ledger_instance = Ledger(ctx) if replay: ctx.category = 'system' ctx.group = 'service' ctx.name = 'ledger' replay_setup.setup(ctx, session_id, ledger, ledger_instance) ledger_instance.run()
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 ping(ctx, times, message): pass_ctx_from_parent(ctx) pyyjj.setup_log(ctx.name) io_device = pyyjj.create_io_device_client(ctx.parent.name) latency = [] for t in range(times): start = pyyjj.now_in_nano() request = {'messsage': message} if message else {} sent_bytes = io_device.publisher.publish(json.dumps(request)) while not io_device.observer.wait(): pass rsp_data = io_device.observer.get_notice() end = pyyjj.now_in_nano() latency.append(end - start) recv_bytes = len(rsp_data) print('[{}] {}/{} bytes time={} ns'.format(t + 1, sent_bytes, recv_bytes, latency[-1])) click.echo( 'round-trip min/avg/max/stddev = {:.0f}/{:.0f}/{:.0f}/{:.0f} ns'. format(numpy.min(latency), numpy.mean(latency), numpy.max(latency), numpy.std(latency)))
def master(ctx, low_latency): pass_ctx_from_parent(ctx) ctx.low_latency = low_latency server = Master(ctx) server.run()
def algo(ctx): pass_ctx_from_parent(ctx)
def bar(ctx, source, time_interval, low_latency): pass_ctx_from_parent(ctx) ctx.mode = pyyjj.mode.LIVE args = {"source": source, "time_interval": time_interval} instance = pywingchun.BarGenerator(ctx.locator, ctx.mode, low_latency, json.dumps(args)) instance.run()