/ scripts / run.jl
run.jl
 1  #!/usr/bin/env julia
 2  
 3  import Pkg
 4  
 5  using Planar
 6  @environment!
 7  
 8  if "--help" in ARGS || "-h" in ARGS
 9      println("""
10      Usage: run.jl [--planar <planar_path>] [--config <config_path>]
11  
12      This script runs multiple strategies in background mode.
13  
14      Options:
15        --planar <planar_path>   Path to the Planar project to activate (default: PlanarDev)
16                                Recommended options: Planar, PlanarDev
17        --config <config_path>   Path to the strategies.toml file (default: strategies.toml in script directory)
18        -h, --help               Show this help message
19  
20      Description:
21        The script loads strategies defined in the config array and starts each one in background mode.
22        It monitors the strategies and restarts any that stop running.
23  
24      Example:
25        julia run.jl --planar PlanarDev --config /path/to/strategies.toml
26        julia run.jl --config ./strategies.toml
27      """)
28      exit(0)
29  end
30  
31  planar_path = "PlanarDev"
32  config_path = joinpath(@__DIR__, "strategies.toml")
33  for (i, arg) in enumerate(ARGS)
34      if arg == "--planar" && i < length(ARGS)
35          planar_path = ARGS[i+1]
36      elseif arg == "--config" && i < length(ARGS)
37          config_path = ARGS[i+1]
38      end
39  end
40  project = "$(ENV["HOME"])/dev/Planar.jl/$planar_path"
41  Pkg.activate(project)
42  
43  mode = Live()
44  sandbox = false
45  @info "strategy run mode $mode"
46  import TOML
47  
48  toml_data = TOML.parsefile(config_path)
49  config = [
50      (;
51          name=Symbol(s["name"]),
52          exchange=Symbol(s["exchange"]),
53          account=s["account"],
54          env=Dict{Symbol,String}([(Symbol(k), v) for (k, v) in s["env"]])
55      ) for s in toml_data["strategy"]
56  ]
57  
58  strats = st.Strategy[]
59  
60  function set_env(env)
61      for (k, v) in pairs(env)
62          ENV[string(k)] = string(v)
63      end
64  end
65  
66  function start_strat(s)
67      try
68          start!(s, foreground=false, with_stdout=false)
69      catch e
70          @error "can't start strategy" exception = e
71      end
72  end
73  
74  for c in config
75      @info "loading strategy $(c.name)"
76      set_env(c.env)
77      s = st.strategy(c.name; mode, sandbox, c.exchange, c.account)
78      start_strat(s)
79      push!(strats, s)
80  end
81  
82  monitor = @async while true
83      for s in strats
84          if !isrunning(s)
85              start_strat(s)
86          end
87      end
88      sleep(5)
89  end
90  @info "monitor task" monitor
91  @info "strategies array" strats