diff --git a/examples/remote-strategy/main.go b/examples/remote-strategy/main.go index bd880ee..ac2b406 100644 --- a/examples/remote-strategy/main.go +++ b/examples/remote-strategy/main.go @@ -1,16 +1,18 @@ // 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. +// with a stop a fixed distance below the close, and close it on the sixth, but +// only if Trader has told it the entry filled and the stop has not already taken +// it out. It asks only; Trader sizes the order, applies the account's risk rules +// and fills it, and reports what it did with the next group. // // Run it, then point a Trader backtest at the address it prints: // // go run ./examples/remote-strategy -addr 127.0.0.1:9100 // -// It serves one connection and exits when Trader closes it. The wire protocol is -// described in package strategy. +// It prints each event Trader reports to standard error. It serves one +// connection and exits when Trader closes it. The wire protocol is described in +// package strategy. package main import ( @@ -27,47 +29,70 @@ import ( const ( openAt = 3 // open on this group... - closeAt = 6 // ...and close on this one + closeAt = 6 // ...and close on this one, if still in ) +// entryRef labels the opening order, so its events can be recognised. +const entryRef = "entry" + // stopDistance is how far below the entry the stop goes. var stopDistance = num.MustParsePrice("0.015") +// worker is the strategy's memory: how many groups it has seen and whether it is +// in a position, as far as Trader has said. +type worker struct { + groups int + inPosition bool +} + func main() { addr := flag.String("addr", "127.0.0.1:0", "loopback address to listen on") flag.Parse() - groups := 0 + w := &worker{} err := strategy.ListenAndServe(*addr, func(a net.Addr) { fmt.Println("listening on", a) }, // for whoever started us - func(bars []marketdata.Bar) ([]strategy.Order, error) { - groups++ - return decide(groups, bars) + func(bars []marketdata.Bar, events []strategy.Event) ([]strategy.Order, error) { + for _, e := range events { + fmt.Fprintf(os.Stderr, "event: %s %s %s %s %s ref=%q id=%q reason=%q\n", + e.Time.Format("2006-01-02"), e.Kind, e.Action, e.Instrument, e.Cause, e.Ref, e.ID, e.Reason) + } + return w.decide(bars, events) }) if err != nil { log.Fatal(err) } - fmt.Fprintf(os.Stderr, "done: %d groups\n", groups) + fmt.Fprintf(os.Stderr, "done: %d groups\n", w.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) { +// decide is the whole strategy: what Trader reported, then the orders to request +// on the next group of bars. +func (w *worker) decide(bars []marketdata.Bar, events []strategy.Event) ([]strategy.Order, error) { + for _, e := range events { + switch { + case e.Ref == entryRef && e.Kind == strategy.Filled: + w.inPosition = true + case e.Kind == strategy.Filled && e.Cause == "stop loss": + w.inPosition = false // the protective stop took us out + } + } + w.groups++ if len(bars) == 0 { return nil, nil } b := bars[0] closePrice := b.OHLC().Close - switch n { - case openAt: + switch { + case w.groups == 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, + Price: closePrice, StopLoss: stop, Ref: entryRef, }}, nil - case closeAt: + case w.groups == closeAt && w.inPosition: return []strategy.Order{{Instrument: b.Instrument, Action: "close", Price: closePrice}}, nil } return nil, nil diff --git a/feedback.go b/feedback.go new file mode 100644 index 0000000..e5b279a --- /dev/null +++ b/feedback.go @@ -0,0 +1,122 @@ +package trader + +import ( + "slices" + + remotestrat "github.com/rustyeddy/trader/strategy" +) + +// feedbackEvent is one entry of an order's history, with the order it belongs to. +type feedbackEvent struct { + order *order + orderEvent +} + +// feedbacker is implemented by a strategy that wants to be told what the trader +// did with its orders. The trader calls feedback once per frame, just before +// onBars, with the events that happened since the last call, in the order they +// happened. They are the broker's work on the frame about to be shown to the +// strategy and the outcome of the orders it asked for in the previous call; never +// the outcome of the orders it is about to return. A strategy that does not +// implement it is simply not told. +type feedbacker interface { + feedback(events []feedbackEvent) +} + +// feedbackLog remembers what has been told to a feedbacker, so that each event is +// delivered once. +type feedbackLog struct { + watched []*order // the strategy's orders whose history is not all delivered + sent map[*order]int // how many history entries of each were delivered + exitsSeen int // how much of broker.completed was looked at for the broker's own exits +} + +// watch starts following a strategy's order. +func (tr *trader) watch(o *order) { + if _, ok := tr.strategy.(feedbacker); ok { + tr.feedbackLog.watched = append(tr.feedbackLog.watched, o) + } +} + +// reported says whether a state is one the strategy is told about. Requested and +// sized are the trader's own steps on the way to accepted. +func reported(s orderState) bool { + switch s { + case stateSubmitted, stateFilled, stateRejected, stateCancelled: + return true + } + return false +} + +// collectEvents returns what the strategy has not yet been told, in the order it +// happened (by time; on a tie the strategy's own orders come before the broker's +// exits, which is how they happen on one bar), and marks it told. It reports: +// +// - the submitted, filled, rejected and cancelled steps of the strategy's orders; +// - the broker's own exits (protective stop, take profit, margin closeout) when +// they fill. Their other steps are the broker's housekeeping and are not +// reported. +func (tr *trader) collectEvents() []feedbackEvent { + log := &tr.feedbackLog + if log.sent == nil { + log.sent = make(map[*order]int) + } + var events []feedbackEvent + for _, o := range log.watched { + for _, e := range o.history[log.sent[o]:] { + if reported(e.state) { + events = append(events, feedbackEvent{o, e}) + } + } + log.sent[o] = len(o.history) + } + log.watched = slices.DeleteFunc(log.watched, func(o *order) bool { + done := o.state == stateFilled || o.state == stateRejected || o.state == stateCancelled + if done { + delete(log.sent, o) + } + return done + }) + + for _, o := range tr.broker.completed[log.exitsSeen:] { + if o.accountID == tr.account.id && o.reason != reasonStrategy && o.state == stateFilled { + events = append(events, feedbackEvent{o, o.history[len(o.history)-1]}) + } + } + log.exitsSeen = len(tr.broker.completed) + + slices.SortStableFunc(events, func(a, b feedbackEvent) int { return a.time.Compare(b.time) }) + return events +} + +// toWireEvent maps an event onto the public form. ref is the worker's label for +// the order, empty for the broker's own orders. +func toWireEvent(e feedbackEvent, ref string) remotestrat.Event { + o := e.order + w := remotestrat.Event{ + Time: e.time, + ID: o.id, + Ref: ref, + Instrument: o.instrument, + Action: o.action.String(), + Units: o.units, + Cause: o.reason.String(), + } + if o.direction != noDirection { + w.Direction = o.direction.String() + } + switch e.state { + case stateSubmitted: + w.Kind = remotestrat.Accepted + case stateFilled: + w.Kind = remotestrat.Filled + w.Price = o.fillPrice + case stateRejected: + w.Kind = remotestrat.Rejected + w.Reason = e.reason + case stateCancelled: + w.Kind = remotestrat.Cancelled + w.Reason = e.reason + } + return w +} diff --git a/feedback_test.go b/feedback_test.go new file mode 100644 index 0000000..06dbf7a --- /dev/null +++ b/feedback_test.go @@ -0,0 +1,213 @@ +package trader + +import ( + "context" + "testing" + "time" + + "github.com/rustyeddy/trader/marketdata" + remotestrat "github.com/rustyeddy/trader/strategy" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// feedbackScript is a strategy that asks for feedback, requests the orders it is +// given by group number, and keeps what it was told with each group. +type feedbackScript struct { + orders map[int]func(b marketdata.Bar) *order + groups int + pending []feedbackEvent + told [][]feedbackEvent +} + +func (*feedbackScript) name() string { return "script" } +func (*feedbackScript) init() error { return nil } + +func (s *feedbackScript) feedback(events []feedbackEvent) { s.pending = append(s.pending, events...) } + +func (s *feedbackScript) onBars(_ context.Context, bars []marketdata.Bar) ([]*order, error) { + s.groups++ + s.told = append(s.told, s.pending) + s.pending = nil + if mk := s.orders[s.groups]; mk != nil { + return []*order{mk(bars[0])}, nil + } + return nil, nil +} + +// told is each group's events as "kind action cause". +func (s *feedbackScript) kinds() [][]string { + out := make([][]string, len(s.told)) + for i, g := range s.told { + for _, e := range g { + w := toWireEvent(e, "") + out[i] = append(out[i], w.Kind+" "+w.Action+" "+w.Cause) + } + } + return out +} + +func entryOrder(stop, target int64) func(marketdata.Bar) *order { + return func(b marketdata.Bar) *order { + o := &order{instrument: b.Instrument, action: open, direction: long, orderType: marketOrder, + price: b.OHLC().Close, stopLoss: px(stop), Time: b.Time} + if target != 0 { + o.takeProfit = px(target) + } + return o + } +} + +func runScript(t *testing.T, s *feedbackScript, balance money) *trader { + t.Helper() + captureLogs(t) + tr := newRemoteTraderWith(t, s, balance) + require.NoError(t, (&backtest{}).runSeries(context.Background(), tr, scenarioPrices(t, remoteScenario))) + return tr +} + +func TestCollectEvents_Timing(t *testing.T) { + // Entry requested on group 3: its acceptance and fill are told with group 4, + // never with group 3, and never again. + s := &feedbackScript{orders: map[int]func(marketdata.Bar) *order{3: entryOrder(108700, 0)}} + runScript(t, s, usd(1000)) + + kinds := s.kinds() + require.GreaterOrEqual(t, len(kinds), 6) + assert.Empty(t, kinds[0], "nothing has happened before the first order") + assert.Empty(t, kinds[1]) + assert.Empty(t, kinds[2], "the order this very call asks for is not in the call that asks") + assert.Equal(t, []string{"accepted open strategy", "filled open strategy"}, kinds[3]) + for i := 4; i < len(kinds); i++ { + assert.Empty(t, kinds[i], "group %d: delivered once", i+1) + } + assert.Equal(t, time.Date(2010, 1, 6, 0, 0, 0, 0, time.UTC), s.told[3][0].time, "the accepted event keeps the time it happened") + assert.Equal(t, time.Date(2010, 1, 7, 0, 0, 0, 0, time.UTC), s.told[3][1].time) +} + +func TestCollectEvents_BrokerExits(t *testing.T) { + t.Run("a protective stop is reported when it fills, and nothing of its housekeeping", func(t *testing.T) { + s := &feedbackScript{orders: map[int]func(marketdata.Bar) *order{3: entryOrder(109800, 0)}} + tr := runScript(t, s, usd(1000)) + + var all []string + for _, g := range s.kinds() { + all = append(all, g...) + } + assert.Equal(t, []string{"accepted open strategy", "filled open strategy", "filled close stop loss"}, all, + "the broker's own stop is not 'accepted' to the worker, and its sibling's cancellation is not reported") + stop := s.told[5][0] + assert.Equal(t, stateFilled, stop.state) + assert.Equal(t, "stop loss", stop.order.reason.String()) + assert.Equal(t, 1, len(tr.account.trades)) + }) + t.Run("a take profit is reported with its cause", func(t *testing.T) { + s := &feedbackScript{orders: map[int]func(marketdata.Bar) *order{3: entryOrder(108700, 110800)}} + runScript(t, s, usd(1000)) + + var all []string + for _, g := range s.kinds() { + all = append(all, g...) + } + assert.Equal(t, []string{"accepted open strategy", "filled open strategy", "filled close take profit"}, all) + }) +} + +func TestCollectEvents_Refusals(t *testing.T) { + t.Run("a refusal at sizing has a reason and no id", func(t *testing.T) { + s := &feedbackScript{orders: map[int]func(marketdata.Bar) *order{3: entryOrder(108700, 0)}} + tr := runScript(t, s, usd(0)) + + require.Len(t, s.told[3], 1) + e := toWireEvent(s.told[3][0], "") + assert.Equal(t, remotestrat.Rejected, e.Kind) + assert.Empty(t, e.ID, "it never reached the broker, which issues ids") + assert.NotEmpty(t, e.Reason) + assert.Len(t, tr.rejected, 1) + for i, g := range s.told { + if i != 3 { + assert.Empty(t, g) + } + } + }) + t.Run("a waiting order that expires is reported cancelled", func(t *testing.T) { + s := &feedbackScript{orders: map[int]func(marketdata.Bar) *order{3: func(b marketdata.Bar) *order { + // a buy limit far below the market, good for two days + return &order{instrument: b.Instrument, action: open, direction: long, orderType: limitOrder, + price: px(100000), stopLoss: px(99000), expires: b.Time.Add(48 * time.Hour), Time: b.Time} + }}} + runScript(t, s, usd(1000)) + + var all []string + for _, g := range s.kinds() { + all = append(all, g...) + } + assert.Equal(t, []string{"accepted open strategy", "cancelled open strategy"}, all) + }) +} + +func TestCollectEvents_OnceAndSorted(t *testing.T) { + tr := newTestTrader() + tr.strategy = &feedbackScript{} + early := &order{id: "a", instrument: "EURUSD", action: open} + late := &order{id: "b", instrument: "EURUSD", action: open} + t1 := time.Date(2010, 1, 5, 0, 0, 0, 0, time.UTC) + t2 := t1.Add(24 * time.Hour) + // watched in the order b, a, but a's step happened first + late.record(stateRequested, t2, "") + late.record(stateSubmitted, t2, "") + early.record(stateRequested, t1, "") + early.record(stateSubmitted, t1, "") + tr.watch(late) + tr.watch(early) + + events := tr.collectEvents() + require.Len(t, events, 2, "requested is the trader's own step, not reported") + assert.Equal(t, []string{"a", "b"}, []string{events[0].order.id, events[1].order.id}, "by time, not by who was watched first") + assert.Empty(t, tr.collectEvents(), "once") + + early.record(stateFilled, t2, "") + again := tr.collectEvents() + require.Len(t, again, 1, "only what is new") + assert.Equal(t, stateFilled, again[0].state) + _, kept := tr.feedbackLog.sent[early] + assert.False(t, kept, "a finished order is forgotten") +} + +func TestTrader_Watch_OnlyForAStrategyThatAsks(t *testing.T) { + tr := newTestTrader() + tr.strategy = &recorder{} // does not implement feedbacker + tr.watch(&order{}) + assert.Empty(t, tr.feedbackLog.watched) +} + +func TestToWireEvent(t *testing.T) { + at := time.Date(2010, 1, 5, 0, 0, 0, 0, time.UTC) + o := &order{id: "t-9", instrument: "EURUSD", action: close, direction: long, units: qty(500), fillPrice: px(110000), reason: reasonMarginCloseout} + tests := []struct { + name string + state orderState + why string + want remotestrat.Event + }{ + {"accepted", stateSubmitted, "", remotestrat.Event{Kind: remotestrat.Accepted}}, + {"filled carries the price", stateFilled, "", remotestrat.Event{Kind: remotestrat.Filled, Price: px(110000)}}, + {"rejected carries the reason", stateRejected, "no margin", remotestrat.Event{Kind: remotestrat.Rejected, Reason: "no margin"}}, + {"cancelled carries the reason", stateCancelled, "position closed", remotestrat.Event{Kind: remotestrat.Cancelled, Reason: "position closed"}}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := toWireEvent(feedbackEvent{o, orderEvent{state: tt.state, time: at, reason: tt.why}}, "label") + want := tt.want + want.Time, want.ID, want.Ref = at, "t-9", "label" + want.Instrument, want.Action, want.Direction = "EURUSD", "close", "long" + want.Units, want.Cause = qty(500), "margin closeout" + assert.Equal(t, want, got) + }) + } + t.Run("an order with no direction leaves it out", func(t *testing.T) { + got := toWireEvent(feedbackEvent{&order{id: "t-1", action: close}, orderEvent{state: stateSubmitted}}, "") + assert.Empty(t, got.Direction) + assert.Equal(t, "strategy", got.Cause) + }) +} diff --git a/strategy/client.go b/strategy/client.go index bc19d0b..5080d07 100644 --- a/strategy/client.go +++ b/strategy/client.go @@ -38,11 +38,12 @@ 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: the orders the worker -// requests, in order, possibly none. It returns an error if the worker does not +// OnBars sends one group of bars with the events since the last group (none is +// fine) 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) { +func (r *Client) OnBars(ctx context.Context, bars []marketdata.Bar, events []Event) ([]Order, error) { timeout := r.Timeout if timeout == 0 { timeout = DefaultTimeout @@ -58,7 +59,7 @@ func (r *Client) OnBars(ctx context.Context, bars []marketdata.Bar) ([]Order, er stop := context.AfterFunc(ctx, func() { _ = r.conn.SetDeadline(time.Unix(1, 0)) }) defer stop() - orders, err := r.exchange(bars) + orders, err := r.exchange(bars, events) switch { case err == nil: return orders, nil @@ -74,8 +75,11 @@ func (r *Client) OnBars(ctx context.Context, bars []marketdata.Bar) ([]Order, er return nil, err } -func (r *Client) exchange(bars []marketdata.Bar) ([]Order, error) { - if err := writeMessage(r.conn, barsMessage{Bars: bars}); err != nil { +func (r *Client) exchange(bars []marketdata.Bar, events []Event) ([]Order, error) { + if events == nil { + events = []Event{} // an empty list on the wire, not null + } + if err := writeMessage(r.conn, barsMessage{Bars: bars, Events: events}); err != nil { return nil, fmt.Errorf("sending bars: %w", err) } var reply ordersMessage diff --git a/strategy/client_test.go b/strategy/client_test.go index 0eab3d7..d635001 100644 --- a/strategy/client_test.go +++ b/strategy/client_test.go @@ -80,35 +80,62 @@ func TestClient_OnBars(t *testing.T) { 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) + orders, err := r.OnBars(ctx, oneBar, nil) require.NoError(t, err) assert.Empty(t, orders) } }) 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":"EURUSD","action":"open","direction":"long","type":"limit","price":"1.10000","stop_loss":"1.09000","take_profit":"1.12000","ref":"entry"}, {"instrument":"GBPUSD","action":"close"}]}`) - orders, err := r.OnBars(ctx, oneBar) + orders, err := r.OnBars(ctx, oneBar, nil) require.NoError(t, err) 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"), + Price: num.MustParsePrice("1.1"), StopLoss: num.MustParsePrice("1.09"), TakeProfit: num.MustParsePrice("1.12"), Ref: "entry", }, 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) + _, err := r.OnBars(ctx, oneBar, nil) assert.ErrorContains(t, err, "unknown field", field) } }) + t.Run("sends the events, and an empty list rather than null when there are none", func(t *testing.T) { + requests := make(chan barsMessage, 2) + addr := rawWorker(t, func(c net.Conn) { + for { + var in barsMessage + if readMessage(c, &in) != nil { + return + } + requests <- in + writeRaw(c, `{"orders":[]}`) + } + }) + r, err := Dial(addr) + require.NoError(t, err) + defer func() { _ = r.Close() }() + + _, err = r.OnBars(ctx, oneBar, nil) + require.NoError(t, err) + first := <-requests + assert.NotNil(t, first.Events) + assert.Empty(t, first.Events) + + want := []Event{{Kind: Rejected, Instrument: "EURUSD", Action: "open", Ref: "entry", Cause: "strategy", Reason: "no margin"}} + _, err = r.OnBars(ctx, oneBar, want) + require.NoError(t, err) + assert.Equal(t, want, (<-requests).Events) + }) 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) + orders, err := r.OnBars(ctx, oneBar, nil) assert.ErrorContains(t, err, "no orders list", body) assert.Empty(t, orders) } @@ -116,7 +143,7 @@ func TestClient_OnBars(t *testing.T) { 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) + orders, err := r.OnBars(ctx, oneBar, nil) assert.Error(t, err, body) assert.Empty(t, orders) } @@ -129,7 +156,7 @@ func TestClient_OnBars(t *testing.T) { r, err := Dial(addr) require.NoError(t, err) defer func() { _ = r.Close() }() - _, err = r.OnBars(ctx, oneBar) + _, err = r.OnBars(ctx, oneBar, nil) assert.ErrorContains(t, err, "reading orders") }) t.Run("a silent worker times out", func(t *testing.T) { @@ -146,7 +173,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, nil) assert.ErrorContains(t, err, "did not answer within") assert.Less(t, time.Since(start), 2*time.Second) }) @@ -165,7 +192,7 @@ func TestClient_OnBars(t *testing.T) { cctx, cancel := context.WithCancel(ctx) time.AfterFunc(50*time.Millisecond, cancel) start := time.Now() - _, err = r.OnBars(cctx, oneBar) + _, err = r.OnBars(cctx, oneBar, nil) assert.ErrorIs(t, err, context.Canceled) assert.Less(t, time.Since(start), 2*time.Second) }) @@ -183,7 +210,7 @@ func TestClient_OnBars(t *testing.T) { cctx, cancel := context.WithTimeout(ctx, 50*time.Millisecond) defer cancel() - _, err = r.OnBars(cctx, oneBar) + _, err = r.OnBars(cctx, oneBar, nil) assert.ErrorIs(t, err, context.DeadlineExceeded) }) } diff --git a/strategy/frame_test.go b/strategy/frame_test.go index cd7fdb0..4281f86 100644 --- a/strategy/frame_test.go +++ b/strategy/frame_test.go @@ -6,6 +6,7 @@ import ( "io" "strings" "testing" + "time" "github.com/rustyeddy/trader/marketdata" "github.com/rustyeddy/trader/num" @@ -75,3 +76,17 @@ func TestOrder_WireForm(t *testing.T) { {"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") } + +func TestEvent_WireForm(t *testing.T) { + at := time.Date(2010, 1, 6, 0, 0, 0, 0, time.UTC) + var buf bytes.Buffer + require.NoError(t, writeMessage(&buf, barsMessage{Events: []Event{ + {Time: at, Kind: Rejected, Instrument: "EURUSD", Action: "open", Direction: "long", Ref: "entry", Cause: "strategy", Reason: "not enough margin"}, + {Time: at, Kind: Filled, ID: "sim-2", Instrument: "EURUSD", Action: "close", Direction: "long", + Units: num.MustParseQuantity("1000"), Price: num.MustParsePrice("1.09"), Cause: "stop loss"}, + }})) + assert.JSONEq(t, `{"bars":null,"events":[ + {"time":"2010-01-06T00:00:00Z","kind":"rejected","ref":"entry","instrument":"EURUSD","action":"open","direction":"long","cause":"strategy","reason":"not enough margin"}, + {"time":"2010-01-06T00:00:00Z","kind":"filled","id":"sim-2","instrument":"EURUSD","action":"close","direction":"long","units":"1000","price":"1.09","cause":"stop loss"}]}`, + buf.String()[4:], "empty fields are left out; prices and sizes are decimal strings") +} diff --git a/strategy/server.go b/strategy/server.go index da52e9d..70e6772 100644 --- a/strategy/server.go +++ b/strategy/server.go @@ -1,6 +1,7 @@ package strategy import ( + "errors" "io" "net" @@ -11,7 +12,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) ([]Order, error)) error { +func ListenAndServe(addr string, ready func(net.Addr), onBars func(bars []marketdata.Bar, events []Event) ([]Order, error)) error { if err := requireLoopback(addr); err != nil { return err } @@ -35,7 +36,7 @@ func ListenAndServe(addr string, ready func(net.Addr), onBars func(bars []market // 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 { +func Serve(conn net.Conn, onBars func(bars []marketdata.Bar, events []Event) ([]Order, error)) error { for { var in barsMessage if err := readMessage(conn, &in); err != nil { @@ -44,7 +45,10 @@ func Serve(conn net.Conn, onBars func(bars []marketdata.Bar) ([]Order, error)) e } return err } - orders, err := onBars(in.Bars) + if in.Events == nil { + return errors.New("request has no events list") + } + orders, err := onBars(in.Bars, in.Events) if err != nil { return err } diff --git a/strategy/server_test.go b/strategy/server_test.go index f498cf6..807e51a 100644 --- a/strategy/server_test.go +++ b/strategy/server_test.go @@ -19,11 +19,11 @@ func TestServe(t *testing.T) { var got [][]marketdata.Bar done := make(chan error, 1) go func() { - done <- Serve(b, func(bars []marketdata.Bar) ([]Order, error) { got = append(got, bars); return nil, nil }) + done <- Serve(b, func(bars []marketdata.Bar, _ []Event) ([]Order, error) { got = append(got, bars); return nil, nil }) }() for i := 0; i < 3; i++ { - require.NoError(t, writeMessage(a, barsMessage{Bars: oneBar})) + require.NoError(t, writeMessage(a, barsMessage{Bars: oneBar, Events: []Event{}})) var out ordersMessage require.NoError(t, readMessage(a, &out)) assert.NotNil(t, out.Orders, "an empty list on the wire, not null") @@ -40,29 +40,54 @@ func TestServe(t *testing.T) { {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 }) }() + go func() { done <- Serve(b, func([]marketdata.Bar, []Event) ([]Order, error) { return want, nil }) }() - require.NoError(t, writeMessage(a, barsMessage{Bars: oneBar})) + require.NoError(t, writeMessage(a, barsMessage{Bars: oneBar, Events: []Event{}})) 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("hands the events to onBars", func(t *testing.T) { + a, b := net.Pipe() + done := make(chan error, 1) + var got []Event + go func() { + done <- Serve(b, func(_ []marketdata.Bar, events []Event) ([]Order, error) { got = events; return nil, nil }) + }() + want := []Event{{Kind: Filled, ID: "sim-1", Instrument: "EURUSD", Action: "open", Cause: "strategy"}} + require.NoError(t, writeMessage(a, barsMessage{Bars: oneBar, Events: want})) + var out ordersMessage + require.NoError(t, readMessage(a, &out)) + require.NoError(t, a.Close()) + require.NoError(t, <-done) + assert.Equal(t, want, got) + }) + t.Run("a request without an events list is an error", func(t *testing.T) { + for _, body := range []string{`{"bars":[]}`, `{"bars":[],"events":null}`} { + a, b := net.Pipe() + done := make(chan error, 1) + go func() { done <- Serve(b, func([]marketdata.Bar, []Event) ([]Order, error) { return nil, nil }) }() + writeRaw(a, body) + assert.ErrorContains(t, <-done, "no events list", body) + _ = a.Close() + } + }) 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) ([]Order, error) { return nil, boom }) }() + go func() { done <- Serve(b, func([]marketdata.Bar, []Event) ([]Order, error) { return nil, boom }) }() - require.NoError(t, writeMessage(a, barsMessage{Bars: oneBar})) + require.NoError(t, writeMessage(a, barsMessage{Bars: oneBar, Events: []Event{}})) assert.ErrorIs(t, <-done, boom) }) 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) ([]Order, error) { return nil, nil }) }() + go func() { done <- Serve(b, func([]marketdata.Bar, []Event) ([]Order, error) { return nil, nil }) }() _, err := a.Write([]byte{0, 0, 0, 4, 'n', 'o', 'p', 'e'}) require.NoError(t, err) @@ -77,14 +102,14 @@ 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) ([]Order, error) { groups++; return nil, nil }) + func([]marketdata.Bar, []Event) ([]Order, error) { groups++; return nil, nil }) }() c, err := Dial((<-addrs).String()) require.NoError(t, err) - _, err = c.OnBars(context.Background(), oneBar) + _, err = c.OnBars(context.Background(), oneBar, nil) require.NoError(t, err) - _, err = c.OnBars(context.Background(), oneBar) + _, err = c.OnBars(context.Background(), oneBar, nil) require.NoError(t, err) require.NoError(t, c.Close()) require.NoError(t, <-done) @@ -98,18 +123,20 @@ func TestListenAndServe(t *testing.T) { require.NoError(t, probe.Close()) done := make(chan error, 1) - go func() { done <- ListenAndServe(addr, nil, func([]marketdata.Bar) ([]Order, error) { return nil, nil }) }() + go func() { + done <- ListenAndServe(addr, nil, func([]marketdata.Bar, []Event) ([]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) - _, err = c.OnBars(context.Background(), oneBar) + _, err = c.OnBars(context.Background(), oneBar, nil) 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) ([]Order, error) { return nil, nil }) + err := ListenAndServe(addr, nil, func([]marketdata.Bar, []Event) ([]Order, error) { return nil, nil }) assert.Error(t, err, addr) } }) @@ -117,6 +144,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) ([]Order, error) { return nil, nil })) + assert.Error(t, ListenAndServe(ln.Addr().String(), nil, func([]marketdata.Bar, []Event) ([]Order, error) { return nil, nil })) }) } diff --git a/strategy/strategy.go b/strategy/strategy.go index 226b5d0..31bf07f 100644 --- a/strategy/strategy.go +++ b/strategy/strategy.go @@ -5,9 +5,10 @@ // The protocol is two JSON messages, each preceded by its length as four // big-endian bytes. For every group of bars the trader sends // -// {"bars":[...]} +// {"bars":[...],"events":[...]} // -// (marketdata.Bar as encoding/json writes it) and the worker answers +// (marketdata.Bar as encoding/json writes it; the events are what the trader did +// since the last group, see Event, and may be empty) and the worker answers // // {"orders":[{"instrument":"EURUSD","action":"open","direction":"long", // "price":"1.10000","stop_loss":"1.09000"}]} @@ -25,13 +26,15 @@ package strategy import ( "fmt" "net" + "time" "github.com/rustyeddy/trader/marketdata" "github.com/rustyeddy/trader/num" ) type barsMessage struct { - Bars []marketdata.Bar `json:"bars"` + Bars []marketdata.Bar `json:"bars"` + Events []Event `json:"events"` } type ordersMessage struct { @@ -52,6 +55,49 @@ type Order struct { 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"` + // Ref is the worker's own label for the order. The trader echoes it on the + // order's events so the worker can tell which request an event is about. It + // means nothing to the trader. + Ref string `json:"ref,omitempty"` +} + +// Event kinds. +const ( + Accepted = "accepted" // the trader sized the order and queued it with the broker + Filled = "filled" // the broker executed it + Rejected = "rejected" // the trader or broker refused it; Reason says why + Cancelled = "cancelled" // it was accepted, then withdrawn before it filled +) + +// Event is something the trader did, reported to the worker with the next group +// of bars. It is a read-only fact: the worker cannot change it, and it carries no +// account or position state. Each event is delivered once, in the order it +// happened, and never before the trader knew it: an order the worker requests in +// one answer is first reported with the group after it. +type Event struct { + // Time is when it happened: the opening time of the bar it happened on. + Time time.Time `json:"time"` + // Kind is one of Accepted, Filled, Rejected, Cancelled. + Kind string `json:"kind"` + // ID is the order's id, assigned by the trader. It is empty for an order + // refused before it was queued, which never gets one. + ID string `json:"id,omitempty"` + // Ref is the Ref the worker gave the order; empty for an order the trader's + // broker made itself, such as a protective stop. + Ref string `json:"ref,omitempty"` + Instrument string `json:"instrument"` + Action string `json:"action"` + Direction string `json:"direction,omitempty"` + // Units is the size the trader chose, for Accepted and Filled. + Units num.Quantity `json:"units,omitzero"` + // Price is the fill price, for Filled. + Price num.Price `json:"price,omitzero"` + // Cause is why the order exists: "strategy" for a worker's order, or + // "stop loss", "take profit" or "margin closeout" for one the broker made. A + // Filled event with cause "stop loss" is a protective-stop exit. + Cause string `json:"cause"` + // Reason says why, for Rejected. + Reason string `json:"reason,omitempty"` } // 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 2812015..4eb2471 100644 --- a/strategy/strategy_test.go +++ b/strategy/strategy_test.go @@ -46,13 +46,13 @@ func TestClientAndServe(t *testing.T) { return } defer func() { _ = conn.Close() }() - done <- Serve(conn, func([]marketdata.Bar) ([]Order, error) { groups++; return nil, nil }) + done <- Serve(conn, func([]marketdata.Bar, []Event) ([]Order, error) { groups++; return nil, nil }) }() r, err := Dial(ln.Addr().String()) require.NoError(t, err) for i := 0; i < 5; i++ { - _, err := r.OnBars(context.Background(), oneBar) + _, err := r.OnBars(context.Background(), oneBar, nil) require.NoError(t, err) } require.NoError(t, r.Close()) diff --git a/strategy_remote.go b/strategy_remote.go index b6d1440..02dc692 100644 --- a/strategy_remote.go +++ b/strategy_remote.go @@ -16,9 +16,16 @@ import ( type remoteStrategy struct { id string client *remotestrat.Client + + // refs is the worker's label for each order it requested, to echo on the + // order's events. pending is what the trader has told it since the last + // group, sent with the next one. + refs map[*order]string + pending []remotestrat.Event } var _ strategy = (*remoteStrategy)(nil) +var _ feedbacker = (*remoteStrategy)(nil) // dialRemote connects to the worker listening at addr (loopback only). func dialRemote(id, addr string) (*remoteStrategy, error) { @@ -36,15 +43,41 @@ 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 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. +// feedback keeps what the trader did since the last group; the next onBars sends +// it to the worker. +func (r *remoteStrategy) feedback(events []feedbackEvent) { + for _, e := range events { + r.pending = append(r.pending, toWireEvent(e, r.refs[e.order])) + if e.state != stateSubmitted { + delete(r.refs, e.order) // filled, rejected or cancelled: nothing more to say + } + } +} + +// onBars hands the frame, and the events since the last one, 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) { - requests, err := r.client.OnBars(ctx, bars) + events := r.pending + r.pending = nil + requests, err := r.client.OnBars(ctx, bars, events) if err != nil { return nil, err } - return toOrders(requests, bars) + orders, err := toOrders(requests, bars) + if err != nil { + return nil, err + } + for i, o := range orders { + if ref := requests[i].Ref; ref != "" { + if r.refs == nil { + r.refs = make(map[*order]string) + } + r.refs[o] = ref + } + } + return orders, nil } // toOrders maps every request, in order, or fails without returning any. diff --git a/strategy_remote_test.go b/strategy_remote_test.go index 5ce83a4..797ad39 100644 --- a/strategy_remote_test.go +++ b/strategy_remote_test.go @@ -161,3 +161,52 @@ func TestToOrders(t *testing.T) { assert.Nil(t, orders, "the valid first order is not returned either") }) } + +func TestRemoteStrategy_Feedback(t *testing.T) { + ctx := context.Background() + bars := []marketdata.Bar{{Instrument: "EURUSD", Time: testStart, AvailableAt: testStart}} + at := testStart + broker := &order{id: "t-2", instrument: "EURUSD", action: close, direction: long, reason: reasonStopLoss, fillPrice: px(109000)} + + reqs := []remotestrat.Order{{Instrument: "EURUSD", Action: "open", Direction: "long", Price: px(110000), StopLoss: px(109000), Ref: "entry"}} + w := startWorker(t, workerOpts{decide: func(n int, _ []marketdata.Bar, _ []remotestrat.Event) ([]remotestrat.Order, error) { + if n == 1 { + return reqs, nil + } + return nil, nil + }}) + r, err := dialRemote("remote", w.addr) + require.NoError(t, err) + + orders, err := r.onBars(ctx, bars) // the worker asks for an order labelled "entry" + require.NoError(t, err) + require.Len(t, orders, 1) + assert.Equal(t, "entry", r.refs[orders[0]], "the adapter remembers the label of the order it made") + + // The trader hands over what happened; it goes with the next group. + r.feedback([]feedbackEvent{ + {orders[0], orderEvent{state: stateSubmitted, time: at}}, + {orders[0], orderEvent{state: stateFilled, time: at}}, + {broker, orderEvent{state: stateFilled, time: at}}, + }) + assert.Len(t, r.pending, 3) + assert.Empty(t, r.refs, "a filled order's label is forgotten") + + _, err = r.onBars(ctx, bars) + require.NoError(t, err) + assert.Empty(t, r.pending, "sent once") + _, err = r.onBars(ctx, bars) + require.NoError(t, err) + require.NoError(t, r.close()) + w.finish() + + require.Len(t, w.events, 3) + assert.Empty(t, w.events[0], "nothing before the first order") + require.Len(t, w.events[1], 3) + assert.Empty(t, w.events[2], "not repeated") + assert.Equal(t, []string{"accepted", "filled", "filled"}, []string{w.events[1][0].Kind, w.events[1][1].Kind, w.events[1][2].Kind}) + assert.Equal(t, "entry", w.events[1][0].Ref) + assert.Equal(t, "entry", w.events[1][1].Ref) + assert.Empty(t, w.events[1][2].Ref, "the broker's own stop was not the worker's order") + assert.Equal(t, "stop loss", w.events[1][2].Cause) +} diff --git a/trader.go b/trader.go index a0700bb..8d092c3 100644 --- a/trader.go +++ b/trader.go @@ -86,6 +86,9 @@ type trader struct { // in the order they were refused. They never join broker.completed, which // keeps the rejections that happen at the fill, so no order is in both. rejected []*order + + // feedbackLog is what a strategy that wants feedback has been told. + feedbackLog feedbackLog } // onBar handles one bar: onFrame with a frame of that bar alone. @@ -110,6 +113,9 @@ func (tr *trader) onFrame(ctx context.Context, f frame) error { return fmt.Errorf("broker on frame at %s: %w", f.availableAt.Format(time.RFC3339), err) } + if fb, ok := tr.strategy.(feedbacker); ok { + fb.feedback(tr.collectEvents()) + } orders, err := tr.strategy.onBars(ctx, f.bars) if err != nil { return fmt.Errorf("strategy %s: %w", tr.strategy.name(), err) @@ -146,6 +152,7 @@ func (tr *trader) handleOrder(ctx context.Context, o *order) error { return fmt.Errorf("strategy order at %s %s: %w", o.instrument, o.Time.Format(time.DateOnly), err) } + tr.watch(o) o.record(stateRequested, o.Time, "") if err := tr.broker.submit(ctx, o); err != nil { if !isRejection(err) { diff --git a/trader_test.go b/trader_test.go index 4bf90cb..28b185d 100644 --- a/trader_test.go +++ b/trader_test.go @@ -6,6 +6,7 @@ import ( "context" "encoding/json" "errors" + "fmt" "net" "os/exec" "path/filepath" @@ -234,7 +235,7 @@ func TestTrader_HandleOrder_Amend(t *testing.T) { 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) + decide func(n int, bars []marketdata.Bar, events []remotestrat.Event) ([]remotestrat.Order, error) } // testWorker is the example worker in process: it listens on loopback, serves one @@ -242,6 +243,7 @@ type workerOpts struct { type testWorker struct { addr string groups [][]marketdata.Bar + events [][]remotestrat.Event // what came with each group done sync.WaitGroup } @@ -260,14 +262,15 @@ func startWorker(t *testing.T, o workerOpts) *testWorker { return } defer func() { _ = conn.Close() }() - _ = remotestrat.Serve(conn, func(bars []marketdata.Bar) ([]remotestrat.Order, error) { + _ = remotestrat.Serve(conn, func(bars []marketdata.Bar, events []remotestrat.Event) ([]remotestrat.Order, error) { w.groups = append(w.groups, bars) + w.events = append(w.events, events) n := len(w.groups) if o.dropOn > 0 && n == o.dropOn { return nil, errors.New("hang up") } if o.decide != nil { - return o.decide(n, bars) + return o.decide(n, bars, events) } return nil, nil }) @@ -304,7 +307,12 @@ func (r *recorder) onBars(_ context.Context, bars []marketdata.Bar) ([]*order, e func newRemoteTrader(t *testing.T, s strategy) *trader { t.Helper() - tr := newTestTrader() + return newRemoteTraderWith(t, s, usd(1000)) +} + +func newRemoteTraderWith(t *testing.T, s strategy, balance money) *trader { + t.Helper() + tr := newTestTraderWithBalance(balance) tr.strategy = s return tr } @@ -398,16 +406,17 @@ func TestRemoteStrategy_ExampleWorker(t *testing.T) { require.NoError(t, remote.close()) require.NoError(t, cmd.Wait(), "the worker exits when the trader closes") - ref := newRemoteTrader(t, &openThenClose{}) + ref := newRemoteTrader(t, newInProcessRule(ruleStopDistance)) 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: ") + assert.Contains(t, stderr.String(), "event: 2010-01-07 filled open EURUSD strategy ref=\"entry\"", "the example prints what Trader reports") } -// 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. +// 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 below the close, +// close on the sixth if the entry filled and the stop has not taken it out. const ( ruleOpenAt = 3 ruleCloseAt = 6 @@ -415,31 +424,80 @@ const ( var ruleStopDistance = num.MustParsePrice("0.015") -func openThenCloseRequests(n int, bars []marketdata.Bar) ([]remotestrat.Order, error) { +// feedbackRule is that rule, driven by the events the trader reports. +type feedbackRule struct { + stopDistance num.Price + groups int + inPosition bool +} + +func (r *feedbackRule) step(bars []marketdata.Bar, events []remotestrat.Event) ([]remotestrat.Order, error) { + for _, e := range events { + switch { + case e.Ref == "entry" && e.Kind == remotestrat.Filled: + r.inPosition = true + case e.Kind == remotestrat.Filled && e.Cause == "stop loss": + r.inPosition = false + } + } + r.groups++ b := bars[0] c := b.OHLC().Close - switch n { - case ruleOpenAt: - stop, err := c.Sub(ruleStopDistance) + switch { + case r.groups == ruleOpenAt: + stop, err := c.Sub(r.stopDistance) 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: "open", Direction: "long", Price: c, StopLoss: stop, Ref: "entry"}}, nil + case r.groups == ruleCloseAt && r.inPosition: 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 } +// ruleWorker is the rule as the worker's callback. +func ruleWorker(distance num.Price) func(int, []marketdata.Bar, []remotestrat.Event) ([]remotestrat.Order, error) { + r := &feedbackRule{stopDistance: distance} + return func(_ int, bars []marketdata.Bar, events []remotestrat.Event) ([]remotestrat.Order, error) { + return r.step(bars, events) + } +} + +// inProcessRule is the same rule as an in-process strategy that asks for feedback: +// the reference the remote run is compared with. +type inProcessRule struct { + rule feedbackRule + entry *order + pending []remotestrat.Event + seen [][]remotestrat.Event // the events given with each group +} + +func newInProcessRule(distance num.Price) *inProcessRule { + return &inProcessRule{rule: feedbackRule{stopDistance: distance}} +} + +func (*inProcessRule) name() string { return "in-process-rule" } +func (*inProcessRule) init() error { return nil } + +func (s *inProcessRule) feedback(events []feedbackEvent) { + for _, e := range events { + ref := "" + if e.order == s.entry { + ref = "entry" + } + s.pending = append(s.pending, toWireEvent(e, ref)) + } +} -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) +func (s *inProcessRule) onBars(_ context.Context, bars []marketdata.Bar) ([]*order, error) { + events := s.pending + if events == nil { + events = []remotestrat.Event{} // as on the wire: an empty list, not null + } + s.pending = nil + s.seen = append(s.seen, events) + requests, err := s.rule.step(bars, events) if err != nil { return nil, err } @@ -448,6 +506,7 @@ func (s *openThenClose) onBars(_ context.Context, bars []marketdata.Bar) ([]*ord 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 + s.entry = o } else { o.action = close } @@ -459,16 +518,18 @@ func (s *openThenClose) onBars(_ context.Context, bars []marketdata.Bar) ([]*ord // 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 + orders []orderResult + rejected []orderResult // refused before they reached the broker + 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), + orders: orderResults(slices.Concat(tr.broker.completed, tr.broker.orders["EURUSD"])), + rejected: orderResults(tr.rejected), + balance: tr.account.balance, + trades: len(tr.account.trades), } } @@ -477,7 +538,7 @@ func TestRemoteStrategy_OrdersMatchInProcess(t *testing.T) { captureLogs(t) prices := scenarioPrices(t, remoteScenario) - inProc := newRemoteTrader(t, &openThenClose{}) + inProc := newRemoteTrader(t, newInProcessRule(ruleStopDistance)) 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") @@ -487,7 +548,7 @@ func TestRemoteStrategy_OrdersMatchInProcess(t *testing.T) { 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}) + w := startWorker(t, workerOpts{decide: ruleWorker(ruleStopDistance)}) remote, err := dialRemote("remote", w.addr) require.NoError(t, err) tr := newRemoteTrader(t, remote) @@ -502,6 +563,54 @@ func TestRemoteStrategy_OrdersMatchInProcess(t *testing.T) { assert.Equal(t, orderHistoryOf(inProc), orderHistoryOf(tr), "same lifecycle") } +func TestRemoteStrategy_FeedbackMatchesInProcess(t *testing.T) { + ctx := context.Background() + captureLogs(t) + tests := []struct { + name string + distance num.Price + balance money + want []string // "group: kind action cause" of every event the worker is told + }{ + {"fill then a close it asked for", ruleStopDistance, usd(1000), []string{ + "4: accepted open strategy", "4: filled open strategy", + "7: accepted close strategy", "7: filled close strategy"}}, + {"a protective stop takes it out and the worker skips its close", num.MustParsePrice("0.002"), usd(1000), []string{ + "4: accepted open strategy", "4: filled open strategy", + "6: filled close stop loss"}}, + {"a refusal at sizing reaches the worker with its reason", ruleStopDistance, usd(0), []string{ + "4: rejected open strategy"}}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + prices := scenarioPrices(t, remoteScenario) + + ref := newInProcessRule(tt.distance) + inProc := newRemoteTraderWith(t, ref, tt.balance) + require.NoError(t, (&backtest{}).runSeries(ctx, inProc, prices)) + + w := startWorker(t, workerOpts{decide: ruleWorker(tt.distance)}) + remote, err := dialRemote("remote", w.addr) + require.NoError(t, err) + tr := newRemoteTraderWith(t, remote, tt.balance) + require.NoError(t, (&backtest{}).runSeries(ctx, tr, prices)) + require.NoError(t, remote.close()) + w.finish() + + var told []string + for i, events := range w.events { + for _, e := range events { + told = append(told, fmt.Sprintf("%d: %s %s %s", i+1, e.Kind, e.Action, e.Cause)) + } + } + assert.Equal(t, tt.want, told) + assert.Equal(t, jsonOf(t, ref.seen), jsonOf(t, w.events), "the worker is told what the in-process strategy is told") + assert.Equal(t, outcomeOf(inProc), outcomeOf(tr), "and the runs end the same") + assert.Len(t, w.events, len(w.groups), "every group carries an events list, possibly empty") + }) + } +} + // orderHistoryOf is every order's lifecycle, by id. func orderHistoryOf(tr *trader) map[string][]orderEvent { histories := make(map[string][]orderEvent) @@ -528,7 +637,7 @@ func TestRemoteStrategy_BadOrdersFailTheRunAndProcessNone(t *testing.T) { } 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) { + w := startWorker(t, workerOpts{decide: func(n int, _ []marketdata.Bar, _ []remotestrat.Event) ([]remotestrat.Order, error) { if n == ruleOpenAt { return tt.orders, nil }