-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtypes.go
281 lines (236 loc) · 5.17 KB
/
types.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package main
import (
"math"
"net/http"
"sort"
"sync"
"time"
"github.com/alpacahq/alpaca-trade-api-go/alpaca"
"golang.org/x/oauth2"
)
const (
MINUTE Units = iota + 1
HOUR
DAY
)
const (
SMA Metrics = iota + 1
EMA
RSI
VWAP
MACD
BBANDS
)
type CheckData struct {
Ticker string
Metric interface{}
EMA EMAData
SMA SMAData
RSI RSIData
MACD MACDData
BBANDS BBANDSData
VWAP VWAPData
}
type EMAData struct {
Raw map[string]interface{} `json:"Technical Analysis: EMA"`
}
type RSIData struct {
Raw map[string]interface{} `json:"Technical Analysis: RSI"`
}
type SMAData struct {
Raw map[string]interface{} `json:"Technical Analysis: SMA"`
}
type MACDData struct {
Raw map[string]interface{} `json:"Technical Analysis: MACD"`
}
type BBANDSData struct {
Raw map[string]interface{} `json:"Technical Analysis: BBANDS"`
}
type VWAPData struct {
Raw map[string]interface{} `json:"Technical Analysis: VWAP"`
}
type BollingerMech struct {
Upper float64
Lower float64
}
type EmaMech struct {
Short float64
Medium float64
Long float64
}
type FetchConfig struct {
Unit Units
Value string
Start string
End string
}
type MainConfig struct {
mu sync.Mutex
c *alpaca.Client
m *MarketInfo
y *oauth2.Config
yc *http.Client
yd time.Time
mOut chan *MarketInfo
tOut chan Ticks
}
type MarketInfo struct {
IsOpen bool
Wait time.Duration
}
type Mechs struct {
Ticker string
StartPeriod int64
EndPeriod int64
Rsi float64
Macd float64
MacdHistogram float64
Ema EmaMech
Sma SmaMech
Bollinger BollingerMech
LastPrice float64
Length int
Config MechConfig
Success bool
}
type Metrics int
func (m Metrics) String() string {
return [...]string{"SMA", "EMA", "RSI", "VWAP", "MACD", "BBANDS"}[m]
}
type MechConfig struct {
RsiWindow int
MacdHistogramWindow int
BollingerBandWindow int
EmaWindow Windows
SmaWindow Windows
MacdWindow Windows
}
type Units int
func (u Units) String() string {
return [...]string{"minute", "hour", "day"}[u]
}
type Techs struct {
Short map[string]Mechs
Long map[string]Mechs
}
func (t *Techs) Set() {
t.Short = make(map[string]Mechs)
t.Long = make(map[string]Mechs)
}
type Ticks struct {
Stats map[string]Stat
HighVolumes map[string]Stat
HighGains map[string]Stat
MaxVolume string
MinVolume string
}
func (t *Ticks) Max() {
var max float64 = math.Inf(-1)
var str string
for key, v := range t.HighVolumes {
if v.IexPercent >= max {
max = v.IexPercent
str = key
}
}
t.MaxVolume = str
}
func (t *Ticks) Min() {
var min float64 = math.Inf(0)
var str string
for key, v := range t.HighVolumes {
if v.IexPercent <= min {
min = v.IexPercent
str = key
}
}
t.MinVolume = str
}
func (t *Ticks) Set() {
t.Stats = make(map[string]Stat)
t.HighGains = make(map[string]Stat)
t.HighVolumes = make(map[string]Stat)
}
func (t *Ticks) Sort(index float64) {
var adds int
var todo []float64
for key, v := range t.Stats {
if v.VolumePercent > index {
t.HighVolumes[key] = v
adds++
} else {
todo = append(todo, v.VolumePercent)
}
}
if len(todo) != 0 && adds < 3 {
sort.Float64s(todo)
for key, v := range t.Stats {
switch v.VolumePercent {
case todo[len(todo)-1]:
t.HighVolumes[key] = v
case todo[len(todo)-2]:
t.HighVolumes[key] = v
case todo[len(todo)-3]:
t.HighVolumes[key] = v
}
}
}
}
type rawStats []RawStat
type RawTicks struct {
Ticker string `json:"ticker"`
Results []struct {
Open float32 `json:"o"`
High float32 `json:"h"`
Low float32 `json:"l"`
Close float32 `json:"c"`
Volume float32 `json:"v"`
VolumeWeight float32 `json:"vw"`
Timestamp int `json:"t"`
} `json:"results"`
}
type RawStat struct {
Symbol string `json:"symbol"`
Company string `json:"companyName"`
Exchange string `json:"primaryExchange"`
CalculationPrice string `json:"calculationPrice"`
Open int `json:"open"`
OpenTime int `json:"openTime"`
Close int `json:"close"`
CloseTime int `json:"closeTime"`
High int `json:"high"`
HighTime int `json:"highTime"`
Low int `json:"low"`
LowTime int `json:"lowTime"`
PreviousVolume int `json:"previousVolume"`
Volume int `json:"volume"`
IexVolume int `json:"iexVolume"`
AverageVolume int `json:"avgTotalVolume"`
PE int `json:"peRatio"`
Change int `json:"change"`
Percent int `json:"changePercent"`
IexPercent float64 `json:"iexStatusPercent"`
Week52High int `json:"week52High"`
Week52Low int `json:"week52Low"`
YtdChange int `json:"ytdChange"`
}
func (s *RawStat) RelativeVolume() float64 {
return float64(s.PreviousVolume) / float64(s.AverageVolume)
}
type SmaMech struct {
Short float64
Medium float64
Long float64
}
type Stat struct {
Tick string
VolumePercent float64
AvgVolume int
PreVolume int
IexPercent float64
}
type Windows struct {
Short int
Medium int
Long int
}