-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
238 lines (204 loc) · 4.52 KB
/
main.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
package main
import (
"fmt"
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
"strings"
"time"
)
type Symbol struct {
Name string `yaml:"name"`
Symbol string `yaml:"symbol"`
}
type SymbolGroups struct {
Groups []map[string][]Symbol `yaml:"groups"`
}
func (s *SymbolGroups) symbols(yml string) (error){
/*
Read stock symbols from the yaml file provided
and populate the provided struct with them
*/
yamlFile,err := ioutil.ReadFile(yml)
if err != nil {
return err
}
err = yaml.Unmarshal(yamlFile, s)
if err != nil{
return err
}
return nil
}
func es(stock_symbol string) (bool, error){
/*
ES index prefix and field to use
*/
index_name_prefix := "/" + strings.ToLower(strings.Replace(stock_symbol, ":", "_", -1))
index_field := "/stock/"
var index_name string
shouldWatch := false
/*
Time Series
*/
ts,err := getTimeSeries(stock_symbol)
if err != nil {
time.Sleep(60 * time.Second)
return shouldWatch, err
}
index_name = index_name_prefix + "-ts" + index_field
/*
Simple Moving Av 50 Day Av
*/
sma50, err := getSimpleMovingAv(stock_symbol, 50)
if err != nil {
time.Sleep(60 * time.Second)
return shouldWatch, err
}
index_name = index_name_prefix + "-sma50" + index_field
/*
Simple Moving Av 15 Day Av
*/
sma15, err := getSimpleMovingAv(stock_symbol, 15)
if err != nil {
time.Sleep(60 * time.Second)
return shouldWatch, err
}
index_name = index_name_prefix + "-sma15" + index_field
/*
Convert the data to an SMAData object to send to ES
The raw data is too much for ES to handle with lots of
symbols.
*/
smaData,err := getSMAData(*ts,*sma50, *sma15)
for _, sma := range(smaData.Data){
dateLayout := "2006-01-02"
t, err := time.Parse(dateLayout, sma.Date)
if err != nil{
fmt.Println("Unable to parse date, skipping " + sma.Date )
continue
}
fmt.Println("Checking time: " + t.String())
fmt.Println("with :" + time.Now().Add(-72*time.Hour).String())
if t.After(time.Now().Add(-96*time.Hour)){
if sma.SMA50Day >= sma.Close && sma.Close > sma.SMA15Day{
fmt.Println("Found a watcher: " + stock_symbol)
shouldWatch = true
} else if sma.Close >= sma.SMA50Day && sma.SMA50Day > sma.SMA15Day{
fmt.Println("Found a watcher: " + stock_symbol)
shouldWatch = true
}
}
}
handle(err)
err = smaData.Send(index_name, 10)
handle(err)
return shouldWatch,nil
}
func from_arg(){
/*
The symbol to pull data for
*/
stock_symbol := os.Args[2]
stock_name := os.Args[3]
/*
Run the stock
*/
es(stock_symbol)
/*
Index
*/
err := createIndex(stock_symbol)
handle(err)
/*
Vis
*/
err = createSMAVisualisation(stock_symbol, stock_name)
handle(err)
}
func from_yaml(symbolsFile string, section string){
var s SymbolGroups
err := s.symbols(symbolsFile)
handle(err)
var watching []string
for _,group := range s.Groups{
for groupName,symbols := range group {
if groupName == section {
watching = nil
for _,ea := range symbols {
fmt.Println(groupName)
/*
Run the stock
*/
shouldWatch, err := es(ea.Symbol)
if err != nil {
continue
}
if shouldWatch {
watching = append(watching, ea.Symbol)
}
/*
Index
*/
err = createIndex(ea.Symbol)
handle(err)
/*
Vis
*/
err = createSMAVisualisation(ea.Symbol, ea.Name)
handle(err)
/*
Pause to let ES catchup
*/
time.Sleep(60 * time.Second)
}
watchDash := groupName + "_Watching"
fmt.Println(watchDash)
fmt.Println(watching)
err = createDashBoard(watchDash, &watching)
handle(err)
}
}
}
/*
Create the dashboards
*/
dashboards_from_yaml(symbolsFile)
}
func dashboards_from_yaml(symbolsFile string){
var s SymbolGroups
err := s.symbols(symbolsFile)
handle(err)
var groupSymbols []string
for _,group := range s.Groups{
for groupName,symbols := range group {
for _,ea := range symbols {
groupSymbols = append(groupSymbols, ea.Symbol )
}
err = createDashBoard(groupName, &groupSymbols)
handle(err)
}
/*
Empty the symbols array
*/
groupSymbols = nil
}
}
func main() {
switch os.Args[1] {
case "symbol":
from_arg()
case "dashonly":
dashboards_from_yaml(os.Args[2])
case "all":
from_yaml(os.Args[2], "fav")
from_yaml(os.Args[2], "ftse100")
from_yaml(os.Args[2], "ftse_aim")
from_yaml(os.Args[2], "ftseSmallCap")
from_yaml(os.Args[2], "techmark")
from_yaml(os.Args[2], "ftseallshare")
from_yaml(os.Args[2], "nasdaq")
dashboards_from_yaml(os.Args[2])
default:
from_yaml(os.Args[1], os.Args[2])
}
}