Show stock lines in red when the change is < 0

master
joce 3 years ago
parent 6d416e9e36
commit 5fbeb457d5
  1. 2
      filter.go
  2. 8
      layout.go
  3. 26
      yahoo_quotes.go

@ -63,7 +63,7 @@ func (filter *Filter) Apply(stocks []Stock) []Stock {
values["avgVolume"] = stringToNumber(stock.AvgVolume) values["avgVolume"] = stringToNumber(stock.AvgVolume)
values["pe"] = stringToNumber(stock.PeRatio) values["pe"] = stringToNumber(stock.PeRatio)
values["peX"] = stringToNumber(stock.PeRatioX) values["peX"] = stringToNumber(stock.PeRatioX)
values["advancing"] = stock.Advancing // Remains bool. values["direction"] = stock.Direction // Remains int.
result, err := filter.profile.filterExpression.Evaluate(values) result, err := filter.profile.filterExpression.Evaluate(values)

@ -146,7 +146,7 @@ func (layout *Layout) prettify(quotes *Quotes) []Stock {
// Iterate over the list of stocks and properly format all its columns. // Iterate over the list of stocks and properly format all its columns.
// //
for i, stock := range quotes.stocks { for i, stock := range quotes.stocks {
pretty[i].Advancing = stock.Advancing pretty[i].Direction = stock.Direction
// //
// Iterate over the list of stock columns. For each column name: // Iterate over the list of stock columns. For each column name:
// - Get current column value. // - Get current column value.
@ -222,7 +222,7 @@ func buildQuotesTemplate() *template.Template {
{{.Header}} {{.Header}}
{{range.Stocks}}{{if .Advancing}}<green>{{end}}{{.Ticker}}{{.LastTrade}}{{.Change}}{{.ChangePct}}{{.Open}}{{.Low}}{{.High}}{{.Low52}}{{.High52}}{{.Volume}}{{.AvgVolume}}{{.PeRatio}}{{.Dividend}}{{.Yield}}{{.MarketCap}}{{.PreOpen}}{{.AfterHours}}</> {{range.Stocks}}{{if eq .Direction 1}}<green>{{else if eq .Direction -1}}<red>{{end}}{{.Ticker}}{{.LastTrade}}{{.Change}}{{.ChangePct}}{{.Open}}{{.Low}}{{.High}}{{.Low52}}{{.High52}}{{.Volume}}{{.AvgVolume}}{{.PeRatio}}{{.Dividend}}{{.Yield}}{{.MarketCap}}{{.PreOpen}}{{.AfterHours}}</>
{{end}}` {{end}}`
return template.Must(template.New(`quotes`).Parse(markup)) return template.Must(template.New(`quotes`).Parse(markup))
@ -243,13 +243,13 @@ func group(stocks []Stock) []Stock {
current := 0 current := 0
for _, stock := range stocks { for _, stock := range stocks {
if stock.Advancing { if stock.Direction >= 0 {
grouped[current] = stock grouped[current] = stock
current++ current++
} }
} }
for _, stock := range stocks { for _, stock := range stocks {
if !stock.Advancing { if stock.Direction < 0 {
grouped[current] = stock grouped[current] = stock
current++ current++
} }

@ -22,7 +22,7 @@ const quotesURLv7QueryParts = `&range=1d&interval=5m&indicators=close&includeTim
const noDataIndicator = `N/A` const noDataIndicator = `N/A`
// Stock stores quote information for the particular stock ticker. The data // Stock stores quote information for the particular stock ticker. The data
// for all the fields except 'Advancing' is fetched using Yahoo market API. // for all the fields except 'Direction' is fetched using Yahoo market API.
type Stock struct { type Stock struct {
Ticker string `json:"symbol"` // Stock ticker. Ticker string `json:"symbol"` // Stock ticker.
LastTrade string `json:"regularMarketPrice"` // l1: last trade. LastTrade string `json:"regularMarketPrice"` // l1: last trade.
@ -42,7 +42,7 @@ type Stock struct {
MarketCap string `json:"marketCap"` // j3: market cap real time. MarketCap string `json:"marketCap"` // j3: market cap real time.
MarketCapX string `json:"marketCap"` // j1: market cap (fallback when real time is N/A). MarketCapX string `json:"marketCap"` // j1: market cap (fallback when real time is N/A).
Currency string `json:"currency"` // String code for currency of stock. Currency string `json:"currency"` // String code for currency of stock.
Advancing bool // True when change is >= $0. Direction int // -1 when change is < $0, 0 when change is = $0, 1 when change is > $0.
PreOpen string `json:"preMarketChangePercent,omitempty"` PreOpen string `json:"preMarketChangePercent,omitempty"`
AfterHours string `json:"postMarketChangePercent,omitempty"` AfterHours string `json:"postMarketChangePercent,omitempty"`
} }
@ -188,8 +188,13 @@ func (quotes *Quotes) parse2(body []byte) (*Quotes, error) {
fmt.Println("-------------------") fmt.Println("-------------------")
*/ */
adv, err := strconv.ParseFloat(quotes.stocks[i].Change, 64) adv, err := strconv.ParseFloat(quotes.stocks[i].Change, 64)
quotes.stocks[i].Direction = 0
if err == nil { if err == nil {
quotes.stocks[i].Advancing = adv >= 0.0 if adv < 0.0 {
quotes.stocks[i].Direction = -1
} else if adv > 0.0 {
quotes.stocks[i].Direction = 1
}
} }
} }
return quotes, nil return quotes, nil
@ -202,7 +207,7 @@ func (quotes *Quotes) parse(body []byte) *Quotes {
quotes.stocks = make([]Stock, len(lines)) quotes.stocks = make([]Stock, len(lines))
// //
// Get the total number of fields in the Stock struct. Skip the last // Get the total number of fields in the Stock struct. Skip the last
// Advanicing field which is not fetched. // Advancing field which is not fetched.
// //
fieldsCount := reflect.ValueOf(quotes.stocks[0]).NumField() - 1 fieldsCount := reflect.ValueOf(quotes.stocks[0]).NumField() - 1
// //
@ -226,10 +231,17 @@ func (quotes *Quotes) parse(body []byte) *Quotes {
quotes.stocks[i].MarketCap = quotes.stocks[i].MarketCapX quotes.stocks[i].MarketCap = quotes.stocks[i].MarketCapX
} }
// //
// Stock is advancing if the change is not negative (i.e. $0.00 // Get the direction of the stock
// is also "advancing").
// //
quotes.stocks[i].Advancing = (quotes.stocks[i].Change[0:1] != `-`) adv, err := strconv.ParseFloat(quotes.stocks[i].Change, 64)
quotes.stocks[i].Direction = 0
if err == nil {
if adv < 0 {
quotes.stocks[i].Direction = -1
} else if (adv > 0) {
quotes.stocks[i].Direction = 1
}
}
} }
return quotes return quotes

Loading…
Cancel
Save