Class: Melaya::BacktestAPI
- Inherits:
-
Object
- Object
- Melaya::BacktestAPI
- Defined in:
- lib/melaya/backtest.rb
Overview
Backtest API — run strategies against historical data on the Rust engine.
Start a single run or a parameter sweep (grid / random), poll the job, then pull metrics, the equity curve, and the trade list. All backtests run natively on Melaya's in-house engine — no per-venue SDK in the loop.
Maps to https://api.melaya.org/api/v1/private/backtest/* on the private plane.
Instance Method Summary collapse
-
#cancel(job_id) ⇒ Object
Cancel an in-flight job.
-
#delete(job_id) ⇒ Object
Soft-delete a single job.
-
#delete_all ⇒ Object
Soft-delete every non-favorited job.
-
#favorites(limit: nil, offset: nil) ⇒ Object
Your favorited backtest jobs (Forge tier and above).
-
#funding_range(exchange:, symbol:) ⇒ Object
Earliest funding-rate timestamp available for an exchange+symbol (ms, or nil).
-
#initialize(http) ⇒ BacktestAPI
constructor
A new instance of BacktestAPI.
-
#job(job_id) ⇒ Object
Job status + progress (+status+,
progress_pct, ...). -
#list(limit: nil, offset: nil) ⇒ Object
Your backtest jobs, newest first.
-
#optimize_apply(opt_run_id, body = {}) ⇒ Object
POST /api/v1/private/backtest/optimize/:optRunId/apply Apply best params from a completed optimization sweep to a strategy.
-
#optimize_cancel(opt_run_id) ⇒ Object
POST /api/v1/private/backtest/optimize/:optRunId/cancel Cancel an in-progress optimization sweep.
-
#optimize_list(params = {}) ⇒ Object
GET /api/v1/private/backtest/optimize List optimization sweep runs.
-
#optimize_start(body) ⇒ Object
POST /api/v1/private/backtest/optimize Start a parameter sweep optimization (genetic/grid) over a strategy config.
-
#optimize_status(opt_run_id) ⇒ Object
GET /api/v1/private/backtest/optimize/:optRunId/status Get the status/progress of an optimization sweep run.
-
#results(job_id) ⇒ Object
Metrics, equity curve, and OHLCV for a completed job.
-
#start(body) ⇒ Object
Start a backtest.
-
#sweep(parent_id, objective: nil, limit: nil) ⇒ Object
Ranked children of a sweep parent (default objective: sharpe DESC).
-
#trades(job_id, limit: nil, offset: nil) ⇒ Object
The trade list for a completed job (default 500, max 5000 per call).
Constructor Details
#initialize(http) ⇒ BacktestAPI
Returns a new instance of BacktestAPI.
12 13 14 |
# File 'lib/melaya/backtest.rb', line 12 def initialize(http) @http = http end |
Instance Method Details
#cancel(job_id) ⇒ Object
Cancel an in-flight job.
86 87 88 |
# File 'lib/melaya/backtest.rb', line 86 def cancel(job_id) @http.post("/api/v1/private/backtest/#{job_id}/cancel") end |
#delete(job_id) ⇒ Object
Soft-delete a single job.
92 93 94 |
# File 'lib/melaya/backtest.rb', line 92 def delete(job_id) @http.delete("/api/v1/private/backtest/#{job_id}") end |
#delete_all ⇒ Object
Soft-delete every non-favorited job. Returns a hash with "deleted" count.
97 98 99 |
# File 'lib/melaya/backtest.rb', line 97 def delete_all @http.delete("/api/v1/private/backtest") end |
#favorites(limit: nil, offset: nil) ⇒ Object
Your favorited backtest jobs (Forge tier and above).
69 70 71 72 73 |
# File 'lib/melaya/backtest.rb', line 69 def favorites(limit: nil, offset: nil) @http.get("/api/v1/private/backtest/favorites", "limit" => limit, "offset" => offset ).dig("data", "jobs") end |
#funding_range(exchange:, symbol:) ⇒ Object
Earliest funding-rate timestamp available for an exchange+symbol (ms, or nil).
78 79 80 81 82 |
# File 'lib/melaya/backtest.rb', line 78 def funding_range(exchange:, symbol:) @http.get("/api/v1/private/backtest/funding-range", "exchange" => exchange, "symbol" => symbol )["earliest_ms"] end |
#job(job_id) ⇒ Object
Job status + progress (+status+, progress_pct, ...).
27 28 29 |
# File 'lib/melaya/backtest.rb', line 27 def job(job_id) @http.get("/api/v1/private/backtest/jobs/#{job_id}") end |
#list(limit: nil, offset: nil) ⇒ Object
Your backtest jobs, newest first.
60 61 62 63 64 |
# File 'lib/melaya/backtest.rb', line 60 def list(limit: nil, offset: nil) @http.get("/api/v1/private/backtest", "limit" => limit, "offset" => offset ).dig("data", "jobs") end |
#optimize_apply(opt_run_id, body = {}) ⇒ Object
POST /api/v1/private/backtest/optimize/:optRunId/apply Apply best params from a completed optimization sweep to a strategy.
135 136 137 |
# File 'lib/melaya/backtest.rb', line 135 def optimize_apply(opt_run_id, body = {}) @http.post("/api/v1/private/backtest/optimize/#{URI.encode_www_form_component(opt_run_id.to_s)}/apply", body) end |
#optimize_cancel(opt_run_id) ⇒ Object
POST /api/v1/private/backtest/optimize/:optRunId/cancel Cancel an in-progress optimization sweep.
127 128 129 |
# File 'lib/melaya/backtest.rb', line 127 def optimize_cancel(opt_run_id) @http.post("/api/v1/private/backtest/optimize/#{URI.encode_www_form_component(opt_run_id.to_s)}/cancel") end |
#optimize_list(params = {}) ⇒ Object
GET /api/v1/private/backtest/optimize List optimization sweep runs.
113 114 115 |
# File 'lib/melaya/backtest.rb', line 113 def optimize_list(params = {}) @http.get("/api/v1/private/backtest/optimize", params) end |
#optimize_start(body) ⇒ Object
POST /api/v1/private/backtest/optimize Start a parameter sweep optimization (genetic/grid) over a strategy config.
106 107 108 |
# File 'lib/melaya/backtest.rb', line 106 def optimize_start(body) @http.post("/api/v1/private/backtest/optimize", body) end |
#optimize_status(opt_run_id) ⇒ Object
GET /api/v1/private/backtest/optimize/:optRunId/status Get the status/progress of an optimization sweep run.
120 121 122 |
# File 'lib/melaya/backtest.rb', line 120 def optimize_status(opt_run_id) @http.get("/api/v1/private/backtest/optimize/#{URI.encode_www_form_component(opt_run_id.to_s)}/status") end |
#results(job_id) ⇒ Object
Metrics, equity curve, and OHLCV for a completed job.
33 34 35 |
# File 'lib/melaya/backtest.rb', line 33 def results(job_id) @http.get("/api/v1/private/backtest/results/#{job_id}")["result"] end |
#start(body) ⇒ Object
Start a backtest. mode defaults to a single run; pass "grid_sweep" /
"random_sweep" with param_ranges to fan out a parameter search.
Returns a hash with "job_id" (and optionally "count").
21 22 23 |
# File 'lib/melaya/backtest.rb', line 21 def start(body) @http.post("/api/v1/private/backtest/start", body) end |
#sweep(parent_id, objective: nil, limit: nil) ⇒ Object
Ranked children of a sweep parent (default objective: sharpe DESC).
51 52 53 54 55 |
# File 'lib/melaya/backtest.rb', line 51 def sweep(parent_id, objective: nil, limit: nil) @http.get("/api/v1/private/backtest/sweep/#{parent_id}", "objective" => objective, "limit" => limit ) end |
#trades(job_id, limit: nil, offset: nil) ⇒ Object
The trade list for a completed job (default 500, max 5000 per call).
41 42 43 44 45 |
# File 'lib/melaya/backtest.rb', line 41 def trades(job_id, limit: nil, offset: nil) @http.get("/api/v1/private/backtest/trades/#{job_id}", "limit" => limit, "offset" => offset )["trades"] end |