def get_algo(script, **kwargs):
    functions = get_functions(script)
    return Algorithm(
        backend='pylivetrader.testing.fixtures',
        **functions,
        **kwargs,
    )
Exemple #2
0
def process_shell_params(
    ctx,
    file,
    backend,
    backend_config,
    algofile=None,
):
    if file:
        algofile = file
    if algofile is None or algofile == '':
        ctx.fail("must specify algo file with '-f' ")

    if not (Path(algofile).exists() and Path(algofile).is_file()):
        ctx.fail("couldn't find algofile '{}'".format(algofile))

    algomodule = get_algomodule_by_path(algofile)

    backend_options = None
    if backend_config is not None:
        backend_options = configloader.load_config(backend_config)

    algorithm = Algorithm(
        backend=backend,
        backend_options=backend_options,
    )
    ctx.algorithm = algorithm
    ctx.algomodule = algomodule
    # ctx.retry = retry
    return ctx
def process_algo_params(ctx, file, algofile, backend, backend_config,
                        data_frequency, statefile):
    if len(algofile) > 0:
        algofile = algofile[0]
    elif file:
        algofile = file
    else:
        algofile = None

    if algofile is None or algofile == '':
        ctx.fail("must specify algo file with '-f' ")

    if not (Path(algofile).exists() and Path(algofile).is_file()):
        ctx.fail("couldn't find algofile '{}'".format(algofile))

    algomodule = get_algomodule_by_path(algofile)
    functions = get_api_functions(algomodule)

    backend_options = None
    if backend_config is not None:
        backend_options = configloader.load_config(backend_config)

    algorithm = Algorithm(
        backend=backend,
        backend_options=backend_options,
        data_frequency=data_frequency,
        algoname=extract_filename(algofile),
        statefile=statefile,
        **functions,
    )
    ctx.algorithm = algorithm
    ctx.algomodule = algomodule
    return ctx
def test_algorithm_init():
    # check init
    algo = Algorithm(backend='pylivetrader.testing.fixtures')
    assert not algo.initialized

    algo = get_algo('''
def initialize(ctx):
    pass

def handle_data(ctx, data):
    pass
    ''')

    simulate_init_and_handle(algo)
Exemple #5
0
def run_smoke(algo, before_run_hook=None, pipeline_hook=None):
    fake_clock = clock.FaketimeClock()
    # fake_clock.rollback(1)
    be = backend.Backend(clock=fake_clock)

    a = Algorithm(
        initialize=getattr(algo, 'initialize', noop),
        handle_data=getattr(algo, 'handle_data', noop),
        before_trading_start=getattr(algo, 'before_trading_start', noop),
        backend=be,
        pipeline_hook=pipeline_hook,
        statefile='smoke-algo-state.pkl'
    )

    with LiveTraderAPI(a), \
            patch('pylivetrader.executor.executor.RealtimeClock') as rc:
        def make_clock(*args, **kwargs):
            # may want to reconfigure clock
            return fake_clock
        rc.side_effect = make_clock

        if before_run_hook is not None:
            before_run_hook(a, be)
        a.run()