/
list.go
225 lines (187 loc) · 4.74 KB
/
list.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
package reports
import (
"database/sql"
"fmt"
"math"
"strings"
"github.com/dude333/rapina/parsers"
"github.com/pkg/errors"
"golang.org/x/text/collate"
"golang.org/x/text/language"
"golang.org/x/text/message"
)
//
// ListCompanies shows all available companies
//
func ListCompanies(db *sql.DB) (names []string, err error) {
info, err := companies(db)
if err != nil {
fmt.Println("[x] Falha:", err)
return
}
if len(info) == 0 {
err = fmt.Errorf("lista vazia")
return
}
// Extract companies names
names = make([]string, len(info))
for i, co := range info {
names[i] = co.name
}
// Sort accents correctly
cl := collate.New(language.BrazilianPortuguese, collate.Loose)
cl.SortStrings(names)
return
}
//
// ListTickers shows all available tickers for a companie name
//
func ListTickers(db *sql.DB, companyName string) (names []string, err error) {
info, err := tickers(db, companyName)
if err != nil {
fmt.Println("[x] Falha:", err)
return
}
if len(info) == 0 {
err = fmt.Errorf("lista vazia")
return
}
// Extract companies names
names = make([]string, len(info))
for i, co := range info {
names[i] = co.name
}
// Sort accents correctly
cl := collate.New(language.BrazilianPortuguese, collate.Loose)
cl.SortStrings(names)
return
}
//
// ListTickers returns SpcfctnCd of a ticker
//
func GetSpcfctnCd(db *sql.DB, companyName string, ticker string) string {
info, err := tickers(db, companyName)
if err != nil {
fmt.Println("[x] Falha:", err)
return ""
}
if len(info) == 0 {
fmt.Println("[x] Lista vazia")
return ""
}
// Extract companies names
var spcfctnCd string = ""
for _, co := range info {
if co.name == ticker {
spcfctnCd = co.SpcfctnCd
}
}
return spcfctnCd
}
//
// ListSector shows all companies from the same sector as 'company'
//
func ListSector(db *sql.DB, company, yamlFile string) (err error) {
// Companies from the same sector
secCo, secName, err := parsers.FromSector(company, yamlFile)
if len(secCo) <= 1 || err != nil {
err = errors.Wrap(err, "erro ao ler arquivo dos setores "+yamlFile)
return
}
// All companies stored on db
list, err := ListCompanies(db)
if err != nil {
err = errors.Wrap(err, "erro ao listar empresas")
return
}
// Translate company names to match the name stored on db
fmt.Printf("%-40s %s\n", "ARQUIVO YAML", "BANCO DE DADOS")
fmt.Printf("%-40s %s\n", strings.Repeat("-", 40), strings.Repeat("-", 40))
for _, s := range secCo {
z := parsers.FuzzyFind(s, list, 3)
if len(z) > 0 {
fmt.Printf("%-40s %s\n", s, z)
} else {
fmt.Printf("%-40s %s\n", s, "Nao encontrado")
}
}
fmt.Printf("\nSETOR: %s\n", secName)
return
}
//
// ListCompaniesProfits lists companies by net profit: more sustainable growth
// listed first
//
func ListCompaniesProfits(db *sql.DB, rate float32) error {
info, err := companies(db)
if err != nil {
return fmt.Errorf("falha ao obter a lista de empresas (%v)", err)
}
yi, yf, err := timeRange(db)
if err != nil {
return fmt.Errorf("falha ao obter a faixa de datas (%v)", err)
}
// Header
var sep string
fmt.Printf("%20s ", " ") // Space to match company name
for y := yi; y <= yf; y++ {
fmt.Printf("%10d ", y)
sep += fmt.Sprintf("%s ", strings.Repeat("-", 10))
}
fmt.Printf("%10s\n", "CAGR")
sep += fmt.Sprintf("%s ", strings.Repeat("-", 10))
fmt.Printf("%20s %s\n", " ", sep)
// Profits
pt := message.NewPrinter(language.Portuguese)
for _, co := range info {
profits, err := companyProfits(db, co.id)
if err != nil {
return fmt.Errorf("falha ao obter lucros de %s (%v)", co.name, err)
}
// FILTERS ----------------------------
// At least 4 years
if len(profits) < 4 {
continue
}
// Ignore if there is no recent data
if profits[len(profits)-1].year < yf-1 {
continue
}
pi := profits[0].profit
pf := profits[len(profits)-1].profit
if pf < pi {
continue
}
// Ignore if next profix < 'rate' * current profit
profitable := true
for i := 1; i < len(profits); i++ {
if profits[i].profit < 0 || profits[i].profit < (1+rate)*profits[i-1].profit {
profitable = false
break
}
}
if !profitable {
continue
}
// COMPANY NAME -----------------------
fmt.Printf("%-20.20s ", co.name)
// PROFIT VALUES ----------------------
i := 0
for y := yi; y <= yf; y++ {
if i < len(profits) && profits[i].year == y {
pt.Printf("%10.0f ", profits[i].profit)
i++
} else {
fmt.Printf("%10s ", " ")
}
}
// CAGR -------------------------------
if pi != 0 && pf != 0 && pf*pi >= 0 {
cagr := math.Pow(float64(pf/pi), 1/float64(yf-yi-1)) - 1
pt.Printf("%10.1f%%", cagr*100)
}
fmt.Println()
} // next c
fmt.Printf("\nEmpresas com lucros crescentes e variação mínima de %0.0f%% de um ano para o outro.\n\n", rate*100)
return nil
}