forked from ivanbojer/ticker-term
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstrument.go
166 lines (145 loc) · 3.5 KB
/
instrument.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package main
import (
"encoding/json"
"errors"
"fmt"
"strconv"
"strings"
"github.com/fatih/color"
)
type Instrument struct {
Row struct {
Last string `json:"last"`
Ma string `json:"ma"`
MaClass string `json:"ma_class"`
Clock string `json:"clock"`
} `json:"row"`
SummaryLast string `json:"summaryLast"`
SummaryName string `json:"summaryName"`
SummaryNameAlt string `json:"summaryNameAlt"`
SummaryChange string `json:"summaryChange"`
SummaryChangeClass string `json:"summaryChangeClass"`
TechnicalSummary string `json:"technicalSummary"`
TechnicalSummaryClass string `json:"technicalSummaryClass"`
MaBuy int `json:"maBuy"`
MaSell int `json:"maSell"`
TiBuy int `json:"tiBuy"`
TiSell int `json:"tiSell"`
}
type InstrumentMap map[string]*Instrument
func newInstrumentMap(in []byte) (imap InstrumentMap, err error) {
imap = make(InstrumentMap)
err = json.Unmarshal(in, &imap)
if err != nil {
return nil, err
}
return
}
func (im InstrumentMap) Color(sym string) color.Attribute {
if id, ok := Symbols[sym]; ok {
sym = id
}
if s, ok := im[sym]; ok {
if s.SummaryChangeClass == "redFont" {
return color.FgRed
}
return color.FgGreen
}
return color.Concealed
}
func (im InstrumentMap) Last(sym string) (str string, val float64, err error) {
if id, ok := Symbols[sym]; ok {
sym = id
}
if s, ok := im[sym]; ok {
str = strings.ReplaceAll(s.Row.Last, ",", "")
if len(str) == 0 {
return "", 0, errors.New("InstrumentMap.Last @ get Symbol.Row.Last -- empty string")
}
val, err = strconv.ParseFloat(str, 32)
if err != nil {
return
}
}
return
}
func (im InstrumentMap) Title(sym string) (val string) {
id := sym
var useSym bool
if lbl, ok := Display[sym]; ok {
return lbl
}
if pairID, ok := Symbols[sym]; ok {
id = pairID
useSym = true
}
if s, ok := im[id]; ok {
if useSym {
val = sym
return
}
val = s.SummaryName
}
return
}
func (im InstrumentMap) Change(sym string) (change, changePct string) {
if id, ok := Symbols[sym]; ok {
sym = id
}
split := strings.Split(im[sym].SummaryChange, " ")
return split[0], split[1]
}
func (im InstrumentMap) Technical(sym string, titleAttr color.Attribute) (val string) {
if id, ok := Symbols[sym]; ok {
sym = id
}
if s, ok := im[sym]; ok {
ma := tline(s.Row.Ma, s.MaBuy, s.MaSell, titleAttr, 10)
ti := tline(s.TechnicalSummary, s.TiBuy, s.TiSell, titleAttr, 4)
return fmt.Sprintf("%s%s", ma, ti)
}
return
}
/////////////////////////////////////////
func tline(class string, nBuy, nSell int, titleAttr color.Attribute, pad int) string {
offset := 6
var (
arrow string
clr color.Attribute
)
var lbl string
switch class {
case "Buy":
arrow = up
clr = color.FgGreen
case "Strong Buy":
arrow = up + up
clr = color.FgGreen
if nSell == 0 {
arrow = arrow + up
}
case "Sell":
arrow = down
clr = color.FgRed
case "Strong Sell":
arrow = down + down
clr = color.FgRed
if nBuy == 0 {
arrow = arrow + down
}
case "Neutral":
arrow = left + right
clr = color.FgWhite
}
lbl = toColor(fmt.Sprintf("%s%s", arrow, padding(arrow, offset)), clr, titleAttr)
buyStr, sellStr := strconv.Itoa(nBuy), strconv.Itoa(nSell)
sep := toColor("/", color.Concealed, titleAttr)
bs := toColor(buyStr, color.FgGreen, titleAttr)
ss := toColor(sellStr, color.FgRed, titleAttr)
cntStr := bs + sep + ss
return fmt.Sprintf("%s %s%s",
lbl,
cntStr,
padding(buyStr+sellStr, pad),
)
}