forked from mop-tracker/mop
-
Notifications
You must be signed in to change notification settings - Fork 10
/
quotes.go
222 lines (201 loc) · 8.1 KB
/
quotes.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
// Copyright (c) 2013-2021 by Michael Dvorkin, Brandon Lee Camilleri and contributors. All Rights Reserved. Use of this source code is governed by a MIT-style license that can be found in README.md.
package TerminalStocks
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"reflect"
"strconv"
"strings"
)
const quotesURLv7 = `https://query1.finance.yahoo.com/v7/finance/quote?symbols=%s`
const quotesURLv7QueryParts = `&range=1d&interval=5m&indicators=close&includeTimestamps=false&includePrePost=false&corsDomain=finance.yahoo.com&.tsrc=finance`
const noDataIndicator = `N/A`
// Stock stores quote information for the particular stock ticker.
// The data for all the fields except 'Advancing' is fetched using Yahoo market API.
type Stock struct {
Ticker string `json:"symbol"` // Stock ticker.
LastTrade string `json:"regularMarketPrice"` // l1: last trade.
Change string `json:"regularMarketChange"` // c6: change real time.
ChangePct string `json:"regularMarketChangePercent"` // k2: percent change real time.
Open string `json:"regularMarketOpen"` // o: market open price.
Low string `json:"regularMarketDayLow"` // g: day's low.
High string `json:"regularMarketDayHigh"` // h: day's high.
Low52 string `json:"fiftyTwoWeekLow"` // j: 52-weeks low.
High52 string `json:"fiftyTwoWeekHigh"` // k: 52-weeks high.
Volume string `json:"regularMarketVolume"` // v: volume.
AvgVolume string `json:"averageDailyVolume10Day"` // a2: average volume.
PeRatio string `json:"trailingPE"` // r2: P/E ration real time.
PeRatioX string `json:"trailingPE"` // r: P/E ration (fallback when real time is N/A).
Dividend string `json:"trailingAnnualDividendRate"` // d: dividend.
Yield string `json:"trailingAnnualDividendYield"` // y: dividend yield.
MarketCap string `json:"marketCap"` // j3: market cap real time.
MarketCapX string `json:"marketCap"` // j1: market cap (fallback when real time is N/A).
Currency string `json:"currency"` // String code for currency of stock.
PreOpen string `json:"preMarketPrice,omitempty"`
PreOpenPct string `json:"preMarketChangePercent,omitempty"`
AfterHours string `json:"postMarketPrice,omitempty"`
AfterHoursPct string `json:"postMarketChangePercent,omitempty"`
Advancing bool // True when change is >= $0.
}
// Quotes stores relevant pointers as well as the array of stock quotes for the tickers we are tracking.
type Quotes struct {
market *Market // Pointer to Market.
profile *Profile // Pointer to Profile.
stocks []Stock // Array of stock quote data.
}
// Sets the initial values and returns new Quotes struct.
func NewQuotes(market *Market, profile *Profile) *Quotes {
return &Quotes{
market: market,
profile: profile,
}
}
// Fetch the latest stock quotes and parse raw fetched data into array of []Stock structs.
func (quotes *Quotes) Fetch() (self *Quotes) {
self = quotes // <-- This ensures we return correct quotes after recover() from panic().
if quotes.isReady() {
defer func() {
}()
url := fmt.Sprintf(quotesURLv7, strings.Join(quotes.profile.Tickers, `,`))
response, err := http.Get(url + quotesURLv7QueryParts)
if err != nil {
panic(err)
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
panic(err)
}
quotes.parse2(body)
}
return quotes
}
// isReady returns true if we haven't fetched the quotes yet *or* the stock market is still open and we might want to grab the latest quotes.
// In both cases we make sure the list of requested tickers is not empty.
func (quotes *Quotes) isReady() bool {
return (quotes.stocks == nil || !quotes.market.IsClosed) && len(quotes.profile.Tickers) > 0
}
// this will parse the json objects
func (quotes *Quotes) parse2(body []byte) (*Quotes, error) {
// response -> quoteResponse -> result|error (array) -> map[string]interface{}
// Stocks has non-int things
// d := map[string]map[string][]Stock{}
// some of these are numbers vs strings
// d := map[string]map[string][]map[string]string{}
d := map[string]map[string][]map[string]interface{}{}
err := json.Unmarshal(body, &d)
if err != nil {
return nil, err
}
results := d["quoteResponse"]["result"]
quotes.stocks = make([]Stock, len(results))
for i, raw := range results {
result := map[string]string{}
for k, v := range raw {
switch v.(type) {
case string:
result[k] = v.(string)
case float64:
result[k] = float2Str(v.(float64))
default:
result[k] = fmt.Sprintf("%v", v)
}
}
quotes.stocks[i].Ticker = result["symbol"]
quotes.stocks[i].LastTrade = result["regularMarketPrice"]
quotes.stocks[i].Change = result["regularMarketChange"]
quotes.stocks[i].ChangePct = result["regularMarketChangePercent"]
quotes.stocks[i].Open = result["regularMarketOpen"]
quotes.stocks[i].Low = result["regularMarketDayLow"]
quotes.stocks[i].High = result["regularMarketDayHigh"]
quotes.stocks[i].Low52 = result["fiftyTwoWeekLow"]
quotes.stocks[i].High52 = result["fiftyTwoWeekHigh"]
quotes.stocks[i].Volume = result["regularMarketVolume"]
quotes.stocks[i].AvgVolume = result["averageDailyVolume10Day"]
quotes.stocks[i].PeRatio = result["trailingPE"]
quotes.stocks[i].PeRatioX = result["trailingPE"]
quotes.stocks[i].Dividend = result["trailingAnnualDividendRate"]
quotes.stocks[i].Yield = result["trailingAnnualDividendYield"]
quotes.stocks[i].MarketCap = result["marketCap"]
quotes.stocks[i].MarketCapX = result["marketCap"]
quotes.stocks[i].Currency = result["currency"]
quotes.stocks[i].PreOpen = result["preMarketPrice"]
quotes.stocks[i].PreOpenPct = result["preMarketChangePercent"]
quotes.stocks[i].AfterHours = result["postMarketPrice"]
quotes.stocks[i].AfterHoursPct = result["postMarketChangePercent"]
/*
fmt.Println(i)
fmt.Println("-------------------")
for k, v := range result {
fmt.Println(k, v)
}
fmt.Println("-------------------")
*/
adv, err := strconv.ParseFloat(quotes.stocks[i].Change, 64)
if err == nil {
quotes.stocks[i].Advancing = adv >= 0.0
}
}
return quotes, nil
}
// Use reflection to parse and assign the quotes data fetched using the Yahoo market API.
func (quotes *Quotes) parse(body []byte) *Quotes {
lines := bytes.Split(body, []byte{'\n'})
quotes.stocks = make([]Stock, len(lines))
//
// Get the total number of fields in the Stock struct. Skip the last Advancing field which is not fetched.
//
fieldsCount := reflect.ValueOf(quotes.stocks[0]).NumField() - 1
//
// Split each line into columns, then iterate over the Stock struct fields to assign column values.
//
for i, line := range lines {
columns := bytes.Split(bytes.TrimSpace(line), []byte{','})
for j := 0; j < fieldsCount; j++ {
// ex. quotes.stocks[i].Ticker = string(columns[0])
reflect.ValueOf("es.stocks[i]).Elem().Field(j).SetString(string(columns[j]))
}
//
// Try realtime value and revert to the last known if the realtime is not available.
//
if quotes.stocks[i].PeRatio == noDataIndicator && quotes.stocks[i].PeRatioX != noDataIndicator {
quotes.stocks[i].PeRatio = quotes.stocks[i].PeRatioX
}
if quotes.stocks[i].MarketCap == noDataIndicator && quotes.stocks[i].MarketCapX != noDataIndicator {
quotes.stocks[i].MarketCap = quotes.stocks[i].MarketCapX
}
//
// Stock is advancing if the change is not negative (i.e. $0.00 is also "advancing").
//
quotes.stocks[i].Advancing = (quotes.stocks[i].Change[0:1] != `-`)
}
return quotes
}
//-----------------------------------------------------------------------------
func sanitize(body []byte) []byte {
return bytes.Replace(bytes.TrimSpace(body), []byte{'"'}, []byte{}, -1)
}
func float2Str(v float64) string {
unit := ""
switch {
case v > 1.0e12:
v = v / 1.0e12
unit = "T"
case v > 1.0e9:
v = v / 1.0e9
unit = "B"
case v > 1.0e6:
v = v / 1.0e6
unit = "M"
case v > 1.0e5:
v = v / 1.0e3
unit = "K"
default:
unit = ""
}
// parse
return fmt.Sprintf("%0.3f%s", v, unit)
}