Inspect a Julia source code from an other Julia code

Hello,

I’m looking for a way to “inspect” a Julia source code from an other Julia code ie I would like to ensure that a code which is stored in a strat.jl file contains an init function from an Julia code named runner.jl.

I did
runner.jl

using ArgParse


function backtest(strategy)
    @info "Running '$strategy'"
    include(strategy)
    initialize(nothing)
end

function parse_commandline()
    s = ArgParseSettings()

    @add_arg_table! s begin
        "--opt1"
            help = "an option with an argument"
        "--opt2", "-o"
            help = "another option with an argument"
            arg_type = Int
            default = 0
        "--flag1"
            help = "an option without argument, i.e. a flag"
            action = :store_true
        "action"
            help = "action (download, backtest, ...)"
            required = true
        "strategy"
            help = "filename of a strategy"
            required = true
    end

    return parse_args(s)
end

function main()
    @info "begin of runner"
    parsed_args = parse_commandline()
    @info "Parsed args:"
    for (arg, val) in parsed_args
         @info "  $arg  =>  $val"
    end
    if parsed_args["action"] == "backtest"
        backtest(parsed_args["strategy"])
    end
    @info "end of runner"
end

main()

and strat.jl

function initialize(state)
    println("strat init")
end

function handler1(state, data)
    println("handler1")
end

but it raises an error that I’ve never seen before…

The applicable method may be too new: running in world age 33497, while current world is 33499.

Code should be run like this

julia runner.jl backtest strat.jl

Any idea how to tackle that ? Sorry if that’s trivial I haven’t looked at Julia recently.

Thanks

Can you explain why you want to do that? It sounds way too convoluted, which makes me suspect an XY problem

2 Likes

I think @gdalle has a good point.

Just to clarify what’s going on though: this is a “world-age issue” which (to my understanding) happens when you define a function that should live in the scope of Main, but you defined it within another function. When you now try to call the newly defined function before you are back to Mains scope, you get a world-age error.

There is some more detailed discussion here and a StackOverflow answer here which shows the behavior of include.

1 Like

I want to have only one entrypoint for running a strategy (a trading program).
This entrypoint will be runner.jl. I want to be able to pass to runner (using command line) the filename of a strategy I want to execute. To achieve this, a strategy have some “requirements”. It must have an initialize function and at least one handler
The runner will be responsible of giving to the strategy all the context required to be executed. So I can’t for example directly run julia strat.jl because the strategy itself can’t do anything without the “context”.
I hope it’s clearer.
This is how work for example Python backtester named zipline

zipline run -f dual_moving_average.py --start 2014-1-1 --end 2018-1-1 -o dma.pickle --no-benchmark

zipline is a Python program which run dual_moving_average.py (strategy) with some context information (start date and end date of backtest)

To me it sounds like a strategy should be a Julia type instead of a file, with several methods designed to dispatch on that type and give it information from the runner.

What you’re trying to specify / check is an interface, and while there is no standard way to do that in Julia, there are several promising (but recent, and thus possibly buggy) attempts: