-
Notifications
You must be signed in to change notification settings - Fork 321
Expand file tree
/
Copy pathsorter.go
More file actions
298 lines (246 loc) · 7.94 KB
/
sorter.go
File metadata and controls
298 lines (246 loc) · 7.94 KB
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
// Copyright (c) 2013-2024 by Michael Dvorkin and contributors. All Rights Reserved.
// Use of this source code is governed by a MIT-style license that can
// be found in the LICENSE file.
package mop
import (
"sort"
"strconv"
"strings"
)
// Sorter gets called to sort stock quotes by one of the columns. The
// setup is rather lengthy; there should probably be more concise way
// that uses reflection and avoids hardcoding the column names.
type Sorter struct {
profile *Profile // Pointer to where we store sort column and order.
}
type sortable []Stock
func (list sortable) Len() int { return len(list) }
func (list sortable) Swap(i, j int) { list[i], list[j] = list[j], list[i] }
type (
byTickerAsc struct{ sortable }
byLastTradeAsc struct{ sortable }
byChangeAsc struct{ sortable }
byChangePctAsc struct{ sortable }
byOpenAsc struct{ sortable }
byLowAsc struct{ sortable }
byHighAsc struct{ sortable }
byLow52Asc struct{ sortable }
byHigh52Asc struct{ sortable }
byVolumeAsc struct{ sortable }
byAvgVolumeAsc struct{ sortable }
byPeRatioAsc struct{ sortable }
byDividendAsc struct{ sortable }
byYieldAsc struct{ sortable }
byMarketCapAsc struct{ sortable }
byPreOpenAsc struct{ sortable }
byAfterHoursAsc struct{ sortable }
)
type (
byTickerDesc struct{ sortable }
byLastTradeDesc struct{ sortable }
byChangeDesc struct{ sortable }
byChangePctDesc struct{ sortable }
byOpenDesc struct{ sortable }
byLowDesc struct{ sortable }
byHighDesc struct{ sortable }
byLow52Desc struct{ sortable }
byHigh52Desc struct{ sortable }
byVolumeDesc struct{ sortable }
byAvgVolumeDesc struct{ sortable }
byPeRatioDesc struct{ sortable }
byDividendDesc struct{ sortable }
byYieldDesc struct{ sortable }
byMarketCapDesc struct{ sortable }
byPreOpenDesc struct{ sortable }
byAfterHoursDesc struct{ sortable }
)
func (list byTickerAsc) Less(i, j int) bool {
return list.sortable[i].Ticker < list.sortable[j].Ticker
}
func (list byLastTradeAsc) Less(i, j int) bool {
return list.sortable[i].LastTrade < list.sortable[j].LastTrade
}
func (list byChangeAsc) Less(i, j int) bool {
return c(list.sortable[i].Change) < c(list.sortable[j].Change)
}
func (list byChangePctAsc) Less(i, j int) bool {
return c(list.sortable[i].ChangePct) < c(list.sortable[j].ChangePct)
}
func (list byOpenAsc) Less(i, j int) bool {
return list.sortable[i].Open < list.sortable[j].Open
}
func (list byLowAsc) Less(i, j int) bool {
return list.sortable[i].Low < list.sortable[j].Low
}
func (list byHighAsc) Less(i, j int) bool {
return list.sortable[i].High < list.sortable[j].High
}
func (list byLow52Asc) Less(i, j int) bool {
return list.sortable[i].Low52 < list.sortable[j].Low52
}
func (list byHigh52Asc) Less(i, j int) bool {
return list.sortable[i].High52 < list.sortable[j].High52
}
func (list byVolumeAsc) Less(i, j int) bool {
return m(list.sortable[i].Volume) < m(list.sortable[j].Volume)
}
func (list byAvgVolumeAsc) Less(i, j int) bool {
return m(list.sortable[i].AvgVolume) < m(list.sortable[j].AvgVolume)
}
func (list byPeRatioAsc) Less(i, j int) bool {
return list.sortable[i].PeRatio < list.sortable[j].PeRatio
}
func (list byDividendAsc) Less(i, j int) bool {
return list.sortable[i].Dividend < list.sortable[j].Dividend
}
func (list byYieldAsc) Less(i, j int) bool {
return list.sortable[i].Yield < list.sortable[j].Yield
}
func (list byMarketCapAsc) Less(i, j int) bool {
return m(list.sortable[i].MarketCap) < m(list.sortable[j].MarketCap)
}
func (list byPreOpenAsc) Less(i, j int) bool {
return c(list.sortable[i].PreOpen) < c(list.sortable[j].PreOpen)
}
func (list byAfterHoursAsc) Less(i, j int) bool {
return c(list.sortable[i].AfterHours) < c(list.sortable[j].AfterHours)
}
func (list byTickerDesc) Less(i, j int) bool {
return list.sortable[j].Ticker < list.sortable[i].Ticker
}
func (list byLastTradeDesc) Less(i, j int) bool {
return list.sortable[j].LastTrade < list.sortable[i].LastTrade
}
func (list byChangeDesc) Less(i, j int) bool {
return c(list.sortable[j].Change) < c(list.sortable[i].Change)
}
func (list byChangePctDesc) Less(i, j int) bool {
return c(list.sortable[j].ChangePct) < c(list.sortable[i].ChangePct)
}
func (list byOpenDesc) Less(i, j int) bool {
return list.sortable[j].Open < list.sortable[i].Open
}
func (list byLowDesc) Less(i, j int) bool {
return list.sortable[j].Low < list.sortable[i].Low
}
func (list byHighDesc) Less(i, j int) bool {
return list.sortable[j].High < list.sortable[i].High
}
func (list byLow52Desc) Less(i, j int) bool {
return list.sortable[j].Low52 < list.sortable[i].Low52
}
func (list byHigh52Desc) Less(i, j int) bool {
return list.sortable[j].High52 < list.sortable[i].High52
}
func (list byVolumeDesc) Less(i, j int) bool {
return m(list.sortable[j].Volume) < m(list.sortable[i].Volume)
}
func (list byAvgVolumeDesc) Less(i, j int) bool {
return m(list.sortable[j].AvgVolume) < m(list.sortable[i].AvgVolume)
}
func (list byPeRatioDesc) Less(i, j int) bool {
return list.sortable[j].PeRatio < list.sortable[i].PeRatio
}
func (list byDividendDesc) Less(i, j int) bool {
return list.sortable[j].Dividend < list.sortable[i].Dividend
}
func (list byYieldDesc) Less(i, j int) bool {
return list.sortable[j].Yield < list.sortable[i].Yield
}
func (list byMarketCapDesc) Less(i, j int) bool {
return m(list.sortable[j].MarketCap) < m(list.sortable[i].MarketCap)
}
func (list byPreOpenDesc) Less(i, j int) bool {
return c(list.sortable[j].PreOpen) < c(list.sortable[i].PreOpen)
}
func (list byAfterHoursDesc) Less(i, j int) bool {
return c(list.sortable[j].AfterHours) < c(list.sortable[i].AfterHours)
}
// Returns new Sorter struct.
func NewSorter(profile *Profile) *Sorter {
return &Sorter{
profile: profile,
}
}
// SortByCurrentColumn builds a list of sort interface based on current sort
// order, then calls sort.Sort to do the actual job.
func (sorter *Sorter) SortByCurrentColumn(stocks []Stock) *Sorter {
var interfaces []sort.Interface
if sorter.profile.Ascending {
interfaces = []sort.Interface{
byTickerAsc{stocks},
byLastTradeAsc{stocks},
byChangeAsc{stocks},
byChangePctAsc{stocks},
byOpenAsc{stocks},
byLowAsc{stocks},
byHighAsc{stocks},
byLow52Asc{stocks},
byHigh52Asc{stocks},
byVolumeAsc{stocks},
byAvgVolumeAsc{stocks},
byPeRatioAsc{stocks},
byDividendAsc{stocks},
byYieldAsc{stocks},
byMarketCapAsc{stocks},
byPreOpenAsc{stocks},
byAfterHoursAsc{stocks},
}
} else {
interfaces = []sort.Interface{
byTickerDesc{stocks},
byLastTradeDesc{stocks},
byChangeDesc{stocks},
byChangePctDesc{stocks},
byOpenDesc{stocks},
byLowDesc{stocks},
byHighDesc{stocks},
byLow52Desc{stocks},
byHigh52Desc{stocks},
byVolumeDesc{stocks},
byAvgVolumeDesc{stocks},
byPeRatioDesc{stocks},
byDividendDesc{stocks},
byYieldDesc{stocks},
byMarketCapDesc{stocks},
byPreOpenDesc{stocks},
byAfterHoursDesc{stocks},
}
}
sort.Sort(interfaces[sorter.profile.SortColumn])
return sorter
}
// The same exact method is used to sort by $Change and Change%. In both cases
// we sort by the value of Change% so that multiple $0.00s get sorted properly.
func c(str string) float32 {
c := "$"
for _, v := range currencies {
if strings.Contains(str, v) {
c = v
}
}
trimmed := strings.Replace(strings.Trim(str, ` %`), c, ``, 1)
value, _ := strconv.ParseFloat(trimmed, 32)
return float32(value)
}
// When sorting by the market value we must first convert 42B etc. notations
// to proper numeric values.
func m(str string) float32 {
if len(str) == 0 {
return 0
}
multiplier := 1.0
switch str[len(str)-1:] { // Check the last character.
case `T`:
multiplier = 1000000000000.0
case `B`:
multiplier = 1000000000.0
case `M`:
multiplier = 1000000.0
case `K`:
multiplier = 1000.0
}
trimmed := strings.Trim(str, ` $TBMK`) // Get rid of non-numeric characters.
value, _ := strconv.ParseFloat(trimmed, 32)
return float32(value * multiplier)
}