-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_20190104.go
93 lines (76 loc) · 1.51 KB
/
main_20190104.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
package main
import (
"fmt"
"github.com/piquette/finance-go/equity"
"sync"
"time"
)
func nowx() int64 {
return time.Now().Unix()
}
type Stock struct {
symbol string
price float64
time int
}
type Stocks []Stock
type PriceDelta []float64
func chkErr(e error) {
if e != nil {
panic(e)
}
}
func reqStkPrice(chStock chan<- Stock, wg *sync.WaitGroup, sym string) {
defer wg.Done()
q, err := equity.Get(sym)
chkErr(err)
s := Stock{symbol: q.Quote.Symbol,
price: q.Quote.RegularMarketPrice,
time: q.Quote.RegularMarketTime}
chStock <- s
}
func getSnapShot(sym []string) Stocks {
// This channel is buffer since we do not want any blocking
chStk := make(chan Stock, len(sym))
var ss Stocks
var wg sync.WaitGroup
wg.Add(len(sym))
for _, x := range sym {
go reqStkPrice(chStk, &wg, x)
}
wg.Wait()
close(chStk)
for i := 1; i <= len(sym); i++ {
ss = append(ss, <-chStk)
}
return ss
}
func Find(stk string, stks Stocks) (Stock, error) {
for _, s := range stks {
if stk == s.symbol {
return s, nil
}
}
return Stock{}, nil
}
func GetDelta(sl []string) PriceDelta {
var pdiffs PriceDelta
s1 := getSnapShot(sl)
time.Sleep(15 * time.Second)
s2 := getSnapShot(sl)
for _, stk := range sl {
x1, _ := Find(stk, s1)
x2, _ := Find(stk, s2)
pdiff := x2.price - x1.price
pdiffs = append(pdiffs, pdiff)
}
return pdiffs
}
var spider = []string{"XLE", "XLB", "XLF"}
func main() {
s := getSnapShot(spider)
fmt.Println(s)
fmt.Println(Find("XLE", s))
pd := GetDelta(spider)
fmt.Println(pd)
}