forked from nhedlund/qdownload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiqfeed.go
443 lines (351 loc) · 12.1 KB
/
iqfeed.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
package main
import (
"4d63.com/tz"
"bufio"
"compress/gzip"
"encoding/csv"
"fmt"
"io"
"net"
"os"
"path/filepath"
"strings"
"sync/atomic"
"time"
"github.com/apex/log"
)
type rowMapper func(iqfeedRow []string, tz *time.Location, config *Config) (outputRow string, err error)
type requestFactory func(symbol string, requestId string, config *Config) string
const (
errorMessage = "E"
stateMessage = "S"
endMessage = "!ENDMSG!"
secondTimestampFormat = "2006-01-02 15:04:05"
millisecondTimestampFormat = "2006-01-02 15:04:05.000"
csvSeparator = ","
tsvSeparator = "\t"
bufferSize = 4 * 1024 * 1024
)
type DownloadFunc func(string, *Config)
var (
previousRequestId int64 = 0
sourceLocation *time.Location
)
func init() {
location, err := tz.LoadLocation("America/New_York")
if err != nil {
log.Fatal("Could not load source time zone: America/New_York")
}
sourceLocation = location
}
func DownloadEod(symbol string, config *Config) {
header := "date,open,high,low,close,volume,oi"
download(symbol, createEodRequest, mapEodBar, header, config)
}
func DownloadMinute(symbol string, config *Config) {
header := "datetime,open,high,low,close,volume"
download(symbol, createMinuteRequest, mapMinuteBar, header, config)
}
func DownloadTicks(symbol string, config *Config) {
header := "datetime,last,lastsize,totalsize,bid,ask,tickid,basis,market,cond"
download(symbol, createTickRequest, mapTick, header, config)
}
func DownloadInterval(symbol string, config *Config) {
header := "datetime,open,high,low,close,volume"
download(symbol, createIntervalRequest, mapIntervalBar, header, config)
}
func download(symbol string, createRequest requestFactory, rowMapper rowMapper, csvHeader string, config *Config) {
successful := false
// Setup log context
ctx := log.WithFields(log.Fields{
"symbol": strings.ToUpper(symbol),
})
// Get target time zone
targetLocation, err := getTargetLocation(config.timeZone)
if err != nil {
ctx.WithError(err).Error("Could not load target time zone")
return
}
// Get output filename
filename := getFilename(symbol, config)
path := filepath.Join(config.outDirectory, filename)
// Check if output file already exists
if fileExists(path) {
ctx.Info("Already downloaded")
return
}
// Connect to IQFeed Historical socket
started := millisecondsTimestamp()
conn, err := net.Dial("tcp", "127.0.0.1:9100")
if err != nil {
ctx.WithError(err).Error("Could not connect to IQFeed at port 9100")
return
}
defer conn.Close()
// Set protocol
_, err = fmt.Fprintf(conn, "S,SET PROTOCOL,%s\r\n", config.protocol)
if err != nil {
ctx.WithError(err).Error("Could not set protocol")
return
}
// Send request
requestId := fmt.Sprintf("%d", atomic.AddInt64(&previousRequestId, 1))
request := createRequest(symbol, requestId, config)
ctx.Debug(request)
_, err = fmt.Fprintf(conn, "%s\r\n", request)
if err != nil {
ctx.WithError(err).Error("Could not send request")
return
}
ctx.Info("Downloading")
// Setup write pipeline
tmpPath := fmt.Sprintf("%s.tmp", path)
of, err := os.Create(tmpPath)
if err != nil {
ctx.WithError(err).Error("Could not create output file")
return
}
var pipe io.WriteCloser = of
if config.gzip {
pipe = gzip.NewWriter(of)
}
writer := bufio.NewWriterSize(pipe, bufferSize)
reader := csv.NewReader(bufio.NewReaderSize(conn, bufferSize))
reader.FieldsPerRecord = -1
// Defer closing output file and removing the file if an error occurred
defer func() {
_ = pipe.Close()
_ = of.Close()
if successful {
err = os.Rename(tmpPath, path)
if err != nil {
ctx.WithError(err).Error("Rename temporary file to output file error")
}
}
if !successful && fileExists(tmpPath) {
err = os.Remove(tmpPath)
if err != nil {
ctx.WithError(err).Error("Delete temporary download output file error")
}
}
}()
// Write header
header := csvHeader
if config.tsv {
header = strings.Replace(csvHeader, csvSeparator, tsvSeparator, -1)
}
_, err = fmt.Fprintln(writer, header)
if err != nil {
ctx.WithError(err).Error("Add header error")
return
}
// Process rows
rowCount := 0
for {
iqfeedRow, err := reader.Read()
if err == io.EOF {
break
} else if err != nil {
ctx.WithError(err).Error("Read row error")
return
}
if config.detailedLogging {
ctx.Debug(strings.Join(iqfeedRow, ","))
}
mappedRow, err := mapRow(iqfeedRow, requestId, rowMapper, targetLocation, config)
if err == io.EOF {
break
} else if err != nil {
ctx.WithError(err).Error("Map row error")
return
} else if mappedRow == "" {
continue
}
_, err = fmt.Fprintln(writer, mappedRow)
if err != nil {
ctx.WithError(err).Error("Write output row error")
return
}
rowCount++
}
err = writer.Flush()
if err != nil {
ctx.WithError(err).Error("Flush output file error")
}
successful = true
duration := millisecondsTimestamp() - started
ctx.WithFields(log.Fields{
"symbol": strings.ToUpper(symbol),
"duration": fmt.Sprintf("%dms", duration),
"rows": rowCount}).Info("Completed")
}
func getFilename(symbol string, config *Config) string {
filename := symbol
if config.tsv {
filename = fmt.Sprintf("%s.tsv", filename)
} else {
filename = fmt.Sprintf("%s.csv", filename)
}
if config.gzip {
filename = fmt.Sprintf("%s.gz", filename)
}
return filename
}
func getTargetLocation(timeZone string) (*time.Location, error) {
abbreviations := map[string]string{
"UTC": "UTC", // Handle lower case utc -> UTC
"": "America/New_York",
"ET": "America/New_York",
"EST": "America/New_York",
"CT": "America/Chicago",
"CST": "America/Chicago",
"PT": "America/Los_Angeles",
"PST": "America/Los_Angeles",
}
if timeZoneFromAbbreviation, found := abbreviations[strings.ToUpper(timeZone)]; found {
timeZone = timeZoneFromAbbreviation
}
return tz.LoadLocation(timeZone)
}
func mapRow(iqfeedRow []string, requestId string, rowMapper rowMapper, targetLocation *time.Location, config *Config) (outputRow string, err error) {
if len(iqfeedRow) == 0 {
return "", fmt.Errorf("empty row")
}
if iqfeedRow[0] == stateMessage {
return "", nil
}
if iqfeedRow[0] != requestId {
return "", fmt.Errorf("incorrect request id")
}
if iqfeedRow[1] == endMessage {
return "", io.EOF
}
if iqfeedRow[1] == errorMessage && len(iqfeedRow) >= 3 {
return "", fmt.Errorf("iqfeed error: %s", iqfeedRow[2])
}
outputRow, err = rowMapper(iqfeedRow, targetLocation, config)
if err != nil && err.Error() == "too few columns" {
return "", nil
} else if err != nil {
return "", fmt.Errorf("map row error: %s", iqfeedRow)
}
if config.tsv {
outputRow = strings.Replace(outputRow, csvSeparator, tsvSeparator, -1)
}
return outputRow, nil
}
func millisecondsTimestamp() int64 {
return time.Now().UnixNano() / (int64(time.Millisecond) / int64(time.Nanosecond))
}
// EOD
func createEodRequest(symbol string, requestId string, config *Config) string {
// HDT,[Symbol],[BeginDate],[EndDate],[MaxDatapoints],[DataDirection],[RequestID],[DatapointsPerSend]<CR><LF>
return fmt.Sprintf("HDT,%s,%s,%s,,1,%s", strings.ToUpper(symbol), config.startDate, config.endDate, requestId)
}
func mapEodBar(iqfeedRow []string, tz *time.Location, config *Config) (outputRow string, err error) {
if len(iqfeedRow) < 8 {
return "", fmt.Errorf("too few columns")
}
// Columns from IQFeed (unorthodox ordering of OHLC with High first):
// 1 2 3 4 5 6 7
// timestamp, high, low, open, close, volume, openInterest
return fmt.Sprintf("%s,%s,%s,%s,%s,%s,%s",
iqfeedRow[1], // date
iqfeedRow[4], // open
iqfeedRow[2], // high
iqfeedRow[3], // low
iqfeedRow[5], // close
iqfeedRow[6], // volume
iqfeedRow[7]), // open interest
nil
}
// Minute bars
func createMinuteRequest(symbol string, requestId string, config *Config) string {
// HIT,[Symbol],[Interval],[BeginDate BeginTime],[EndDate EndTime],[MaxDatapoints],[BeginFilterTime],[EndFilterTime],[DataDirection],[RequestID],[DatapointsPerSend],[IntervalType],[LabelAtBeginning]<CR><LF>
return fmt.Sprintf("HIT,%s,60,%s,%s,,,,1,%s", strings.ToUpper(symbol), config.startDate, config.endDate, requestId)
}
func mapMinuteBar(iqfeedRow []string, tz *time.Location, config *Config) (outputRow string, err error) {
if len(iqfeedRow) < 7 {
return "", fmt.Errorf("too few columns")
}
// NOTE: In version 5 of the IQFeed protocol minute bars are timestamped at the end of the bar
// and have to be adjusted -1 minute to be at the start of the bar (same as EOD bars)
timestamp, err := time.ParseInLocation(secondTimestampFormat, iqfeedRow[1], sourceLocation)
if err != nil {
return "", fmt.Errorf("could not parse minute bar timestamp: %s", err)
}
if !config.endTimestamp {
timestamp = timestamp.Add(-time.Minute * 1)
}
timestamp = timestamp.In(tz)
// Columns from IQFeed (unorthodox ordering of OHLC with High first):
// 1 2 3 4 5 6 7 8
// timestamp, high, low, open, close, totalVolume, periodVolume, numberOfTrades
return fmt.Sprintf("%s,%s,%s,%s,%s,%s",
timestamp.Format(secondTimestampFormat), // datetime
iqfeedRow[4], // open
iqfeedRow[2], // high
iqfeedRow[3], // low
iqfeedRow[5], // close
iqfeedRow[7]), // volume
nil
}
// Interval bars
func createIntervalRequest(symbol string, requestId string, config *Config) string {
// HIT,[Symbol],[Interval],[BeginDate BeginTime],[EndDate EndTime],[MaxDatapoints],[BeginFilterTime],[EndFilterTime],[DataDirection],[RequestID],[DatapointsPerSend],[IntervalType],[LabelAtBeginning]<CR><LF>
label := ""
if config.useLabels && config.endTimestamp {
label = ",0"
} else if config.useLabels && !config.endTimestamp {
label = ",1"
}
return fmt.Sprintf("HIT,%s,%d,%s,%s,,,,1,%s,,%s%s", strings.ToUpper(symbol), config.intervalLength, config.startDate, config.endDate, requestId, config.intervalType, label)
}
func mapIntervalBar(iqfeedRow []string, tz *time.Location, config *Config) (outputRow string, err error) {
if len(iqfeedRow) < 7 {
return "", fmt.Errorf("too few columns")
}
timestamp, err := time.ParseInLocation(secondTimestampFormat, iqfeedRow[1], sourceLocation)
if err != nil {
return "", fmt.Errorf("could not parse interval bar timestamp: %s", err)
}
timestamp = timestamp.In(tz)
// Columns from IQFeed (unorthodox ordering of OHLC with High first):
// 1 2 3 4 5 6 7 8
// timestamp, high, low, open, close, totalVolume, periodVolume, numberOfTrades
return fmt.Sprintf("%s,%s,%s,%s,%s,%s",
timestamp.Format(secondTimestampFormat), // datetime
iqfeedRow[4], // open
iqfeedRow[2], // high
iqfeedRow[3], // low
iqfeedRow[5], // close
iqfeedRow[7]), // volume
nil
}
// Ticks
func createTickRequest(symbol string, requestId string, config *Config) string {
// HTT,[Symbol],[BeginDate BeginTime],[EndDate EndTime],[MaxDatapoints],[BeginFilterTime],[EndFilterTime],[DataDirection],[RequestID],[DatapointsPerSend]<CR><LF>
return fmt.Sprintf("HTT,%s,%s,%s,,,,1,%s", strings.ToUpper(symbol), config.startDate, config.endDate, requestId)
}
func mapTick(iqfeedRow []string, tz *time.Location, config *Config) (outputRow string, err error) {
if len(iqfeedRow) < 11 {
return "", fmt.Errorf("too few columns")
}
timestamp, err := time.ParseInLocation(millisecondTimestampFormat, iqfeedRow[1], sourceLocation)
if err != nil {
return "", fmt.Errorf("could not parse interval bar timestamp: %s", err)
}
timestamp = timestamp.In(tz)
return fmt.Sprintf("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s",
timestamp.Format(millisecondTimestampFormat), // datetime
iqfeedRow[2], // last
iqfeedRow[3], // last size
iqfeedRow[4], // total size
iqfeedRow[5], // bid
iqfeedRow[6], // ask
iqfeedRow[7], // tick id
iqfeedRow[8], // basis
iqfeedRow[9], // market
iqfeedRow[10]), // conditions
nil
}