-
Notifications
You must be signed in to change notification settings - Fork 0
/
tradingsession.go
400 lines (335 loc) · 8.61 KB
/
tradingsession.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
package main
import (
"encoding/csv"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"math/rand"
"net/http"
"net/url"
"sort"
"strconv"
"strings"
"time"
)
// tradingSessionSource is a source of trading sessions.
type tradingSessionSource string
// List of possible tradingSessionSource values.
const (
google tradingSessionSource = "google"
yahoo = "yahoo"
random = "random"
)
// Random sources to use when the random source is used.
var randomSources = []tradingSessionSource{
google,
yahoo,
}
// tradingSessionFunc is a function that returns tradingSessions.
type tradingSessionFunc func(symbol string, startDate, endDate time.Time) ([]tradingSession, error)
func getTradingSessionFunc(source tradingSessionSource) (tradingSessionFunc, error) {
switch source {
case google:
return getTradingSessionsFromGoogle, nil
case yahoo:
return getTradingSessionsFromYahoo, nil
case random:
return getTradingSessionsFromRandom, nil
default:
return nil, fmt.Errorf("unrecognized value: %s", source)
}
}
// tradingSession contains stats from a single trading session.
type tradingSession struct {
date time.Time
open float64
high float64
low float64
close float64
volume int64
}
func getTradingSessionsFromRandom(symbol string, startDate, endDate time.Time) ([]tradingSession, error) {
for _, v := range rand.Perm(len(randomSources)) {
s := randomSources[v]
getTradingSessions, err := getTradingSessionFunc(s)
if err != nil {
return nil, err
}
tss, err := getTradingSessions(symbol, startDate, endDate)
if err != nil {
log.Printf("tradingFunc %s: %v", s, err)
continue
}
return tss, nil
}
return nil, fmt.Errorf("all %d tradingFuncs failed", len(randomSources))
}
func getTradingSessionsFromGoogle(symbol string, startDate, endDate time.Time) ([]tradingSession, error) {
formatTime := func(date time.Time) string {
return date.Format("Jan 02, 2006")
}
v := url.Values{}
v.Set("q", symbol)
v.Set("startdate", formatTime(startDate))
v.Set("enddate", formatTime(endDate))
v.Set("output", "csv")
u, err := url.Parse("http://www.google.com/finance/historical")
if err != nil {
return nil, err
}
u.RawQuery = v.Encode()
log.Printf("GET %s", u)
resp, err := http.Get(u.String())
if err != nil {
return nil, err
}
defer resp.Body.Close()
var tss []tradingSession
r := csv.NewReader(resp.Body)
for i := 0; ; i++ {
record, err := r.Read()
if err != nil {
if err == io.EOF {
break
}
return nil, err
}
// format: Date, Open, High, Low, Close, Volume
if len(record) != 6 {
return nil, fmt.Errorf("record length should be 6, got %d", len(record))
}
// skip header row
if i != 0 {
parseRecordTime := func(i int) (time.Time, error) {
return time.Parse("2-Jan-06", record[i])
}
parseRecordFloat := func(i int) (float64, error) {
return parseFloat(record[i])
}
parseRecordInt := func(i int) (int64, error) {
return strconv.ParseInt(record[i], 10, 64)
}
date, err := parseRecordTime(0)
if err != nil {
return nil, err
}
open, err := parseRecordFloat(1)
if err != nil {
return nil, err
}
high, err := parseRecordFloat(2)
if err != nil {
return nil, err
}
low, err := parseRecordFloat(3)
if err != nil {
return nil, err
}
close, err := parseRecordFloat(4)
if err != nil {
return nil, err
}
volume, err := parseRecordInt(5)
if err != nil {
return nil, err
}
tss = append(tss, tradingSession{
date: date,
open: open,
high: high,
low: low,
close: close,
volume: volume,
})
}
}
// Most recent trading sessions at the front.
sort.Reverse(sortableTradingSessions(tss))
return tss, nil
}
func getTradingSessionsFromYahoo(symbol string, startDate, endDate time.Time) ([]tradingSession, error) {
v := url.Values{}
v.Set("s", symbol)
v.Set("a", strconv.Itoa(int(startDate.Month())-1))
v.Set("b", strconv.Itoa(startDate.Day()))
v.Set("c", strconv.Itoa(startDate.Year()))
v.Set("d", strconv.Itoa(int(endDate.Month())-1))
v.Set("e", strconv.Itoa(endDate.Day()))
v.Set("f", strconv.Itoa(endDate.Year()))
v.Set("g", "d")
v.Set("ignore", ".csv")
u, err := url.Parse("http://ichart.yahoo.com/table.csv")
if err != nil {
return nil, err
}
u.RawQuery = v.Encode()
log.Printf("GET %s", u)
resp, err := http.Get(u.String())
if err != nil {
return nil, err
}
defer resp.Body.Close()
var tss []tradingSession
r := csv.NewReader(resp.Body)
for i := 0; ; i++ {
record, err := r.Read()
if err != nil {
if err == io.EOF {
break
}
return nil, err
}
// format: Date, Open, High, Low, Close, Volume, Adj. Close
if len(record) != 7 {
return nil, fmt.Errorf("record length should be 7, got %d", len(record))
}
// skip header row
if i != 0 {
parseRecordTime := func(i int) (time.Time, error) {
return time.Parse("2006-01-02", record[i])
}
parseRecordFloat := func(i int) (float64, error) {
return parseFloat(record[i])
}
parseRecordInt := func(i int) (int64, error) {
return strconv.ParseInt(record[i], 10, 64)
}
date, err := parseRecordTime(0)
if err != nil {
return nil, err
}
open, err := parseRecordFloat(1)
if err != nil {
return nil, err
}
high, err := parseRecordFloat(2)
if err != nil {
return nil, err
}
low, err := parseRecordFloat(3)
if err != nil {
return nil, err
}
close, err := parseRecordFloat(4)
if err != nil {
return nil, err
}
volume, err := parseRecordInt(5)
if err != nil {
return nil, err
}
// Ignore adjusted close value to keep Google and Yahoo APIs the same.
tss = append(tss, tradingSession{
date: date,
open: open,
high: high,
low: low,
close: close,
volume: volume,
})
}
}
// Most recent trading sessions at the front.
sort.Reverse(sortableTradingSessions(tss))
return tss, nil
}
type liveTradingSession struct {
symbol string
timestamp time.Time
price float64
change float64
percentChange float64
}
func getLiveTradingSessions(symbols []string) ([]liveTradingSession, error) {
v := url.Values{}
v.Set("client", "ig")
v.Set("q", strings.Join(symbols, ","))
u, err := url.Parse("http://www.google.com/finance/info")
if err != nil {
return nil, err
}
u.RawQuery = v.Encode()
log.Printf("GET %s", u)
resp, err := http.Get(u.String())
if err != nil {
return nil, err
}
defer resp.Body.Close()
parsed := []struct {
T string // ticker symbol
L string // price
C string // change
Cp string // percent change
Lt_dts string // time
}{}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
// Check that data has the expected "//" comment string to trim off.
if len(data) < 3 {
return nil, fmt.Errorf("expected data should be larger")
}
// Unmarshal the data after the "//" comment string.
if err := json.Unmarshal(data[3:], &parsed); err != nil {
return nil, err
}
if len(parsed) == 0 {
return nil, errors.New("expected at least one entry")
}
var lts []liveTradingSession
for _, p := range parsed {
timestamp, err := time.Parse("2006-01-02T15:04:05Z", p.Lt_dts)
if err != nil {
return nil, fmt.Errorf("p: %+v timestamp: %v", p, err)
}
price, err := parseFloat(p.L)
if err != nil {
return nil, fmt.Errorf("p: %+v price: %v", p, err)
}
var change float64
if p.C != "" { // C is empty after market close.
change, err = parseFloat(p.C)
if err != nil {
return nil, fmt.Errorf("p: %+v change: %v", p, err)
}
}
var percentChange float64
if p.Cp != "" { // Cp is empty after market close.
percentChange, err = parseFloat(p.Cp)
if err != nil {
return nil, fmt.Errorf("p: %+v percentChange: %v", p, err)
}
percentChange /= 100.0
}
lts = append(lts, liveTradingSession{
symbol: p.T,
timestamp: timestamp,
price: price,
change: change,
percentChange: percentChange,
})
}
return lts, nil
}
// parseFloat removes commas and then calls parseFloat.
func parseFloat(value string) (float64, error) {
return strconv.ParseFloat(strings.Replace(value, ",", "", -1), 64)
}
// sortableTradingSessions is a sortable tradingSession slice.
type sortableTradingSessions []tradingSession
// Len implements sort.Interface.
func (sts sortableTradingSessions) Len() int {
return len(sts)
}
// Less implements sort.Interface.
func (sts sortableTradingSessions) Less(i, j int) bool {
return sts[i].date.Before(sts[j].date)
}
// Swap implements sort.Interface.
func (sts sortableTradingSessions) Swap(i, j int) {
sts[i], sts[j] = sts[j], sts[i]
}