-
Notifications
You must be signed in to change notification settings - Fork 0
/
global_quote.go
168 lines (149 loc) · 4.7 KB
/
global_quote.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
package alphavantage
import (
"context"
"errors"
"io"
"net/http"
"net/url"
"time"
)
type QuoteFunction string
const (
TimeSeriesIntraday QuoteFunction = "TIME_SERIES_INTRADAY"
TimeSeriesDaily QuoteFunction = "TIME_SERIES_DAILY"
TimeSeriesDailyAdjusted QuoteFunction = "TIME_SERIES_DAILY_ADJUSTED"
TimeSeriesMonthly QuoteFunction = "TIME_SERIES_MONTHLY"
TimeSeriesMonthlyAdjusted QuoteFunction = "TIME_SERIES_MONTHLY_ADJUSTED"
)
func NewQuotesURL(symbol string, function QuoteFunction) (string, error) {
err := function.Validate()
if err != nil {
return "", err
}
u := url.URL{
Scheme: "https",
Host: "www.alphavantage.co",
Path: "/query",
RawQuery: url.Values{
"datatype": []string{"csv"},
"outputsize": []string{"full"},
"function": []string{string(function)},
"symbol": []string{symbol},
}.Encode(),
}
return u.String(), nil
}
func (fn QuoteFunction) Validate() error {
switch fn {
case TimeSeriesIntraday,
TimeSeriesDaily,
TimeSeriesDailyAdjusted,
TimeSeriesMonthly,
TimeSeriesMonthlyAdjusted:
return nil
default:
return errors.New("unknown time series function")
}
}
// Quotes fetches quotes for a given symbol and function. It parses the dates in the given location.
func (client *Client) Quotes(ctx context.Context, symbol string, function QuoteFunction, location *time.Location) ([]Quote, error) {
rc, err := client.DoQuotesRequest(ctx, symbol, function)
if err != nil {
return nil, err
}
defer closeAndIgnoreError(rc)
switch function {
case TimeSeriesIntraday:
list, err := ParseIntraDayQuotes(rc, location)
if err != nil {
return nil, err
}
return convertQuoteElements(list, func(q IntraDayQuote) Quote { return Quote(q) }), nil
default:
quotes, err := ParseQuotes(rc, location)
if err != nil {
return nil, err
}
return quotes, nil
}
}
// Deprecated: use DoQuotesRequest instead. This method will be removed before 2023.
func (client *Client) QuotesRequest(ctx context.Context, symbol string, function QuoteFunction, fn func(r io.Reader) error) error {
rc, err := client.DoQuotesRequest(ctx, symbol, function)
if err != nil {
return err
}
defer closeAndIgnoreError(rc)
return fn(rc)
}
func (client *Client) DoQuotesRequest(ctx context.Context, symbol string, function QuoteFunction) (io.ReadCloser, error) {
requestURL, err := NewQuotesURL(symbol, function)
if err != nil {
return nil, err
}
req, err := http.NewRequestWithContext(ctx,
http.MethodGet,
requestURL,
nil,
)
if err != nil {
return nil, err
}
res, err := client.Do(req)
if err != nil {
return nil, err
}
return checkError(res.Body)
}
type Quote struct {
Time time.Time `column-name:"timestamp"`
Open float64 `column-name:"open"`
High float64 `column-name:"high"`
Low float64 `column-name:"low"`
Close float64 `column-name:"close"`
Volume float64 `column-name:"volume"`
DividendAmount float64 `column-name:"dividend_amount"`
SplitCoefficient float64 `column-name:"split_coefficient"`
}
// ParseQuotes handles parsing the following "Stock Time Series" functions
// - TIME_SERIES_DAILY
// - TIME_SERIES_DAILY_ADJUSTED
// - TIME_SERIES_MONTHLY
// - TIME_SERIES_MONTHLY_ADJUSTED
func ParseQuotes(r io.Reader, location *time.Location) ([]Quote, error) {
var list []Quote
return list, ParseCSV(r, &list, location)
}
// IntraDayQuote is convertable to Quote. The only difference is the time-layout includes additional time information.
type IntraDayQuote struct {
Time time.Time `column-name:"timestamp" time-layout:"2006-01-02 15:04:05"`
Open float64 `column-name:"open"`
High float64 `column-name:"high"`
Low float64 `column-name:"low"`
Close float64 `column-name:"close"`
Volume float64 `column-name:"volume"`
DividendAmount float64 `column-name:"dividend_amount"`
SplitCoefficient float64 `column-name:"split_coefficient"`
}
var _ = Quote(IntraDayQuote{})
// ParseIntraDayQuotes handles parsing the following "Stock Time Series" functions
// - TIME_SERIES_INTRADAY
func ParseIntraDayQuotes(r io.Reader, location *time.Location) ([]IntraDayQuote, error) {
var list []IntraDayQuote
return list, ParseCSV(r, &list, location)
}
//TODO: use this instead after bumping to 1.18
//func convertElements[T1, T2 any](list []T1, convert func(T1) T2) []T2 {
// result := make([]T2, len(list))
// for i := range list {
// result[i] = convert(list[i])
// }
// return result
//}
func convertQuoteElements(list []IntraDayQuote, convert func(quote IntraDayQuote) Quote) []Quote {
result := make([]Quote, len(list))
for i := range list {
result[i] = convert(list[i])
}
return result
}