Skip to content

Commit

Permalink
fix: set precision to two for non-cryptocurrencies
Browse files Browse the repository at this point in the history
  • Loading branch information
achannarasappa committed Feb 21, 2021
1 parent 5851089 commit f6c4898
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 75 deletions.
13 changes: 13 additions & 0 deletions internal/quote/quote.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type ResponseQuote struct {
PreMarketChange float64 `json:"preMarketChange"`
PreMarketChangePercent float64 `json:"preMarketChangePercent"`
PreMarketPrice float64 `json:"preMarketPrice"`
QuoteType string `json:"quoteType"`
}

type Quote struct {
Expand All @@ -43,6 +44,7 @@ type Quote struct {
ChangePercent float64
IsActive bool
IsRegularTradingSession bool
IsVariablePrecision bool
CurrencyConverted string
}

Expand All @@ -53,6 +55,10 @@ type Response struct {
} `json:"quoteResponse"`
}

func isVariablePrecision(responseQuote ResponseQuote) bool {
return responseQuote.QuoteType == "CRYPTOCURRENCY"
}

func transformResponseQuote(ctx c.Context, responseQuote ResponseQuote) Quote {

currencyRate, _, currencyCode := currency.GetCurrencyRateFromContext(ctx, responseQuote.Currency)
Expand All @@ -69,6 +75,7 @@ func transformResponseQuote(ctx c.Context, responseQuote ResponseQuote) Quote {
ChangePercent: responseQuote.RegularMarketChangePercent,
IsActive: true,
IsRegularTradingSession: true,
IsVariablePrecision: isVariablePrecision(responseQuote),
CurrencyConverted: currencyCode,
}
}
Expand All @@ -85,6 +92,7 @@ func transformResponseQuote(ctx c.Context, responseQuote ResponseQuote) Quote {
ChangePercent: responseQuote.RegularMarketChangePercent,
IsActive: true,
IsRegularTradingSession: false,
IsVariablePrecision: isVariablePrecision(responseQuote),
CurrencyConverted: currencyCode,
}
}
Expand All @@ -101,6 +109,7 @@ func transformResponseQuote(ctx c.Context, responseQuote ResponseQuote) Quote {
ChangePercent: responseQuote.RegularMarketChangePercent,
IsActive: false,
IsRegularTradingSession: false,
IsVariablePrecision: isVariablePrecision(responseQuote),
CurrencyConverted: currencyCode,
}
}
Expand All @@ -117,6 +126,7 @@ func transformResponseQuote(ctx c.Context, responseQuote ResponseQuote) Quote {
ChangePercent: responseQuote.PostMarketChangePercent + responseQuote.RegularMarketChangePercent,
IsActive: true,
IsRegularTradingSession: false,
IsVariablePrecision: isVariablePrecision(responseQuote),
CurrencyConverted: currencyCode,
}
}
Expand All @@ -133,6 +143,7 @@ func transformResponseQuote(ctx c.Context, responseQuote ResponseQuote) Quote {
ChangePercent: responseQuote.PreMarketChangePercent,
IsActive: true,
IsRegularTradingSession: false,
IsVariablePrecision: isVariablePrecision(responseQuote),
CurrencyConverted: currencyCode,
}
}
Expand All @@ -149,6 +160,7 @@ func transformResponseQuote(ctx c.Context, responseQuote ResponseQuote) Quote {
ChangePercent: responseQuote.PostMarketChangePercent + responseQuote.RegularMarketChangePercent,
IsActive: false,
IsRegularTradingSession: false,
IsVariablePrecision: isVariablePrecision(responseQuote),
CurrencyConverted: currencyCode,
}
}
Expand All @@ -164,6 +176,7 @@ func transformResponseQuote(ctx c.Context, responseQuote ResponseQuote) Quote {
ChangePercent: responseQuote.RegularMarketChangePercent,
IsActive: false,
IsRegularTradingSession: false,
IsVariablePrecision: isVariablePrecision(responseQuote),
CurrencyConverted: currencyCode,
}

