コード例 #1
0
ファイル: account.py プロジェクト: whut-aaron/kungfu
def account_test(ctx, type, case):
    # master_proc = subprocess.Popen(("yarn dev -l trace master &").split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE, preexec_fn=os.setsid)
    # time.sleep(2)
    # os.system("yarn dev -l trace watcher &"); time.sleep(2)
    # os.system("yarn dev -l trace md -s passive &"); time.sleep(2)
    # os.system("yarn dev -l trace td -s passive -a test &"); time.sleep(5)
    pass_ctx_from_parent(ctx)
    test_io_location = pyyjj.location(pyyjj.mode.LIVE, pyyjj.category.SYSTEM,
                                      "test", "test", ctx.locator)
    io_device = pyyjj.io_device_client(test_io_location, False)
    click.echo('account test')
    td_location = pyyjj.location(pyyjj.mode.LIVE, pyyjj.category.TD, 'passive',
                                 'test', ctx.locator)
    td_passive_rep_sock = ctx.io_device.connect_socket(td_location,
                                                       pyyjj.protocol.REQUEST)
    # send_to_passive(ctx, td_passive_rep_sock, td_location, {'msg_type': 401,'state': 3})

    watcher_location = pyyjj.location(pyyjj.mode.LIVE, pyyjj.category.SYSTEM,
                                      "watcher", "watcher", ctx.locator)
    watcher_pub_sock = io_device.connect_socket(watcher_location,
                                                pyyjj.protocol.SUBSCRIBE,
                                                30 * 1000)
    watcher_pub_sock.setsockopt(nn.SUB, nn.SUB_SUBSCRIBE, '')
    if type == 1:
        login_test(case, watcher_pub_sock, ctx, td_passive_rep_sock,
                   td_location)
    if type == 2:
        trade_test(case, watcher_pub_sock, ctx, td_passive_rep_sock,
                   td_location)
コード例 #2
0
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')
コード例 #3
0
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()
コード例 #4
0
ファイル: strategy.py プロジェクト: jbro885/quantsystem
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()
コード例 #5
0
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))
コード例 #6
0
ファイル: gateway.py プロジェクト: yanqiong/kungfu
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)
コード例 #7
0
ファイル: gateway.py プロジェクト: penggiant/kungfu
def run_extension(ctx, registry):
    if registry.has_extension(ctx.source):
        sys_location = pyyjj.location(pyyjj.mode.LIVE, pyyjj.category.SYSTEM,
                                      'etc', 'kungfu', ctx.locator)
        config = DataProxy(make_url(ctx.locator, sys_location,
                                    "task")).get_task_config(ctx.name)
        config_str = {}
        config_int = {}
        config_double = {}
        for pname in config:
            if type(config[pname]) == str:
                config_str[pname] = config[pname]
            elif type(config[pname]) == int:
                config_int[pname] = config[pname]
            elif type(config[pname]) == float:
                config_double[pname] = config[pname]
            else:
                ctx.logger.error('unknown config %s, %s', type(config[pname]),
                                 config[pname])
        if 'client_id' not in config_int:
            config_int['client_id'] = 1
        config_str['save_file_path'] = '{}/runtime'.format(ctx.home)
        gateway = registry.get_extension(ctx.source)(ctx.low_latency,
                                                     ctx.locator, config_str,
                                                     config_int, config_double)
        gateway.run()
    else:
        ctx.logger.error('Unrecognized %s arg %s', registry.ext_type.lower(),
                         ctx.name)
コード例 #8
0
def setup(ctx, session_id, cmd, instance):
    #ctx.mode = 'live'  # to get live data
    ctx.journal_util_location = pyyjj.location(pyyjj.mode.LIVE,
                                               pyyjj.category.SYSTEM, 'util',
                                               'journal', ctx.locator)
    if not session_id:
        all_sessions = kfj.find_sessions(ctx)
        all_sessions['begin_time'] = all_sessions['begin_time'].apply(
            lambda t: kft.strftime(t, kft.SESSION_DATETIME_FORMAT))
        all_sessions['end_time'] = all_sessions['end_time'].apply(
            lambda t: kft.strftime(t, kft.SESSION_DATETIME_FORMAT))
        all_sessions['duration'] = all_sessions['duration'].apply(
            lambda t: kft.strftime(t - kft.DURATION_TZ_ADJUST, kft.
                                   DURATION_FORMAT))
        click.echo(cmd.get_help(ctx))
        click.echo('please select sessions:')
        click.echo(
            tabulate(all_sessions.values,
                     headers=all_sessions.columns,
                     tablefmt='simple'))
        sys.exit(-1)
    else:
        session = kfj.find_session(ctx, session_id)
        instance.set_begin_time(session['begin_time'])
        instance.set_end_time(session['end_time'])
        instance.set_bt_source(ctx.group)
