/
fields.go
311 lines (289 loc) · 7.79 KB
/
fields.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
package quote
import (
"fmt"
"strings"
finance "github.com/piquette/finance-go"
"github.com/piquette/qtrn/utils"
)
const (
// Quote fields.
Symbol = "Symbol"
Market = "Market"
Time = "Time"
Last = "Last"
Change = "Change"
Vol = "Volume"
Bid = "Bid"
BidSize = "Bid Size"
Ask = "Ask"
AskSize = "Ask Size"
Open = "Open"
PrevClose = "Prev Close"
Exchange = "Exchange"
DayHigh = "Day High"
DayLow = "Day Low"
YearHigh = "52wk High"
YearLow = "52wk Low"
FiftyMA = "50D MA"
THundMA = "200D MA"
AvgDailyVol = "Avg. Vol"
Security = "Security"
Name = "Name"
// Equity fields.
EpsTrailing = "EPS Trailing"
EpsForward = "EPS Forward"
EpsDate = "Earnings Date"
DivTrailing = "Div. Trailing"
DivYield = "Div. Yield"
DivDate = "Ex. Div. Date"
PETrailing = "P/E Trailing"
PEForward = "P/E Forward"
BookValue = "Book Value"
PB = "P/B"
MarketCap = "Market Cap"
// ETF/MutualFund fields.
YTDReturn = "YTD Return"
QtrReturn = "Qtr Return"
QtrNavReturn = "Qtr NAV Return"
// Option/Future fields.
Underlier = "Underlying Symbol"
OpenInterest = "Open Interest"
ExpireDate = "Expiration"
Strike = "Strike"
// Crypto fields.
Algorithm = "Algorithm"
StartDate = "Start"
MaxSupply = "Max Supply"
Circulating = "Circulation"
)
// FieldsQuote returns the fields for a plain quote.
func FieldsQuote() (fields []string) {
return []string{Symbol, Market, Time, Last, Change, Vol,
Bid, BidSize, Ask, AskSize, Open, PrevClose}
}
// FieldsInfoQuote returns the fields for an informative quote.
func FieldsInfoQuote() (fields []string) {
return append(FieldsQuote(), Exchange,
DayHigh, DayLow, YearHigh, YearLow, FiftyMA, THundMA, AvgDailyVol, Security, Name)
}
// FieldsEquity returns the fields for an informative quote.
func FieldsEquity() (fields []string) {
return append(FieldsInfoQuote(), EpsTrailing, EpsForward, EpsDate, DivTrailing,
DivYield, DivDate, PETrailing, PEForward, BookValue, PB, MarketCap)
}
// FieldsETF returns the fields for an etf.
func FieldsETF() (fields []string) {
return append(FieldsInfoQuote(), YTDReturn, QtrReturn, QtrNavReturn)
}
// FieldsFuture returns the fields for a future.
func FieldsFuture() (fields []string) {
return append([]string{Underlier, OpenInterest, ExpireDate, Strike}, FieldsInfoQuote()...)
}
// FieldsForex returns the fields for an informative quote.
func FieldsForex() (fields []string) {
return append(FieldsInfoQuote(), Exchange,
DayHigh, DayLow, YearHigh, YearLow, FiftyMA, THundMA, AvgDailyVol)
}
// FieldsCrypto returns the fields for an informative quote.
func FieldsCrypto() (fields []string) {
return append(FieldsInfoQuote(), Algorithm, StartDate, MaxSupply, Circulating)
}
// FieldIndex returns the fields for an informative quote.
func FieldIndex() (fields []string) {
return FieldsInfoQuote()
}
// FieldsOption returns the fields for an informative quote.
func FieldsOption() (fields []string) {
return append([]string{Underlier, OpenInterest, ExpireDate, Strike}, FieldsInfoQuote()...)
}
// FieldsMutualFund returns the fields for an informative quote.
func FieldsMutualFund() (fields []string) {
return append(FieldsInfoQuote(), YTDReturn, QtrReturn, QtrNavReturn)
}
// MapQuote maps quote fields.
func MapQuote(field string, q *finance.Quote) string {
switch field {
case Symbol:
return q.Symbol
case Market:
return utils.MktStateF(q.MarketState)
case Time:
return utils.DateF(q.RegularMarketTime)
case Last:
return utils.ToStringF(q.RegularMarketPrice)
case Change:
{
direction := utils.PriceDirection(q)
change := fmt.Sprintf("%s [%s%%]", utils.ToStringF(q.RegularMarketChange), utils.ToStringF(q.RegularMarketChangePercent))
return utils.Color(change, direction)
}
case Vol:
return utils.NumberF(q.RegularMarketVolume)
case Bid:
return utils.ToStringF(q.Bid)
case BidSize:
return utils.ToString(q.BidSize * 100)
case Ask:
return utils.ToStringF(q.Ask)
case AskSize:
return utils.ToString(q.AskSize * 100)
case Open:
return utils.ToStringF(q.RegularMarketOpen)
case PrevClose:
return utils.ToStringF(q.RegularMarketPreviousClose)
case Exchange:
return q.ExchangeID
case DayHigh:
return utils.ToStringF(q.RegularMarketDayHigh)
case DayLow:
return utils.ToStringF(q.RegularMarketDayLow)
case YearHigh:
return utils.ToStringF(q.FiftyTwoWeekHigh)
case YearLow:
return utils.ToStringF(q.FiftyTwoWeekLow)
case FiftyMA:
return utils.ToStringF(q.FiftyDayAverage)
case THundMA:
return utils.ToStringF(q.TwoHundredDayAverage)
case AvgDailyVol:
return utils.NumberF(q.AverageDailyVolume10Day)
case Name:
return q.ShortName
case Security:
return strings.ToTitle(string(q.QuoteType))
default:
return ""
}
}
// MapEquity maps equity fields.
func MapEquity(field string, q *finance.Equity) string {
v := MapQuote(field, &q.Quote)
if v != "" {
return v
}
switch field {
case EpsTrailing:
return utils.ToStringF(q.EpsTrailingTwelveMonths)
case EpsForward:
return utils.ToStringF(q.EpsForward)
case EpsDate:
return utils.DateF(q.EarningsTimestamp)
case DivTrailing:
return utils.ToStringF(q.TrailingAnnualDividendRate)
case DivYield:
return utils.ToStringF(q.TrailingAnnualDividendYield)
case DivDate:
return utils.DateF(q.DividendDate)
case PETrailing:
return utils.ToStringF(q.TrailingPE)
case PEForward:
return utils.ToStringF(q.ForwardPE)
case BookValue:
return utils.ToStringF(q.BookValue)
case PB:
return utils.ToStringF(q.PriceToBook)
case MarketCap:
return utils.NumberFancyF(q.MarketCap)
default:
return ""
}
}
// MapETF maps ETF fields.
func MapETF(field string, q *finance.ETF) string {
v := MapQuote(field, &q.Quote)
if v != "" {
return v
}
switch field {
case YTDReturn:
return utils.ToStringF(q.YTDReturn)
case QtrReturn:
return utils.ToStringF(q.TrailingThreeMonthReturns)
case QtrNavReturn:
return utils.ToStringF(q.TrailingThreeMonthNavReturns)
default:
return ""
}
}
// MapMutualFund maps mutual fund fields.
func MapMutualFund(field string, q *finance.MutualFund) string {
v := MapQuote(field, &q.Quote)
if v != "" {
return v
}
switch field {
case YTDReturn:
return utils.ToStringF(q.YTDReturn)
case QtrReturn:
return utils.ToStringF(q.TrailingThreeMonthReturns)
case QtrNavReturn:
return utils.ToStringF(q.TrailingThreeMonthNavReturns)
default:
return ""
}
}
// MapIndex maps index fields.
func MapIndex(field string, q *finance.Index) string {
return MapQuote(field, &q.Quote)
}
// MapOption maps option fields.
func MapOption(field string, q *finance.Option) string {
v := MapQuote(field, &q.Quote)
if v != "" {
return v
}
switch field {
case Underlier:
return q.UnderlyingSymbol
case OpenInterest:
return utils.ToString(q.OpenInterest)
case ExpireDate:
return utils.DateF(q.ExpireDate)
case Strike:
return utils.ToStringF(q.Strike)
default:
return ""
}
}
// MapFuture maps future fields.
func MapFuture(field string, q *finance.Future) string {
v := MapQuote(field, &q.Quote)
if v != "" {
return v
}
switch field {
case Underlier:
return q.UnderlyingSymbol
case OpenInterest:
return utils.ToString(q.OpenInterest)
case ExpireDate:
return utils.DateF(q.ExpireDate)
case Strike:
return utils.ToStringF(q.Strike)
default:
return ""
}
}
// MapForex maps forex pair fields.
func MapForex(field string, q *finance.ForexPair) string {
return MapQuote(field, &q.Quote)
}
// MapCrypto maps crypto pair fields.
func MapCrypto(field string, q *finance.CryptoPair) string {
v := MapQuote(field, &q.Quote)
if v != "" {
return v
}
switch field {
case Algorithm:
return q.Algorithm
case StartDate:
return utils.DateF(q.StartDate)
case MaxSupply:
return utils.NumberF(q.MaxSupply)
case Circulating:
return utils.NumberF(q.CirculatingSupply)
default:
return ""
}
}