-
Notifications
You must be signed in to change notification settings - Fork 121
/
historical_data_test.go
121 lines (97 loc) · 2.7 KB
/
historical_data_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
package ib
import (
"testing"
"time"
)
func TestHistoricalData(t *testing.T) {
engine := NewTestEngine(t)
defer engine.ConditionalStop(t)
contract := Contract{
Symbol: "GBP",
SecurityType: "CASH",
Exchange: "IDEALPRO",
Currency: "USD",
}
req := &RequestHistoricalData{
Contract: contract,
EndDateTime: time.Now(),
Duration: "1 M",
BarSize: HistBarSize1Day,
WhatToShow: HistMidpoint,
UseRTH: true,
}
id := engine.NextRequestID()
req.SetID(id)
ch := make(chan Reply)
engine.Subscribe(ch, id)
defer engine.Unsubscribe(ch, id)
defer engine.Send(&CancelHistoricalData{id})
if err := engine.Send(req); err != nil {
t.Fatalf("client %d: cannot send a historical data request: %s", engine.ClientID(), err)
}
rep, err := engine.expect(t, 30, ch, []IncomingMessageID{mHistoricalData})
logreply(t, rep, err)
if err != nil {
t.Fatalf("client %d: cannot receive historical data: %s", engine.ClientID(), err)
}
checkHistDataReply(t, rep)
}
func TestHistBarSizes(t *testing.T) {
engine := NewTestEngine(t)
defer engine.ConditionalStop(t)
m := map[HistDataBarSize]string{
HistBarSize1Sec: "30 S",
HistBarSize30Sec: "600 S",
HistBarSize1Min: "600 S",
HistBarSize5Min: "1800 S",
HistBarSize1Hour: "1 D",
HistBarSize1Day: "5 D",
}
contract := Contract{
Symbol: "EUR",
SecurityType: "CASH",
Exchange: "IDEALPRO",
Currency: "USD",
}
ch := make(chan Reply)
for barSize, duration := range m {
req := &RequestHistoricalData{
Contract: contract,
EndDateTime: time.Now(),
Duration: duration,
BarSize: barSize,
WhatToShow: HistMidpoint,
UseRTH: true,
}
id := engine.NextRequestID()
req.SetID(id)
t.Logf("barSize: %s duration: %s id: %d", barSize, duration, id)
engine.Subscribe(ch, id)
defer engine.Unsubscribe(ch, id)
defer engine.Send(&CancelHistoricalData{id})
if err := engine.Send(req); err != nil {
t.Fatalf("client %d: cannot send a historical data request: %s", engine.ClientID(), err)
}
rep, err := engine.expect(t, 30, ch, []IncomingMessageID{mHistoricalData})
if err != nil {
t.Fatalf("error in reply, error: %v", err)
}
// logreply(t, rep, err)
checkHistDataReply(t, rep)
}
}
func checkHistDataReply(t *testing.T, rep Reply) {
hd, ok := rep.(*HistoricalData)
if !ok {
t.Fatalf("couldn't convert the reply to HistoricalData, got %T", rep)
}
if len(hd.Data) < 1 {
t.Fatalf("expected some data, got: %v", hd.Data)
}
if hd.Data[0].Close <= 0.0 {
t.Fatalf("expected a close greater than zero, got: %.4f", hd.Data[0].Close)
}
for _, data := range hd.Data {
t.Logf("%s: %.4f %.4f %.4f %.4f\n", data.Date, data.Open, data.High, data.Low, data.Close)
}
}