-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathv8.go
213 lines (186 loc) · 7.03 KB
/
v8.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
package goyfinance
import (
"fmt"
"github.com/valyala/fasthttp"
"sync"
"time"
)
// GetQuoteJSONString returns a JSON string from Yahoo Finance.
// If an error occurs, the JSON string will be empty.
func GetQuoteJSONString(ticker string, interval Interval, period Period) (string, error) {
req := fasthttp.AcquireRequest()
defer fasthttp.ReleaseRequest(req)
period1, period2 := getUnixTimestamps(period)
req.SetRequestURI(fmt.Sprintf("https://query1.finance.yahoo.com/v8/finance/chart/%s?interval=%s&period1=%d&period2=%d", ticker, interval, period1, period2))
req.Header.SetMethod("GET")
req.Header.Set("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:86.0) Gecko/20100101 Firefox/86.0")
resp := fasthttp.AcquireResponse()
defer fasthttp.ReleaseRequest(req)
err := fasthttp.Do(req, resp)
if err != nil {
return "", err
}
return string(resp.Body()), nil
}
// GetQuoteJSONStringBatch returns a slice of JSON strings from Yahoo Finance.
// The order of the slice is the same as the order of the tickers slice.
// If an error occurs, the JSON string will be empty.
func GetQuoteJSONStringBatch(tickers []string, interval Interval, period Period) ([]string, error) {
var wg sync.WaitGroup
var res []string
for _, ticker := range tickers {
wg.Add(1)
go func(ticker string) {
defer wg.Done()
r, err := GetQuoteJSONString(ticker, interval, period)
if err != nil {
return
}
res = append(res, r)
}(ticker)
}
wg.Wait()
return res, nil
}
// GetQuoteJSON returns a JSONQuote struct from Yahoo Finance.
// If an error occurs, the JSONQuote struct will be empty.
func GetQuoteJSON(ticker string, interval Interval, period Period) (JSONQuote, error) {
req := fasthttp.AcquireRequest()
defer fasthttp.ReleaseRequest(req)
period1, period2 := getUnixTimestamps(period)
req.SetRequestURI(fmt.Sprintf("https://query1.finance.yahoo.com/v8/finance/chart/%s?interval=%s&period1=%d&period2=%d", ticker, interval, period1, period2))
req.Header.SetMethod("GET")
req.Header.Set("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:86.0) Gecko/20100101 Firefox/86.0")
resp := fasthttp.AcquireResponse()
defer fasthttp.ReleaseResponse(resp)
err := fasthttp.Do(req, resp)
if err != nil {
return JSONQuote{}, err
}
return parseJSONToJSONQuote(resp.Body())
}
// GetQuoteJSONBatch returns a slice of JSONQuote structs from Yahoo Finance.
// The order of the slice is the same as the order of the tickers slice.
// If an error occurs, the JSONQuote struct will be empty.
func GetQuoteJSONBatch(tickers []string, interval Interval, period Period) ([]JSONQuote, error) {
var wg sync.WaitGroup
var res []JSONQuote
for _, ticker := range tickers {
wg.Add(1)
go func(ticker string) {
defer wg.Done()
r, err := GetQuoteJSON(ticker, interval, period)
if err != nil {
return
}
res = append(res, r)
}(ticker)
}
wg.Wait()
return res, nil
}
// GetQuote returns a Quote struct from Yahoo Finance.
// If an error occurs, the Quote struct will be empty.
// This function is (surprisingly) around the same speed as GetQuoteJSON.
// and a tad faster than GetQuoteJSONString.
func GetQuote(ticker string, interval Interval, period Period) (Quote, error) {
req := fasthttp.AcquireRequest()
defer fasthttp.ReleaseRequest(req)
period1, period2 := getUnixTimestamps(period)
req.SetRequestURI(fmt.Sprintf("https://query1.finance.yahoo.com/v8/finance/chart/%s?interval=%s&period1=%d&period2=%d", ticker, interval, period1, period2))
req.Header.SetMethod("GET")
req.Header.Set("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:86.0) Gecko/20100101 Firefox/86.0")
resp := fasthttp.AcquireResponse()
defer fasthttp.ReleaseResponse(resp)
err := fasthttp.Do(req, resp)
if err != nil {
return Quote{}, err
}
quote, err := parseJSONtoQuote(resp.Body(), ticker, period1, period2)
if err != nil {
return Quote{}, err
}
return quote, nil
}
// GetQuoteBatch returns a slice of Quote structs from Yahoo Finance.
// The order of the slice is the same as the order of the tickers slice.
// If an error occurs, the Quote struct will be empty.
// This function is (surprisingly) around the same speed as GetQuoteJSONBatch.
// and a tad faster than GetQuoteJSONStringBatch.
func GetQuoteBatch(tickers []string, interval Interval, period Period) ([]Quote, error) {
var wg sync.WaitGroup
var res []Quote
for _, ticker := range tickers {
wg.Add(1)
go func(ticker string) {
defer wg.Done()
r, err := GetQuote(ticker, interval, period)
if err != nil {
return
}
res = append(res, r)
}(ticker)
}
wg.Wait()
return res, nil
}
// GetQuoteCSVString returns a CSV string with OHLCV data from Yahoo Finance.
// If an error occurs, the CSV string will be empty.
func GetQuoteCSVString(ticker string, interval Interval, period Period) (string, error) {
req := fasthttp.AcquireRequest()
period1, period2 := getUnixTimestamps(period)
req.SetRequestURI(fmt.Sprintf("https://query1.finance.yahoo.com/v7/finance/download/%s?interval=%s&period1=%d&period2=%d&events=history", ticker, interval, period1, period2))
req.Header.SetMethod("GET")
req.Header.Set("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:86.0) Gecko/20100101 Firefox/86.0")
resp := fasthttp.AcquireResponse()
defer fasthttp.ReleaseRequest(req)
err := fasthttp.Do(req, resp)
if err != nil {
return "", err
}
return string(resp.Body()), nil
}
// GetQuoteCSVStringBatch returns a slice of CSV strings with OHLCV data from Yahoo Finance.
// The order of the slice is the same as the order of the tickers slice.
// If an error occurs, the CSV string will be empty.
func GetQuoteCSVStringBatch(tickers []string, interval Interval, period Period) ([]string, error) {
var wg sync.WaitGroup
var res []string
for _, ticker := range tickers {
wg.Add(1)
go func(ticker string) {
defer wg.Done()
r, err := GetQuoteCSVString(ticker, interval, period)
if err != nil {
return
}
res = append(res, r)
}(ticker)
}
wg.Wait()
return res, nil
}
// ContinuousPriceUpdater updates a channel with the latest price data.
// The channel will be updated every updateIntervalSeconds seconds.
// The stopSignal channel is used to stop the function.
// The errorChannel is used to send errors to the caller.
// The priceChannel is used to send the latest price data as a PriceData struct to the caller.
// ticker, interval, period are used to fetch the price data from GetQuote.
// Note: It runs forever, so you should probably run it in a goroutine.
// Note: Sending the stopSignal channel will not close the channels.
func ContinuousPriceUpdater(priceChannel chan PriceData, errorChannel chan error, ticker string, interval Interval, period Period, updateIntervalSeconds float64, stopSignal chan struct{}) {
for {
select {
case <-stopSignal:
return // We chose not to close all the channels because we don't know if they are used elsewhere
default:
price, err := GetQuote(ticker, interval, period)
if err != nil {
errorChannel <- err
} else if len(price.PriceHistoric) > 0 {
priceChannel <- price.PriceHistoric[len(price.PriceHistoric)-1]
}
time.Sleep(time.Duration(updateIntervalSeconds) * time.Second)
}
}
}