-
Notifications
You must be signed in to change notification settings - Fork 0
/
results.go
61 lines (51 loc) · 1.19 KB
/
results.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
package main
import (
"fmt"
"github.com/piquette/finance-go"
"sort"
)
type Result struct {
quote *finance.Quote
pGrowth float64
sellPrice float64
}
func (r Result) New(quote *finance.Quote) Result {
x := Result{
quote: quote,
pGrowth: r.getPotentialGrowth(quote),
sellPrice: r.getSellPrice(quote),
}
return x
}
func (r Result) getPotentialGrowth(quote *finance.Quote) float64 {
return (quote.FiftyTwoWeekHigh - quote.RegularMarketPrice) / quote.FiftyTwoWeekHigh
}
func (r Result) getSellPrice(quote *finance.Quote) float64 {
return (quote.FiftyTwoWeekHigh + quote.RegularMarketPrice) / 2
}
func (r Result) asString() string {
return fmt.Sprintf(
"%s is $%.2f per share and we may sell it for $%.2f (%v) \n",
r.quote.Symbol,
r.quote.RegularMarketPrice,
r.sellPrice,
r.quote.FullExchangeName,
)
}
type Results []Result
func (results Results) sortByPGrowth() Results {
sort.Slice(results, func(i, j int) bool {
return results[i].pGrowth > results[j].pGrowth
})
return results
}
func (results Results) asString(cnt int) string {
output := ""
for i, result := range results {
output = output + result.asString()
if i+1 == cnt {
break
}
}
return output
}