github.com/wtfutil/wtf@v0.43.0/modules/stocks/yfinance/yquote.go (about) 1 package yfinance 2 3 import ( 4 "github.com/jedib0t/go-pretty/v6/table" 5 "github.com/jedib0t/go-pretty/v6/text" 6 "github.com/piquette/finance-go/quote" 7 ) 8 9 type MarketState string 10 11 type yquote struct { 12 Trend string // can be bigup (>3%), up, drop or bigdrop (<3%) 13 Symbol string 14 Currency string 15 MarketState string 16 MarketPrice float64 17 MarketChange float64 18 MarketChangePct float64 19 } 20 21 func tableStyle() table.Style { 22 return table.Style{ 23 Name: "yfinance", 24 Box: table.BoxStyle{ 25 BottomLeft: "", 26 BottomRight: "", 27 BottomSeparator: "", 28 Left: "", 29 LeftSeparator: "", 30 MiddleHorizontal: " ", 31 MiddleSeparator: "", 32 MiddleVertical: "", 33 PaddingLeft: " ", 34 PaddingRight: "", 35 Right: "", 36 RightSeparator: "", 37 TopLeft: "", 38 TopRight: "", 39 TopSeparator: "", 40 UnfinishedRow: "", 41 }, 42 Color: table.ColorOptions{ 43 Footer: text.Colors{}, 44 Header: text.Colors{}, 45 Row: text.Colors{}, 46 RowAlternate: text.Colors{}, 47 }, 48 Format: table.FormatOptions{ 49 Footer: text.FormatUpper, 50 Header: text.FormatUpper, 51 Row: text.FormatDefault, 52 }, 53 Options: table.Options{ 54 DrawBorder: false, 55 SeparateColumns: false, 56 SeparateFooter: false, 57 SeparateHeader: false, 58 SeparateRows: false, 59 }, 60 } 61 } 62 63 func quotes(symbols []string) []yquote { 64 var yquotes []yquote 65 for _, symbol := range symbols { 66 var yq yquote 67 68 var MarketPrice float64 69 var MarketChange float64 70 var MarketChangePct float64 71 72 q, err := quote.Get(symbol) 73 if q == nil || err != nil { 74 yq = yquote{ 75 Symbol: symbol, 76 Trend: "?", 77 MarketState: "?", 78 } 79 } else { 80 if q.MarketState == "PRE" { 81 MarketPrice = q.PreMarketPrice 82 MarketChange = q.PreMarketChange 83 MarketChangePct = q.PreMarketChangePercent 84 85 } else if q.MarketState == "POST" { 86 MarketPrice = q.PostMarketPrice 87 MarketChange = q.PostMarketChange 88 MarketChangePct = q.PostMarketChangePercent 89 } else { 90 MarketPrice = q.RegularMarketPrice 91 MarketChange = q.RegularMarketChange 92 MarketChangePct = q.RegularMarketChangePercent 93 } 94 yq = yquote{ 95 Symbol: q.Symbol, 96 Currency: q.CurrencyID, 97 Trend: GetTrend(MarketChangePct), 98 MarketState: string(q.MarketState), 99 MarketPrice: MarketPrice, 100 MarketChange: MarketChange, 101 MarketChangePct: MarketChangePct, 102 } 103 } 104 yquotes = append(yquotes, yq) 105 } 106 return yquotes 107 } 108 109 func GetMarketIcon(state string) string { 110 states := map[string]string{ 111 "PRE": "⏭", 112 "REGULAR": "▶", 113 "POST": "⏮", 114 "?": "?", 115 } 116 if icon, ok := states[state]; ok { 117 return icon 118 } else { 119 return "⏹" 120 } 121 } 122 123 func GetTrendIcon(trend string) string { 124 icons := map[string]string{ 125 "bigup": "⬆️ ", 126 "up": "↗️ ", 127 "drop": "↘️ ", 128 "bigdrop": "⬇️ ", 129 } 130 return icons[trend] 131 } 132 133 func GetTrend(pct float64) string { 134 var trend string 135 if pct > 3 { 136 trend = "bigup" 137 } else if pct > 0 { 138 trend = "up" 139 } else if pct > -3 { 140 trend = "drop" 141 } else { 142 trend = "bigdrop" 143 } 144 return trend 145 }