Repository navigation
Expand file tree
/
Copy pathexecution_test.go
More file actions
127 lines (114 loc) · 6.96 KB
/
Copy pathexecution_test.go
File metadata and controls
127 lines (114 loc) · 6.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package trader
import (
"testing"
"time"
"github.com/rustyeddy/trader/marketdata"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestOrder_Execution is the one table for which side of a bar triggers a
// resting order and what it fills at. Every row is a submitted EURUSD order
// made on day 0 seeing the day-1 bar.
func TestOrder_Execution(t *testing.T) {
type row struct {
name string
typ orderType
act action
dir direction
p int64
b marketdata.Bar
want int64
hit bool
}
rows := []row{
// Buy limit (long entry) and sell stop (long's protective stop) wait for a fall.
{"long limit entry, bar stays above", limitOrder, open, long, 109000, stopBar(110000, 110400, 109500, 110200), 0, false},
{"long limit entry, low touches", limitOrder, open, long, 109000, stopBar(110000, 110400, 109000, 110200), 109000, true},
{"long limit entry, gap below fills at the better open", limitOrder, open, long, 109000, stopBar(108000, 108500, 107500, 108200), 108000, true},
{"long stop exit, bar stays above", stopOrder, close, long, 108900, stopBar(110000, 110500, 109000, 110200), 0, false},
{"long stop exit, low touches", stopOrder, close, long, 108900, stopBar(110000, 110500, 108900, 110200), 108900, true},
{"long stop exit, trades through", stopOrder, close, long, 108900, stopBar(110000, 110500, 108000, 109500), 108900, true},
{"long stop exit, gap below fills at the worse open", stopOrder, close, long, 108900, stopBar(108000, 108500, 107500, 108200), 108000, true},
// Sell limit (long's take profit) and buy stop (long entry) wait for a rise.
{"long take profit, bar stays below", limitOrder, close, long, 112000, stopBar(110000, 111900, 109500, 110200), 0, false},
{"long take profit, high touches", limitOrder, close, long, 112000, stopBar(110000, 112000, 109500, 110200), 112000, true},
{"long take profit, gap above fills at the better open", limitOrder, close, long, 112000, stopBar(112500, 113000, 112200, 112800), 112500, true},
{"long stop entry, bar stays below", stopOrder, open, long, 110500, stopBar(110000, 110400, 109500, 110200), 0, false},
{"long stop entry, high touches", stopOrder, open, long, 110500, stopBar(110000, 110500, 109500, 110200), 110500, true},
{"long stop entry, gap above fills at the worse open", stopOrder, open, long, 110500, stopBar(111000, 111500, 110800, 111200), 111000, true},
// Shorts mirror.
{"short limit entry, high touches", limitOrder, open, short, 111000, stopBar(110000, 111000, 109500, 110500), 111000, true},
{"short limit entry, gap above fills at the better open", limitOrder, open, short, 111000, stopBar(111500, 111800, 111200, 111600), 111500, true},
{"short stop exit, bar stays below", stopOrder, close, short, 111100, stopBar(110000, 110500, 109000, 110200), 0, false},
{"short stop exit, high touches", stopOrder, close, short, 111100, stopBar(110000, 111100, 109000, 110200), 111100, true},
{"short stop exit, gap above fills at the worse open", stopOrder, close, short, 111100, stopBar(112000, 112500, 111800, 112200), 112000, true},
{"short take profit, bar stays above", limitOrder, close, short, 108000, stopBar(110000, 110400, 108100, 110200), 0, false},
{"short take profit, gap below fills at the better open", limitOrder, close, short, 108000, stopBar(107500, 107800, 107000, 107600), 107500, true},
{"short stop entry, bar stays above", stopOrder, open, short, 109000, stopBar(110000, 110400, 109500, 110200), 0, false},
{"short stop entry, low touches", stopOrder, open, short, 109000, stopBar(110000, 110400, 109000, 110200), 109000, true},
{"short stop entry, gap below fills at the worse open", stopOrder, open, short, 109000, stopBar(108000, 108500, 107500, 108200), 108000, true},
// Market orders fill at the open, whatever the range.
{"market open", marketOrder, open, long, 0, stopBar(110000, 110400, 109500, 110200), 110000, true},
{"market close", marketOrder, close, noDirection, 0, stopBar(110000, 110400, 109500, 110200), 110000, true},
// Unsupported types never execute, even when the bar crosses their price.
{"missing order type", noOrder, open, long, 110000, stopBar(110000, 110400, 109500, 110200), 0, false},
{"unknown order type", orderType(99), open, long, 110000, stopBar(110000, 110400, 109500, 110200), 0, false},
}
for _, tt := range rows {
t.Run(tt.name, func(t *testing.T) {
o := &order{instrument: "EURUSD", orderType: tt.typ, action: tt.act, direction: tt.dir,
price: px(tt.p), state: stateSubmitted, Time: testStart}
p, hit := o.execution(tt.b)
assert.Equal(t, tt.hit, hit)
assert.Equal(t, px(tt.want), p)
})
}
}
func TestOrder_Execution_StateInstrumentAndTime(t *testing.T) {
hit := stopBar(110000, 110500, 108000, 109500) // day 1, through 108900 and 110500
mk := func() *order {
return &order{instrument: "EURUSD", orderType: stopOrder, action: close, direction: long,
reason: reasonStopLoss, price: px(108900), state: stateSubmitted, Time: testStart}
}
o := mk()
o.state = stateFilled
_, ok := o.execution(hit)
assert.False(t, ok, "only a submitted order executes")
o = mk()
o.instrument = "GBPUSD"
_, ok = o.execution(hit)
assert.False(t, ok, "another instrument's bar says nothing")
o = mk()
o.Time = hit.Time.AddDate(0, 0, 1)
_, ok = o.execution(hit)
assert.False(t, ok, "never against a bar older than the order")
o = mk()
o.Time = hit.Time
_, ok = o.execution(hit)
assert.True(t, ok, "an exit may fill on the bar it was placed on")
entry := mk()
entry.reason, entry.action, entry.orderType, entry.price, entry.Time = reasonStrategy, open, limitOrder, px(109000), hit.Time
_, ok = entry.execution(hit)
assert.False(t, ok, "a strategy's order never fills on the bar that produced it")
market := mk()
market.reason, market.orderType, market.Time = reasonStrategy, marketOrder, hit.Time
_, ok = market.execution(hit)
assert.False(t, ok, "a strategy's market close included")
}
// The engine reads a bar's price through OHLC(): mid first, so a bar that also
// carries bid and ask fills on the mid, and a bar with only a bid fills on that.
func TestOrder_Execution_ReadsThePriceSeriesThroughOHLC(t *testing.T) {
mid := marketdata.OHLC{Open: px(110000), High: px(110400), Low: px(109500), Close: px(110200)}
bid := marketdata.OHLC{Open: px(109990), High: px(110390), Low: px(109490), Close: px(110190)}
ask := marketdata.OHLC{Open: px(110010), High: px(110410), Low: px(109510), Close: px(110210)}
mk := func() *order {
return &order{instrument: "EURUSD", orderType: marketOrder, action: open, direction: long,
state: stateSubmitted, Time: testStart}
}
next := testStart.Add(24 * time.Hour) // a strategy's order fills on a later bar
withAll, hit1 := mk().execution(marketdata.Bar{Instrument: "EURUSD", Time: next, Bid: bid, Ask: ask, Mid: mid})
bidOnly, hit2 := mk().execution(marketdata.Bar{Instrument: "EURUSD", Time: next, Bid: bid})
require.True(t, hit1 && hit2)
assert.Equal(t, px(110000), withAll, "mid wins when it is there")
assert.Equal(t, px(109990), bidOnly, "else the series the bar has")
}