/
reports_fii.go
238 lines (203 loc) · 5.24 KB
/
reports_fii.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
package reports
import (
"database/sql"
"fmt"
"math"
"strings"
"sync"
"github.com/dude333/rapina"
"github.com/dude333/rapina/fetch"
"github.com/dude333/rapina/progress"
"golang.org/x/text/language"
"golang.org/x/text/message"
)
var line = strings.Repeat("-", 67)
// Type of report output
const (
Rtable = iota + 1
Rcsv
Rcsvrend
)
// FIITerminal implements reports related to FII funds on the terminal.
type FIITerminal struct {
fetchFII *fetch.FII
fetchStock *fetch.Stock
reportFormat int
}
type FIITerminalOptions struct {
APIKey, DataDir string
}
// NewFIITerminal creates a new instace of a FIITerminal
func NewFIITerminal(db *sql.DB, opts FIITerminalOptions) (*FIITerminal, error) {
var log rapina.Logger
fetchStock, err := fetch.NewStock(db, log, opts.APIKey, opts.DataDir)
if err != nil {
return nil, err
}
fetchFII, err := fetch.NewFII(db, log)
if err != nil {
return nil, err
}
return &FIITerminal{
fetchFII: fetchFII,
fetchStock: fetchStock,
reportFormat: Rtable,
}, nil
}
// SetParms set the terminal reports parameters.
func (t *FIITerminal) SetParms(parms map[string]string) {
if _, ok := parms["verbose"]; ok {
progress.SetDebug(true)
}
if r, ok := parms["format"]; ok {
switch r {
case "table", "tabela", "tab":
t.reportFormat = Rtable
case "csv":
t.reportFormat = Rcsv
case "csvrend":
t.reportFormat = Rcsvrend
}
}
}
// Dividends prints the dividends report on terminal.
func (t FIITerminal) Dividends(codes []string, n int) error {
// Header
if t.reportFormat == Rcsv {
fmt.Println("Código,Data Com,Rendimento,Cotação,Yeld,Yeld a.a.")
}
if t.reportFormat == Rcsvrend {
fmt.Print(`Código/Data-Com`)
for _, date := range revMonthsFromToday(n) {
fmt.Printf(",%s", date)
}
fmt.Println()
}
// Remove codes
c := make([]string, 0, len(codes))
for _, code := range codes {
if len(code) == 6 {
c = append(c, code)
} else {
progress.ErrorMsg("Código inválido: %s. Padrão esperado: ABCD11.", code)
}
}
codes = c
dividends := make(map[string]*[]rapina.Dividend, len(codes))
var wg sync.WaitGroup
for _, code := range codes {
wg.Add(1)
go func(code string, n int) {
defer wg.Done()
div, err := t.fetchFII.Dividends(code, n)
if err != nil {
progress.ErrorMsg("%s: %v", code, err)
return
}
dividends[code] = div
}(code, n)
}
wg.Wait()
for _, code := range codes {
if _, ok := dividends[code]; !ok {
continue
}
var buf *strings.Builder
var err error
switch t.reportFormat {
case Rcsv:
buf, err = t.csvDividends(code, dividends[code])
case Rcsvrend:
buf, err = t.csvDividendsOnly(code, n, dividends[code])
default:
buf, err = t.printDividends(code, dividends[code])
}
if err != nil {
progress.Error(err)
} else {
fmt.Print(buf)
}
}
// Footer
if t.reportFormat == Rtable {
fmt.Println(line)
}
return nil
}
func (t FIITerminal) printDividends(code string, dividends *[]rapina.Dividend) (*strings.Builder, error) {
buf := &strings.Builder{}
p := message.NewPrinter(language.BrazilianPortuguese)
p.Fprintln(buf, line)
p.Fprintln(buf, code)
p.Fprintln(buf, line)
p.Fprintln(buf, " DATA COM RENDIMENTO COTAÇÃO YELD YELD a.a.")
p.Fprintln(buf, " ---------- ---------- ---------- ------ ---------")
for _, d := range *dividends {
p.Fprintf(buf, " %s R$%8.2f ", d.Date, d.Val)
q, err := t.fetchStock.Quote(code, d.Date)
if err != nil {
progress.ErrorMsg("Cotação de %s (%s): %v", code, d.Date, err)
}
if q > 0 && err == nil {
i := d.Val / q
p.Fprintf(buf, "R$%8.2f %8.2f%% %8.2f%%", q, 100*i, 100*(math.Pow(1+i, 12)-1))
}
buf.WriteByte('\n')
}
return buf, nil
}
func (t FIITerminal) csvDividends(code string, dividends *[]rapina.Dividend) (*strings.Builder, error) {
buf := &strings.Builder{}
p := message.NewPrinter(language.BrazilianPortuguese)
for _, d := range *dividends {
p.Fprintf(buf, `%s,%s,"%f",`, code, d.Date, d.Val)
q, err := t.fetchStock.Quote(code, d.Date)
if err != nil {
progress.ErrorMsg("Cotação de %s (%s): %v", code, d.Date, err)
}
if q > 0 && err == nil {
i := d.Val / q
p.Fprintf(buf, `"%f","%f%%","%f%%"`, q, 100*i, 100*(math.Pow(1+i, 12)-1))
} else {
buf.WriteString(`"","",""`)
}
buf.WriteByte('\n')
}
return buf, nil
}
func (t FIITerminal) csvDividendsOnly(code string, n int, dividends *[]rapina.Dividend) (*strings.Builder, error) {
buf := &strings.Builder{}
p := message.NewPrinter(language.BrazilianPortuguese)
buf.WriteString(code)
for _, month := range revMonthsFromToday(n) {
found := false
for _, div := range *dividends {
if div.Date[0:len("YYYY-MM")] == month {
p.Fprintf(buf, `,"%f"`, div.Val)
found = true
break
}
}
if !found {
buf.WriteString(`,""`)
}
}
buf.WriteByte('\n')
return buf, nil
}
func revMonthsFromToday(n int) []string {
rev := make([]string, 0, n)
dates := rapina.MonthsFromToday(n)
for i := len(dates) - 1; i >= 0; i-- {
rev = append(rev, dates[i][0:len("YYYY-MM")])
}
return rev
}
/* ------- MONTHLY REPORTS -------- */
func (t FIITerminal) Monthly(codes []string, n int) error {
for _, c := range codes {
ii, err := t.fetchFII.MonthlyReportIDs(c, n)
progress.Status("indexes: %v (err: %v)", ii, err)
}
return nil
}