-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrade_test.go
365 lines (337 loc) · 8.27 KB
/
trade_test.go
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
package ibsync
import (
"reflect"
"testing"
"time"
)
func TestStatus_IsActive(t *testing.T) {
tests := []struct {
name string
status Status
want bool
}{
{"PendingSubmit is active", PendingSubmit, true},
{"ApiPending is active", ApiPending, true},
{"PreSubmitted is active", PreSubmitted, true},
{"Submitted is active", Submitted, true},
{"Cancelled is not active", Cancelled, false},
{"Filled is not active", Filled, false},
{"ApiCancelled is not active", ApiCancelled, false},
{"Inactive is not active", Inactive, false},
{"Empty status is not active", Status(""), false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.status.IsActive(); got != tt.want {
t.Errorf("Status.IsActive() = %v, want %v", got, tt.want)
}
})
}
}
func TestStatus_IsDone(t *testing.T) {
tests := []struct {
name string
status Status
want bool
}{
{"Filled is done", Filled, true},
{"Cancelled is done", Cancelled, true},
{"ApiCancelled is done", ApiCancelled, true},
{"PendingSubmit is not done", PendingSubmit, false},
{"Submitted is not done", Submitted, false},
{"PreSubmitted is not done", PreSubmitted, false},
{"ApiPending is not done", ApiPending, false},
{"Inactive is not done", Inactive, false},
{"Empty status is not done", Status(""), false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.status.IsDone(); got != tt.want {
t.Errorf("Status.IsDone() = %v, want %v", got, tt.want)
}
})
}
}
func TestFill_Matches(t *testing.T) {
timeStr := "20240102-15:04:05"
testTime, _ := time.Parse("20060102-15:04:05", timeStr)
tests := []struct {
name string
fill *Fill
filter ExecutionFilter
want bool
}{
{
name: "nil fill never matches",
fill: nil,
filter: ExecutionFilter{},
want: false,
},
{
name: "empty filter matches valid fill",
fill: &Fill{
Contract: &Contract{Symbol: "AAPL", SecType: "STK"},
Execution: &Execution{
AcctNumber: "123",
ClientID: 456,
Exchange: "NASDAQ",
Side: "BUY",
},
Time: testTime,
},
filter: ExecutionFilter{},
want: true,
},
{
name: "matching all criteria",
fill: &Fill{
Contract: &Contract{Symbol: "AAPL", SecType: "STK"},
Execution: &Execution{
AcctNumber: "123",
ClientID: 456,
Exchange: "NASDAQ",
Side: "BUY",
},
Time: testTime,
},
filter: ExecutionFilter{
AcctCode: "123",
ClientID: 456,
Exchange: "NASDAQ",
SecType: "STK",
Side: "BUY",
Symbol: "AAPL",
Time: timeStr,
},
want: true,
},
{
name: "non-matching account",
fill: &Fill{
Contract: &Contract{Symbol: "AAPL", SecType: "STK"},
Execution: &Execution{AcctNumber: "123"},
},
filter: ExecutionFilter{AcctCode: "456"},
want: false,
},
{
name: "non-matching client ID",
fill: &Fill{
Contract: &Contract{Symbol: "AAPL", SecType: "STK"},
Execution: &Execution{
AcctNumber: "123",
ClientID: 456,
},
},
filter: ExecutionFilter{ClientID: 789},
want: false,
},
{
name: "non-matching exchange",
fill: &Fill{
Contract: &Contract{Symbol: "AAPL", SecType: "STK"},
Execution: &Execution{
AcctNumber: "123",
Exchange: "NASDAQ",
},
},
filter: ExecutionFilter{Exchange: "NYSE"},
want: false,
},
{
name: "non-matching security type",
fill: &Fill{
Contract: &Contract{Symbol: "AAPL", SecType: "STK"},
Execution: &Execution{AcctNumber: "123"},
},
filter: ExecutionFilter{SecType: "OPT"},
want: false,
},
{
name: "non-matching side",
fill: &Fill{
Contract: &Contract{Symbol: "AAPL", SecType: "STK"},
Execution: &Execution{
AcctNumber: "123",
Side: "BUY",
},
},
filter: ExecutionFilter{Side: "SELL"},
want: false,
},
{
name: "non-matching symbol",
fill: &Fill{
Contract: &Contract{Symbol: "AAPL", SecType: "STK"},
Execution: &Execution{AcctNumber: "123"},
},
filter: ExecutionFilter{Symbol: "MSFT"},
want: false,
},
{
name: "non-matching time",
fill: &Fill{
Contract: &Contract{Symbol: "AAPL", SecType: "STK"},
Execution: &Execution{AcctNumber: "123"},
Time: testTime,
},
filter: ExecutionFilter{Time: "20240102-16:04:05"},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.fill.Matches(tt.filter); got != tt.want {
t.Errorf("Fill.Matches() = %v, want %v", got, tt.want)
}
})
}
}
func TestNewTrade(t *testing.T) {
contract := &Contract{Symbol: "AAPL"}
order := &Order{OrderID: 123}
customStatus := OrderStatus{
OrderID: 123,
Status: Submitted,
}
tests := []struct {
name string
contract *Contract
order *Order
status []OrderStatus
wantErr bool
}{
{
name: "basic creation",
contract: contract,
order: order,
status: nil,
wantErr: false,
},
{
name: "with custom status",
contract: contract,
order: order,
status: []OrderStatus{customStatus},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
trade := NewTrade(tt.contract, tt.order, tt.status...)
if (trade == nil) != tt.wantErr {
t.Errorf("NewTrade() error = %v, wantErr %v", trade == nil, tt.wantErr)
return
}
if trade != nil {
if trade.Contract != tt.contract {
t.Errorf("NewTrade() contract = %v, want %v", trade.Contract, tt.contract)
}
if trade.Order != tt.order {
t.Errorf("NewTrade() order = %v, want %v", trade.Order, tt.order)
}
}
})
}
}
func TestTrade_Fills(t *testing.T) {
trade := NewTrade(&Contract{}, &Order{})
fill1 := &Fill{Time: time.Now()}
fill2 := &Fill{Time: time.Now()}
trade.addFill(fill1)
trade.addFill(fill2)
fills := trade.Fills()
if len(fills) != 2 {
t.Errorf("Trade.Fills() len = %v, want %v", len(fills), 2)
}
// Verify that modifying returned fills doesn't affect original
fills[0] = &Fill{}
if reflect.DeepEqual(fills[0], trade.fills[0]) {
t.Error("Trade.Fills() returned slice should be a copy")
}
}
func TestTrade_Logs(t *testing.T) {
trade := NewTrade(&Contract{}, &Order{})
// Verify initial log entry
logs := trade.Logs()
if len(logs) != 1 {
t.Errorf("New trade should have 1 initial log entry, got %d", len(logs))
}
if logs[0].Status != PendingSubmit {
t.Errorf("Initial log status = %v, want %v", logs[0].Status, PendingSubmit)
}
// Add new log entry
newEntry := TradeLogEntry{
Time: time.Now(),
Status: Submitted,
Message: "Test message",
}
trade.addLog(newEntry)
// Verify log was added
logs = trade.Logs()
if len(logs) != 2 {
t.Errorf("Trade.Logs() len = %v, want %v", len(logs), 2)
}
if !reflect.DeepEqual(logs[1], newEntry) {
t.Errorf("Trade.Logs()[1] = %v, want %v", logs[1], newEntry)
}
}
func TestTrade_IsActive(t *testing.T) {
tests := []struct {
name string
status Status
want bool
}{
{"active status", PendingSubmit, true},
{"inactive status", Cancelled, false},
{"done status", Filled, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
trade := NewTrade(&Contract{}, &Order{}, OrderStatus{Status: tt.status})
if got := trade.IsActive(); got != tt.want {
t.Errorf("Trade.IsActive() = %v, want %v", got, tt.want)
}
})
}
}
func TestTrade_IsDone(t *testing.T) {
tests := []struct {
name string
status Status
want bool
}{
{"done status", Filled, true},
{"active status", PendingSubmit, false},
{"inactive status", Inactive, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
trade := NewTrade(&Contract{}, &Order{}, OrderStatus{Status: tt.status})
if got := trade.IsDone(); got != tt.want {
t.Errorf("Trade.IsDone() = %v, want %v", got, tt.want)
}
})
}
}
func TestTrade_Done(t *testing.T) {
trade := NewTrade(&Contract{}, &Order{})
// Test initial state
select {
case <-trade.Done():
t.Error("Done channel should not be closed initially")
default:
// Expected behavior
}
// Mark as done
trade.markDoneSafe()
// Verify channel is closed
select {
case <-trade.Done():
// Expected behavior
default:
t.Error("Done channel should be closed after markDoneSafe()")
}
// Verify multiple markDone calls don't panic
trade.markDoneSafe()
}