github.com/polygon-io/client-go@v1.16.4/rest/trades.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 ListTradesPath = "/v3/trades/{ticker}" 14 GetLastTradePath = "/v2/last/trade/{ticker}" 15 GetLastCryptoTradePath = "/v1/last/crypto/{from}/{to}" 16 ) 17 18 // TradesClient defines a REST client for the Polygon trades API. 19 type TradesClient struct { 20 client.Client 21 } 22 23 // ListTrades retrieves trades for a specified ticker. For more details see 24 // https://polygon.io/docs/stocks/get_v3_trades__stockticker. 25 // 26 // This method returns an iterator that should be used to access the results via this pattern: 27 // 28 // iter := c.ListTrades(context.TODO(), params, opts...) 29 // for iter.Next() { 30 // log.Print(iter.Item()) // do something with the current value 31 // } 32 // if iter.Err() != nil { 33 // return iter.Err() 34 // } 35 func (c *TradesClient) ListTrades(ctx context.Context, params *models.ListTradesParams, options ...models.RequestOption) *iter.Iter[models.Trade] { 36 return iter.NewIter(ctx, ListTradesPath, params, func(uri string) (iter.ListResponse, []models.Trade, error) { 37 res := &models.ListTradesResponse{} 38 err := c.CallURL(ctx, http.MethodGet, uri, res, options...) 39 return res, res.Results, err 40 }) 41 } 42 43 // GetLastTrade retrieves the last trade for a specified ticker. For more details see 44 // https://polygon.io/docs/stocks/get_v2_last_trade__stocksticker. 45 func (c *TradesClient) GetLastTrade(ctx context.Context, params *models.GetLastTradeParams, options ...models.RequestOption) (*models.GetLastTradeResponse, error) { 46 res := &models.GetLastTradeResponse{} 47 err := c.Call(ctx, http.MethodGet, GetLastTradePath, params, res, options...) 48 return res, err 49 } 50 51 // GetLastCryptoTrade retrieves the last trade for a crypto pair. For more details see 52 // https://polygon.io/docs/crypto/get_v1_last_crypto__from___to. 53 func (c *TradesClient) GetLastCryptoTrade(ctx context.Context, params *models.GetLastCryptoTradeParams, options ...models.RequestOption) (*models.GetLastCryptoTradeResponse, error) { 54 res := &models.GetLastCryptoTradeResponse{} 55 err := c.Call(ctx, http.MethodGet, GetLastCryptoTradePath, params, res, options...) 56 return res, err 57 }