github.com/polygon-io/client-go@v1.16.4/rest/snapshot.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  	GetAllTickersSnapshotPath     = "/v2/snapshot/locale/{locale}/markets/{marketType}/tickers"
    14  	GetTickerSnapshotPath         = "/v2/snapshot/locale/{locale}/markets/{marketType}/tickers/{ticker}"
    15  	GetGainersLosersSnapshotPath  = "/v2/snapshot/locale/{locale}/markets/{marketType}/{direction}"
    16  	GetOptionContractSnapshotPath = "/v3/snapshot/options/{underlyingAsset}/{optionContract}"
    17  	ListOptionsChainSnapshotPath  = "/v3/snapshot/options/{underlyingAsset}"
    18  	GetCryptoFullBookSnapshotPath = "/v2/snapshot/locale/global/markets/crypto/tickers/{ticker}/book"
    19  	GetIndicesSnapshotPath        = "/v3/snapshot/indices"
    20  	ListUniversalSnapshotsPath    = "/v3/snapshot"
    21  
    22  	// Deprecated: Please use UniversalSnapshot types instead of AssetSnapshot types.
    23  	ListAssetSnapshots = ListUniversalSnapshotsPath
    24  )
    25  
    26  // SnapshotClient defines a REST client for the Polygon snapshot API.
    27  type SnapshotClient struct {
    28  	client.Client
    29  }
    30  
    31  // ListOptionsChainSnapshot retrieves the snapshot of all options contracts for an underlying ticker. For more details see
    32  // https://polygon.io/docs/options/get_v3_snapshot_options__underlyingasset.
    33  //
    34  // This method returns an iterator that should be used to access the results via this pattern:
    35  //
    36  //	iter := c.ListOptionsChainSnapshot(context.TODO(), params, opts...)
    37  //	for iter.Next() {
    38  //		log.Print(iter.Item()) // do something with the current value
    39  //	}
    40  //	if iter.Err() != nil {
    41  //		return iter.Err()
    42  //	}
    43  func (ac *SnapshotClient) ListOptionsChainSnapshot(ctx context.Context, params *models.ListOptionsChainParams, options ...models.RequestOption) *iter.Iter[models.OptionContractSnapshot] {
    44  	return iter.NewIter(ctx, ListOptionsChainSnapshotPath, params, func(uri string) (iter.ListResponse, []models.OptionContractSnapshot, error) {
    45  		res := &models.ListOptionsChainSnapshotResponse{}
    46  		err := ac.CallURL(ctx, http.MethodGet, uri, res, options...)
    47  		return res, res.Results, err
    48  	})
    49  }
    50  
    51  // GetAllTickersSnapshot gets the current minute, day, and previous day's aggregate, as well as the last trade and quote
    52  // for all symbols of a specified market type.
    53  //
    54  // Note: Snapshot data is cleared at 12am EST and gets populated as data is received from the exchanges. This can happen
    55  // as early as 4am EST.
    56  //
    57  // For more details see https://polygon.io/docs/stocks/get_v2_snapshot_locale_us_markets_stocks_tickers.
    58  func (ac *SnapshotClient) GetAllTickersSnapshot(ctx context.Context, params *models.GetAllTickersSnapshotParams, opts ...models.RequestOption) (*models.GetAllTickersSnapshotResponse, error) {
    59  	res := &models.GetAllTickersSnapshotResponse{}
    60  	err := ac.Call(ctx, http.MethodGet, GetAllTickersSnapshotPath, params, res, opts...)
    61  	return res, err
    62  }
    63  
    64  // GetTickerSnapshot gets the current minute, day, and previous day's aggregate, as well as the last trade and quote for
    65  // a single traded symbol of a specified market type.
    66  //
    67  // Note: Snapshot data is cleared at 12am EST and gets populated as data is received from the exchanges. This can happen
    68  // as early as 4am EST.
    69  //
    70  // For more details see https://polygon.io/docs/stocks/get_v2_snapshot_locale_us_markets_stocks_tickers__stocksticker.
    71  func (ac *SnapshotClient) GetTickerSnapshot(ctx context.Context, params *models.GetTickerSnapshotParams, opts ...models.RequestOption) (*models.GetTickerSnapshotResponse, error) {
    72  	res := &models.GetTickerSnapshotResponse{}
    73  	err := ac.Call(ctx, http.MethodGet, GetTickerSnapshotPath, params, res, opts...)
    74  	return res, err
    75  }
    76  
    77  // GetGainersLosersSnapshot gets the current top 20 gainers or losers of the day in a specific market type.
    78  //
    79  // Top gainers are those tickers whose price has increased by the highest percentage since the previous day's close. Top
    80  // losers are those tickers whose price has decreased by the highest percentage since the previous day's close.
    81  //
    82  // Note: Snapshot data is cleared at 12am EST and gets populated as data is received from the exchanges.
    83  //
    84  // For more details see https://polygon.io/docs/stocks/get_v2_snapshot_locale_us_markets_stocks__direction.
    85  func (ac *SnapshotClient) GetGainersLosersSnapshot(ctx context.Context, params *models.GetGainersLosersSnapshotParams, opts ...models.RequestOption) (*models.GetGainersLosersSnapshotResponse, error) {
    86  	res := &models.GetGainersLosersSnapshotResponse{}
    87  	err := ac.Call(ctx, http.MethodGet, GetGainersLosersSnapshotPath, params, res, opts...)
    88  	return res, err
    89  }
    90  
    91  // GetOptionContractSnapshot gets the snapshot of an option contract for a stock equity. For more details see
    92  // https://polygon.io/docs/options/get_v3_snapshot_options__underlyingasset___optioncontract.
    93  func (ac *SnapshotClient) GetOptionContractSnapshot(ctx context.Context, params *models.GetOptionContractSnapshotParams, opts ...models.RequestOption) (*models.GetOptionContractSnapshotResponse, error) {
    94  	res := &models.GetOptionContractSnapshotResponse{}
    95  	err := ac.Call(ctx, http.MethodGet, GetOptionContractSnapshotPath, params, res, opts...)
    96  	return res, err
    97  }
    98  
    99  // GetCryptoFullBookSnapshot gets the current level 2 book of a single cryptocurrency ticker. This is the combined book
   100  // from all of the exchanges.
   101  //
   102  // Note: Snapshot data is cleared at 12am EST and gets populated as data is received from the exchanges.
   103  //
   104  // For more details see
   105  // https://polygon.io/docs/crypto/get_v2_snapshot_locale_global_markets_crypto_tickers__ticker__book.
   106  func (ac *SnapshotClient) GetCryptoFullBookSnapshot(ctx context.Context, params *models.GetCryptoFullBookSnapshotParams, opts ...models.RequestOption) (*models.GetCryptoFullBookSnapshotResponse, error) {
   107  	res := &models.GetCryptoFullBookSnapshotResponse{}
   108  	err := ac.Call(ctx, http.MethodGet, GetCryptoFullBookSnapshotPath, params, res, opts...)
   109  	return res, err
   110  }
   111  
   112  func (ac *SnapshotClient) GetIndicesSnapshot(ctx context.Context, params *models.GetIndicesSnapshotParams, opts ...models.RequestOption) (*models.GetIndicesSnapshotResponse, error) {
   113  	res := &models.GetIndicesSnapshotResponse{}
   114  	err := ac.Call(ctx, http.MethodGet, GetIndicesSnapshotPath, params, res, opts...)
   115  	return res, err
   116  }
   117  
   118  // Deprecated: Please use UniversalSnapshot types instead of AssetSnapshot types.
   119  func (ac *SnapshotClient) ListAssetSnapshots(ctx context.Context, params *models.ListAssetSnapshotsParams, options ...models.RequestOption) *iter.Iter[models.SnapshotResponseModel] {
   120  	return ac.ListUniversalSnapshots(ctx, (*models.ListUniversalSnapshotsParams)(params), options...)
   121  }
   122  
   123  // ListUniversalSnapshots retrieves the snapshots for the specified tickers for the specified time. For more details see:
   124  // - https://staging.polygon.io/docs/stocks/get_v3_snapshot
   125  // - https://staging.polygon.io/docs/options/get_v3_snapshot
   126  //
   127  // This method returns an iterator that should be used to access the results via this pattern:
   128  //
   129  //	iter := c.ListUniversalSnapshots(context, params, opts...)
   130  //	for iter.Next() {
   131  //		log.Print(iter.Item()) // do something with the current value
   132  //	}
   133  //	if iter.Err() != nil {
   134  //		return iter.Err()
   135  //	}
   136  func (ac *SnapshotClient) ListUniversalSnapshots(ctx context.Context, params *models.ListUniversalSnapshotsParams, options ...models.RequestOption) *iter.Iter[models.SnapshotResponseModel] {
   137  	return iter.NewIter(ctx, ListUniversalSnapshotsPath, params, func(uri string) (iter.ListResponse, []models.SnapshotResponseModel, error) {
   138  		res := &models.ListUniversalSnapshotsResponse{}
   139  		err := ac.CallURL(ctx, http.MethodGet, uri, res, options...)
   140  		return res, res.Results, err
   141  	})
   142  }