From 2083536e59dc1dbb52bf8435cb0f74ff278a7db8 Mon Sep 17 00:00:00 2001 From: Rusty Eddy Date: Sat, 21 Nov 2026 13:32:02 -0800 Subject: [PATCH] Remote EMAX over TCP: --worker, examples/emax and parity with the built-in EMAX (#702) RunOptions.Worker swaps the built-in strategy for a worker dialed over loopback; everything else in the run is unchanged, journal included. examples/emax is the EMAX strategy written against the public SDK only, with a README. Parity tests run the built-in EMAX and the worker from the same data, settings and account (the six scenarios, a long two-instrument walk, and a real month through the store) and require identical journals, summaries and final positions. A worker that dies leaves a failed run with the events recorded until then. Co-Authored-By: Claude Sonnet 5.5 --- backtest.go | 7 +- cmd/trader/backtest.go | 11 +- cmd/trader/backtest_test.go | 68 ++++++++++ docs/DevelopersGuide.md | 5 +- docs/UsersGuide.md | 12 ++ examples/emax/README.md | 85 +++++++++++++ examples/emax/emax/emax.go | 218 ++++++++++++++++++++++++++++++++ examples/emax/emax/emax_test.go | 158 +++++++++++++++++++++++ examples/emax/main.go | 55 ++++++++ historical.go | 39 ++++-- internal/results/run.go | 1 + service/backtest.go | 19 ++- service/backtest_test.go | 140 ++++++++++++++++++++ trader_test.go | 160 +++++++++++++++++++++++ 14 files changed, 962 insertions(+), 16 deletions(-) create mode 100644 examples/emax/README.md create mode 100644 examples/emax/emax/emax.go create mode 100644 examples/emax/emax/emax_test.go create mode 100644 examples/emax/main.go diff --git a/backtest.go b/backtest.go index e6d6f56..e5a66b9 100644 --- a/backtest.go +++ b/backtest.go @@ -112,6 +112,7 @@ func readError(last marketdata.Bar, err error) error { // stops included; pending is what is still waiting when the data ends. type result struct { scenario string + strategy string // the name of the strategy that traded bars int submitted, filled, rejected, cancelled, pending int @@ -136,8 +137,12 @@ func newResult(scenario string, bars int, tr *trader) (*result, error) { if err != nil { return nil, err } + name := "" + if tr.strategy != nil { // a trader built without one, as some tests do + name = tr.strategy.name() + } r := &result{ - scenario: scenario, bars: bars, rejected: len(tr.rejected), + scenario: scenario, strategy: name, bars: bars, rejected: len(tr.rejected), balance: act.balance, unrealized: unrealized, equity: equity, realized: realized, } diff --git a/cmd/trader/backtest.go b/cmd/trader/backtest.go index d9fd3f6..0e36e3e 100644 --- a/cmd/trader/backtest.go +++ b/cmd/trader/backtest.go @@ -18,6 +18,7 @@ type backtestFlags struct { statusFlags source string resultsRoot string + worker string } func newBacktestCmd(a *app) *cobra.Command { @@ -43,7 +44,13 @@ Every run is journaled: it gets a RunID and a directory of that name under the results root, holding run.json (what the run was, and how it ended) and events.jsonl (every order transition). The results root is --results-root, then ` + envResultsRoot + `, then results.root in ` + defaultConfigPath + `, then the default ` + service.DefaultResultsRoot + `. -A run that fails or is cancelled is journaled too, with its status and why.`, +A run that fails or is cancelled is journaled too, with its status and why. + +--worker host:port trades with an external strategy process listening on that +loopback address (examples/emax is the canonical one) instead of the built-in EMAX: +every group of bars goes to it over TCP and JSON and it answers with the orders it +wants; Trader sizes, admits and fills them exactly as for the built-in strategy. A +worker is not warmed up, so the months before --from are not loaded for it.`, Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, _ []string) error { return a.runBacktest(cmd, f) @@ -57,6 +64,7 @@ A run that fails or is cancelled is journaled too, with its status and why.`, fl.StringVar(&f.interval, "interval", "", "h1 or d1 (default: marketdata.interval)") fl.StringVar(&f.from, "from", "", "first instant, UTC: YYYY-MM-DD or RFC 3339 (required)") fl.StringVar(&f.to, "to", "", "end of the range, exclusive, UTC: YYYY-MM-DD or RFC 3339 (required)") + fl.StringVar(&f.worker, "worker", "", "loopback host:port of a worker process that trades instead of the built-in EMAX (see examples/emax); it is not warmed up") fl.StringVar(&f.resultsRoot, "results-root", "", "results root (default: "+envResultsRoot+", then results.root in the config file, then "+service.DefaultResultsRoot+")") fl.BoolVar(&f.allowBad, "allow-bad", false, "accept months that have bad records (their good bars load)") fl.BoolVar(&f.allowMissing, "allow-missing", false, "accept months with no canonical file (a gap in the series)") @@ -76,6 +84,7 @@ func (a *app) runBacktest(cmd *cobra.Command, f backtestFlags) error { res, err := service.RunBacktest(ctx, logging.WithComponent(slog.Default(), logging.ComponentMarketData), service.BacktestRequest{ Source: f.source, Instruments: opts.symbols, Interval: opts.interval, From: opts.from, To: opts.to, AllowBad: f.allowBad, AllowMissing: f.allowMissing, + Worker: f.worker, ResultsRoot: orDefault(resolveRoot(f.resultsRoot, a.getenv(envResultsRoot), opts.resultsRoot), service.DefaultResultsRoot), }) if err != nil { diff --git a/cmd/trader/backtest_test.go b/cmd/trader/backtest_test.go index 5275be1..25f61a3 100644 --- a/cmd/trader/backtest_test.go +++ b/cmd/trader/backtest_test.go @@ -1,10 +1,12 @@ package main import ( + "bufio" "fmt" "go/parser" "go/token" "os" + "os/exec" "path/filepath" "strconv" "strings" @@ -197,3 +199,69 @@ func TestBacktestCommand_IsTransportOnly(t *testing.T) { } } } + +// startEMAXProcess builds examples/emax and runs it as a separate process, as an +// operator would, and returns the address it prints. +func startEMAXProcess(t *testing.T) string { + t.Helper() + bin := filepath.Join(t.TempDir(), "emax") + out, err := exec.Command("go", "build", "-o", bin, "../../examples/emax").CombinedOutput() + require.NoError(t, err, string(out)) + + cmd := exec.Command(bin, "-addr", "127.0.0.1:0") + stdout, err := cmd.StdoutPipe() + require.NoError(t, err) + require.NoError(t, cmd.Start()) + t.Cleanup(func() { _ = cmd.Process.Kill(); _ = cmd.Wait() }) + + line, err := bufio.NewReader(stdout).ReadString('\n') + require.NoError(t, err) + return strings.TrimSpace(strings.TrimPrefix(line, "listening on ")) +} + +// TestBacktest_WorkerTradesLikeTheBuiltInStrategy runs the command twice on the same +// stored data: with the built-in EMAX, and with the EMAX example as a separate process +// over loopback TCP. The summaries are the same. +func TestBacktest_WorkerTradesLikeTheBuiltInStrategy(t *testing.T) { + root := storetest.Root(t) + putWave(t, root, "EURUSD", 110000) + putWave(t, root, "GBPUSD", 125000) + builtin, _, err := runData(t, nil, backtestArgs(t, root, "2024-02-01")...) + require.NoError(t, err) + + addr := startEMAXProcess(t) + results := t.TempDir() + args := backtestArgs(t, root, "2024-02-01", "--worker", addr) + args[len(args)-3] = results // --results-root's value, which comes before --worker + remote, _, err := runData(t, nil, args...) + + require.NoError(t, err) + summary := func(out string) string { return out[:strings.LastIndex(strings.TrimSpace(out), "\nrun ")] } + assert.Equal(t, summary(builtin), summary(remote), "the same orders, trades and money") + assert.Contains(t, remote, "orders: submitted") + assert.NotContains(t, remote, "submitted 0,", "it traded") + dirs := runDirs(t, results) + require.Len(t, dirs, 1) + rec, rerr := os.ReadFile(filepath.Join(results, dirs[0], "run.json")) + require.NoError(t, rerr) + assert.Contains(t, string(rec), `"transport": "tcp"`) + assert.Contains(t, string(rec), `"addr": "`+addr+`"`) +} + +func TestBacktest_AWorkerThatIsNotListeningIsAFailedRun(t *testing.T) { + root := storetest.Root(t) + putWave(t, root, "EURUSD", 110000) + putWave(t, root, "GBPUSD", 125000) + results := t.TempDir() + args := backtestArgs(t, root, "2024-02-01", "--worker", "127.0.0.1:1") + args[len(args)-3] = results + + _, _, err := runData(t, nil, args...) + + require.Error(t, err) + dirs := runDirs(t, results) + require.Len(t, dirs, 1, "the run exists, and says it failed") + rec, rerr := os.ReadFile(filepath.Join(results, dirs[0], "run.json")) + require.NoError(t, rerr) + assert.Contains(t, string(rec), `"status": "failed"`) +} diff --git a/docs/DevelopersGuide.md b/docs/DevelopersGuide.md index 67bb3ad..ea9f593 100644 --- a/docs/DevelopersGuide.md +++ b/docs/DevelopersGuide.md @@ -154,7 +154,10 @@ account and broker (`replay.go`, `backtest.go`): and the run directory (`internal/results`, the one package that opens those files), then `LoadSeries` (the market-data service's policy for missing, bad and stale months), then `RunSeriesWith`, then finalizes `run.json`; `trader backtest` only - parses flags and prints. + parses flags and prints. `RunOptions.Worker` (`--worker`) swaps the built-in strategy + for a remote one (`strategy_remote.go`, the transport in package `strategy`) with + everything else unchanged; `examples/emax` is the canonical worker, and its parity + with the built-in EMAX is tested (`trader_test.go`, `service/backtest_test.go`). - Mid prices and ticks (#667). A canonical mid is the exact midpoint of bid and ask, so it can sit half a tick from a whole tick (1.100005 for ticks of 0.00001). `trader.RunSeries` rounds every bar price to the nearest tick of the market (a diff --git a/docs/UsersGuide.md b/docs/UsersGuide.md index a232dbc..9ae3f7c 100644 --- a/docs/UsersGuide.md +++ b/docs/UsersGuide.md @@ -288,6 +288,18 @@ stored data is not changed. Output: the engine's summary for the run (bars, orders, trades, money, open positions), then `run : `. +### An external strategy + +`--worker host:port` trades with a strategy running as its own process on a loopback +address instead of the built-in EMAX. Trader sends each group of completed bars over TCP +and JSON; the worker answers with the orders it wants (it never says how many units); +Trader sizes, admits, fills and journals them as it does for the built-in strategy. A +worker is not warmed up: the month before `--from` is not loaded for it, and it starts +cold. [`examples/emax`](../examples/emax/README.md) is the canonical worker (the same +EMAX, written against the public SDK) and its README says how to run it and read the +journal. A worker that cannot be reached, disconnects or crashes ends the run as +`failed`, with the events recorded until then. + ### Results Every run is journaled, whether it completes, fails or is cancelled. It gets a RunID diff --git a/examples/emax/README.md b/examples/emax/README.md new file mode 100644 index 0000000..82acbad --- /dev/null +++ b/examples/emax/README.md @@ -0,0 +1,85 @@ +# EMAX as an external strategy + +The canonical example of a strategy that runs as its own process. It is the EMAX +strategy Trader has built in, written against the public Go SDK only (`strategy`, +`marketdata`, `num`), so it shows what an outside worker looks like: Trader sends +each group of completed bars over loopback TCP and JSON, the worker answers with the +orders it wants, and Trader sizes, admits, fills and journals them exactly as it +does for the built-in strategy. + +| File | What it is | +|---|---| +| `main.go` | The program: flags, then `strategy.ListenAndServe`. | +| `emax/emax.go` | The strategy: `emax.New(cfg)` and `OnBars`, plus the EMA. | + +## The rule + +Long while the short EMA of the close is above the long one, short while it is +below. A change of side is a close on the crossover bar and the opposite open on the +next. Every open carries a stop a fixed fraction of the entry price away, rounded +down to the price tick (at least one tick). Each instrument has its own EMAs and +signal state. The strategy asks only; it never says how many units. + +## Run it + +Start the worker. It listens on a loopback address (a non-loopback one is refused) +and prints the address it bound: + +```sh +go run ./examples/emax -addr 127.0.0.1:9100 +``` + +| Flag | Default | Meaning | +|---|---|---| +| `-addr` | `127.0.0.1:0` | loopback address to listen on (`:0` picks a free port) | +| `-short`, `-long` | `2`, `4` | EMA windows (the built-in EMAX uses 2 and 4) | +| `-stop` | `0.01` | stop distance as a fraction of the price (1%) | +| `-tick` | `0.00001` | price tick of the instruments traded (EURUSD, GBPUSD) | + +Then run a backtest that trades with it, in another terminal. The data is the +canonical bars in the store, as for any `trader backtest`: + +```sh +trader backtest --worker 127.0.0.1:9100 \ + --instrument EURUSD,GBPUSD --interval h1 --from 2024-01-01 --to 2024-04-01 +``` + +The worker serves one run and exits when Trader closes the connection. To compare +with the built-in strategy, run the same command without `--worker`: the summaries +are the same (see Parity). + +A worker is not warmed up. The built-in EMAX is prefilled with the month before +`--from`; a worker is only shown bars from `--from` on, so it starts cold. For the +comparison, use a range whose earlier month is not stored, or accept that the first +few bars may differ. + +## Where the results are + +Every run is journaled under the results root (`--results-root`, `TRADER_RESULTS_ROOT`, +`results.root`, default `/srv/trading/backtest/results`), in a directory named by its +RunID. The command prints it at the end: + +```text +run run_01KB5Q3V7T2N4XG9YB6HFA1V7Z: /srv/trading/backtest/results/run_01KB5Q3V7T2N4XG9YB6HFA1V7Z +``` + +```sh +dir=/srv/trading/backtest/results/run_01KB5Q3V7T2N4XG9YB6HFA1V7Z +jq '{status, error, strategy, result: .result.orders}' $dir/run.json # transport tcp, status completed +jq -c 'select(.state=="filled")' $dir/events.jsonl # every fill, stops included +``` + +A run whose worker disconnects or crashes is journaled too: `run.json` says +`failed` and why, and `events.jsonl` keeps what had happened until then. + +## Parity + +The worker must trade exactly as the built-in EMAX does. The root package's tests +hold it to that: the six EMAX scenarios, a 1,500-bar two-instrument walk, and a real +month built through the store (`service`) are each run with the built-in strategy and +with this worker over TCP from the same data, settings and account, and the +journals (every order transition, fill and protective exit, in order, to the price), +the summaries and the final positions must be identical. A run that disagrees is a +regression to explain, not a difference in protocol. The worker's EMA is a copy of +the built-in one (a worker cannot import Trader's private code), and a test checks the +two give the same value to the last bit. diff --git a/examples/emax/emax/emax.go b/examples/emax/emax/emax.go new file mode 100644 index 0000000..5b4c9b4 --- /dev/null +++ b/examples/emax/emax/emax.go @@ -0,0 +1,218 @@ +// Package emax is the EMAX strategy as an external worker writes it: the public +// Go SDK (package strategy) and the public models (marketdata, num), nothing from +// Trader's private packages. +// +// EMAX is long while the short EMA of the close is above the long one and short +// while it is below. A change of side is a close on the crossover bar and the +// opposite open on the next. Every open carries a stop a fixed fraction of the +// price away. The strategy asks only: Trader sizes, admits and fills. +// +// This is the same rule as the EMAX built into Trader (strategy.go, indicators.go +// in the root package), written again here because a worker cannot import it. The +// two are kept honest by a parity test in the root package: the same series, the +// same settings and the same account must give the same orders, fills and result +// through the worker over TCP as through the built-in strategy. If you change the +// rule in either place, that test says so. +package emax + +import ( + "fmt" + + "github.com/rustyeddy/trader/marketdata" + "github.com/rustyeddy/trader/num" + "github.com/rustyeddy/trader/strategy" +) + +// MaxPrice is the largest price Trader accepts in an order. An entry whose stop +// would be above it is skipped, as the built-in EMAX skips it. +var MaxPrice = num.MustParsePrice("10000") + +// Config is the strategy's settings. +type Config struct { + Short, Long int // EMA windows; Short < Long + StopPct num.Rate // the stop's distance from the entry, as a fraction of it: 0 < StopPct < 1 + Tick num.Price // the instruments' price tick; the stop is a whole number of ticks +} + +// Validate reports a setting EMAX cannot run with. +func (c Config) Validate() error { + switch { + case c.Short <= 0 || c.Long <= 0: + return fmt.Errorf("emax: windows must be > 0 (short=%d long=%d)", c.Short, c.Long) + case c.Short >= c.Long: + return fmt.Errorf("emax: short window (%d) must be less than long window (%d)", c.Short, c.Long) + case c.StopPct.Sign() <= 0 || c.StopPct.Cmp(num.MustParseRate("1")) >= 0: + return fmt.Errorf("emax: stop must be between 0%% and 100%% of price (stop=%s)", c.StopPct) + case c.Tick.IsZero(): + return fmt.Errorf("emax: no price tick") + } + return nil +} + +// Strategy is EMAX on each instrument it is shown, with its own EMAs and signal +// state per instrument. It is not safe for concurrent use: a session calls OnBars +// one group at a time. +type Strategy struct { + cfg Config + by map[string]*instrument +} + +// New returns a Strategy with the settings, or the reason they will not do. +func New(cfg Config) (*Strategy, error) { + if err := cfg.Validate(); err != nil { + return nil, err + } + return &Strategy{cfg: cfg, by: make(map[string]*instrument)}, nil +} + +// side is the position the signal is in, or is about to take. +type side string + +const ( + flat side = "" + long side = "long" + short side = "short" +) + +// instrument is EMAX's state on one instrument. pos is the last entry signal +// consumed, not the position: a stop-out does not change it, so after one the +// strategy stays flat until the trend turns. pending is the side to open on the +// next bar, after a flipping close. +type instrument struct { + short, long *EMA + pos side + pending side +} + +// OnBars is the rule for one group of bars, each handed to its instrument alone, so +// it asks for at most one order per bar, in the group's order. The events Trader +// reports are not used: EMAX decides from the bars. +func (s *Strategy) OnBars(bars []marketdata.Bar, _ []strategy.Event) ([]strategy.Order, error) { + var orders []strategy.Order + for _, b := range bars { + st := s.by[b.Instrument] + if st == nil { + st = &instrument{short: NewEMA(s.cfg.Short), long: NewEMA(s.cfg.Long)} + s.by[b.Instrument] = st + } + if o, ok := s.onBar(st, b); ok { + orders = append(orders, o) + } + } + return orders, nil +} + +func (s *Strategy) onBar(st *instrument, b marketdata.Bar) (strategy.Order, bool) { + // Feed both EMAs every bar so the long one warms up on the same data. + sv, sok := st.short.OnBar(b) + lv, lok := st.long.OnBar(b) + if !sok || !lok { + return strategy.Order{}, false + } + + // Finish a flip: the close went out last bar, now open the other side. + if st.pending != flat { + d := st.pending + st.pending = flat + st.pos = d + return s.open(b, d) + } + + var trend side + switch { + case sv > lv: + trend = long + case sv < lv: + trend = short + default: + return strategy.Order{}, false // equal: no change + } + + switch { + case trend == st.pos: + return strategy.Order{}, false + case st.pos == flat: + st.pos = trend + return s.open(b, trend) + default: + // In the opposite side: close now, open the new side next bar. A close takes + // no direction: it acts on the existing position. + st.pos = flat + st.pending = trend + return strategy.Order{Instrument: b.Instrument, Action: "close", Price: b.OHLC().Close}, true + } +} + +// open asks to open d at the bar's close, with the stop. An entry whose stop cannot +// be placed is skipped, and not retried: the next order comes from the next +// crossover. +func (s *Strategy) open(b marketdata.Bar, d side) (strategy.Order, bool) { + entry := b.OHLC().Close + stop, err := s.stopFor(entry, d) + if err != nil || stop.IsZero() || stop.Cmp(MaxPrice) > 0 { + return strategy.Order{}, false + } + return strategy.Order{ + Instrument: b.Instrument, + Action: "open", + Direction: string(d), + Price: entry, + StopLoss: stop, + }, true +} + +// stopFor places the stop StopPct of the entry price away on the losing side: below +// a long, above a short. The distance rounds down to the tick but is at least one +// tick. +func (s *Strategy) stopFor(entry num.Price, d side) (num.Price, error) { + raw, err := entry.MulRate(s.cfg.StopPct) + if err != nil { + return num.Price{}, err + } + dist, err := raw.RoundDown(s.cfg.Tick) + if err != nil { + return num.Price{}, err + } + if dist.IsZero() { + dist = s.cfg.Tick + } + if d == short { + return entry.Add(dist) + } + return entry.Sub(dist) +} + +// EMA is an exponential moving average of the close, seeded with the simple average +// of the first window closes. +type EMA struct { + window int + alpha float64 + count int + sum float64 // while seeding + value float64 +} + +// NewEMA returns an EMA of the given window, which must be positive. +func NewEMA(window int) *EMA { + if window <= 0 { + panic("emax: ema window must be > 0") + } + return &EMA{window: window, alpha: 2 / float64(window+1)} +} + +// OnBar takes the bar's close and returns the EMA, and false until a full window +// has been seen to seed it. +func (e *EMA) OnBar(b marketdata.Bar) (float64, bool) { + val := b.OHLC().Close.Float64() + if e.count < e.window { + e.count++ + e.sum += val + if e.count < e.window { + return 0, false + } + e.value = e.sum / float64(e.window) + return e.value, true + } + e.value += e.alpha * (val - e.value) + return e.value, true +} diff --git a/examples/emax/emax/emax_test.go b/examples/emax/emax/emax_test.go new file mode 100644 index 0000000..18101e2 --- /dev/null +++ b/examples/emax/emax/emax_test.go @@ -0,0 +1,158 @@ +package emax + +import ( + "go/parser" + "go/token" + "path/filepath" + "strconv" + "strings" + "testing" + + "github.com/rustyeddy/trader/marketdata" + "github.com/rustyeddy/trader/num" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// The example is written against the public SDK alone: it must import nothing from +// Trader's private packages, or an outside worker could not be written the way it +// is. (The root package's parity test imports this one, not the other way round.) +func TestPackage_UsesOnlyThePublicSDK(t *testing.T) { + const mod = "github.com/rustyeddy/trader/" + public := map[string]bool{mod + "strategy": true, mod + "marketdata": true, mod + "num": true} + files, err := filepath.Glob("*.go") + require.NoError(t, err) + for _, name := range files { + if strings.HasSuffix(name, "_test.go") { + continue + } + f, err := parser.ParseFile(token.NewFileSet(), name, nil, parser.ImportsOnly) + require.NoError(t, err) + for _, imp := range f.Imports { + path, err := strconv.Unquote(imp.Path.Value) + require.NoError(t, err) + if strings.HasPrefix(path, mod) { + assert.True(t, public[path], "%s imports %s", name, path) + } + } + } +} + +func price(s string) num.Price { return num.MustParsePrice(s) } + +func cfg() Config { + return Config{Short: 2, Long: 4, StopPct: num.MustParseRate("0.01"), Tick: price("0.00001")} +} + +func bar(instrument, close string) marketdata.Bar { + c := price(close) + return marketdata.Bar{Instrument: instrument, Mid: marketdata.OHLC{Open: c, High: c, Low: c, Close: c}} +} + +func TestConfig_Validate(t *testing.T) { + require.NoError(t, cfg().Validate()) + tests := []struct { + name string + edit func(*Config) + want string + }{ + {"short window", func(c *Config) { c.Short = 0 }, "windows must be > 0"}, + {"long window", func(c *Config) { c.Long = -1 }, "windows must be > 0"}, + {"short not below long", func(c *Config) { c.Short = 4 }, "must be less than"}, + {"no stop", func(c *Config) { c.StopPct = num.Rate{} }, "stop must be between"}, + {"stop of the whole price", func(c *Config) { c.StopPct = num.MustParseRate("1") }, "stop must be between"}, + {"no tick", func(c *Config) { c.Tick = num.Price{} }, "no price tick"}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + c := cfg() + tt.edit(&c) + assert.ErrorContains(t, c.Validate(), tt.want) + _, err := New(c) + assert.Error(t, err) + }) + } +} + +func TestEMA(t *testing.T) { + e := NewEMA(3) + _, ok := e.OnBar(bar("X", "3")) + assert.False(t, ok, "not seeded until a full window") + _, ok = e.OnBar(bar("X", "6")) + assert.False(t, ok) + v, ok := e.OnBar(bar("X", "9")) + require.True(t, ok) + assert.Equal(t, 6.0, v, "seeded with the simple average of the first window") + v, _ = e.OnBar(bar("X", "10")) + assert.Equal(t, 8.0, v, "then 2/(window+1) of the way to each close: 6 + 0.5*(10-6)") + + assert.Panics(t, func() { NewEMA(0) }) +} + +func TestOnBars_FlipsByAClosingThenAnOpeningBar(t *testing.T) { + s, err := New(cfg()) + require.NoError(t, err) + step := func(close string) (actions []string) { + orders, err := s.OnBars([]marketdata.Bar{bar("EURUSD", close)}, nil) + require.NoError(t, err) + for _, o := range orders { + actions = append(actions, o.Action+" "+o.Direction) + } + return actions + } + + for _, c := range []string{"1.1", "1.1", "1.1"} { + assert.Empty(t, step(c), "the long EMA is not seeded yet") + } + assert.Equal(t, []string{"open long"}, step("1.2"), "short EMA above long: open long") + assert.Empty(t, step("1.3"), "still long: nothing to say") + assert.Equal(t, []string{"close "}, step("1.0"), "the trend turned: a close, with no direction") + assert.Equal(t, []string{"open short"}, step("0.9"), "and the opposite open on the next bar") +} + +func TestOnBars_AnOpenCarriesTheEntryAndAStopAWholeNumberOfTicksAway(t *testing.T) { + s, err := New(cfg()) + require.NoError(t, err) + var open []string + var stops []num.Price + for _, c := range []string{"1.1", "1.1", "1.1", "1.2"} { + orders, err := s.OnBars([]marketdata.Bar{bar("EURUSD", c)}, nil) + require.NoError(t, err) + for _, o := range orders { + open = append(open, o.Price.String()) + stops = append(stops, o.StopLoss) + } + } + require.Equal(t, []string{"1.2"}, open, "reference price is the close") + assert.Equal(t, price("1.188"), stops[0], "1% of 1.2 is 0.012, below a long") +} + +func TestOnBars_EachInstrumentHasItsOwnState(t *testing.T) { + s, err := New(cfg()) + require.NoError(t, err) + // EURUSD trends up and GBPUSD stays flat, in the same groups. + var got []string + for _, c := range []string{"1.1", "1.1", "1.1", "1.2"} { + orders, err := s.OnBars([]marketdata.Bar{bar("EURUSD", c), bar("GBPUSD", "1.3")}, nil) + require.NoError(t, err) + for _, o := range orders { + got = append(got, o.Instrument+" "+o.Action) + } + } + assert.Equal(t, []string{"EURUSD open"}, got) +} + +func TestOnBars_AShortWhoseStopWouldExceedTheLargestPriceIsSkippedAndNotRetried(t *testing.T) { + s, err := New(cfg()) + require.NoError(t, err) + var asked []string + // Falling closes near the ceiling: the short entry at 9950 has a stop 1% above, over 10000. + for _, c := range []string{"9990", "9990", "9990", "9950", "9940"} { + orders, err := s.OnBars([]marketdata.Bar{bar("X", c)}, nil) + require.NoError(t, err) + for _, o := range orders { + asked = append(asked, o.Action+" "+o.Direction) + } + } + assert.Empty(t, asked, "the open is skipped, and the signal is consumed, so it is not asked again on the next bar") +} diff --git a/examples/emax/main.go b/examples/emax/main.go new file mode 100644 index 0000000..06be925 --- /dev/null +++ b/examples/emax/main.go @@ -0,0 +1,55 @@ +// Command emax is Trader's canonical external-strategy example: the EMAX strategy +// as a separate process that Trader backtests over loopback TCP and JSON, using +// only the public Go SDK. See README.md. +// +// go run ./examples/emax -addr 127.0.0.1:9100 +// trader backtest --worker 127.0.0.1:9100 --instrument EURUSD --interval h1 \ +// --from 2024-01-01 --to 2024-04-01 +package main + +import ( + "flag" + "fmt" + "log" + "net" + "os" + + "github.com/rustyeddy/trader/examples/emax/emax" + "github.com/rustyeddy/trader/marketdata" + "github.com/rustyeddy/trader/num" + "github.com/rustyeddy/trader/strategy" +) + +func main() { + addr := flag.String("addr", "127.0.0.1:0", "loopback address to listen on") + short := flag.Int("short", 2, "short EMA window") + long := flag.Int("long", 4, "long EMA window") + stop := flag.String("stop", "0.01", "stop distance from the entry, as a fraction of the price") + tick := flag.String("tick", "0.00001", "price tick of the instruments traded (EURUSD and GBPUSD: 0.00001)") + flag.Parse() + + cfg := emax.Config{Short: *short, Long: *long} + var err error + if cfg.StopPct, err = num.ParseRate(*stop); err != nil { + log.Fatalf("-stop: %v", err) + } + if cfg.Tick, err = num.ParsePrice(*tick); err != nil { + log.Fatalf("-tick: %v", err) + } + s, err := emax.New(cfg) + if err != nil { + log.Fatal(err) + } + + groups := 0 + err = strategy.ListenAndServe(*addr, + func(a net.Addr) { fmt.Println("listening on", a) }, // for whoever started us + func(bars []marketdata.Bar, events []strategy.Event) ([]strategy.Order, error) { + groups++ + return s.OnBars(bars, events) + }) + if err != nil { + log.Fatal(err) + } + fmt.Fprintf(os.Stderr, "done: %d groups\n", groups) +} diff --git a/historical.go b/historical.go index f842f06..3eb04b5 100644 --- a/historical.go +++ b/historical.go @@ -60,7 +60,7 @@ type Position struct { func (r *Result) Summary() Summary { x := r.r s := Summary{ - Strategy: runStrategy, Account: runAccount, InitialBalance: num.MustParseMoney(runBalance, usdCurrency), + Strategy: x.strategy, Account: runAccount, InitialBalance: num.MustParseMoney(runBalance, usdCurrency), Bars: x.bars, Orders: OrderCount{Submitted: x.submitted, Filled: x.filled, Rejected: x.rejected, Cancelled: x.cancelled, Pending: x.pending}, Trades: x.trades, Wins: x.wins, Losses: x.losses, BreakEven: x.breakEven, @@ -82,10 +82,12 @@ const ( runLong = 4 runBalance = "10000" // USD - // StrategyEMAX names the strategy RunSeries trades. - StrategyEMAX = "emax" - runStrategy = StrategyEMAX - runAccount = "sim123" + // StrategyEMAX names the strategy RunSeries trades unless RunOptions names a + // worker, which is StrategyRemote. + StrategyEMAX = "emax" + StrategyRemote = "remote" + runStrategy = StrategyEMAX + runAccount = "sim123" ) // RunOptions are the optional parts of a run. @@ -103,6 +105,14 @@ type RunOptions struct { // file; the caller owns the writer. If a write fails the run stops with that // error, and what was written before it stays. Journal io.Writer + + // Worker, when not empty, is the loopback host:port of a worker process that + // trades instead of the built-in EMAX: Trader connects to it and, for every group + // of bars, asks what it wants (package strategy). Everything else is the same run: + // the same engine loop, account sizing, broker and journal. A worker is not warmed + // up: the bars before tradeFrom are not shown to it, so it starts cold at + // tradeFrom, as a strategy that does not implement warmer always does. + Worker string } // RunSeries backtests the series, which a reader has already loaded, on one @@ -169,10 +179,21 @@ func RunSeriesWith(ctx context.Context, opts RunOptions, name string, tradeFrom } slices.Sort(instruments) - strat := &emaxEach{id: runStrategy, instruments: instruments, short: runShort, long: runLong, - stopPct: rateOnePercent, markets: markets} - if err := strat.init(); err != nil { - return nil, err + var strat strategy + if opts.Worker != "" { + remote, err := dialRemote(StrategyRemote, opts.Worker) + if err != nil { + return nil, err + } + defer func() { _ = remote.close() }() // the worker sees the connection close and stops + strat = remote + } else { + emax := &emaxEach{id: runStrategy, instruments: instruments, short: runShort, long: runLong, + stopPct: rateOnePercent, markets: markets} + if err := emax.init(); err != nil { + return nil, err + } + strat = emax } br := newBroker("sim", markets) if opts.Journal != nil { diff --git a/internal/results/run.go b/internal/results/run.go index 37049e0..9f80931 100644 --- a/internal/results/run.go +++ b/internal/results/run.go @@ -48,6 +48,7 @@ type Record struct { Strategy struct { Name string `json:"name"` Transport string `json:"transport"` + Addr string `json:"addr,omitempty"` // the worker's address, for a tcp strategy } `json:"strategy"` Source string `json:"source"` Interval string `json:"interval"` diff --git a/service/backtest.go b/service/backtest.go index 5e8df99..b455a4e 100644 --- a/service/backtest.go +++ b/service/backtest.go @@ -38,6 +38,11 @@ type BacktestRequest struct { AllowBad bool AllowMissing bool + // Worker, when not empty, is the loopback host:port of a worker process that + // trades instead of the built-in EMAX (see trader.RunOptions.Worker). It is not + // warmed up, so the months before From are not loaded for it. + Worker string + // ResultsRoot is the directory the run's results go under (results.DefaultRoot // unless the transport was told otherwise). It is required: every run is // journaled, so there is no way to run without one. @@ -92,6 +97,9 @@ func RunBacktest(ctx context.Context, log *slog.Logger, req BacktestRequest) (*B Instruments: req.Instruments, From: req.From.UTC(), To: req.To.UTC(), StartedAt: clock.Clock(), } rec.Strategy.Name, rec.Strategy.Transport = trader.StrategyEMAX, "in-process" + if req.Worker != "" { + rec.Strategy.Name, rec.Strategy.Transport, rec.Strategy.Addr = trader.StrategyRemote, "tcp", req.Worker + } run, err := results.Start(req.ResultsRoot, rec) if err != nil { return nil, fmt.Errorf("backtest: %w", err) @@ -135,9 +143,12 @@ func runBacktest(ctx context.Context, log *slog.Logger, req BacktestRequest, sou if bars == 0 { return nil, fmt.Errorf("backtest %s: no bars in the range", what) } - warm, err := warmupBars(ctx, log, req) - if err != nil { - return nil, err + var warm map[string][]marketdata.Bar + if req.Worker == "" { // a worker is not warmed up: the engine would not show it these bars + warm, err = warmupBars(ctx, log, req) + if err != nil { + return nil, err + } } series := make([]marketdata.Series, 0, len(results)) for _, r := range results { @@ -147,7 +158,7 @@ func runBacktest(ctx context.Context, log *slog.Logger, req BacktestRequest, sou } series = append(series, s) } - res, err := trader.RunSeriesWith(ctx, trader.RunOptions{Journal: journal}, what, req.From, series...) + res, err := trader.RunSeriesWith(ctx, trader.RunOptions{Journal: journal, Worker: req.Worker}, what, req.From, series...) if err != nil { return nil, fmt.Errorf("backtest %s: %w", what, err) } diff --git a/service/backtest_test.go b/service/backtest_test.go index 9ecd8d7..dd42b6c 100644 --- a/service/backtest_test.go +++ b/service/backtest_test.go @@ -5,17 +5,23 @@ import ( "bytes" "context" "encoding/json" + "errors" "fmt" + "math/rand/v2" + "net" "os" "path/filepath" "testing" "time" + "github.com/rustyeddy/trader/examples/emax/emax" "github.com/rustyeddy/trader/internal/id" "github.com/rustyeddy/trader/internal/results" "github.com/rustyeddy/trader/internal/store/storetest" + "github.com/rustyeddy/trader/marketdata" "github.com/rustyeddy/trader/num" "github.com/rustyeddy/trader/provider/oanda" + "github.com/rustyeddy/trader/strategy" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -518,3 +524,137 @@ func TestRunBacktest_EveryRunGetsItsOwnDirectory(t *testing.T) { a, b := readRunDir(t, first.Dir), readRunDir(t, second.Dir) assert.Equal(t, a.events, b.events, "the same run writes the same journal; only run.json carries a run's own identity") } + +// walkPoints is a seeded walk in ticks above a base, hour by hour, for a month of +// quotes with many crossovers. +func walkPoints(seed uint64, hours int) []int64 { + rng := rand.New(rand.NewPCG(seed, seed*7+1)) + points := make([]int64, hours) + var p int64 + for i := range points { + p += rng.Int64N(301) - 150 + points[i] = p + } + return points +} + +// serveEMAX serves examples/emax in this process over loopback TCP, through the +// public SDK, and returns its address. If failAfter is positive the worker gives up +// on that group, as a crashing process would. It reports how the worker ended. +func serveEMAX(t *testing.T, failAfter int) (addr string, ended func() error) { + t.Helper() + s, err := emax.New(emax.Config{Short: 2, Long: 4, StopPct: num.MustParseRate("0.01"), Tick: num.MustParsePrice("0.00001")}) + require.NoError(t, err) + addrs := make(chan string, 1) + done := make(chan error, 1) + groups := 0 + go func() { + done <- strategy.ListenAndServe("127.0.0.1:0", func(a net.Addr) { addrs <- a.String() }, + func(bars []marketdata.Bar, events []strategy.Event) ([]strategy.Order, error) { + groups++ + if failAfter > 0 && groups > failAfter { + return nil, errors.New("worker crashed") + } + return s.OnBars(bars, events) + }) + }() + return <-addrs, func() error { return <-done } +} + +// A real month, built into canonical bars through the store, then run with the +// built-in EMAX and with the EMAX example as a worker, from the same data and +// settings, journaled each. +func TestRunBacktest_RemoteEMAXMatchesTheBuiltInEMAX(t *testing.T) { + storetest.Root(t) + // An odd spread gives half-tick mids, which the engine rounds: the parity holds + // through that too. + monthSpread(t, "EURUSD", time.January, 110000, 3, walkPoints(1, 600)...) + monthSpread(t, "GBPUSD", time.January, 125000, 3, walkPoints(2, 600)...) + buildCanonical(t, janToMar("EURUSD", "GBPUSD")) + builtinReq := backtestReq(t, "EURUSD", "GBPUSD") + builtin, err := RunBacktest(context.Background(), nil, builtinReq) + require.NoError(t, err) + + addr, ended := serveEMAX(t, 0) + remoteReq := backtestReq(t, "EURUSD", "GBPUSD") + remoteReq.Worker = addr + remote, err := RunBacktest(context.Background(), nil, remoteReq) + require.NoError(t, err) + require.NoError(t, ended(), "the worker stops cleanly when the run closes the connection") + + b, r := readRunDir(t, builtin.Dir), readRunDir(t, remote.Dir) + + // What names the run or its strategy, or reads the wall clock, may differ. Nothing else. + assert.Equal(t, map[string]any{"name": "emax", "transport": "in-process"}, b.rec["strategy"]) + assert.Equal(t, map[string]any{"name": "remote", "transport": "tcp", "addr": addr}, r.rec["strategy"]) + for _, rec := range []map[string]any{b.rec, r.rec} { + delete(rec, "run_id") + delete(rec, "strategy") + delete(rec, "started_at") + delete(rec, "ended_at") + delete(rec["result"].(map[string]any), "strategy") + } + assert.Equal(t, b.rec, r.rec, "status, instruments, range and the engine's result: orders, trades, balance, equity, open positions") + assert.Equal(t, "completed", b.rec["status"]) + assert.Equal(t, b.events, r.events, "every order, fill and protective exit, in order, to the price") + require.Greater(t, len(b.events), 100, "a real comparison, not an empty one") + assert.Positive(t, b.rec["result"].(map[string]any)["trades"]) +} + +func TestRunBacktest_ARemoteRunThatLosesItsWorkerKeepsThePartialJournal(t *testing.T) { + storetest.Root(t) + monthSpread(t, "EURUSD", time.January, 110000, 3, walkPoints(1, 600)...) + buildCanonical(t, janToMar("EURUSD")) + full, err := RunBacktest(context.Background(), nil, backtestReq(t, "EURUSD")) + require.NoError(t, err) + + addr, ended := serveEMAX(t, 250) // dies on its 251st group + req := backtestReq(t, "EURUSD") + req.Worker = addr + _, err = RunBacktest(context.Background(), nil, req) + require.Error(t, err) + require.Error(t, ended()) + + assert.ErrorContains(t, err, "strategy remote: reading orders", "the failure says it was the worker") + run := readRunDir(t, onlyRun(t, req.ResultsRoot)) + assert.Equal(t, "failed", run.rec["status"]) + assert.Contains(t, run.rec["error"], "strategy remote: reading orders") + assert.NotContains(t, run.rec, "result") + assert.Equal(t, map[string]any{"name": "remote", "transport": "tcp", "addr": addr}, run.rec["strategy"]) + + whole := readRunDir(t, full.Dir).events + require.NotEmpty(t, run.events, "what was recorded before the worker died is kept") + require.Less(t, len(run.events), len(whole), "and the run did not get to the end") + assert.Equal(t, whole[:len(run.events)], run.events, "the partial journal is the start of the full one: nothing lost, nothing invented") +} + +func TestRunBacktest_ARemoteRunWithNoWorkerListeningIsAFailedRun(t *testing.T) { + storetest.Root(t) + monthSpread(t, "EURUSD", time.January, 110000, 3, walkPoints(1, 100)...) + buildCanonical(t, janToMar("EURUSD")) + ln, err := net.Listen("tcp", "127.0.0.1:0") + require.NoError(t, err) + addr := ln.Addr().String() + require.NoError(t, ln.Close()) + req := backtestReq(t, "EURUSD") + req.Worker = addr + + _, err = RunBacktest(context.Background(), nil, req) + + require.Error(t, err) + run := readRunDir(t, onlyRun(t, req.ResultsRoot)) + assert.Equal(t, "failed", run.rec["status"]) + assert.Empty(t, run.events) +} + +func TestRunBacktest_AWorkerMustBeOnLoopback(t *testing.T) { + storetest.Root(t) + monthSpread(t, "EURUSD", time.January, 110000, 3, walkPoints(1, 100)...) + buildCanonical(t, janToMar("EURUSD")) + req := backtestReq(t, "EURUSD") + req.Worker = "8.8.8.8:9100" + + _, err := RunBacktest(context.Background(), nil, req) + + assert.ErrorContains(t, err, "not loopback") +} diff --git a/trader_test.go b/trader_test.go index 28b185d..084b8fe 100644 --- a/trader_test.go +++ b/trader_test.go @@ -7,6 +7,7 @@ import ( "encoding/json" "errors" "fmt" + "math/rand/v2" "net" "os/exec" "path/filepath" @@ -14,7 +15,9 @@ import ( "strings" "sync" "testing" + "time" + emaxworker "github.com/rustyeddy/trader/examples/emax/emax" "github.com/rustyeddy/trader/marketdata" "github.com/rustyeddy/trader/num" remotestrat "github.com/rustyeddy/trader/strategy" @@ -659,3 +662,160 @@ func TestRemoteStrategy_BadOrdersFailTheRunAndProcessNone(t *testing.T) { }) } } + +// emaxConfig is the built-in EMAX's settings (historical.go: windows 2 and 4, a 1% +// stop) and the tick of the instruments the scenarios trade, for the worker. +func emaxConfig() emaxworker.Config { + return emaxworker.Config{Short: runShort, Long: runLong, StopPct: rateOnePercent, Tick: px(1)} +} + +// serveEMAX serves examples/emax in this process over loopback TCP, through the +// public SDK only, and returns its address. The worker stops when the trader hangs +// up, which the run does when it ends; the test waits for that. +func serveEMAX(t *testing.T, cfg emaxworker.Config) string { + t.Helper() + s, err := emaxworker.New(cfg) + require.NoError(t, err) + + addrs := make(chan string, 1) + done := make(chan error, 1) + go func() { + done <- remotestrat.ListenAndServe("127.0.0.1:0", + func(a net.Addr) { addrs <- a.String() }, + func(bars []marketdata.Bar, events []remotestrat.Event) ([]remotestrat.Order, error) { + return s.OnBars(bars, events) + }) + }() + addr := <-addrs + t.Cleanup(func() { require.NoError(t, <-done, "the worker ends cleanly when the run closes the connection") }) + return addr +} + +// parityRun is one run, journaled. +type parityRun struct { + journal string + summary Summary + text string // Result.Write +} + +func runParity(t *testing.T, opts RunOptions, series ...marketdata.Series) parityRun { + t.Helper() + var buf bytes.Buffer + opts.Journal = &buf + res, err := RunSeriesWith(context.Background(), opts, "parity", time.Time{}, series...) + require.NoError(t, err) + var text bytes.Buffer + require.NoError(t, res.Write(&text)) + return parityRun{journal: buf.String(), summary: res.Summary(), text: text.String()} +} + +// assertParity says the remote run is the built-in run: the same orders in the same +// order, the same sizes and fills and exits, the same final positions and account. +// Only what names the strategy may differ. +func assertParity(t *testing.T, builtin, remote parityRun) { + t.Helper() + require.NotEmpty(t, builtin.journal) + assert.Equal(t, builtin.journal, remote.journal, + "every order transition, fill and protective exit, in the same order and to the same price") + b, r := builtin.summary, remote.summary + assert.Equal(t, StrategyEMAX, b.Strategy) + assert.Equal(t, StrategyRemote, r.Strategy) + b.Strategy, r.Strategy = "", "" + assert.Equal(t, b, r, "orders, trades, balance, equity and open positions") + assert.Equal(t, builtin.text, remote.text, "the summary a person reads") +} + +func TestEMAXParity_RemoteMatchesBuiltInOnTheScenarios(t *testing.T) { + for _, scenario := range scenarios { + t.Run(scenario, func(t *testing.T) { + captureLogs(t) + prices := scenarioPrices(t, scenario) + + builtin := runParity(t, RunOptions{}, prices) + remote := runParity(t, RunOptions{Worker: serveEMAX(t, emaxConfig())}, prices) + + assertParity(t, builtin, remote) + }) + } +} + +// randomWalk is hourly mid bars for an instrument: a seeded walk in ticks, so a +// test gets many crossovers, stops and flips from data that is the same every time. +func randomWalk(instrument string, start int64, bars int, seed uint64) marketdata.Series { + rng := rand.New(rand.NewPCG(seed, seed*7+1)) + t0 := time.Date(2024, 1, 2, 0, 0, 0, 0, time.UTC) + s := marketdata.Series{Source: "oanda", Instrument: instrument, Interval: "h1"} + prev := start + for i := range bars { + closeP := prev + rng.Int64N(401) - 200 // up to 20 pips a bar + hi := max(prev, closeP) + rng.Int64N(60) + lo := min(prev, closeP) - rng.Int64N(60) + at := t0.Add(time.Duration(i) * time.Hour) + s.Bars = append(s.Bars, marketdata.Bar{ + Instrument: instrument, Time: at, AvailableAt: at.Add(time.Hour), Volume: 100, + Mid: marketdata.OHLC{Open: px(prev), High: px(hi), Low: px(lo), Close: px(closeP)}, + }) + prev = closeP + } + return s +} + +func TestEMAXParity_RemoteMatchesBuiltInOnALongTwoInstrumentWalk(t *testing.T) { + captureLogs(t) + eur, gbp := randomWalk("EURUSD", 110000, 1500, 1), randomWalk("GBPUSD", 125000, 1500, 2) + + builtin := runParity(t, RunOptions{}, eur, gbp) + remote := runParity(t, RunOptions{Worker: serveEMAX(t, emaxConfig())}, eur, gbp) + + assertParity(t, builtin, remote) + // The walk is not a vacuous comparison: it opens, flips and stops out. + assert.Greater(t, builtin.summary.Orders.Filled, 100) + assert.Greater(t, builtin.summary.Trades, 20) + assert.Contains(t, builtin.journal, `"cause":"stop loss"`) + assert.Contains(t, builtin.journal, `"direction":"short"`) + assert.Positive(t, builtin.summary.Orders.Cancelled, "a close cancels the position's stop") +} + +// The comparison can fail: a worker that differs in one setting is not the built-in +// strategy, and the journals say so. +func TestEMAXParity_ADifferentWorkerIsCaught(t *testing.T) { + captureLogs(t) + walk := randomWalk("EURUSD", 110000, 400, 6) + wider := emaxConfig() + wider.StopPct = num.MustParseRate("0.02") + + builtin := runParity(t, RunOptions{}, walk) + other := runParity(t, RunOptions{Worker: serveEMAX(t, wider)}, walk) + + assert.NotEqual(t, builtin.journal, other.journal) +} + +func TestRunSeriesWith_AWorkerIsNotShownTheBarsBeforeTradeFrom(t *testing.T) { + captureLogs(t) + walk := randomWalk("EURUSD", 110000, 300, 3) + from := walk.Bars[100].Time + w := startWorker(t, workerOpts{}) + + res, err := RunSeriesWith(context.Background(), RunOptions{Worker: w.addr}, "x", from, walk) + + require.NoError(t, err) + groups := w.finish() + require.Len(t, groups, 200, "one group for each bar from tradeFrom on") + assert.Equal(t, from, groups[0][0].Time, "the warm-up bars are not sent: a worker starts cold at tradeFrom") + assert.Equal(t, 200, res.Summary().Bars) +} + +// The worker's EMA is a copy of the built-in one (a worker cannot import it), so the +// two must give the same number for the same closes, to the last bit. +func TestEMAXParity_TheWorkersEMAIsTheBuiltInEMA(t *testing.T) { + walk := randomWalk("EURUSD", 110000, 400, 4) + for _, window := range []int{1, 2, 4, 10, 50, 200} { + builtin, worker := newEMA(window), emaxworker.NewEMA(window) + for i, b := range walk.Bars { + bv, bok := builtin.onBar(b) + wv, wok := worker.OnBar(b) + require.Equal(t, bok, wok, "window %d bar %d: seeded at the same bar", window, i) + require.Equal(t, bv, wv, "window %d bar %d", window, i) + } + } +}