-
Notifications
You must be signed in to change notification settings - Fork 0
/
csvtssource.go
74 lines (62 loc) · 1.48 KB
/
csvtssource.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
package gopeat
import (
"encoding/csv"
"io"
"time"
)
// CsvToTs converts a csv line slice to a TimeStamper value
type CsvToTs func([]string) (TimeStamper, error)
// CsvTsSource implement a time stamped data source for
// csv data(with header). Client must provide CsvToTs to
// convert csv data to timestamper value
type CsvTsSource struct {
Symbol string
CsvStream io.Reader
CsvTsConv CsvToTs
csvReader *csv.Reader
startTime time.Time
endTime time.Time
recCount int64
MaxRecs int64
}
// Next implements an iterator for the contents of the csv data
func (st *CsvTsSource) Next() (TimeStamper, bool) {
if st.startTime.IsZero() {
panic("staticTradeSource: starttime not set")
}
if st.csvReader == nil {
st.csvReader = csv.NewReader(st.CsvStream)
_, _ = st.csvReader.Read()
}
var trd TimeStamper
for {
line, err := st.csvReader.Read()
if err == io.EOF {
break
} else if err != nil {
panic(err)
}
trd, _ = st.CsvTsConv(line)
if trd.GetTimeStamp().Before(st.startTime) {
continue
}
if trd.GetTimeStamp().After(st.endTime) {
break
}
st.recCount++
//early out if max recs count hit
if st.recCount == st.MaxRecs {
break
}
return trd, true
}
return nil, false
}
// SetStartTime sets min timpstamp for data provided
func (st *CsvTsSource) SetStartTime(startTime time.Time) {
st.startTime = startTime
}
// SetEndTime sets max timpstamp for data provided
func (st *CsvTsSource) SetEndTime(endTime time.Time) {
st.endTime = endTime
}