Skip to content

Instantly share code, notes, and snippets.

@kyp0717
Created December 23, 2019 16:00
Show Gist options
  • Select an option

  • Save kyp0717/d0fb2424592cb2d4438aaed6c70abb44 to your computer and use it in GitHub Desktop.

Select an option

Save kyp0717/d0fb2424592cb2d4438aaed6c70abb44 to your computer and use it in GitHub Desktop.
Get stock price using go version 2
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
}
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) []Stock {
// This channel is buffer since we do not want any blocking
chStk := make(chan Stock, len(sym))
var s []Stock
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++ {
s = append(s, <-chStk)
}
return s
}
// Find returns the smallest index i at which x == a[i],
// or len(a) if there is no such index.
func Find(a []string, x string) int {
for i, n := range a {
if x == n {
return i
}
}
return len(a)
}
//func GetDelta(lst []string) []int {
// d1 := getSnapShot(lst)
// time.Sleep(10 * time.Second)
// d2 := getSnapShot(lst)
// // find is faster than sort
// for x := range lst {
//
// }
//
//}
//
var spider = []string{"XLE", "XLB", "XLF"}
func main() {
s := getSnapShot(spider)
fmt.Println(s)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment