-
Notifications
You must be signed in to change notification settings - Fork 26
/
time_series.go
230 lines (199 loc) · 5.25 KB
/
time_series.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
package av
import (
"encoding/csv"
"io"
"sort"
"time"
"github.com/pkg/errors"
)
// TimeSeries specifies a given time series to query for.
// For valid options, see the TimeSeries* package constants.
type TimeSeries uint8
const (
TimeSeriesDaily TimeSeries = iota
TimeSeriesDailyAdjusted
TimeSeriesWeekly
TimeSeriesWeeklyAdjusted
TimeSeriesMonthly
TimeSeriesMonthlyAdjusted
timeSeriesIntraday // intentionally not exported
)
func (t TimeSeries) String() string {
switch t {
case timeSeriesIntraday:
return "TimeSeriesIntraday"
case TimeSeriesDaily:
return "TimeSeriesDaily"
case TimeSeriesDailyAdjusted:
return "TimeSeriesDailyAdjusted"
case TimeSeriesWeekly:
return "TimeSeriesWeekly"
case TimeSeriesWeeklyAdjusted:
return "TimeSeriesWeeklyAdjusted"
case TimeSeriesMonthly:
return "TimeSeriesMonthly"
case TimeSeriesMonthlyAdjusted:
return "TimeSeriesMonthlyAdjusted"
}
return "TimeSeriesUnknown"
}
// keyName returns the name of the TimeSeries used for Alpha Vantage API
func (t TimeSeries) keyName() string {
switch t {
case timeSeriesIntraday:
return "TIME_SERIES_INTRADAY"
case TimeSeriesDaily:
return "TIME_SERIES_DAILY"
case TimeSeriesDailyAdjusted:
return "TIME_SERIES_DAILY_ADJUSTED"
case TimeSeriesWeekly:
return "TIME_SERIES_WEEKLY"
case TimeSeriesWeeklyAdjusted:
return "TIME_SERIES_WEEKLY_ADJUSTED"
case TimeSeriesMonthly:
return "TIME_SERIES_MONTHLY"
case TimeSeriesMonthlyAdjusted:
return "TIME_SERIES_MONTHLY_ADJUSTED"
}
return "UNKNOWN"
}
// TimeInterval specifies a frequency to query for intraday stock data.
// For valid options, see the TimeInterval* package constants.
type TimeInterval uint8
const (
TimeIntervalOneMinute TimeInterval = iota
TimeIntervalFiveMinute
TimeIntervalFifteenMinute
TimeIntervalThirtyMinute
TimeIntervalSixtyMinute
)
func (t TimeInterval) String() string {
switch t {
case TimeIntervalOneMinute:
return "TimeIntervalOneMinute"
case TimeIntervalFiveMinute:
return "TimeIntervalFiveMinute"
case TimeIntervalFifteenMinute:
return "TimeIntervalFifteenMinute"
case TimeIntervalThirtyMinute:
return "TimeIntervalThirtyMinute"
case TimeIntervalSixtyMinute:
return "TimeIntervalSixtyMinute"
}
return "TimeIntervalUnknown"
}
// keyName returns the name of the TimeInterval used for Alpha Vantage API
func (t TimeInterval) keyName() string {
switch t {
case TimeIntervalOneMinute:
return "1min"
case TimeIntervalFiveMinute:
return "5min"
case TimeIntervalFifteenMinute:
return "15min"
case TimeIntervalThirtyMinute:
return "30min"
case TimeIntervalSixtyMinute:
return "60min"
}
return "unknown"
}
var (
// timeSeriesDateFormats are the expected date formats in time series data
timeSeriesDateFormats = []string{
"2006-01-02",
"2006-01-02 15:04:05",
}
)
// TimeSeriesValue is a piece of data for a given time about stock prices
type TimeSeriesValue struct {
Time time.Time
Open float64
High float64
Low float64
Close float64
Volume float64
}
// sortTimeSeriesValuesByDate allows TimeSeriesValue
// slices to be sorted by date in ascending order
type sortTimeSeriesValuesByDate []*TimeSeriesValue
func (b sortTimeSeriesValuesByDate) Len() int { return len(b) }
func (b sortTimeSeriesValuesByDate) Less(i, j int) bool { return b[i].Time.Before(b[j].Time) }
func (b sortTimeSeriesValuesByDate) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
// parseTimeSeriesData will parse csv data from a reader
func parseTimeSeriesData(r io.Reader) ([]*TimeSeriesValue, error) {
reader := csv.NewReader(r)
reader.ReuseRecord = true // optimization
reader.LazyQuotes = true
reader.TrailingComma = true
reader.TrimLeadingSpace = true
// strip header
if _, err := reader.Read(); err != nil {
if err == io.EOF {
return nil, nil
}
return nil, err
}
values := make([]*TimeSeriesValue, 0, 64)
for {
record, err := reader.Read()
if err != nil {
if err == io.EOF {
break
}
return nil, err
}
value, err := parseTimeSeriesRecord(record)
if err != nil {
return nil, err
}
values = append(values, value)
}
// sort values by date
sort.Sort(sortTimeSeriesValuesByDate(values))
return values, nil
}
// parseDigitalCurrencySeriesRecord will parse an individual csv record
func parseTimeSeriesRecord(s []string) (*TimeSeriesValue, error) {
// these are the expected columns in the csv record
const (
timestamp = iota
open
high
low
close
volume
)
value := &TimeSeriesValue{}
d, err := parseDate(s[timestamp], timeSeriesDateFormats...)
if err != nil {
return nil, errors.Wrapf(err, "error parsing timestamp %s", s[timestamp])
}
value.Time = d
f, err := parseFloat(s[open])
if err != nil {
return nil, errors.Wrapf(err, "error parsing open %s", s[open])
}
value.Open = f
f, err = parseFloat(s[high])
if err != nil {
return nil, errors.Wrapf(err, "error parsing high %s", s[high])
}
value.High = f
f, err = parseFloat(s[low])
if err != nil {
return nil, errors.Wrapf(err, "error parsing low %s", s[low])
}
value.Low = f
f, err = parseFloat(s[close])
if err != nil {
return nil, errors.Wrapf(err, "error parsing close %s", s[close])
}
value.Close = f
f, err = parseFloat(s[volume])
if err != nil {
return nil, errors.Wrapf(err, "error parsing volume %s", s[volume])
}
value.Volume = f
return value, nil
}