diff --git a/examples/remote-strategy/main.go b/examples/remote-strategy/main.go index 9aa4399..bd880ee 100644 --- a/examples/remote-strategy/main.go +++ b/examples/remote-strategy/main.go @@ -1,6 +1,9 @@ -// Command remote-strategy is a minimal external strategy for Trader: it listens -// on a loopback port, takes each group of bars Trader sends and always answers -// with no orders. +// Command remote-strategy is a tiny external strategy for Trader. It listens on a +// loopback port and, for every group of bars Trader sends, applies one fixed +// rule: open a long position in the first bar's instrument on the third group, +// with a stop a fixed distance below the close, and close it on the sixth. It +// asks only; Trader sizes the order, applies the account's risk rules and fills +// it. // // Run it, then point a Trader backtest at the address it prints: // @@ -18,25 +21,54 @@ import ( "os" "github.com/rustyeddy/trader/marketdata" + "github.com/rustyeddy/trader/num" "github.com/rustyeddy/trader/strategy" ) +const ( + openAt = 3 // open on this group... + closeAt = 6 // ...and close on this one +) + +// stopDistance is how far below the entry the stop goes. +var stopDistance = num.MustParsePrice("0.015") + func main() { addr := flag.String("addr", "127.0.0.1:0", "loopback address to listen on") flag.Parse() groups := 0 err := strategy.ListenAndServe(*addr, - func(a net.Addr) { - fmt.Println("listening on", a) - }, // for whoever started us - func([]marketdata.Bar) error { - // A real strategy decides here. This one only counts. + func(a net.Addr) { fmt.Println("listening on", a) }, // for whoever started us + func(bars []marketdata.Bar) ([]strategy.Order, error) { groups++ - return nil + return decide(groups, bars) }) if err != nil { log.Fatal(err) } fmt.Fprintf(os.Stderr, "done: %d groups\n", groups) } + +// decide is the whole strategy: the orders to request on the n-th group of bars. +func decide(n int, bars []marketdata.Bar) ([]strategy.Order, error) { + if len(bars) == 0 { + return nil, nil + } + b := bars[0] + closePrice := b.OHLC().Close + switch n { + case openAt: + stop, err := closePrice.Sub(stopDistance) + if err != nil { + return nil, err + } + return []strategy.Order{{ + Instrument: b.Instrument, Action: "open", Direction: "long", + Price: closePrice, StopLoss: stop, + }}, nil + case closeAt: + return []strategy.Order{{Instrument: b.Instrument, Action: "close", Price: closePrice}}, nil + } + return nil, nil +} diff --git a/strategy/client.go b/strategy/client.go index ad1c639..bc19d0b 100644 --- a/strategy/client.go +++ b/strategy/client.go @@ -38,11 +38,11 @@ func Dial(addr string) (*Client, error) { // Close ends the session; the worker sees the connection close and stops. func (r *Client) Close() error { return r.conn.Close() } -// OnBars sends one group of bars and waits for the answer. It returns an error if -// the worker does not answer within the timeout or ctx ends first, if the -// connection fails, or if the answer holds any orders. After an error the -// connection should be closed. -func (r *Client) OnBars(ctx context.Context, bars []marketdata.Bar) error { +// OnBars sends one group of bars and waits for the answer: the orders the worker +// requests, in order, possibly none. It returns an error if the worker does not +// answer within the timeout or ctx ends first, if the connection fails, or if the +// answer is not a valid message. After an error the connection should be closed. +func (r *Client) OnBars(ctx context.Context, bars []marketdata.Bar) ([]Order, error) { timeout := r.Timeout if timeout == 0 { timeout = DefaultTimeout @@ -52,38 +52,38 @@ func (r *Client) OnBars(ctx context.Context, bars []marketdata.Bar) error { deadline = d } if err := r.conn.SetDeadline(deadline); err != nil { - return err + return nil, err } // Cancelling ctx expires the deadline at once, which wakes a blocked read. stop := context.AfterFunc(ctx, func() { _ = r.conn.SetDeadline(time.Unix(1, 0)) }) defer stop() - err := r.exchange(bars) + orders, err := r.exchange(bars) switch { case err == nil: - return nil + return orders, nil case ctx.Err() != nil: - return ctx.Err() + return nil, ctx.Err() case errors.Is(err, os.ErrDeadlineExceeded): // The socket's timer can fire a moment before ctx notices its own deadline. if d, ok := ctx.Deadline(); ok && !time.Now().Before(d) { - return context.DeadlineExceeded + return nil, context.DeadlineExceeded } - return fmt.Errorf("worker did not answer within %s: %w", timeout, err) + return nil, fmt.Errorf("worker did not answer within %s: %w", timeout, err) } - return err + return nil, err } -func (r *Client) exchange(bars []marketdata.Bar) error { +func (r *Client) exchange(bars []marketdata.Bar) ([]Order, error) { if err := writeMessage(r.conn, barsMessage{Bars: bars}); err != nil { - return fmt.Errorf("sending bars: %w", err) + return nil, fmt.Errorf("sending bars: %w", err) } var reply ordersMessage if err := readMessage(r.conn, &reply); err != nil { - return fmt.Errorf("reading orders: %w", err) + return nil, fmt.Errorf("reading orders: %w", err) } - if len(reply.Orders) > 0 { - return fmt.Errorf("worker returned %d orders; orders over the wire are not supported yet", len(reply.Orders)) + if reply.Orders == nil { + return nil, errors.New("answer has no orders list; send an empty one, {\"orders\":[]}") } - return nil + return reply.Orders, nil } diff --git a/strategy/client_test.go b/strategy/client_test.go index 126f2ab..0eab3d7 100644 --- a/strategy/client_test.go +++ b/strategy/client_test.go @@ -2,12 +2,13 @@ package strategy import ( "context" - "encoding/json" + "encoding/binary" "net" "testing" "time" "github.com/rustyeddy/trader/marketdata" + "github.com/rustyeddy/trader/num" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -49,35 +50,76 @@ func TestDial_NothingListening(t *testing.T) { assert.Error(t, err) } +// writeRaw sends body as one message, bypassing the types, so a test can send what +// a worker must not. +func writeRaw(c net.Conn, body string) { + _, _ = c.Write(append(binary.BigEndian.AppendUint32(nil, uint32(len(body))), body...)) +} + +// answerWith is a worker that answers every group with the given raw body. +func answerWith(t *testing.T, body string) *Client { + t.Helper() + addr := rawWorker(t, func(c net.Conn) { + for { + var in barsMessage + if readMessage(c, &in) != nil { + return + } + writeRaw(c, body) + } + }) + r, err := Dial(addr) + require.NoError(t, err) + t.Cleanup(func() { _ = r.Close() }) + return r +} + func TestClient_OnBars(t *testing.T) { ctx := context.Background() - t.Run("an empty answer is success", func(t *testing.T) { - addr := rawWorker(t, func(c net.Conn) { - for { - var in barsMessage - if readMessage(c, &in) != nil { - return - } - _ = writeMessage(c, ordersMessage{}) - } - }) - r, err := Dial(addr) - require.NoError(t, err) - defer func() { _ = r.Close() }() - assert.NoError(t, r.OnBars(ctx, oneBar)) - assert.NoError(t, r.OnBars(ctx, oneBar)) + t.Run("an empty answer is no orders", func(t *testing.T) { + r := answerWith(t, `{"orders":[]}`) + for range 2 { + orders, err := r.OnBars(ctx, oneBar) + require.NoError(t, err) + assert.Empty(t, orders) + } }) - t.Run("orders in the answer are an error", func(t *testing.T) { - addr := rawWorker(t, func(c net.Conn) { - var in barsMessage - _ = readMessage(c, &in) - _ = writeMessage(c, ordersMessage{Orders: []json.RawMessage{json.RawMessage(`{}`)}}) - }) - r, err := Dial(addr) + t.Run("requested orders come back in order, prices exact", func(t *testing.T) { + r := answerWith(t, `{"orders":[ + {"instrument":"EURUSD","action":"open","direction":"long","type":"limit","price":"1.10000","stop_loss":"1.09000","take_profit":"1.12000"}, + {"instrument":"GBPUSD","action":"close"}]}`) + orders, err := r.OnBars(ctx, oneBar) require.NoError(t, err) - defer func() { _ = r.Close() }() - assert.ErrorContains(t, r.OnBars(ctx, oneBar), "not supported") + require.Len(t, orders, 2) + assert.Equal(t, Order{ + Instrument: "EURUSD", Action: "open", Direction: "long", Type: "limit", + Price: num.MustParsePrice("1.1"), StopLoss: num.MustParsePrice("1.09"), TakeProfit: num.MustParsePrice("1.12"), + }, orders[0]) + assert.Equal(t, Order{Instrument: "GBPUSD", Action: "close"}, orders[1]) + }) + t.Run("a field the worker may not send is an error", func(t *testing.T) { + for _, field := range []string{`"units":"1000"`, `"id":"x"`, `"state":"filled"`, `"account":"a"`, `"fill_price":"1.1"`, `"nonsense":1`} { + r := answerWith(t, `{"orders":[{"instrument":"EURUSD","action":"close",`+field+`}]}`) + _, err := r.OnBars(ctx, oneBar) + assert.ErrorContains(t, err, "unknown field", field) + } + }) + t.Run("an answer without an orders list is an error", func(t *testing.T) { + for _, body := range []string{`{}`, `{"orders":null}`} { + r := answerWith(t, body) + orders, err := r.OnBars(ctx, oneBar) + assert.ErrorContains(t, err, "no orders list", body) + assert.Empty(t, orders) + } + }) + t.Run("a malformed answer is an error", func(t *testing.T) { + for _, body := range []string{`nope`, `{"orders":[`, `{"orders":{}}`, `{"orders":[{"price":1.1}]}`, `{"orders":[]} x`, `{"orders":[{"price":"abc"}]}`} { + r := answerWith(t, body) + orders, err := r.OnBars(ctx, oneBar) + assert.Error(t, err, body) + assert.Empty(t, orders) + } }) t.Run("a worker that hangs up is an error", func(t *testing.T) { addr := rawWorker(t, func(c net.Conn) { @@ -87,7 +129,8 @@ func TestClient_OnBars(t *testing.T) { r, err := Dial(addr) require.NoError(t, err) defer func() { _ = r.Close() }() - assert.ErrorContains(t, r.OnBars(ctx, oneBar), "reading orders") + _, err = r.OnBars(ctx, oneBar) + assert.ErrorContains(t, err, "reading orders") }) t.Run("a silent worker times out", func(t *testing.T) { release := make(chan struct{}) @@ -103,7 +146,7 @@ func TestClient_OnBars(t *testing.T) { r.Timeout = 50 * time.Millisecond start := time.Now() - err = r.OnBars(ctx, oneBar) + _, err = r.OnBars(ctx, oneBar) assert.ErrorContains(t, err, "did not answer within") assert.Less(t, time.Since(start), 2*time.Second) }) @@ -122,7 +165,8 @@ func TestClient_OnBars(t *testing.T) { cctx, cancel := context.WithCancel(ctx) time.AfterFunc(50*time.Millisecond, cancel) start := time.Now() - assert.ErrorIs(t, r.OnBars(cctx, oneBar), context.Canceled) + _, err = r.OnBars(cctx, oneBar) + assert.ErrorIs(t, err, context.Canceled) assert.Less(t, time.Since(start), 2*time.Second) }) t.Run("a context deadline shorter than the timeout wins", func(t *testing.T) { @@ -139,6 +183,7 @@ func TestClient_OnBars(t *testing.T) { cctx, cancel := context.WithTimeout(ctx, 50*time.Millisecond) defer cancel() - assert.ErrorIs(t, r.OnBars(cctx, oneBar), context.DeadlineExceeded) + _, err = r.OnBars(cctx, oneBar) + assert.ErrorIs(t, err, context.DeadlineExceeded) }) } diff --git a/strategy/frame.go b/strategy/frame.go index e584da8..b596e15 100644 --- a/strategy/frame.go +++ b/strategy/frame.go @@ -1,6 +1,7 @@ package strategy import ( + "bytes" "encoding/binary" "encoding/json" "errors" @@ -49,5 +50,15 @@ func readMessage(r io.Reader, v any) error { } return err } - return json.Unmarshal(body, v) + // Strict: a field the protocol does not define is an error, so a worker cannot + // slip in something the trader would ignore without saying so. + dec := json.NewDecoder(bytes.NewReader(body)) + dec.DisallowUnknownFields() + if err := dec.Decode(v); err != nil { + return err + } + if _, err := dec.Token(); err != io.EOF { + return errors.New("unexpected data after the message") + } + return nil } diff --git a/strategy/frame_test.go b/strategy/frame_test.go index ee8eef0..cd7fdb0 100644 --- a/strategy/frame_test.go +++ b/strategy/frame_test.go @@ -8,6 +8,7 @@ import ( "testing" "github.com/rustyeddy/trader/marketdata" + "github.com/rustyeddy/trader/num" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -61,3 +62,16 @@ func TestReadMessage_Errors(t *testing.T) { assert.ErrorContains(t, readMessage(bytes.NewReader(huge), &v), "exceeds") }) } + +func TestOrder_WireForm(t *testing.T) { + var buf bytes.Buffer + require.NoError(t, writeMessage(&buf, ordersMessage{Orders: []Order{ + {Instrument: "EURUSD", Action: "close"}, + {Instrument: "EURUSD", Action: "open", Direction: "short", Price: num.MustParsePrice("1.1"), StopLoss: num.MustParsePrice("1.11")}, + }})) + body := buf.String()[4:] + assert.JSONEq(t, `{"orders":[ + {"instrument":"EURUSD","action":"close"}, + {"instrument":"EURUSD","action":"open","direction":"short","price":"1.1","stop_loss":"1.11"}]}`, body, + "zero prices and empty words are left out, prices are decimal strings") +} diff --git a/strategy/server.go b/strategy/server.go index 94e3c55..da52e9d 100644 --- a/strategy/server.go +++ b/strategy/server.go @@ -1,7 +1,6 @@ package strategy import ( - "encoding/json" "io" "net" @@ -12,7 +11,7 @@ import ( // from the trader and serves it (see Serve) until the trader closes it. ready, if // not nil, is called once with the address actually bound, which is how a caller // learns the port of "127.0.0.1:0". -func ListenAndServe(addr string, ready func(net.Addr), onBars func(bars []marketdata.Bar) error) error { +func ListenAndServe(addr string, ready func(net.Addr), onBars func(bars []marketdata.Bar) ([]Order, error)) error { if err := requireLoopback(addr); err != nil { return err } @@ -33,10 +32,10 @@ func ListenAndServe(addr string, ready func(net.Addr), onBars func(bars []market } // Serve is the worker's end of a connection. It reads each group of bars, calls -// onBars with it and answers with no orders, until the trader closes the -// connection (Serve then returns nil). If onBars returns an error Serve returns -// it without answering. Serve does not close conn. -func Serve(conn net.Conn, onBars func(bars []marketdata.Bar) error) error { +// onBars with it and answers with the orders it returns (none is fine), until +// the trader closes the connection (Serve then returns nil). If onBars returns an +// error Serve returns it without answering. Serve does not close conn. +func Serve(conn net.Conn, onBars func(bars []marketdata.Bar) ([]Order, error)) error { for { var in barsMessage if err := readMessage(conn, &in); err != nil { @@ -45,10 +44,14 @@ func Serve(conn net.Conn, onBars func(bars []marketdata.Bar) error) error { } return err } - if err := onBars(in.Bars); err != nil { + orders, err := onBars(in.Bars) + if err != nil { return err } - if err := writeMessage(conn, ordersMessage{Orders: []json.RawMessage{}}); err != nil { + if orders == nil { + orders = []Order{} // an empty list on the wire, not null + } + if err := writeMessage(conn, ordersMessage{Orders: orders}); err != nil { return err } } diff --git a/strategy/server_test.go b/strategy/server_test.go index 21262b6..f498cf6 100644 --- a/strategy/server_test.go +++ b/strategy/server_test.go @@ -8,6 +8,7 @@ import ( "time" "github.com/rustyeddy/trader/marketdata" + "github.com/rustyeddy/trader/num" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -18,7 +19,7 @@ func TestServe(t *testing.T) { var got [][]marketdata.Bar done := make(chan error, 1) go func() { - done <- Serve(b, func(bars []marketdata.Bar) error { got = append(got, bars); return nil }) + done <- Serve(b, func(bars []marketdata.Bar) ([]Order, error) { got = append(got, bars); return nil, nil }) }() for i := 0; i < 3; i++ { @@ -32,12 +33,28 @@ func TestServe(t *testing.T) { require.NoError(t, <-done, "a closed connection is a clean end") assert.Len(t, got, 3) }) + t.Run("answers with the orders onBars returns, in order", func(t *testing.T) { + a, b := net.Pipe() + done := make(chan error, 1) + want := []Order{ + {Instrument: "EURUSD", Action: "open", Direction: "long", Price: num.MustParsePrice("1.1"), StopLoss: num.MustParsePrice("1.09")}, + {Instrument: "EURUSD", Action: "close"}, + } + go func() { done <- Serve(b, func([]marketdata.Bar) ([]Order, error) { return want, nil }) }() + + require.NoError(t, writeMessage(a, barsMessage{Bars: oneBar})) + var out ordersMessage + require.NoError(t, readMessage(a, &out)) + assert.Equal(t, want, out.Orders) + require.NoError(t, a.Close()) + require.NoError(t, <-done) + }) t.Run("an error from onBars ends it without an answer", func(t *testing.T) { a, b := net.Pipe() defer func() { _ = a.Close() }() boom := errors.New("boom") done := make(chan error, 1) - go func() { done <- Serve(b, func([]marketdata.Bar) error { return boom }) }() + go func() { done <- Serve(b, func([]marketdata.Bar) ([]Order, error) { return nil, boom }) }() require.NoError(t, writeMessage(a, barsMessage{Bars: oneBar})) assert.ErrorIs(t, <-done, boom) @@ -45,7 +62,7 @@ func TestServe(t *testing.T) { t.Run("a garbled message is an error", func(t *testing.T) { a, b := net.Pipe() done := make(chan error, 1) - go func() { done <- Serve(b, func([]marketdata.Bar) error { return nil }) }() + go func() { done <- Serve(b, func([]marketdata.Bar) ([]Order, error) { return nil, nil }) }() _, err := a.Write([]byte{0, 0, 0, 4, 'n', 'o', 'p', 'e'}) require.NoError(t, err) @@ -60,13 +77,15 @@ func TestListenAndServe(t *testing.T) { done := make(chan error, 1) go func() { done <- ListenAndServe("127.0.0.1:0", func(a net.Addr) { addrs <- a }, - func([]marketdata.Bar) error { groups++; return nil }) + func([]marketdata.Bar) ([]Order, error) { groups++; return nil, nil }) }() c, err := Dial((<-addrs).String()) require.NoError(t, err) - require.NoError(t, c.OnBars(context.Background(), oneBar)) - require.NoError(t, c.OnBars(context.Background(), oneBar)) + _, err = c.OnBars(context.Background(), oneBar) + require.NoError(t, err) + _, err = c.OnBars(context.Background(), oneBar) + require.NoError(t, err) require.NoError(t, c.Close()) require.NoError(t, <-done) assert.Equal(t, 2, groups) @@ -79,17 +98,18 @@ func TestListenAndServe(t *testing.T) { require.NoError(t, probe.Close()) done := make(chan error, 1) - go func() { done <- ListenAndServe(addr, nil, func([]marketdata.Bar) error { return nil }) }() + go func() { done <- ListenAndServe(addr, nil, func([]marketdata.Bar) ([]Order, error) { return nil, nil }) }() var c *Client require.Eventually(t, func() bool { c, err = Dial(addr); return err == nil }, 2*time.Second, 10*time.Millisecond) - require.NoError(t, c.OnBars(context.Background(), oneBar)) + _, err = c.OnBars(context.Background(), oneBar) + require.NoError(t, err) require.NoError(t, c.Close()) require.NoError(t, <-done) }) t.Run("a non-loopback address is refused", func(t *testing.T) { for _, addr := range []string{"0.0.0.0:0", ":0", "8.8.8.8:9100", "no-port"} { - err := ListenAndServe(addr, nil, func([]marketdata.Bar) error { return nil }) + err := ListenAndServe(addr, nil, func([]marketdata.Bar) ([]Order, error) { return nil, nil }) assert.Error(t, err, addr) } }) @@ -97,6 +117,6 @@ func TestListenAndServe(t *testing.T) { ln, err := net.Listen("tcp", "127.0.0.1:0") require.NoError(t, err) defer func() { _ = ln.Close() }() - assert.Error(t, ListenAndServe(ln.Addr().String(), nil, func([]marketdata.Bar) error { return nil })) + assert.Error(t, ListenAndServe(ln.Addr().String(), nil, func([]marketdata.Bar) ([]Order, error) { return nil, nil })) }) } diff --git a/strategy/strategy.go b/strategy/strategy.go index f4bb13c..226b5d0 100644 --- a/strategy/strategy.go +++ b/strategy/strategy.go @@ -9,9 +9,13 @@ // // (marketdata.Bar as encoding/json writes it) and the worker answers // -// {"orders":[]} +// {"orders":[{"instrument":"EURUSD","action":"open","direction":"long", +// "price":"1.10000","stop_loss":"1.09000"}]} // -// Orders are not carried yet: the answer must be empty. +// The list must be present, but may be empty; an answer without it, or with +// null, is an error. Each order is a request (see Order): the trader decides the +// size, whether to admit it, and everything that follows. A field the protocol +// does not define, such as units, is an error, not ignored. // // The trader side is Dial and Client; the worker side is ListenAndServe, or Serve // for a connection already in hand. Both sides are loopback-only: nothing here is @@ -19,11 +23,11 @@ package strategy import ( - "encoding/json" "fmt" "net" "github.com/rustyeddy/trader/marketdata" + "github.com/rustyeddy/trader/num" ) type barsMessage struct { @@ -31,7 +35,23 @@ type barsMessage struct { } type ordersMessage struct { - Orders []json.RawMessage `json:"orders"` + Orders []Order `json:"orders"` +} + +// Order is what a worker may ask for. It is a request on the wire, not the +// trader's order: the trader maps it onto its own order, sizes it, applies the +// account's risk rules and the broker executes it. A worker cannot say how many +// units, nor name an id, account, state or fill; such a field is refused. +// +// Prices are decimal strings. Action and Direction use the trader's own words. +type Order struct { + Instrument string `json:"instrument"` + Action string `json:"action"` // Action is "open", "add", "close" or "reduce". + Direction string `json:"direction,omitempty"` // Direction is "long" or "short", for an open only. + Type string `json:"type,omitempty"` // Type is "market" (the default when empty), "limit" or "stop". + Price num.Price `json:"price,omitzero"` // the reference price of a market order, or the trigger of a limit or stop order. + StopLoss num.Price `json:"stop_loss,omitzero"` // StopLoss is where the protective stop goes; an open requires it. + TakeProfit num.Price `json:"take_profit,omitzero"` } // requireLoopback refuses an address that is not on this machine's loopback diff --git a/strategy/strategy_test.go b/strategy/strategy_test.go index af43c4c..2812015 100644 --- a/strategy/strategy_test.go +++ b/strategy/strategy_test.go @@ -46,13 +46,14 @@ func TestClientAndServe(t *testing.T) { return } defer func() { _ = conn.Close() }() - done <- Serve(conn, func([]marketdata.Bar) error { groups++; return nil }) + done <- Serve(conn, func([]marketdata.Bar) ([]Order, error) { groups++; return nil, nil }) }() r, err := Dial(ln.Addr().String()) require.NoError(t, err) for i := 0; i < 5; i++ { - require.NoError(t, r.OnBars(context.Background(), oneBar)) + _, err := r.OnBars(context.Background(), oneBar) + require.NoError(t, err) } require.NoError(t, r.Close()) require.NoError(t, <-done) diff --git a/strategy_remote.go b/strategy_remote.go index 120115a..b6d1440 100644 --- a/strategy_remote.go +++ b/strategy_remote.go @@ -3,6 +3,7 @@ package trader import ( "context" "fmt" + "time" "github.com/rustyeddy/trader/marketdata" remotestrat "github.com/rustyeddy/trader/strategy" @@ -35,8 +36,115 @@ func (r *remoteStrategy) init() error { return nil } // close ends the session; the worker sees the connection close and exits. func (r *remoteStrategy) close() error { return r.client.Close() } -// onBars hands the frame to the worker, which answers with no orders. Any failure -// is returned and stops the run. +// onBars hands the frame to the worker and maps the orders it requests onto the +// trader's own. Any failure, including an order that does not map or validate, is +// returned and stops the run, and then none of the group's orders is processed. func (r *remoteStrategy) onBars(ctx context.Context, bars []marketdata.Bar) ([]*order, error) { - return nil, r.client.OnBars(ctx, bars) + requests, err := r.client.OnBars(ctx, bars) + if err != nil { + return nil, err + } + return toOrders(requests, bars) +} + +// toOrders maps every request, in order, or fails without returning any. +func toOrders(requests []remotestrat.Order, bars []marketdata.Bar) ([]*order, error) { + var orders []*order + for i, q := range requests { + o, err := toOrder(q, orderTime(q.Instrument, bars)) + if err != nil { + return nil, fmt.Errorf("requested order %d (%s %s): %w", i+1, q.Action, q.Instrument, err) + } + orders = append(orders, o) + } + return orders, nil +} + +// orderTime is the time an order is made on: the opening time of the group's bar +// of that instrument or, for an instrument with no bar in the group, when the +// group became available (its bars' latest AvailableAt, which is the same for +// every bar of a frame). Which instruments a worker may order is not checked here. +func orderTime(instrument string, bars []marketdata.Bar) time.Time { + var available time.Time + for _, b := range bars { + if b.Instrument == instrument { + return b.Time + } + if b.AvailableAt.After(available) { + available = b.AvailableAt + } + } + return available +} + +// toOrder maps one request onto an order and validates it. It sets only what the +// worker may say: the trader stamps the account and decision time, and the +// broker the id, size and state. +func toOrder(q remotestrat.Order, at time.Time) (*order, error) { + act, err := parseAction(q.Action) + if err != nil { + return nil, err + } + dir, err := parseDirection(q.Direction) + if err != nil { + return nil, err + } + typ, err := parseOrderType(q.Type) + if err != nil { + return nil, err + } + o := &order{ + instrument: q.Instrument, + action: act, + direction: dir, + orderType: typ, + price: q.Price, + stopLoss: q.StopLoss, + takeProfit: q.TakeProfit, + Time: at, + } + if err := o.validate(); err != nil { + return nil, err + } + return o, nil +} + +// parseAction accepts the actions a worker may request. amend is not one of them +// yet. +func parseAction(s string) (action, error) { + switch s { + case "open": + return open, nil + case "add": + return add, nil + case "close": + return close, nil + case "reduce": + return reduce, nil + } + return noAction, fmt.Errorf("unsupported action %q", s) +} + +func parseDirection(s string) (direction, error) { + switch s { + case "": + return noDirection, nil + case "long": + return long, nil + case "short": + return short, nil + } + return noDirection, fmt.Errorf("unknown direction %q", s) +} + +func parseOrderType(s string) (orderType, error) { + switch s { + case "", "market": + return marketOrder, nil + case "limit": + return limitOrder, nil + case "stop": + return stopOrder, nil + } + return noOrder, fmt.Errorf("unknown order type %q", s) } diff --git a/strategy_remote_test.go b/strategy_remote_test.go index 4e8bacc..5ce83a4 100644 --- a/strategy_remote_test.go +++ b/strategy_remote_test.go @@ -4,8 +4,11 @@ import ( "context" "net" "testing" + "time" "github.com/rustyeddy/trader/marketdata" + "github.com/rustyeddy/trader/num" + remotestrat "github.com/rustyeddy/trader/strategy" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -54,3 +57,107 @@ func TestRemoteStrategy_OnBars(t *testing.T) { assert.Empty(t, orders) }) } + +func TestToOrder(t *testing.T) { + at := testStart + req := remotestrat.Order{Instrument: "EURUSD", Action: "open", Direction: "long", Price: px(110000), StopLoss: px(109000)} + + t.Run("an open maps every field it may carry and nothing else", func(t *testing.T) { + q := req + q.Type, q.TakeProfit = "limit", px(112000) + o, err := toOrder(q, at) + require.NoError(t, err) + assert.Equal(t, &order{ + instrument: "EURUSD", action: open, direction: long, orderType: limitOrder, + price: px(110000), stopLoss: px(109000), takeProfit: px(112000), Time: at, + }, o) + assert.Empty(t, o.id) + assert.Empty(t, o.accountID) + assert.True(t, o.decidedAt.IsZero(), "the trader stamps these, the worker never does") + assert.Zero(t, o.units) + assert.Equal(t, stateRequested, o.state) + }) + t.Run("an empty type is a market order", func(t *testing.T) { + o, err := toOrder(req, at) + require.NoError(t, err) + assert.Equal(t, marketOrder, o.orderType) + }) + t.Run("a close needs no direction, price or stop", func(t *testing.T) { + o, err := toOrder(remotestrat.Order{Instrument: "EURUSD", Action: "close"}, at) + require.NoError(t, err) + assert.Equal(t, close, o.action) + assert.Equal(t, noDirection, o.direction) + }) + t.Run("what the trader's own validation refuses is refused", func(t *testing.T) { + tests := []struct { + name string + edit func(*remotestrat.Order) + want string + }{ + {"missing stop", func(q *remotestrat.Order) { q.StopLoss = num.Price{} }, "stop loss"}, + {"missing price", func(q *remotestrat.Order) { q.Price = num.Price{} }, "reference price"}, + {"stop on the wrong side", func(q *remotestrat.Order) { q.StopLoss = px(111000) }, "not beyond"}, + {"open without a direction", func(q *remotestrat.Order) { q.Direction = "" }, "direction"}, + {"open without an instrument", func(q *remotestrat.Order) { q.Instrument = "" }, "instrument"}, + {"close with a direction", func(q *remotestrat.Order) { q.Action, q.StopLoss = "close", num.Price{} }, "direction"}, + {"unsupported action", func(q *remotestrat.Order) { q.Action = "amend" }, `unsupported action "amend"`}, + {"no action", func(q *remotestrat.Order) { q.Action = "" }, "unsupported action"}, + {"unknown direction", func(q *remotestrat.Order) { q.Direction = "sideways" }, "unknown direction"}, + {"unknown type", func(q *remotestrat.Order) { q.Type = "iceberg" }, "unknown order type"}, + {"limit order without a price", func(q *remotestrat.Order) { q.Type, q.Price = "limit", num.Price{} }, "price"}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + q := req + tt.edit(&q) + _, err := toOrder(q, at) + assert.ErrorContains(t, err, tt.want) + }) + } + }) + t.Run("an order without the bar's time is refused", func(t *testing.T) { + _, err := toOrder(req, time.Time{}) + assert.ErrorContains(t, err, "time") + }) +} + +func TestToOrders(t *testing.T) { + t1, t2 := testStart, testStart.Add(time.Hour) + avail := t2.Add(time.Hour) // bars of different opening times, available together + bars := []marketdata.Bar{ + {Instrument: "EURUSD", Time: t1, AvailableAt: avail}, + {Instrument: "GBPUSD", Time: t2, AvailableAt: avail}, + } + good := remotestrat.Order{Instrument: "EURUSD", Action: "open", Direction: "long", Price: px(110000), StopLoss: px(109000)} + + t.Run("none is none", func(t *testing.T) { + orders, err := toOrders(nil, bars) + require.NoError(t, err) + assert.Empty(t, orders) + }) + t.Run("keeps the worker's order and takes each time from its instrument's bar", func(t *testing.T) { + gbp := good + gbp.Instrument = "GBPUSD" + orders, err := toOrders([]remotestrat.Order{gbp, good, {Instrument: "EURUSD", Action: "close"}}, bars) + require.NoError(t, err) + require.Len(t, orders, 3) + assert.Equal(t, []string{"GBPUSD", "EURUSD", "EURUSD"}, []string{orders[0].instrument, orders[1].instrument, orders[2].instrument}) + assert.Equal(t, t2, orders[0].Time) + assert.Equal(t, t1, orders[1].Time) + }) + t.Run("an instrument outside the group is not refused; it takes the time the group became available", func(t *testing.T) { + other := good + other.Instrument = "USDJPY" + orders, err := toOrders([]remotestrat.Order{other}, bars) + require.NoError(t, err) + require.Len(t, orders, 1) + assert.Equal(t, avail, orders[0].Time, "not the opening time of some other instrument's bar") + }) + t.Run("one bad order fails them all and names it", func(t *testing.T) { + bad := good + bad.StopLoss = num.Price{} + orders, err := toOrders([]remotestrat.Order{good, bad}, bars) + assert.ErrorContains(t, err, "requested order 2 (open EURUSD)") + assert.Nil(t, orders, "the valid first order is not returned either") + }) +} diff --git a/trader_test.go b/trader_test.go index 7e7fc45..4bf90cb 100644 --- a/trader_test.go +++ b/trader_test.go @@ -15,6 +15,7 @@ import ( "testing" "github.com/rustyeddy/trader/marketdata" + "github.com/rustyeddy/trader/num" remotestrat "github.com/rustyeddy/trader/strategy" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -232,6 +233,8 @@ func TestTrader_HandleOrder_Amend(t *testing.T) { // until the trader hangs up. type workerOpts struct { dropOn int // hang up on receiving this group without answering it; 0 never + // decide, if set, says which orders to request on the n-th group (from 1). + decide func(n int, bars []marketdata.Bar) ([]remotestrat.Order, error) } // testWorker is the example worker in process: it listens on loopback, serves one @@ -257,12 +260,16 @@ func startWorker(t *testing.T, o workerOpts) *testWorker { return } defer func() { _ = conn.Close() }() - _ = remotestrat.Serve(conn, func(bars []marketdata.Bar) error { + _ = remotestrat.Serve(conn, func(bars []marketdata.Bar) ([]remotestrat.Order, error) { w.groups = append(w.groups, bars) - if o.dropOn > 0 && len(w.groups) == o.dropOn { - return errors.New("hang up") + n := len(w.groups) + if o.dropOn > 0 && n == o.dropOn { + return nil, errors.New("hang up") } - return nil + if o.decide != nil { + return o.decide(n, bars) + } + return nil, nil }) }() return w @@ -391,6 +398,155 @@ func TestRemoteStrategy_ExampleWorker(t *testing.T) { require.NoError(t, remote.close()) require.NoError(t, cmd.Wait(), "the worker exits when the trader closes") - assert.Empty(t, tr.account.trades) + ref := newRemoteTrader(t, &openThenClose{}) + require.NoError(t, (&backtest{}).runSeries(ctx, ref, prices)) + assert.Equal(t, outcomeOf(ref), outcomeOf(tr), "the real example trades like the in-process rule") + assert.Equal(t, 1, len(tr.account.trades), "it opened and closed one position") assert.Contains(t, stderr.String(), "done: ") } + +// The example worker's rule, once as the worker answers it and once as an in-process +// strategy: open long on the third group with a stop 0.015 below the close, close on +// the sixth. +const ( + ruleOpenAt = 3 + ruleCloseAt = 6 +) + +var ruleStopDistance = num.MustParsePrice("0.015") + +func openThenCloseRequests(n int, bars []marketdata.Bar) ([]remotestrat.Order, error) { + b := bars[0] + c := b.OHLC().Close + switch n { + case ruleOpenAt: + stop, err := c.Sub(ruleStopDistance) + if err != nil { + return nil, err + } + return []remotestrat.Order{{Instrument: b.Instrument, Action: "open", Direction: "long", Price: c, StopLoss: stop}}, nil + case ruleCloseAt: + return []remotestrat.Order{{Instrument: b.Instrument, Action: "close", Price: c}}, nil + } + return nil, nil +} + +// openThenClose is the same rule as an in-process strategy, the reference the +// remote run is compared with. +type openThenClose struct{ n int } + +func (*openThenClose) name() string { return "open-then-close" } +func (*openThenClose) init() error { return nil } +func (s *openThenClose) onBars(_ context.Context, bars []marketdata.Bar) ([]*order, error) { + s.n++ + requests, err := openThenCloseRequests(s.n, bars) + if err != nil { + return nil, err + } + var orders []*order + for _, q := range requests { + o := &order{instrument: q.Instrument, price: q.Price, stopLoss: q.StopLoss, orderType: marketOrder, Time: bars[0].Time} + if q.Action == "open" { + o.action, o.direction = open, long + } else { + o.action = close + } + orders = append(orders, o) + } + return orders, nil +} + +// outcome is what a run came to: every order that ended or waits, with its sizing +// and fill, and the account. +type outcome struct { + orders []orderResult + balance num.Money + trades int +} + +func outcomeOf(tr *trader) outcome { + return outcome{ + orders: orderResults(slices.Concat(tr.broker.completed, tr.broker.orders["EURUSD"])), + balance: tr.account.balance, + trades: len(tr.account.trades), + } +} + +func TestRemoteStrategy_OrdersMatchInProcess(t *testing.T) { + ctx := context.Background() + captureLogs(t) + prices := scenarioPrices(t, remoteScenario) + + inProc := newRemoteTrader(t, &openThenClose{}) + require.NoError(t, (&backtest{}).runSeries(ctx, inProc, prices)) + want := outcomeOf(inProc) + require.Len(t, want.orders, 3, "the open, the close, and the broker's stop cancelled by it") + assert.Equal(t, stateFilled, want.orders[0].state, "open") + assert.Equal(t, stateFilled, want.orders[1].state, "close") + assert.Equal(t, stateCancelled, want.orders[2].state, "the stop") + assert.Equal(t, 1, want.trades) + assert.NotZero(t, want.orders[0].units, "the trader sized it, the strategy did not") + + w := startWorker(t, workerOpts{decide: openThenCloseRequests}) + remote, err := dialRemote("remote", w.addr) + require.NoError(t, err) + tr := newRemoteTrader(t, remote) + require.NoError(t, (&backtest{}).runSeries(ctx, tr, prices)) + require.NoError(t, remote.close()) + w.finish() + + got := outcomeOf(tr) + assert.Equal(t, want.orders, got.orders, "same orders, sizes, fills and times") + assert.True(t, want.balance.Equal(got.balance), "same final balance: %s vs %s", want.balance, got.balance) + assert.Equal(t, want.trades, got.trades) + assert.Equal(t, orderHistoryOf(inProc), orderHistoryOf(tr), "same lifecycle") +} + +// orderHistoryOf is every order's lifecycle, by id. +func orderHistoryOf(tr *trader) map[string][]orderEvent { + histories := make(map[string][]orderEvent) + for _, o := range slices.Concat(tr.broker.completed, tr.broker.orders["EURUSD"]) { + histories[o.id] = o.history + } + return histories +} + +func TestRemoteStrategy_BadOrdersFailTheRunAndProcessNone(t *testing.T) { + ctx := context.Background() + captureLogs(t) + prices := scenarioPrices(t, remoteScenario) + valid := remotestrat.Order{Instrument: "EURUSD", Action: "open", Direction: "long", Price: px(110000), StopLoss: px(109000)} + + tests := []struct { + name string + orders []remotestrat.Order + want string + }{ + {"open without a stop", []remotestrat.Order{{Instrument: "EURUSD", Action: "open", Direction: "long", Price: px(110000)}}, "stop loss"}, + {"unsupported action", []remotestrat.Order{{Instrument: "EURUSD", Action: "amend", StopLoss: px(109000)}}, `unsupported action "amend"`}, + {"a bad order after a good one", []remotestrat.Order{valid, {Instrument: "EURUSD", Action: "frobnicate"}}, "requested order 2"}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + w := startWorker(t, workerOpts{decide: func(n int, _ []marketdata.Bar) ([]remotestrat.Order, error) { + if n == ruleOpenAt { + return tt.orders, nil + } + return nil, nil + }}) + remote, err := dialRemote("remote", w.addr) + require.NoError(t, err) + defer func() { _ = remote.close() }() + tr := newRemoteTrader(t, remote) + + err = (&backtest{}).runSeries(ctx, tr, prices) + require.ErrorContains(t, err, tt.want) + assert.Empty(t, tr.broker.completed, "no order of the failed group was processed") + assert.Empty(t, tr.broker.orders["EURUSD"]) + assert.Empty(t, tr.rejected) + assert.Empty(t, tr.account.trades) + require.NoError(t, remote.close()) // the worker stops when the trader hangs up + assert.Len(t, w.finish(), ruleOpenAt, "the run stopped at the group that failed") + }) + } +}