Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 41 additions & 9 deletions examples/remote-strategy/main.go
Original file line number Diff line number Diff line change
@@ -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:
//
Expand All @@ -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
}
36 changes: 18 additions & 18 deletions strategy/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
103 changes: 74 additions & 29 deletions strategy/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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) {
Expand All @@ -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{})
Expand All @@ -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)
})
Expand All @@ -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) {
Expand All @@ -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)
})
}
13 changes: 12 additions & 1 deletion strategy/frame.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package strategy

import (
"bytes"
"encoding/binary"
"encoding/json"
"errors"
Expand Down Expand Up @@ -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
}
14 changes: 14 additions & 0 deletions strategy/frame_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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")
}
19 changes: 11 additions & 8 deletions strategy/server.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package strategy

import (
"encoding/json"
"io"
"net"

Expand All @@ -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
}
Expand All @@ -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 {
Expand All @@ -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
}
}
Expand Down
Loading
Loading