Skip to content

Add inputs argument to ScriptRunner #26

@takumi-miyajima

Description

@takumi-miyajima

Is your feature request related to a problem? Please describe.
Parameter configuration is currently limited to .toml files only, which makes programmatic parameter passing difficult.

Describe the solution you'd like
I would like to be able to pass parameters as arguments directly to the script runner, allowing for more flexible parameter configuration.

"""
@pyne
"""

from pynecore.lib import input

def main(
    short_length: int = input.int(5),
    long_length: int = input.int(10),
):
import pathlib

from pynecore.core.script_runner import ScriptRunner


def run(
    script_path: pathlib.Path,
    plot_path: pathlib.Path,
    strat_path: pathlib.Path,
    trade_path: pathlib.Path,
    ohlcv_iter,
    syminfo,
):
    runner: ScriptRunner = ScriptRunner(
        script_path=script_path,
        plot_path=plot_path,
        strat_path=strat_path,
        trade_path=trade_path,
        ohlcv_iter=ohlcv_iter,
        syminfo=syminfo,
        inputs={"short_length": 10, "long_length": 20}  # <- arguments directly
    )
    runner.run_iter()

Describe alternatives you've considered
Using the internal old_input_values variable as a workaround:

import pathlib

from pynecore.core.script_runner import ScriptRunner

from pynecore.core.script import old_input_values
from typing import Any

def run(
    script_path: pathlib.Path,
    plot_path: pathlib.Path,
    strat_path: pathlib.Path,
    trade_path: pathlib.Path,
    ohlcv_iter,
    syminfo,
):
    inputs = {"short_length": 5, "long_length": 10}
    _inject_arguments_directly(inputs)  # <- workaround

    runner: ScriptRunner = ScriptRunner(
        script_path=script_path,
        plot_path=plot_path,
        strat_path=strat_path,
        trade_path=trade_path,
        ohlcv_iter=ohlcv_iter,
        syminfo=syminfo,
    )
    runner.run_iter()


def _inject_arguments_directly(script_args: dict[str, Any]):
    for key, value in script_args.items():
        old_input_values[key] = value
        old_input_values[key + "__global__"] = value


# src/pynecore/core/script.py
# for arg_name, arg_data in data['inputs'].items():
#     if 'value' not in arg_data:
#         continue
#     old_input_values[arg_name] = arg_data['value']
#     old_input_values[arg_name + '__global__'] = arg_data['value']  # For strict mode

Additional context
Thank you for creating such an excellent library.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions