-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathround.go
69 lines (61 loc) · 1.72 KB
/
round.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
// Command gdax gets a single quote from http requests to https://api.gdax.com or streams quotes from websocket at wss://ws-feed.gdax.com
package main
import (
"fmt"
"strconv"
"strings"
"github.com/fatih/color"
)
// rndPrice rounds prices to 2 or 5 decimal places
func rndPrice(price string) string {
num, err := strconv.ParseFloat(price, 64)
if err != nil {
return "-----"
}
if num >= 10 {
num = float64(int64(num*100+0.5)) / 100
return fmt.Sprintf("%.2f", num)
}
num = float64(int64(num*100000+0.5)) / 100000
return fmt.Sprintf("%.5f", num)
}
// rndSize rounds last size data to 8 decimal places
func rndSize(size string) string {
num, err := strconv.ParseFloat(size, 64)
if err != nil {
return "-----"
}
num = float64(int64(num*100000000+0.5)) / 100000000
return fmt.Sprintf("%.8f", num)
}
// rndVol rounds volume data to the nearest whole number
func rndVol(vol string) string {
num, err := strconv.ParseFloat(vol, 64)
if err != nil {
return "-----"
}
return fmt.Sprint(int64(num + 0.5))
}
// setDelta returns price delta rounded to two decimal places as a string
// returns the print color based on the delta
func setDelta(price string, open string) string {
p, _ := strconv.ParseFloat(price, 64)
o, _ := strconv.ParseFloat(open, 64)
delta := ((p - o) / o) * 100
b := strings.Builder{}
b.WriteString(strconv.FormatFloat(delta, 'f', 2, 64))
b.WriteString("%")
return b.String()
}
// SetColor uses the delta filed of Product type to set color either red or green
func SetColor(delta string) *color.Color {
c := &color.Color{}
slice := strings.Split(delta, "%")
d, _ := strconv.ParseFloat(slice[0], 64)
if d > 0 {
c.Add(color.FgGreen, color.Bold)
} else {
c.Add(color.FgRed, color.Bold)
}
return c
}