-
Notifications
You must be signed in to change notification settings - Fork 1
/
tickers.go
50 lines (38 loc) · 1.14 KB
/
tickers.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
package main
import (
"context"
polygon "github.com/polygon-io/client-go/rest"
"github.com/polygon-io/client-go/rest/models"
"log"
"os"
"github.com/joho/godotenv"
)
var limit = 5
func initPolygonClient() *polygon.Client {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
apiKey := os.Getenv("API_KEY")
return polygon.New(apiKey)
}
func SearchTicker(ticker string) []models.Ticker {
c := initPolygonClient()
// To avoid rate limits, limit results to 5 and manually fetch next page.
res := c.ListTickers(context.Background(), &models.ListTickersParams{Search: &ticker, Limit: &limit})
var tickers []models.Ticker
for i := 0; i < limit && res.Next(); i++ {
tickers = append(tickers, res.Item())
}
return tickers
}
func SearchTickerNews(ticker string) []models.TickerNews {
c := initPolygonClient()
// To avoid rate limits, limit results to 5 and manually fetch next page.
res := c.ListTickerNews(context.Background(), &models.ListTickerNewsParams{TickerEQ: &ticker, Limit: &limit})
var news []models.TickerNews
for i := 0; i < limit && res.Next(); i++ {
news = append(news, res.Item())
}
return news
}