github.com/polygon-io/client-go@v1.16.4/rest/aggs.go (about) 1 package polygon 2 3 import ( 4 "context" 5 "net/http" 6 7 "github.com/polygon-io/client-go/rest/client" 8 "github.com/polygon-io/client-go/rest/iter" 9 "github.com/polygon-io/client-go/rest/models" 10 ) 11 12 const ( 13 ListAggsPath = "/v2/aggs/ticker/{ticker}/range/{multiplier}/{timespan}/{from}/{to}" 14 GetAggsPath = "/v2/aggs/ticker/{ticker}/range/{multiplier}/{timespan}/{from}/{to}" 15 GetGroupedDailyAggsPath = "/v2/aggs/grouped/locale/{locale}/market/{marketType}/{date}" 16 GetDailyOpenCloseAggPath = "/v1/open-close/{ticker}/{date}" 17 GetPreviousCloseAggPath = "/v2/aggs/ticker/{ticker}/prev" 18 ) 19 20 // AggsClient defines a REST client for the Polygon aggs API. 21 type AggsClient struct { 22 client.Client 23 } 24 25 // ListAggs retrieves aggregate bars for a specified ticker over a given date range in custom time window sizes. 26 // For example, if timespan = 'minute' and multiplier = '5' then 5-minute bars will be returned. 27 // For more details see https://polygon.io/docs/stocks/get_v2_aggs_ticker__stocksticker__range__multiplier___timespan___from___to. 28 // 29 // This method returns an iterator that should be used to access the results via this pattern: 30 // 31 // iter := c.ListAggs(context.TODO(), params, opts...) 32 // for iter.Next() { 33 // log.Print(iter.Item()) // do something with the current value 34 // } 35 // if iter.Err() != nil { 36 // return iter.Err() 37 // } 38 func (ac *AggsClient) ListAggs(ctx context.Context, params *models.ListAggsParams, options ...models.RequestOption) *iter.Iter[models.Agg] { 39 return iter.NewIter(ctx, ListAggsPath, params, func(uri string) (iter.ListResponse, []models.Agg, error) { 40 res := &models.ListAggsResponse{} 41 err := ac.CallURL(ctx, http.MethodGet, uri, res, options...) 42 return res, res.Results, err 43 }) 44 } 45 46 // GetAggs retrieves aggregate bars for a specified ticker over a given date range in custom time window sizes. 47 // For example, if timespan = 'minute' and multiplier = '5' then 5-minute bars will be returned. 48 // For more details see https://polygon.io/docs/stocks/get_v2_aggs_ticker__stocksticker__range__multiplier___timespan___from___to. 49 // 50 // Deprecated: This method does not return an iterator and forces users to handle pagination manually. Use 51 // pkg.go.dev/github.com/polygon-io/client-go/rest#AggsClient.ListAggs instead if you want automatic pagination. 52 func (ac *AggsClient) GetAggs(ctx context.Context, params *models.GetAggsParams, opts ...models.RequestOption) (*models.GetAggsResponse, error) { 53 res := &models.GetAggsResponse{} 54 err := ac.Call(ctx, http.MethodGet, GetAggsPath, params, res, opts...) 55 return res, err 56 } 57 58 // GetGroupedDailyAggs retrieves the daily open, high, low, and close (OHLC) for the specified market type. 59 // For more details see https://polygon.io/docs/stocks/get_v2_aggs_grouped_locale_us_market_stocks__date. 60 func (ac *AggsClient) GetGroupedDailyAggs(ctx context.Context, params *models.GetGroupedDailyAggsParams, opts ...models.RequestOption) (*models.GetGroupedDailyAggsResponse, error) { 61 res := &models.GetGroupedDailyAggsResponse{} 62 err := ac.Call(ctx, http.MethodGet, GetGroupedDailyAggsPath, params, res, opts...) 63 return res, err 64 } 65 66 // GetDailyOpenCloseAgg retrieves the open, close and afterhours prices of a specific symbol on a certain date. 67 // For more details see https://polygon.io/docs/stocks/get_v1_open-close__stocksticker___date. 68 func (ac *AggsClient) GetDailyOpenCloseAgg(ctx context.Context, params *models.GetDailyOpenCloseAggParams, opts ...models.RequestOption) (*models.GetDailyOpenCloseAggResponse, error) { 69 res := &models.GetDailyOpenCloseAggResponse{} 70 err := ac.Call(ctx, http.MethodGet, GetDailyOpenCloseAggPath, params, res, opts...) 71 return res, err 72 } 73 74 // GetPreviousCloseAgg retrieves the previous day's open, high, low, and close (OHLC) for the specified ticker. 75 // For more details see https://polygon.io/docs/stocks/get_v2_aggs_ticker__stocksticker__prev. 76 func (ac *AggsClient) GetPreviousCloseAgg(ctx context.Context, params *models.GetPreviousCloseAggParams, opts ...models.RequestOption) (*models.GetPreviousCloseAggResponse, error) { 77 res := &models.GetPreviousCloseAggResponse{} 78 err := ac.Call(ctx, http.MethodGet, GetPreviousCloseAggPath, params, res, opts...) 79 return res, err 80 }