-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
282 lines (244 loc) · 7.67 KB
/
parser.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
// Go ddfplus API
//
// Copyright 2019 Barchart.com, Inc. All rights reserved.
//
// This Source Code Form is subject to the terms of the GNU license
// available at https://github.com/barchart/go-ddfplus-api/blob/master/LICENSE.
package ddf
import (
"bytes"
"encoding/xml"
"fmt"
"log"
"strconv"
"time"
)
func ParseTimestamp(ba []byte, etxpos int) (time.Time, error) {
var (
t time.Time
)
st := etxpos + 1
xlen := len(ba) - st
if xlen != 7 && xlen != 9 {
return t, fmt.Errorf("invalid time length %d", len(ba)-st)
}
if ba[etxpos] != 3 {
return t, fmt.Errorf("invalid char when <ETX> expected. %d", ba[etxpos])
}
if ba[st] != 20 {
return t, fmt.Errorf("invalid century %d. Should be 20", ba[st])
}
year := int(ba[st])*100 + int(ba[st+1]-64)
month := int(ba[st+2] - 64)
date := int(ba[st+3] - 64)
hour := int(ba[st+4] - 64)
minute := int(ba[st+5] - 64)
second := int(ba[st+6] - 64)
ms := 0
if xlen == 9 {
ms = int((0xFF & ba[st+7])) + ((0xFF & int(ba[st+8])) << 8)
}
t = time.Date(year, time.Month(month), date, hour, minute, second, ms*1000, time.UTC)
return t, nil
}
func Parse(ba []byte) (Message, error) {
if len(ba) == 0 {
return nil, nil
}
if len(ba) < 10 {
log.Printf("Malformed message? %s\n", string(ba))
log.Println(ba)
return nil, nil
}
switch ba[0] {
case 1: // DDF message
switch ba[1] { // Record
case '#': // Timestamp
t, err := time.Parse("20060102150405", string(ba[2:16]))
if err == nil {
m := MessageTimestamp{}
m.Timestamp = t
return m, nil
}
case '2': //
info := DDFMessageInfo{}
info.Record = ba[1] // Record
// Find the comma
i := bytes.IndexByte(ba, ',')
if i == -1 {
return nil, fmt.Errorf("no comma in type 2")
}
pos := i
sym := string(ba[2:pos])
info.Subrecord = ba[pos+1]
info.BaseCode = string(ba[pos+3])
info.Exchange = string(ba[pos+4])
info.Delay, _ = strconv.Atoi(string(ba[pos+5 : pos+7]))
switch ba[pos+1] {
case '7': // Trades
m := MessageTrade{}
m.Symbol = sym
pos += 7
i := bytes.IndexByte(ba[pos:], ',')
if i == -1 {
return nil, fmt.Errorf("missing comma for trade")
}
m.Trade, _ = ParseFloat(string(ba[pos:pos+i]), info.BaseCode)
pos += i + 1
i = bytes.IndexByte(ba[pos:], ',')
if i == -1 {
return nil, fmt.Errorf("missing comma for trade size")
}
m.TradeSize, _ = strconv.ParseInt(string(ba[pos:pos+i]), 10, 64)
pos += i + 1
info.DayCode = ba[pos]
info.Session = ba[pos+1]
m.Timestamp, _ = ParseTimestamp(ba, pos+2)
m.Info = info
return m, nil
case '8': // Bid/Ask
m := MessageBidAsk{}
m.Symbol = sym
pos += 7
i := bytes.IndexByte(ba[pos:], ',')
if i == -1 {
return nil, fmt.Errorf("missing comma for bid")
}
m.Bid, _ = ParseFloat(string(ba[pos:pos+i]), info.BaseCode)
pos += i + 1
i = bytes.IndexByte(ba[pos:], ',')
if i == -1 {
return nil, fmt.Errorf("missing comma for bid size")
}
m.BidSize, _ = strconv.ParseInt(string(ba[pos:pos+i]), 10, 64)
pos += i + 1
i = bytes.IndexByte(ba[pos:], ',')
if i == -1 {
return nil, fmt.Errorf("missing comma for ask")
}
m.Ask, _ = ParseFloat(string(ba[pos:pos+i]), info.BaseCode)
pos += i + 1
i = bytes.IndexByte(ba[pos:], ',')
if i == -1 {
return nil, fmt.Errorf("missing comma for ask size")
}
m.AskSize, _ = strconv.ParseInt(string(ba[pos:pos+i]), 10, 64)
pos += i + 1
info.DayCode = ba[pos]
info.Session = ba[pos+1]
m.Timestamp, _ = ParseTimestamp(ba, pos+2)
m.Info = info
return m, nil
}
}
case 37: // '%' Refresh Message
if ba[1] == '<' {
type XMLSession struct {
XMLName xml.Name `xml:"SESSION"`
ID string `xml:"id,attr"`
Day string `xml:"day,attr"`
Session string `xml:"session,attr"`
Timestamp string `xml:"timestamp,attr"`
Open string `xml:"open,attr"`
High string `xml:"high,attr"`
Low string `xml:"low,attr"`
Last string `xml:"last,attr"`
Previous string `xml:"previous,attr"`
Settlement string `xml:"settlement,attr"`
TradeSize string `xml:"tradesize,attr"`
Volume string `xml:"volume,attr"`
OpenInterest string `xml:"openinterest,attr"`
NumTrades string `xml:"numtrades,attr"`
PriceVolume string `xml:"pricevolume,attr"`
TradeTime string `xml:"tradetime,attr"`
Ticks string `xml:"ticks,attr"`
}
type XMLQuote struct {
XMLName xml.Name `xml:"QUOTE"`
Sessions []XMLSession `xml:"SESSION"`
Symbol string `xml:"symbol,attr"`
Name string `xml:"name,attr"`
Exchange string `xml:"exchange,attr"`
BaseCode string `xml:"basecode,attr"`
PointValue string `xml:"pointvalue,attr"`
TickIncrement string `xml:"tickincrement,attr"`
DDFExchange string `xml:"ddfexchange,attr"`
Flag string `xml:"flag,attr"`
LastUpdate string `xml:"lastupdate,attr"`
Bid string `xml:"bid,attr"`
BidSize string `xml:"bidsize,attr"`
Ask string `xml:"ask,attr"`
AskSize string `xml:"asksize,attr"`
Mode string `xml:"mode,attr"`
}
var q XMLQuote
err := xml.Unmarshal(ba[1:], &q)
if err != nil {
return nil, err
}
m := MessageRefresh{}
m.Symbol = q.Symbol
m.Name = q.Name
m.Exchange = q.Exchange
m.DDFExchange = q.DDFExchange
m.BaseCode = q.BaseCode
m.TickIncrement, err = strconv.Atoi(q.TickIncrement)
m.PointValue, err = strconv.ParseFloat(q.PointValue, 32)
m.LastUpdate, err = time.Parse("20060102150405", q.LastUpdate)
m.Bid, err = ParseFloat(q.Bid, q.BaseCode)
m.BidSize, err = strconv.ParseInt(q.BidSize, 10, 64)
m.Ask, err = ParseFloat(q.Ask, q.BaseCode)
m.AskSize, err = strconv.ParseInt(q.AskSize, 10, 64)
for _, session := range q.Sessions {
var ptr *struct {
Day string
Session string
Timestamp time.Time
Open float64
High float64
Low float64
Last float64
Previous float64
Settlement float64
TradeSize int64
Volume int64
OpenInterest int64
NumTrades int64
PriceVolume float64
TradeTime time.Time
Ticks string
}
switch session.ID {
case "combined":
ptr = &m.CurrentSession
case "previous":
ptr = &m.PreviousSession
default:
continue
}
ptr.Day = session.Day
ptr.Session = session.Session
ptr.Timestamp, err = time.Parse("20060102150405", session.Timestamp)
ptr.Open, err = ParseFloat(session.Open, q.BaseCode)
ptr.High, err = ParseFloat(session.High, q.BaseCode)
ptr.Low, err = ParseFloat(session.Low, q.BaseCode)
ptr.Last, err = ParseFloat(session.Last, q.BaseCode)
ptr.Previous, err = ParseFloat(session.Previous, q.BaseCode)
ptr.Settlement, err = ParseFloat(session.Settlement, q.BaseCode)
ptr.TradeSize, err = strconv.ParseInt(session.TradeSize, 10, 64)
ptr.Volume, err = strconv.ParseInt(session.Volume, 10, 64)
ptr.OpenInterest, err = strconv.ParseInt(session.OpenInterest, 10, 64)
ptr.NumTrades, err = strconv.ParseInt(session.NumTrades, 10, 64)
ptr.PriceVolume, err = strconv.ParseFloat(session.OpenInterest, 64)
ptr.TradeTime, err = time.Parse("20060102150405", session.TradeTime)
ptr.Ticks = session.Ticks
}
return m, nil
} else {
return nil, fmt.Errorf("unsupported refresh message")
}
default:
return nil, fmt.Errorf("malformed message: %d \"%s\"", ba[0], string(ba))
}
return nil, nil
}