-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_quote.go
138 lines (117 loc) · 3.11 KB
/
cmd_quote.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package main
import (
"fmt"
"github.com/guptarohit/asciigraph"
"github.com/olekukonko/tablewriter"
"github.com/urfave/cli/v2"
"math"
"os"
"strings"
"time"
)
var graphData = []float64{}
func cmdQuote() *cli.Command {
return &cli.Command{
Name: "quote",
Usage: "Print quote",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "watch",
Aliases: []string{"w"},
Value: false,
Usage: "watch mode",
},
},
Action: func(c *cli.Context) error {
symbols := []string{}
if c.NArg() > 0 {
symbols = strings.Split(c.Args().Get(0), ",")
}
quoteInfo(symbols, c.Bool("watch"))
return nil
},
}
}
func printQuotes(quotes []Quote) {
table := tablewriter.NewWriter(os.Stdout)
headers := []string{"Symbol", "Price", "Pct", "State", "Name", "Trading Hours"}
for _, q := range quotes {
color := tablewriter.FgGreenColor
event := ""
if (q.State == "CLOSED" || q.State == "PREPRE") && q.MarketInfo.DurationUntilOpenPre != nil {
event = fmt.Sprintf("Pre market opens in %s", formatDuration(*q.MarketInfo.DurationUntilOpenPre))
} else if (q.State == "CLOSED" || q.State == "PREPRE") && q.MarketInfo.DurationUntilOpen != nil {
event = fmt.Sprintf("Market opens in %s", formatDuration(*q.MarketInfo.DurationUntilOpen))
} else if q.State == "PRE" && q.MarketInfo.DurationUntilOpen != nil {
event = fmt.Sprintf("Market opens in %s", formatDuration(*q.MarketInfo.DurationUntilOpen))
} else if q.State == "REGULAR" && q.MarketInfo.DurationUntilClose != nil {
event = fmt.Sprintf("Market closes in %s", formatDuration(*q.MarketInfo.DurationUntilClose))
} else if q.State == "POST" && q.MarketInfo.DurationUntilClosePost != nil {
event = fmt.Sprintf("Post market closes in %s", formatDuration(*q.MarketInfo.DurationUntilClosePost))
}
row := []string{
q.Symbol,
formatPrice(q.Price),
fmt.Sprintf("%.2f", q.Pct),
q.State,
q.Name,
event,
}
if q.Pct < 0 {
color = tablewriter.FgRedColor
}
table.Rich(row, []tablewriter.Colors{
{},
{},
{tablewriter.Bold, color},
})
}
table.SetHeader(headers)
table.Render()
}
func printQuoteGraph(q Quote) {
if len(graphData) > 80 {
graphData = graphData[1:]
}
graphData = append(graphData, q.Price)
graph := asciigraph.Plot(graphData, asciigraph.Height(16))
fmt.Println("")
fmt.Println(graph)
}
func quoteInfo(symbols []string, watch bool) {
if watch {
ticker := time.NewTicker(2 * time.Second)
for ; true; <-ticker.C {
quotes := []Quote{}
for _, symbol := range symbols {
q, err := getQuote(symbol, true)
if err == nil {
quotes = append(quotes, q)
}
}
fmt.Print("\033[H\033[2J")
printQuotes(quotes)
if len(quotes) == 1 && quotes[0].State != "CLOSED" {
printQuoteGraph(quotes[0])
}
}
}
quotes := []Quote{}
for _, symbol := range symbols {
q, err := getQuote(symbol, true)
if err == nil {
quotes = append(quotes, q)
}
}
printQuotes(quotes)
}
func formatDuration(d time.Duration) string {
mins := math.Mod(d.Minutes(), 60)
secs := math.Mod(d.Seconds(), 60)
return fmt.Sprintf(
"%d hours %d min %d sec",
int64(d.Hours()),
int64(mins),
int64(secs),
)
}