Add pre and post market change percentage.

master
Adelina Tuvenie 5 years ago committed by Adelina Tuvenie
parent 4f481911f3
commit b19ce4f210
  1. 20
      layout.go
  2. 22
      yahoo_quotes.go

@ -5,13 +5,13 @@
package mop package mop
import ( import (
`bytes` "bytes"
`fmt` "fmt"
`reflect` "reflect"
`regexp` "regexp"
`strings` "strings"
`text/template` "text/template"
`time` "time"
) )
// Column describes formatting rules for individual column within the list // Column describes formatting rules for individual column within the list
@ -53,6 +53,8 @@ func NewLayout() *Layout {
{9, `Dividend`, `Dividend`, zero}, {9, `Dividend`, `Dividend`, zero},
{9, `Yield`, `Yield`, percent}, {9, `Yield`, `Yield`, percent},
{11, `MarketCap`, `MktCap`, currency}, {11, `MarketCap`, `MktCap`, currency},
{13, `PreOpen`, `PreMktChg%`, last},
{13, `AfterHours`, `AfterMktChg%`, last},
} }
layout.regex = regexp.MustCompile(`(\.\d+)[BMK]?$`) layout.regex = regexp.MustCompile(`(\.\d+)[BMK]?$`)
layout.marketTemplate = buildMarketTemplate() layout.marketTemplate = buildMarketTemplate()
@ -208,7 +210,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}}</> {{range.Stocks}}{{if .Advancing}}<green>{{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))
@ -298,7 +300,7 @@ func currency(str... string) string {
} }
//default to $ //default to $
symbol := "$" symbol := "$"
switch (str[1]){ switch str[1] {
case "JPY": case "JPY":
symbol = "¥" symbol = "¥"
break break

@ -5,14 +5,14 @@
package mop package mop
import ( import (
`bytes` "bytes"
`encoding/json` "encoding/json"
`fmt` "fmt"
`io/ioutil` "io/ioutil"
`net/http` "net/http"
`reflect` "reflect"
`strconv` "strconv"
`strings` "strings"
) )
// const quotesURL = `http://download.finance.yahoo.com/d/quotes.csv?s=%s&f=sl1c1p2oghjkva2r2rdyj3j1` // const quotesURL = `http://download.finance.yahoo.com/d/quotes.csv?s=%s&f=sl1c1p2oghjkva2r2rdyj3j1`
@ -43,6 +43,8 @@ type Stock struct {
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. Advancing bool // True when change is >= $0.
PreOpen string `json:"preMarketChangePercent,omitempty"`
AfterHours string `json:"postMarketChangePercent,omitempty"`
} }
// Quotes stores relevant pointers as well as the array of stock quotes for // Quotes stores relevant pointers as well as the array of stock quotes for
@ -173,7 +175,8 @@ func (quotes *Quotes) parse2(body []byte) (*Quotes, error) {
// TODO calculate rt? // TODO calculate rt?
quotes.stocks[i].MarketCapX = result["marketCap"] quotes.stocks[i].MarketCapX = result["marketCap"]
quotes.stocks[i].Currency = result["currency"] quotes.stocks[i].Currency = result["currency"]
quotes.stocks[i].PreOpen = result["preMarketChangePercent"]
quotes.stocks[i].AfterHours = result["postMarketChangePercent"]
/* /*
fmt.Println(i) fmt.Println(i)
fmt.Println("-------------------") fmt.Println("-------------------")
@ -256,4 +259,3 @@ func sanitize(body []byte) []byte {
// parse // parse
return fmt.Sprintf("%0.3f%s", v, unit) return fmt.Sprintf("%0.3f%s", v, unit)
} }

Loading…
Cancel
Save