コード例 #9
0
ファイル: strategy.py プロジェクト: whfkqq/kungfu
 def __init_ledger(self):
     ledger_location = pyyjj.location(pyyjj.get_mode_by_name(self.ctx.mode), pyyjj.category.SYSTEM, 'service', 'ledger', self.ctx.locator)
     self.ctx.ledger_db = LedgerDB(ledger_location, "ledger")
     self.ctx.inst_infos = { inst["instrument_id"]: inst for inst in self.ctx.ledger_db.all_instrument_infos() }
     self.ctx.ledger = self.ctx.ledger_db.load(ctx=self.ctx,ledger_category=LedgerCategory.Portfolio, client_id=self.ctx.name)
     if self.ctx.ledger is None:
         self.ctx.ledger = AccountBook(self.ctx, ledger_category=LedgerCategory.Portfolio,client_id=self.ctx.name, avail=1e7, trading_day=self.ctx.trading_day)
コード例 #10
0
 def __init__(self, locator, name="ledger_client"):
     self.commander_location = pyyjj.location(pyyjj.mode.LIVE,
                                              pyyjj.category.SYSTEM,
                                              'service', 'ledger', locator)
     io_device = pyyjj.io_device(self.commander_location)
     self.cmd_sock = io_device.connect_socket(self.commander_location,
                                              pyyjj.protocol.REQUEST, 10000)
コード例 #11
0
def setup(ctx, session_id, cmd, instance):
    ctx.journal_util_location = pyyjj.location(pyyjj.mode.LIVE,
                                               pyyjj.category.SYSTEM, 'util',
                                               'journal', ctx.locator)
    if not session_id:
        all_sessions = kfj.find_sessions(ctx)
        all_sessions['begin_time'] = all_sessions['begin_time'].apply(
            lambda t: kft.strftime(t, kft.SESSION_DATETIME_FORMAT))
        all_sessions['end_time'] = all_sessions['end_time'].apply(
            lambda t: kft.strftime(t, kft.SESSION_DATETIME_FORMAT))
        all_sessions['duration'] = all_sessions['duration'].apply(
            lambda t: kft.strftime(t - kft.DURATION_TZ_ADJUST, kft.
                                   DURATION_FORMAT))
        click.echo(cmd.get_help(ctx))
        click.echo('please select sessions:')
        click.echo(
            tabulate(all_sessions.values,
                     headers=all_sessions.columns,
                     tablefmt='simple'))
        sys.exit(-1)
    else:
        session = kfj.find_session(ctx, session_id)
        instance.set_begin_time(session['begin_time'])
        instance.set_end_time(session['end_time'])


# yarn dev -l trace backtest -g tushare -n 20190808 run -p ../examples/strategy/py/strategy_demo.py -i 1
コード例 #12
0
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()
コード例 #13
0
def get_location_from_json(ctx, data):
    if 'mode' in data and 'category' in data and 'group' in data and 'name' in data:
        return pyyjj.location(MODES[data['mode']],
                              CATEGORIES[data['category']], data['group'],
                              data['name'], ctx.locator)
    else:
        return None
コード例 #14
0
ファイル: __init__.py プロジェクト: zengjinjie/kungfu
def account(ctx, source):
    pass_ctx_from_root(ctx)
    ctx.source = source
    ctx.location = pyyjj.location(pyyjj.mode.LIVE, pyyjj.category.SYSTEM,
                                  'etc', 'kungfu', ctx.locator)
    ctx.db = AccountsDB(ctx.location, 'accounts')
    ctx.schema = ACCOUNT_SCHEMA[source]
コード例 #15
0
def kfc(ctx, home, log_level, name):
    if not home:
        osname = platform.system()
        user_home = os.path.expanduser('~')
        if osname == 'Linux':
            xdg_config_home = os.getenv('XDG_CONFIG_HOME')
            home = xdg_config_home if xdg_config_home else os.path.join(
                user_home, '.config')
        if osname == 'Darwin':
            home = os.path.join(user_home, 'Library', 'Application Support')
        if osname == 'Windows':
            home = os.getenv('APPDATA')
        home = os.path.join(home, 'kungfu', 'app')

    os.environ['KF_HOME'] = ctx.home = home
    os.environ['KF_LOG_LEVEL'] = ctx.log_level = log_level

    # have to keep locator alive from python side
    # https://github.com/pybind/pybind11/issues/1546
    ctx.locator = kfj.Locator(home)
    ctx.system_config_location = pyyjj.location(pyyjj.mode.LIVE,
                                                pyyjj.category.SYSTEM, 'etc',
                                                'kungfu', ctx.locator)

    if ctx.invoked_subcommand is None:
        click.echo(kfc.get_help(ctx))
    else:
        ctx.name = name if name else ctx.invoked_subcommand
    pass
