Skip to content

Commit

Permalink
fix: converting fundamentals currency
Browse files Browse the repository at this point in the history
  • Loading branch information
achannarasappa committed Feb 15, 2021
1 parent fb09ef1 commit dd03701
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 79 deletions.
34 changes: 34 additions & 0 deletions internal/quote/quote.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ type ResponseQuote struct {
RegularMarketPreviousClose float64 `json:"regularMarketPreviousClose"`
RegularMarketOpen float64 `json:"regularMarketOpen"`
RegularMarketDayRange string `json:"regularMarketDayRange"`
RegularMarketDayHigh float64 `json:"regularMarketDayHigh"`
RegularMarketDayLow float64 `json:"regularMarketDayLow"`
PostMarketChange float64 `json:"postMarketChange"`
PostMarketChangePercent float64 `json:"postMarketChangePercent"`
PostMarketPrice float64 `json:"postMarketPrice"`
Expand All @@ -33,6 +35,10 @@ type ResponseQuote struct {
type Quote struct {
ResponseQuote
Price float64
PricePrevClose float64
PriceOpen float64
PriceDayHigh float64
PriceDayLow float64
Change float64
ChangePercent float64
IsActive bool
Expand All @@ -55,6 +61,10 @@ func transformResponseQuote(ctx c.Context, responseQuote ResponseQuote) Quote {
return Quote{
ResponseQuote: responseQuote,
Price: responseQuote.RegularMarketPrice * currencyRate,
PricePrevClose: responseQuote.RegularMarketPreviousClose * currencyRate,
PriceOpen: responseQuote.RegularMarketOpen * currencyRate,
PriceDayHigh: responseQuote.RegularMarketDayHigh * currencyRate,
PriceDayLow: responseQuote.RegularMarketDayLow * currencyRate,
Change: (responseQuote.RegularMarketChange) * currencyRate,
ChangePercent: responseQuote.RegularMarketChangePercent,
IsActive: true,
Expand All @@ -67,6 +77,10 @@ func transformResponseQuote(ctx c.Context, responseQuote ResponseQuote) Quote {
return Quote{
ResponseQuote: responseQuote,
Price: responseQuote.RegularMarketPrice * currencyRate,
PricePrevClose: responseQuote.RegularMarketPreviousClose * currencyRate,
PriceOpen: responseQuote.RegularMarketOpen * currencyRate,
PriceDayHigh: responseQuote.RegularMarketDayHigh * currencyRate,
PriceDayLow: responseQuote.RegularMarketDayLow * currencyRate,
Change: (responseQuote.RegularMarketChange) * currencyRate,
ChangePercent: responseQuote.RegularMarketChangePercent,
IsActive: true,
Expand All @@ -79,6 +93,10 @@ func transformResponseQuote(ctx c.Context, responseQuote ResponseQuote) Quote {
return Quote{
ResponseQuote: responseQuote,
Price: responseQuote.RegularMarketPrice * currencyRate,
PricePrevClose: responseQuote.RegularMarketPreviousClose * currencyRate,
PriceOpen: responseQuote.RegularMarketOpen * currencyRate,
PriceDayHigh: responseQuote.RegularMarketDayHigh * currencyRate,
PriceDayLow: responseQuote.RegularMarketDayLow * currencyRate,
Change: (responseQuote.RegularMarketChange) * currencyRate,
ChangePercent: responseQuote.RegularMarketChangePercent,
IsActive: false,
Expand All @@ -91,6 +109,10 @@ func transformResponseQuote(ctx c.Context, responseQuote ResponseQuote) Quote {
return Quote{
ResponseQuote: responseQuote,
Price: responseQuote.PostMarketPrice * currencyRate,
PricePrevClose: responseQuote.RegularMarketPreviousClose * currencyRate,
PriceOpen: responseQuote.RegularMarketOpen * currencyRate,
PriceDayHigh: responseQuote.RegularMarketDayHigh * currencyRate,
PriceDayLow: responseQuote.RegularMarketDayLow * currencyRate,
Change: (responseQuote.PostMarketChange + responseQuote.RegularMarketChange) * currencyRate,
ChangePercent: responseQuote.PostMarketChangePercent + responseQuote.RegularMarketChangePercent,
IsActive: true,
Expand All @@ -103,6 +125,10 @@ func transformResponseQuote(ctx c.Context, responseQuote ResponseQuote) Quote {
return Quote{
ResponseQuote: responseQuote,
Price: responseQuote.PreMarketPrice * currencyRate,
PricePrevClose: responseQuote.RegularMarketPreviousClose * currencyRate,
PriceOpen: responseQuote.RegularMarketOpen * currencyRate,
PriceDayHigh: responseQuote.RegularMarketDayHigh * currencyRate,
PriceDayLow: responseQuote.RegularMarketDayLow * currencyRate,
Change: (responseQuote.PreMarketChange) * currencyRate,
ChangePercent: responseQuote.PreMarketChangePercent,
IsActive: true,
Expand All @@ -115,6 +141,10 @@ func transformResponseQuote(ctx c.Context, responseQuote ResponseQuote) Quote {
return Quote{
ResponseQuote: responseQuote,
Price: responseQuote.PostMarketPrice * currencyRate,
PricePrevClose: responseQuote.RegularMarketPreviousClose * currencyRate,
PriceOpen: responseQuote.RegularMarketOpen * currencyRate,
PriceDayHigh: responseQuote.RegularMarketDayHigh * currencyRate,
PriceDayLow: responseQuote.RegularMarketDayLow * currencyRate,
Change: (responseQuote.PostMarketChange + responseQuote.RegularMarketChange) * currencyRate,
ChangePercent: responseQuote.PostMarketChangePercent + responseQuote.RegularMarketChangePercent,
IsActive: false,
Expand All @@ -126,6 +156,10 @@ func transformResponseQuote(ctx c.Context, responseQuote ResponseQuote) Quote {
return Quote{
ResponseQuote: responseQuote,
Price: responseQuote.RegularMarketPrice * currencyRate,
PricePrevClose: responseQuote.RegularMarketPreviousClose * currencyRate,
PriceOpen: responseQuote.RegularMarketOpen * currencyRate,
PriceDayHigh: responseQuote.RegularMarketDayHigh * currencyRate,
PriceDayLow: responseQuote.RegularMarketDayLow * currencyRate,
Change: (responseQuote.RegularMarketChange) * currencyRate,
ChangePercent: responseQuote.RegularMarketChangePercent,
IsActive: false,
Expand Down
102 changes: 30 additions & 72 deletions internal/quote/quote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,19 @@ var _ = Describe("Quote", func() {
{
"marketState": "REGULAR",
"shortName": "Cloudflare, Inc.",
"preMarketChange": 1.0399933,
"preMarketChangePercent": 1.2238094,
"preMarketPrice": 86.03,
"regularMarketChange": 3.0800018,
"regularMarketChangePercent": 3.7606857,
"regularMarketTime": 1608832801,
"regularMarketPrice": 84.98,
"regularMarketPreviousClose": 81.9,
"regularMarketPreviousClose": 84.00,
"regularMarketOpen": 85.22,
"regularMarketDayHigh": 90.00,
"regularMarketDayLow": 80.00,
"postMarketChange": 1.37627,
"postMarketChangePercent": 1.35735,
"postMarketPrice": 86.56,
"symbol": "NET"
}
],
Expand All @@ -40,25 +48,15 @@ var _ = Describe("Quote", func() {

inputCtx := c.Context{}
output := GetQuotes(inputCtx, *client, []string{"NET"})()
expected := []Quote{
{
ResponseQuote: ResponseQuote{
ShortName: "Cloudflare, Inc.",
Symbol: "NET",
MarketState: "REGULAR",
RegularMarketChange: 3.0800018,
RegularMarketChangePercent: 3.7606857,
RegularMarketPrice: 84.98,
RegularMarketPreviousClose: 81.9,
},
Price: 84.98,
Change: 3.0800018,
ChangePercent: 3.7606857,
IsActive: true,
IsRegularTradingSession: true,
},
}
Expect(output).To(Equal(expected))
Expect(output[0].Price).To(Equal(84.98))
Expect(output[0].PricePrevClose).To(Equal(84.00))
Expect(output[0].PriceOpen).To(Equal(85.22))
Expect(output[0].PriceDayHigh).To(Equal(90.00))
Expect(output[0].PriceDayLow).To(Equal(80.00))
Expect(output[0].Change).To(Equal(3.0800018))
Expect(output[0].ChangePercent).To(Equal(3.7606857))
Expect(output[0].IsActive).To(BeTrue())
Expect(output[0].IsRegularTradingSession).To(BeTrue())
})

When("the market is in a pre-market trading session", func() {
Expand All @@ -71,12 +69,10 @@ var _ = Describe("Quote", func() {
"shortName": "Cloudflare, Inc.",
"preMarketChange": 1.0399933,
"preMarketChangePercent": 1.2238094,
"preMarketPrice": 86.02,
"preMarketPrice": 86.03,
"regularMarketChange": 3.0800018,
"regularMarketChangePercent": 3.7606857,
"regularMarketTime": 1608832801,
"regularMarketPrice": 84.98,
"regularMarketPreviousClose": 81.9,
"symbol": "NET"
}
],
Expand All @@ -92,28 +88,11 @@ var _ = Describe("Quote", func() {

inputCtx := c.Context{}
output := GetQuotes(inputCtx, *client, []string{"NET"})()
expected := []Quote{
{
ResponseQuote: ResponseQuote{
ShortName: "Cloudflare, Inc.",
Symbol: "NET",
MarketState: "PRE",
RegularMarketChange: 3.0800018,
RegularMarketChangePercent: 3.7606857,
RegularMarketPrice: 84.98,
RegularMarketPreviousClose: 81.9,
PreMarketChange: 1.0399933,
PreMarketChangePercent: 1.2238094,
PreMarketPrice: 86.02,
},
Price: 86.02,
Change: 1.0399933,
ChangePercent: 1.2238094,
IsActive: true,
IsRegularTradingSession: false,
},
}
Expect(output).To(Equal(expected))
Expect(output[0].Price).To(Equal(86.03))
Expect(output[0].Change).To(Equal(1.0399933))
Expect(output[0].ChangePercent).To(Equal(1.2238094))
Expect(output[0].IsActive).To(BeTrue())
Expect(output[0].IsRegularTradingSession).To(BeFalse())
})

When("there is no pre-market price", func() {
Expand All @@ -126,9 +105,7 @@ var _ = Describe("Quote", func() {
"shortName": "Cloudflare, Inc.",
"regularMarketChange": 3.0800018,
"regularMarketChangePercent": 3.7606857,
"regularMarketTime": 1608832801,
"regularMarketPrice": 84.98,
"regularMarketPreviousClose": 81.9,
"symbol": "NET"
}
],
Expand Down Expand Up @@ -166,9 +143,7 @@ var _ = Describe("Quote", func() {
"postMarketPrice": 86.02,
"regularMarketChange": 3.0800018,
"regularMarketChangePercent": 3.7606857,
"regularMarketTime": 1608832801,
"regularMarketPrice": 84.98,
"regularMarketPreviousClose": 81.9,
"symbol": "NET"
}
],
Expand All @@ -184,28 +159,11 @@ var _ = Describe("Quote", func() {

inputCtx := c.Context{}
output := GetQuotes(inputCtx, *client, []string{"NET"})()
expected := []Quote{
{
ResponseQuote: ResponseQuote{
ShortName: "Cloudflare, Inc.",
Symbol: "NET",
MarketState: "POST",
RegularMarketChange: 3.0800018,
RegularMarketChangePercent: 3.7606857,
RegularMarketPrice: 84.98,
RegularMarketPreviousClose: 81.9,
PostMarketChange: 1.0399933,
PostMarketChangePercent: 1.2238094,
PostMarketPrice: 86.02,
},
Price: 86.02,
Change: 4.1199951,
ChangePercent: 4.9844951,
IsActive: true,
IsRegularTradingSession: false,
},
}
Expect(output).To(Equal(expected))
Expect(output[0].Price).To(Equal(86.02))
Expect(output[0].Change).To(Equal(4.1199951))
Expect(output[0].ChangePercent).To(Equal(4.9844951))
Expect(output[0].IsActive).To(BeTrue())
Expect(output[0].IsRegularTradingSession).To(BeFalse())
})

When("there is no post-market price", func() {
Expand Down
12 changes: 6 additions & 6 deletions internal/ui/component/watchlist/watchlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func extraInfoFundamentals(show bool, q Quote, width int) string {
return "\n" + Line(
width,
Cell{
Text: dayRangeText(q.RegularMarketDayRange),
Text: dayRangeText(q.PriceDayHigh, q.PriceDayLow),
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.RegularMarketPreviousClose)),
Text: StyleNeutral(ConvertFloatToString(q.PricePrevClose)),
Align: RightAlign,
},
Cell{
Expand All @@ -167,7 +167,7 @@ func extraInfoFundamentals(show bool, q Quote, width int) string {
},
Cell{
Width: 10,
Text: StyleNeutral(ConvertFloatToString(q.RegularMarketOpen)),
Text: StyleNeutral(ConvertFloatToString(q.PriceOpen)),
Align: RightAlign,
},
)
Expand Down Expand Up @@ -212,11 +212,11 @@ func extraInfoHoldings(show bool, p Position, width int) string {
)
}

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

func exchangeDelayText(delay float64) string {
Expand Down
8 changes: 7 additions & 1 deletion internal/ui/component/watchlist/watchlist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,10 @@ var _ = Describe("Watchlist", func() {
RegularMarketDayRange: "10000 - 10000",
},
Price: 50000.0,
PricePrevClose: 10000.0,
PriceOpen: 10000.0,
PriceDayHigh: 10000.0,
PriceDayLow: 10000.0,
Change: 10000.0,
ChangePercent: 20.0,
IsActive: true,
Expand All @@ -472,7 +476,7 @@ var _ = Describe("Watchlist", func() {
expected := strings.Join([]string{
"BTC-USD ● 50000.00",
"Bitcoin ↑ 10000.00 (20.00%)",
" Day Range: 10000 - 10000 Prev Close: 10000.00 Open: 10000.00",
"Day Range: 10000.00 - 10000.00 Prev Close: 10000.00 Open: 10000.00",
}, "\n")
Expect(removeFormatting(m.View())).To(Equal(expected))
})
Expand All @@ -496,6 +500,8 @@ var _ = Describe("Watchlist", func() {
RegularMarketOpen: 10000.0,
},
Price: 50000.0,
PricePrevClose: 10000.0,
PriceOpen: 10000.0,
Change: 10000.0,
ChangePercent: 20.0,
IsActive: true,
Expand Down

0 comments on commit dd03701

Please sign in to comment.