Skip to content

Commit

Permalink
Improve report string() & report generation
Browse files Browse the repository at this point in the history
  • Loading branch information
diogosilva96 committed Nov 16, 2023
1 parent d16226e commit 4bc586d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
16 changes: 8 additions & 8 deletions internal/data/report/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,20 +96,20 @@ func generateIntervalReport(etf data.Etf, numberOfDays int) (*EtfIntervalReport,
report.MinPrice = calculateMin(h.Price, report.MinPrice)
}

report.MinPriceChange = calculateChange(etf.Price, report.MinPrice)
report.MinPricePercentChange = calculatePercentChange(etf.Price, report.MinPrice)
report.MaxPriceChange = calculateChange(etf.Price, report.MaxPrice)
report.MaxPricePercentChange = calculatePercentChange(etf.Price, report.MaxPrice)
report.MinPriceChange = calculateChange(report.MinPrice, etf.Price)
report.MinPricePercentChange = calculatePercentChange(report.MinPrice, etf.Price)
report.MaxPriceChange = calculateChange(report.MaxPrice, etf.Price)
report.MaxPricePercentChange = calculatePercentChange(report.MaxPrice, etf.Price)

return report, nil
}

func calculatePercentChange(currentValue float32, previousValue float32) float32 {
return ((currentValue - previousValue) / float32(math.Abs(float64(previousValue)))) * 100
func calculatePercentChange(from float32, to float32) float32 {
return ((from - to) / float32(math.Abs(float64(to)))) * 100
}

func calculateChange(currentValue float32, previousValue float32) float32 {
return currentValue - previousValue
func calculateChange(from float32, to float32) float32 {
return from - to
}

func calculateMax(a, b float32) float32 {
Expand Down
12 changes: 6 additions & 6 deletions internal/data/report/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ type EtfIntervalReport struct {
func (r EtfReport) String() string {
var sb strings.Builder
if r.Change > 0 {
sb.WriteString(fmt.Sprintf("[%s] Price: %.2f, Change: +%.2f", r.Symbol, r.CurrentPrice, r.Change))
sb.WriteString(fmt.Sprintf("[%s] Current Price: %.2f (+%.2f)", r.Symbol, r.CurrentPrice, r.Change))
} else {
sb.WriteString(fmt.Sprintf("[%s] Price: %.2f, Change: %.2f", r.Symbol, r.CurrentPrice, r.Change))
sb.WriteString(fmt.Sprintf("[%s] Current Price: %.2f (%.2f)", r.Symbol, r.CurrentPrice, r.Change))
}
for _, i := range r.IntervalReports {
sb.WriteString("\n")
if i.MinPriceChange >= 0 {
sb.WriteString(fmt.Sprintf("[%v days] Price range: [%.2f (+%.2f)", i.IntervalInDays, i.MinPrice, i.MinPriceChange))
sb.WriteString(fmt.Sprintf("[%v days] Min: %.2f (+%.2f)", i.IntervalInDays, i.MinPrice, i.MinPriceChange))
} else {
sb.WriteString(fmt.Sprintf("[%v days] Price range: [%.2f (%.2f)", i.IntervalInDays, i.MinPrice, i.MinPriceChange))
sb.WriteString(fmt.Sprintf("[%v days] Min: %.2f (%.2f)", i.IntervalInDays, i.MinPrice, i.MinPriceChange))
}
if i.MaxPriceChange >= 0 {
sb.WriteString(fmt.Sprintf(", %.2f (+%.2f)]", i.MaxPrice, i.MaxPriceChange))
sb.WriteString(fmt.Sprintf(", Max: %.2f (+%.2f)", i.MaxPrice, i.MaxPriceChange))
} else {
sb.WriteString(fmt.Sprintf(", %.2f (%.2f)]", i.MaxPrice, i.MaxPriceChange))
sb.WriteString(fmt.Sprintf(", Max: %.2f (%.2f)", i.MaxPrice, i.MaxPriceChange))
}
}
return sb.String()
Expand Down

0 comments on commit 4bc586d

Please sign in to comment.