-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathinstrument_test.go
484 lines (414 loc) · 11.4 KB
/
instrument_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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
package goanda
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"time"
)
func TestGranularityFromDuration(t *testing.T) {
defer logTestResult(t, "GranularityFromDuration")
tests := []struct {
duration time.Duration
expected Granularity
hasError bool
}{
{time.Second * 5, GranularityFiveSeconds, false},
{time.Minute, GranularityMinute, false},
{time.Hour * 24, GranularityDay, false},
{time.Hour * 24 * 7, GranularityWeek, false},
{time.Second * 7, 0, true}, // Invalid granularity
}
for _, test := range tests {
result, err := GranularityFromDuration(test.duration)
if test.hasError {
if err == nil {
t.Errorf("Expected error for duration %v, but got none", test.duration)
}
} else {
if err != nil {
t.Errorf("Unexpected error for duration %v: %v", test.duration, err)
}
if result != test.expected {
t.Errorf("For duration %v, expected %v, but got %v", test.duration, test.expected, result)
}
}
}
}
func TestGranularityDuration(t *testing.T) {
defer logTestResult(t, "GranularityDuration")
tests := []struct {
granularity Granularity
expected time.Duration
}{
{GranularityFiveSeconds, time.Second * 5},
{GranularityMinute, time.Minute},
{GranularityDay, time.Hour * 24},
{GranularityWeek, time.Hour * 24 * 7},
}
for _, test := range tests {
result := test.granularity.Duration()
if result != test.expected {
t.Errorf("For granularity %v, expected %v, but got %v", test.granularity, test.expected, result)
}
}
}
func TestGranularityString(t *testing.T) {
defer logTestResult(t, "GranularityString")
tests := []struct {
granularity Granularity
expected string
}{
{GranularityFiveSeconds, "S5"},
{GranularityMinute, "M1"},
{GranularityDay, "D"},
{GranularityWeek, "W"},
}
for _, test := range tests {
result := test.granularity.String()
if result != test.expected {
t.Errorf("For granularity %v, expected %v, but got %v", test.granularity, test.expected, result)
}
}
}
func TestGetCandles(t *testing.T) {
defer logTestResult(t, "GetCandles")
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/instruments/EUR_USD/candles" {
t.Errorf("Unexpected path: %s", r.URL.Path)
http.Error(w, "Invalid path", http.StatusBadRequest)
return
}
count := r.URL.Query().Get("count")
if count != "10" {
t.Errorf("Expected count to be 10, got %s", count)
}
granularity := r.URL.Query().Get("granularity")
if granularity != "M1" {
t.Errorf("Expected granularity to be M1, got %s", granularity)
}
response := InstrumentHistory{
Instrument: "EUR_USD",
Granularity: "M1",
Candles: []Candles{
{
Complete: true,
Volume: 100,
Time: time.Now(),
Mid: Candle{
Open: 1.1000,
High: 1.1010,
Low: 1.0990,
Close: 1.1005,
},
},
},
}
json.NewEncoder(w).Encode(response)
}))
defer server.Close()
c := &Connection{
hostname: server.URL,
client: *server.Client(),
}
history, err := c.GetCandles("EUR_USD", 10, GranularityMinute)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if history.Instrument != "EUR_USD" {
t.Errorf("Expected Instrument to be EUR_USD, got %s", history.Instrument)
}
if history.Granularity != "M1" {
t.Errorf("Expected Granularity to be M1, got %s", history.Granularity)
}
if len(history.Candles) != 1 {
t.Fatalf("Expected 1 candle, got %d", len(history.Candles))
}
candle := history.Candles[0]
if !candle.Complete {
t.Errorf("Expected Complete to be true")
}
if candle.Volume != 100 {
t.Errorf("Expected Volume to be 100, got %d", candle.Volume)
}
if candle.Mid.Open != 1.1000 {
t.Errorf("Expected Open to be 1.1000, got %f", candle.Mid.Open)
}
}
func TestGetTimeToCandles(t *testing.T) {
defer logTestResult(t, "GetTimeToCandles")
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/instruments/EUR_USD/candles" {
t.Errorf("Unexpected path: %s", r.URL.Path)
http.Error(w, "Invalid path", http.StatusBadRequest)
return
}
count := r.URL.Query().Get("count")
if count != "10" {
t.Errorf("Expected count to be 10, got %s", count)
}
to := r.URL.Query().Get("to")
if to == "" {
t.Errorf("Expected 'to' parameter, but it's missing")
}
granularity := r.URL.Query().Get("granularity")
if granularity != "M1" {
t.Errorf("Expected granularity to be M1, got %s", granularity)
}
response := InstrumentHistory{
Instrument: "EUR_USD",
Granularity: "M1",
Candles: []Candles{
{
Complete: true,
Volume: 100,
Time: time.Now(),
Mid: Candle{
Open: 1.1000,
High: 1.1010,
Low: 1.0990,
Close: 1.1005,
},
},
},
}
json.NewEncoder(w).Encode(response)
}))
defer server.Close()
c := &Connection{
hostname: server.URL,
client: *server.Client(),
}
to := time.Now()
history, err := c.GetTimeToCandles("EUR_USD", 10, GranularityMinute, to)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if history.Instrument != "EUR_USD" {
t.Errorf("Expected Instrument to be EUR_USD, got %s", history.Instrument)
}
if history.Granularity != "M1" {
t.Errorf("Expected Granularity to be M1, got %s", history.Granularity)
}
if len(history.Candles) != 1 {
t.Fatalf("Expected 1 candle, got %d", len(history.Candles))
}
}
func TestGetTimeFromCandles(t *testing.T) {
defer logTestResult(t, "GetTimeFromCandles")
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/instruments/EUR_USD/candles" {
t.Errorf("Unexpected path: %s", r.URL.Path)
http.Error(w, "Invalid path", http.StatusBadRequest)
return
}
count := r.URL.Query().Get("count")
if count != "10" {
t.Errorf("Expected count to be 10, got %s", count)
}
from := r.URL.Query().Get("from")
if from == "" {
t.Errorf("Expected 'from' parameter, but it's missing")
}
granularity := r.URL.Query().Get("granularity")
if granularity != "M1" {
t.Errorf("Expected granularity to be M1, got %s", granularity)
}
response := InstrumentHistory{
Instrument: "EUR_USD",
Granularity: "M1",
Candles: []Candles{
{
Complete: true,
Volume: 100,
Time: time.Now(),
Mid: Candle{
Open: 1.1000,
High: 1.1010,
Low: 1.0990,
Close: 1.1005,
},
},
},
}
json.NewEncoder(w).Encode(response)
}))
defer server.Close()
c := &Connection{
hostname: server.URL,
client: *server.Client(),
}
from := time.Now().Add(-time.Hour)
history, err := c.GetTimeFromCandles("EUR_USD", 10, GranularityMinute, from)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if history.Instrument != "EUR_USD" {
t.Errorf("Expected Instrument to be EUR_USD, got %s", history.Instrument)
}
if history.Granularity != "M1" {
t.Errorf("Expected Granularity to be M1, got %s", history.Granularity)
}
if len(history.Candles) != 1 {
t.Fatalf("Expected 1 candle, got %d", len(history.Candles))
}
}
func TestGetBidAskCandles(t *testing.T) {
defer logTestResult(t, "GetBidAskCandles")
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/instruments/EUR_USD/candles" {
t.Errorf("Unexpected path: %s", r.URL.Path)
http.Error(w, "Invalid path", http.StatusBadRequest)
return
}
count := r.URL.Query().Get("count")
if count != "10" {
t.Errorf("Expected count to be 10, got %s", count)
}
granularity := r.URL.Query().Get("granularity")
if granularity != "M1" {
t.Errorf("Expected granularity to be M1, got %s", granularity)
}
price := r.URL.Query().Get("price")
if price != "BA" {
t.Errorf("Expected price to be BA, got %s", price)
}
response := BidAskCandles{
Candles: []struct {
Ask struct {
C float64 `json:"c,string"`
H float64 `json:"h,string"`
L float64 `json:"l,string"`
O float64 `json:"o,string"`
} `json:"ask"`
Bid struct {
C float64 `json:"c,string"`
H float64 `json:"h,string"`
L float64 `json:"l,string"`
O float64 `json:"o,string"`
} `json:"bid"`
Complete bool `json:"complete"`
Time time.Time `json:"time"`
Volume int `json:"volume"`
}{
{
Ask: struct {
C float64 `json:"c,string"`
H float64 `json:"h,string"`
L float64 `json:"l,string"`
O float64 `json:"o,string"`
}{
O: 1.1000,
H: 1.1010,
L: 1.0990,
C: 1.1005,
},
Bid: struct {
C float64 `json:"c,string"`
H float64 `json:"h,string"`
L float64 `json:"l,string"`
O float64 `json:"o,string"`
}{
O: 1.0998,
H: 1.1008,
L: 1.0988,
C: 1.1003,
},
Complete: true,
Time: time.Now(),
Volume: 100,
},
},
}
json.NewEncoder(w).Encode(response)
}))
defer server.Close()
c := &Connection{
hostname: server.URL,
client: *server.Client(),
}
candles, err := c.GetBidAskCandles("EUR_USD", "10", GranularityMinute)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if len(candles.Candles) != 1 {
t.Fatalf("Expected 1 candle, got %d", len(candles.Candles))
}
candle := candles.Candles[0]
if !candle.Complete {
t.Errorf("Expected Complete to be true")
}
if candle.Volume != 100 {
t.Errorf("Expected Volume to be 100, got %d", candle.Volume)
}
if candle.Ask.O != 1.1000 {
t.Errorf("Expected Ask Open to be 1.1000, got %f", candle.Ask.O)
}
if candle.Bid.O != 1.0998 {
t.Errorf("Expected Bid Open to be 1.0998, got %f", candle.Bid.O)
}
}
func TestOrderBook(t *testing.T) {
defer logTestResult(t, "OrderBook")
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/instruments/EUR_USD/orderBook" {
t.Errorf("Unexpected path: %s", r.URL.Path)
http.Error(w, "Invalid path", http.StatusBadRequest)
return
}
response := BrokerBook{
Instrument: "EUR_USD",
Time: time.Now(),
Price: "1.1000",
BucketWidth: "0.0001",
Buckets: []Bucket{
{
Price: "1.0999",
LongCountPercent: "40",
ShortCountPercent: "60",
},
{
Price: "1.1000",
LongCountPercent: "50",
ShortCountPercent: "50",
},
{
Price: "1.1001",
LongCountPercent: "60",
ShortCountPercent: "40",
},
},
}
json.NewEncoder(w).Encode(response)
}))
defer server.Close()
c := &Connection{
hostname: server.URL,
client: *server.Client(),
}
book, err := c.OrderBook("EUR_USD")
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if book.Instrument != "EUR_USD" {
t.Errorf("Expected Instrument to be EUR_USD, got %s", book.Instrument)
}
if book.Price != "1.1000" {
t.Errorf("Expected Price to be 1.1000, got %s", book.Price)
}
if book.BucketWidth != "0.0001" {
t.Errorf("Expected BucketWidth to be 0.0001, got %s", book.BucketWidth)
}
if len(book.Buckets) != 3 {
t.Fatalf("Expected 3 buckets, got %d", len(book.Buckets))
}
if book.Buckets[0].Price != "1.0999" {
t.Errorf("Expected first bucket Price to be 1.0999, got %s", book.Buckets[0].Price)
}
if book.Buckets[0].LongCountPercent != "40" {
t.Errorf("Expected first bucket LongCountPercent to be 40, got %s", book.Buckets[0].LongCountPercent)
}
if book.Buckets[0].ShortCountPercent != "60" {
t.Errorf("Expected first bucket ShortCountPercent to be 60, got %s", book.Buckets[0].ShortCountPercent)
}
}