コード例 #16
0
ファイル: gateway.py プロジェクト: yanqiong/kungfu
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)
コード例 #17
0
def history(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')
コード例 #18
0
ファイル: strategy.py プロジェクト: jbro885/quantsystem
 def __add_account(self, source, account, cash_limit):
     self.wc_context.add_account(source, account, cash_limit)
     location = pyyjj.location(pyyjj.mode.LIVE, pyyjj.category.TD, source,
                               account, self.ctx.locator)
     book = AccountBook(self.ctx, location)
     self.ctx.books[location.uid] = book
     self.book_context.add_book(location, book)
     self.ctx.logger.info("added book {}@{}".format(account, source))
コード例 #19
0
def extension(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('extension', 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')
コード例 #20
0
ファイル: td.py プロジェクト: zengjinjie/kungfu
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()
コード例 #21
0
ファイル: journal.py プロジェクト: yanqiong/kungfu
 def default_to_system_db(self, location, name):
     file = os.path.join(self.layout_dir(location, pyyjj.layout.SQLITE), "{}.{}".format(name, pyyjj.get_layout_name(pyyjj.layout.SQLITE)))
     if os.path.exists(file):
         return file
     else:
         system_location = pyyjj.location(pyyjj.mode.LIVE, pyyjj.category.SYSTEM, "etc", "kungfu", self)
         system_file = os.path.join(self.layout_dir(system_location, pyyjj.layout.SQLITE),
                                    "{}.{}".format(name, pyyjj.get_layout_name(pyyjj.layout.SQLITE)))
         shutil.copy(system_file, file)
         return file
コード例 #22
0
ファイル: account.py プロジェクト: yanqiong/kungfu
def account(ctx):
    pass_ctx_from_parent(ctx)
    click.echo('account test')
    td_location = pyyjj.location(pyyjj.mode.LIVE, pyyjj.category.TD, 'passive',
                                 'test', ctx.locator)
    td_passive_rep_sock = ctx.io_device.connect_socket(td_location,
                                                       pyyjj.protocol.REQUEST)
    send_to_passive(ctx, td_passive_rep_sock, td_location, {
        'msg_type': 401,
        'state': 3
    })
コード例 #23
0
ファイル: apprentice.py プロジェクト: jbro885/quantsystem
    def __init__(self, ctx):
        pyyjj.apprentice.__init__(self,
                                  pyyjj.location(kfj.MODES[ctx.mode],
                                                 kfj.CATEGORIES[ctx.category],
                                                 ctx.group, ctx.name,
                                                 ctx.locator),
                                  low_latency=ctx.low_latency)
        self.ctx = ctx
        self._process = psutil.Process()

        os_signal.handle_os_signals(self.exit_gracefully)
コード例 #24
0
 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)
     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.accounts = {}
コード例 #25
0
ファイル: converter.py プロジェクト: lufy0839/kungfu
 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)
コード例 #26
0
 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))
コード例 #27
0
ファイル: __init__.py プロジェクト: zengjinjie/kungfu
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')
コード例 #28
0
ファイル: master.py プロジェクト: yanqiong/kungfu
    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

        os_signal.handle_os_signals(self.exit_gracefully)
コード例 #29
0
def kfc(ctx, home, log_level, name):
    os.environ['KF_HOME'] = ctx.home = home
    os.environ['KF_LOG_LEVEL'] = ctx.log_level = log_level

    # have to keep locator alive from python side
    # https://github.com/pybind/pybind11/issues/1546
    ctx.locator = kfj.Locator(home)
    ctx.system_config_location = pyyjj.location(pyyjj.mode.LIVE, pyyjj.category.SYSTEM, 'etc', 'kungfu', ctx.locator)

    if ctx.invoked_subcommand is None:
        click.echo(kfc.get_help(ctx))
    else:
        ctx.name = name if name else ctx.invoked_subcommand
    pass
コード例 #30
0
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()