def test_backend_param():
    class Backend:
        pass

    bknd = Backend()
    algo = Algorithm(backend=bknd)
    assert algo._backend == bknd

    with pytest.raises(RuntimeError):
        Algorithm(backend='foo.does.not.exist')
def get_algo(script, **kwargs):
    functions = get_functions(script)
    return Algorithm(
        backend='pylivetrader.testing.fixtures',
        **functions,
        **kwargs,
    )
Beispiel #3
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,
    )

    if pipeline_hook is not None:

        def _pipeline_output(name):
            return pipeline_hook.output(a, name)

        a.pipeline_output = _pipeline_output

    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()
Beispiel #4
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
Beispiel #5
0
def process_algo_params(ctx, file, algofile, backend, backend_config,
                        data_frequency, statefile, retry):
    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
    ctx.retry = retry
    return ctx
Beispiel #6
0
def run(ctx, algofile, backend, backend_config, data_frequency, statefile,
        zipline):
    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))

    functions = get_functions_by_path(algofile, use_translate=zipline)

    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,
    )

    with LiveTraderAPI(algorithm):

        algorithm.run()
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)