Expand Down
6 changes: 3 additions & 3 deletions internal/ui/component/summary/summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ func (m Model) View() string {

func quoteChangeText(change float64, changePercent float64) string {
if change == 0.0 {
return StyleNeutralFaded(ConvertFloatToString(change) + " (" + ConvertFloatToString(changePercent) + "%)")
return StyleNeutralFaded(ConvertFloatToString(change, false) + " (" + ConvertFloatToString(changePercent, false) + "%)")
}

if change > 0.0 {
return StylePricePositive(changePercent)("↑ " + ConvertFloatToString(change) + " (" + ConvertFloatToString(changePercent) + "%)")
return StylePricePositive(changePercent)("↑ " + ConvertFloatToString(change, false) + " (" + ConvertFloatToString(changePercent, false) + "%)")
}

return StylePriceNegative(changePercent)("↓ " + ConvertFloatToString(change) + " (" + ConvertFloatToString(changePercent) + "%)")
return StylePriceNegative(changePercent)("↓ " + ConvertFloatToString(change, false) + " (" + ConvertFloatToString(changePercent, false) + "%)")
}
6 changes: 3 additions & 3 deletions internal/ui/component/summary/summary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var _ = Describe("Summary", func() {
DayChangePercent: 10.0,
}
Expect(removeFormatting(m.View())).To(Equal(strings.Join([]string{
"Day: ↑ 100.00 (10.000%) • Change: ↑ 9000.00 (1000.00%) • Value: 10000.00",
"Day: ↑ 100.00 (10.00%) • Change: ↑ 9000.00 (1000.00%) • Value: 10000.00",
"━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━",
}, "\n")))
})
Expand All @@ -47,7 +47,7 @@ var _ = Describe("Summary", func() {
DayChangePercent: -10.0,
}
Expect(removeFormatting(m.View())).To(Equal(strings.Join([]string{
"Day: ↓ -100.00 (-10.000%) • Change: ↓ -9000.00 (-1000.00%) • Value: 1000.00",
"Day: ↓ -100.00 (-10.00%) • Change: ↓ -9000.00 (-1000.00%) • Value: 1000.00",
"━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━",
}, "\n")))
})
Expand All @@ -57,7 +57,7 @@ var _ = Describe("Summary", func() {
It("should render an empty summary", func() {
m := NewModel()
Expect(removeFormatting(m.View())).To(Equal(strings.Join([]string{
"Day: 0.0000 (0.0000%) • Change: 0.0000 (0.0000%) • Value: ",
"Day: 0.00 (0.00%) • Change: 0.00 (0.00%) • Value: ",
"━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━",
}, "\n")))
})
Expand Down
38 changes: 19 additions & 19 deletions internal/ui/component/watchlist/watchlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (m Model) View() string {
strings.Join(
[]string{
item(quote, m.Positions[quote.Symbol], m.Width),
extraInfoHoldings(m.Context.Config.ShowHoldings, m.Positions[quote.Symbol], m.Width),
extraInfoHoldings(m.Context.Config.ShowHoldings, quote, m.Positions[quote.Symbol], m.Width),
extraInfoFundamentals(m.ExtraInfoFundamentals, quote, m.Width),
extraInfoExchange(m.ExtraInfoExchange, quote, m.Context.Config.Currency, m.Width),
},
Expand Down Expand Up @@ -96,7 +96,7 @@ func item(q Quote, p Position, width int) string {
},
Cell{
Width: 25,
Text: StyleNeutral(ConvertFloatToString(q.Price)),
Text: StyleNeutral(ConvertFloatToString(q.Price, q.IsVariablePrecision)),
Align: RightAlign,
},
),
Expand All @@ -107,12 +107,12 @@ func item(q Quote, p Position, width int) string {
},
Cell{
Width: 25,
Text: valueChangeText(p.TotalChange, p.TotalChangePercent),
Text: valueChangeText(p.TotalChange, p.TotalChangePercent, q.IsVariablePrecision),
Align: RightAlign,
},
Cell{
Width: 25,
Text: quoteChangeText(q.Change, q.ChangePercent),
Text: quoteChangeText(q.Change, q.ChangePercent, q.IsVariablePrecision),
Align: RightAlign,
},
),
Expand Down Expand Up @@ -147,7 +147,7 @@ func extraInfoFundamentals(show bool, q Quote, width int) string {
return "\n" + Line(
width,
Cell{
Text: dayRangeText(q.PriceDayHigh, q.PriceDayLow),
Text: dayRangeText(q.PriceDayHigh, q.PriceDayLow, q.IsVariablePrecision),
Align: RightAlign,
},
Cell{
Expand All @@ -157,7 +157,7 @@ func extraInfoFundamentals(show bool, q Quote, width int) string {
},
Cell{
Width: 10,
Text: StyleNeutral(ConvertFloatToString(q.PricePrevClose)),
Text: StyleNeutral(ConvertFloatToString(q.PricePrevClose, q.IsVariablePrecision)),
Align: RightAlign,
},
Cell{
Expand All @@ -167,13 +167,13 @@ func extraInfoFundamentals(show bool, q Quote, width int) string {
},
Cell{
Width: 10,
Text: StyleNeutral(ConvertFloatToString(q.PriceOpen)),
Text: StyleNeutral(ConvertFloatToString(q.PriceOpen, q.IsVariablePrecision)),
Align: RightAlign,
},
)
}

func extraInfoHoldings(show bool, p Position, width int) string {
func extraInfoHoldings(show bool, q Quote, p Position, width int) string {
if (p == Position{} || !show) {
return ""
}
Expand All @@ -186,7 +186,7 @@ func extraInfoHoldings(show bool, p Position, width int) string {
},
Cell{
Width: 7,
Text: StyleNeutral(ConvertFloatToString(p.Weight)) + "%",
Text: StyleNeutral(ConvertFloatToString(p.Weight, q.IsVariablePrecision)) + "%",
Align: RightAlign,
},
Cell{
Expand All @@ -196,7 +196,7 @@ func extraInfoHoldings(show bool, p Position, width int) string {
},
Cell{
Width: 10,
Text: StyleNeutral(ConvertFloatToString(p.AverageCost)),
Text: StyleNeutral(ConvertFloatToString(p.AverageCost, q.IsVariablePrecision)),
Align: RightAlign,
},
Cell{
Expand All @@ -206,17 +206,17 @@ func extraInfoHoldings(show bool, p Position, width int) string {
},
Cell{
Width: 10,
Text: StyleNeutral(ConvertFloatToString(p.Quantity)),
Text: StyleNeutral(ConvertFloatToString(p.Quantity, q.IsVariablePrecision)),
Align: RightAlign,
},
)
}

func dayRangeText(high float64, low float64) string {
func dayRangeText(high float64, low float64, isVariablePrecision bool) string {
if high == 0.0 || low == 0.0 {
return ""
}
return StyleNeutralFaded("Day Range: ") + StyleNeutral(ConvertFloatToString(high)+" - "+ConvertFloatToString(low))
return StyleNeutralFaded("Day Range: ") + StyleNeutral(ConvertFloatToString(high, isVariablePrecision)+" - "+ConvertFloatToString(low, isVariablePrecision))
}

func exchangeDelayText(delay float64) string {
Expand All @@ -243,22 +243,22 @@ func marketStateText(q Quote) string {
return ""
}

func valueChangeText(change float64, changePercent float64) string {
func valueChangeText(change float64, changePercent float64, isVariablePrecision bool) string {
if change == 0.0 {
return ""
}

return quoteChangeText(change, changePercent)
return quoteChangeText(change, changePercent, isVariablePrecision)
}

func quoteChangeText(change float64, changePercent float64) string {
func quoteChangeText(change float64, changePercent float64, isVariablePrecision bool) string {
if change == 0.0 {
return StyleNeutralFaded(" " + ConvertFloatToString(change) + " (" + ConvertFloatToString(changePercent) + "%)")
return StyleNeutralFaded(" " + ConvertFloatToString(change, isVariablePrecision) + " (" + ConvertFloatToString(changePercent, false) + "%)")
}

if change > 0.0 {
return StylePricePositive(changePercent)("↑ " + ConvertFloatToString(change) + " (" + ConvertFloatToString(changePercent) + "%)")
return StylePricePositive(changePercent)("↑ " + ConvertFloatToString(change, isVariablePrecision) + " (" + ConvertFloatToString(changePercent, false) + "%)")
}

return StylePriceNegative(changePercent)("↓ " + ConvertFloatToString(change) + " (" + ConvertFloatToString(changePercent) + "%)")
return StylePriceNegative(changePercent)("↓ " + ConvertFloatToString(change, isVariablePrecision) + " (" + ConvertFloatToString(changePercent, false) + "%)")
}
Loading

0 comments on commit f6c4898

Please sign in to comment.