Skip to content

Commit

Permalink
feat: added market cap and volume
Browse files Browse the repository at this point in the history
  • Loading branch information
achannarasappa committed May 31, 2021
1 parent 18bb224 commit ead91a1
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 27 deletions.
2 changes: 2 additions & 0 deletions internal/quote/quote.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type ResponseQuote struct {
RegularMarketDayRange string `json:"regularMarketDayRange"`
RegularMarketDayHigh float64 `json:"regularMarketDayHigh"`
RegularMarketDayLow float64 `json:"regularMarketDayLow"`
RegularMarketVolume float64 `json:"regularMarketVolume"`
PostMarketChange float64 `json:"postMarketChange"`
PostMarketChangePercent float64 `json:"postMarketChangePercent"`
PostMarketPrice float64 `json:"postMarketPrice"`
Expand All @@ -33,6 +34,7 @@ type ResponseQuote struct {
FiftyTwoWeekHigh float64 `json:"fiftyTwoWeekHigh"`
FiftyTwoWeekLow float64 `json:"fiftyTwoWeekLow"`
QuoteType string `json:"quoteType"`
MarketCap float64 `json:"marketCap"`
}

type Quote struct {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
STOCK1Day Range: 90.00 - 120.00 Prev. Close: 100.00 105.00
Stock 1 Inc. (gain) 52wk Range: 0.00 - 0.00 Open: 110.005.00 (5.00%)
Real-Time
STOCK2Day Range: 90.00 - 120.00 Prev. Close: 100.00 95.00
Stock 2 Inc. (loss) 52wk Range: 0.00 - 150.00 Open: 110.00-5.00 (-5.00%)
Real-Time
STOCK3Day Range: 90.00 - 120.00 Prev. Close: 100.00 105.00
Stock 3 Inc. (gain, 52wk Range: 0.00 - 150.00 Open: 110.005.00 (5.00%)
Real-Time
STOCK4Day Range: 90.00 - 120.00 Prev. Close: 100.00 Avg. Cost: 0.00 105.00 (0.00%) 105.00
Stock 4 Inc. (positi 52wk Range: 0.00 - 150.00 Open: 110.00 Quantity: 100.0055.00 (110.00%) ↑ 5.00 (5.00%)
Real-Time
STOCK5Day Range: 90.00 - 120.00 Prev. Close: 100.00 Avg. Cost: 0.00 105.00 (0.00%) 105.00
Stock 5 Inc. (positi 52wk Range: 0.00 - 150.00 Open: 110.00 Quantity: 100.00-45.00 (-30.00%) ↑ 5.00 (5.00%)
Real-Time
STOCK6Day Range: 90.00 - 120.00 Prev. Close: 100.00 Avg. Cost: 0.00 95.00 (0.00%) 95.00
Stock 6 Inc. (positi 52wk Range: 0.00 - 150.00 Open: 110.00 Quantity: 100.00-55.00 (-36.67%) ↓ -5.00 (-5.00%)
Real-Time
STOCK7Day Range: 90.00 - 120.00 Prev. Close: 100.00 Avg. Cost: 0.00 95.00 (0.00%) 95.00
Stock 7 Inc. (positi 52wk Range: 0.00 - 0.00 Open: 110.00 Quantity: 100.0045.00 (90.00%) ↓ -5.00 (-5.00%)
Real-Time
STOCK8 Day Range: 90.00 - 120.00 Prev. Close: 100.00 Avg. Cost: 0.00 95.00 (0.00%) 95.00
Stock 8 Inc. (positi 52wk Range: 0.00 - 0.00 Open: 110.00 Quantity: 100.0045.00 (90.00%) 0.00 (0.00%)
Real-Time
STOCK1 Market Cap: 23.468 M Day Range: 90.00 - 120.00 Prev. Close: 100.00 105.00
Stock 1 Inc. (gain) Volume: 4.2398 B 52wk Range: 0.00 - 0.00 Open: 110.005.00 (5.00%)
Real-Time
STOCK2 Market Cap: 0.00 Day Range: 90.00 - 120.00 Prev. Close: 100.00 95.00
Stock 2 Inc. (loss) Volume: 0.00 52wk Range: 0.00 - 150.00 Open: 110.00-5.00 (-5.00%)
Real-Time
STOCK3 Market Cap: 0.00 Day Range: 90.00 - 120.00 Prev. Close: 100.00 105.00
Stock 3 Inc. (gain, Volume: 0.00 52wk Range: 0.00 - 150.00 Open: 110.005.00 (5.00%)
Real-Time
STOCK4 Market Cap: 0.00 Day Range: 90.00 - 120.00 Prev. Close: 100.00 Avg. Cost: 0.00 105.00 (0.00%) 105.00
Stock 4 Inc. (positi Volume: 0.00 52wk Range: 0.00 - 150.00 Open: 110.00 Quantity: 100.0055.00 (110.00%) ↑ 5.00 (5.00%)
Real-Time
STOCK5 Market Cap: 0.00 Day Range: 90.00 - 120.00 Prev. Close: 100.00 Avg. Cost: 0.00 105.00 (0.00%) 105.00
Stock 5 Inc. (positi Volume: 0.00 52wk Range: 0.00 - 150.00 Open: 110.00 Quantity: 100.00-45.00 (-30.00%) ↑ 5.00 (5.00%)
Real-Time
STOCK6 Market Cap: 0.00 Day Range: 90.00 - 120.00 Prev. Close: 100.00 Avg. Cost: 0.00 95.00 (0.00%) 95.00
Stock 6 Inc. (positi Volume: 0.00 52wk Range: 0.00 - 150.00 Open: 110.00 Quantity: 100.00-55.00 (-36.67%) ↓ -5.00 (-5.00%)
Real-Time
STOCK7 Market Cap: 0.00 Day Range: 90.00 - 120.00 Prev. Close: 100.00 Avg. Cost: 0.00 95.00 (0.00%) 95.00
Stock 7 Inc. (positi Volume: 0.00 52wk Range: 0.00 - 0.00 Open: 110.00 Quantity: 100.0045.00 (90.00%) ↓ -5.00 (-5.00%)
Real-Time
STOCK8 Market Cap: 0.00 Day Range: 90.00 - 120.00 Prev. Close: 100.00 Avg. Cost: 0.00 95.00 (0.00%) 95.00
Stock 8 Inc. (positi Volume: 0.00 52wk Range: 0.00 - 0.00 Open: 110.00 Quantity: 100.0045.00 (90.00%) 0.00 (0.00%)
Real-Time
33 changes: 33 additions & 0 deletions internal/ui/component/watchlist/watchlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type CellWidths struct {
WidthQuoteRange int
WidthPosition int
WidthPositionExtended int
WidthVolumeMarketCap int
}

// NewModel returns a model with default values.
Expand Down Expand Up @@ -117,6 +118,8 @@ func getCellWidths(quotes []Quote, positions map[string]Position) CellWidths {
for _, quote := range quotes {
var quoteLength int

volumeMarketCapLength := len(ConvertFloatToString(quote.MarketCap, quote.IsVariablePrecision))

if quote.FiftyTwoWeekHigh == 0.0 {
quoteLength = len(ConvertFloatToString(quote.Price, quote.IsVariablePrecision))
}
Expand All @@ -125,6 +128,10 @@ func getCellWidths(quotes []Quote, positions map[string]Position) CellWidths {
quoteLength = len(ConvertFloatToString(quote.FiftyTwoWeekHigh, quote.IsVariablePrecision))
}

if volumeMarketCapLength > cellMaxWidths.WidthVolumeMarketCap {
cellMaxWidths.WidthVolumeMarketCap = volumeMarketCapLength
}

if quoteLength > cellMaxWidths.quoteLength {
cellMaxWidths.quoteLength = quoteLength
cellMaxWidths.WidthQuote = quoteLength + WIDTH_CHANGE_STATIC
Expand Down Expand Up @@ -213,6 +220,18 @@ func buildCells(quote Quote, position Position, config c.Config, styles c.Styles
if config.ExtraInfoFundamentals {
cells = append(
[]grid.Cell{
{
Text: textVolumeMarketCapLabels(quote, styles),
Width: WIDTH_LABEL,
Align: grid.Right,
VisibleMinWidth: widthMinTerm + cellWidths.WidthQuoteExtended + (6 * WIDTH_GUTTER) + (3 * WIDTH_LABEL) + cellWidths.WidthQuoteRange + cellWidths.WidthVolumeMarketCap,
},
{
Text: textVolumeMarketCap(quote, styles),
Width: cellWidths.WidthVolumeMarketCap,
Align: grid.Right,
VisibleMinWidth: widthMinTerm + cellWidths.WidthQuoteExtended + (5 * WIDTH_GUTTER) + (2 * WIDTH_LABEL) + cellWidths.WidthQuoteRange + cellWidths.WidthVolumeMarketCap,
},
{
Text: textQuoteRangeLabels(quote, styles),
Width: WIDTH_LABEL,
Expand Down Expand Up @@ -358,6 +377,20 @@ func textQuoteRangeLabels(quote Quote, styles c.Styles) string {
return textDayRange
}

func textVolumeMarketCap(quote Quote, styles c.Styles) string {

return ConvertFloatToString(quote.ResponseQuote.MarketCap, true) +
"\n" +
ConvertFloatToString(quote.ResponseQuote.RegularMarketVolume, true)
}

func textVolumeMarketCapLabels(quote Quote, styles c.Styles) string {

return styles.TextLabel("Market Cap:") +
"\n" +
styles.TextLabel("Volume:")
}

func textSeparator(width int, styles c.Styles) string {
return styles.TextLine(strings.Repeat("─", width))
}
Expand Down
4 changes: 2 additions & 2 deletions internal/ui/component/watchlist/watchlist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ var _ = Describe("Watchlist", func() {
Sort: "alpha",
},
})
m.Width = 150
m.Width = 200
m.Positions = map[string]Position{
"STOCK4": {
AggregatedLot: AggregatedLot{Symbol: "STOCK4", Quantity: 100.0, Cost: 50.0},
Expand Down Expand Up @@ -79,7 +79,7 @@ var _ = Describe("Watchlist", func() {
}
m.Quotes = []Quote{
{
ResponseQuote: ResponseQuote{Symbol: "STOCK1", ShortName: "Stock 1 Inc. (gain)"},
ResponseQuote: ResponseQuote{Symbol: "STOCK1", ShortName: "Stock 1 Inc. (gain)", MarketCap: 23467907, RegularMarketVolume: 4239786698},
Price: 105.00, PricePrevClose: 100.00, PriceOpen: 110.00, PriceDayHigh: 120.00, PriceDayLow: 90.00,
Change: 5.0, ChangePercent: 5.0,
IsActive: true, IsRegularTradingSession: true,
Expand Down
19 changes: 18 additions & 1 deletion internal/ui/util/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,30 @@ func getPrecision(f float64) int {

func ConvertFloatToString(f float64, isVariablePrecision bool) string {

var unit string

if !isVariablePrecision {
return strconv.FormatFloat(f, 'f', 2, 64)
}

if f > 1000000000000 {
f = f / 1000000000000
unit = " T"
}

if f > 1000000000 {
f = f / 1000000000
unit = " B"
}

if f > 1000000 {
f = f / 1000000
unit = " M"
}

prec := getPrecision(f)

return strconv.FormatFloat(f, 'f', prec, 64)
return strconv.FormatFloat(f, 'f', prec, 64) + unit
}

func ValueText(value float64, styles c.Styles) string {
Expand Down
12 changes: 12 additions & 0 deletions internal/ui/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ var _ = Describe("Util", func() {
output := ConvertFloatToString(10000.0, true)
Expect(output).To(Equal("10000"))
})
It("should append a M when the value is over a million", func() {
output := ConvertFloatToString(43523398, true)
Expect(output).To(Equal("43.523 M"))
})
It("should append a M when the value is over a billion", func() {
output := ConvertFloatToString(43523398000, true)
Expect(output).To(Equal("43.523 B"))
})
It("should append a M when the value is over a trillion", func() {
output := ConvertFloatToString(43523398000000, true)
Expect(output).To(Equal("43.523 T"))
})
})

})
Expand Down

0 comments on commit ead91a1

Please sign in to comment.