polygon-io / client-go

The official Go client library for the Polygon REST and WebSocket API.
MIT License
127 stars 36 forks source link

Receiving incorrect data using ListAggs #428

Closed kaykhan closed 1 month ago

kaykhan commented 2 months ago

Im on the free plan, running the below code gives me today data, rather than the first 5 days of the year.

Can you help me understand what is happening here?

Using the rest api works correctly.

curl https://api.polygon.io/v2/aggs/ticker/SPY/range/1/day/2024-01-01/2024-01-05\?adjusted\=true\&sort\=asc\&apiKey\=<REDACTED> | jq
package main

import (
    "context"
    polygon "github.com/polygon-io/client-go/rest"
    "github.com/polygon-io/client-go/rest/models"
    "log"
    "time"
)

func main() {
    // init client
    c := polygon.New("REDACTED")

    iter := c.ListAggs(context.Background(), models.ListAggsParams{
        Ticker:     "SPY",
        Multiplier: 1,
        Timespan:   "day",
        From:       models.Millis(time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC)),
        To:         models.Millis(time.Date(2024, 1, 5, 0, 0, 0, 0, time.UTC)),
    }.WithOrder(models.Asc).WithLimit(1000).WithAdjusted(true))

    for iter.Next() {
        log.Print(iter.Item()) // do something with the current value
    }

    // if the loop breaks, it has either reached the end of the list or an error has occurred
    // you can check if something went wrong with iter.Err()
    if iter.Err() != nil {
        log.Fatal(iter.Err())
    }

}
kaykhan commented 2 months ago

fmt.Println(models.Millis(time.Date(2024, 1, 5, 0, 0, 0, 0, time.UTC)))

{0 63840009600 }

fmt.Println(time.Date(2024, 1, 5, 0, 0, 0, 0, time.UTC).Unix()*1000)

1704412800000

Am i misunderstanding something or is there an issue with models.Milliis?

justinpolygon commented 2 months ago

Hey @kaykhan, I'm checking this out an not able to reproduce. Here's what I see:

// Stocks - Aggregates (Bars)
// https://polygon.io/docs/stocks/get_v2_aggs_ticker__stocksticker__range__multiplier___timespan___from___to
// https://github.com/polygon-io/client-go/blob/master/rest/aggs.go
package main

import (
    "context"
    "log"
    "os"
    "time"

    polygon "github.com/polygon-io/client-go/rest"
    "github.com/polygon-io/client-go/rest/models"
)

func main() {

    // init client
    c := polygon.New(os.Getenv("POLYGON_API_KEY"))

    // set params
    params := models.ListAggsParams{
        Ticker:     "SPY",
        Multiplier: 1,
        Timespan:   "day",
        From:       models.Millis(time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC)),
        To:         models.Millis(time.Date(2024, 1, 5, 0, 0, 0, 0, time.UTC)),
    }.WithOrder(models.Desc).WithLimit(50000).WithAdjusted(true)

    // make request
    iter := c.ListAggs(context.Background(), params)

    // do something with the result
    for iter.Next() {

        // Convert Nanos to time.Time
        timestamp := time.Time(iter.Item().Timestamp)

        // Print the datetime
        log.Printf("Datetime: %v, unixtime (millisecond) %d", timestamp, timestamp.UnixMilli())

    }
    if iter.Err() != nil {
        log.Fatal(iter.Err())
    }

}

And here's the output:

go run main.go
2024/06/18 14:24:51 Datetime: 2024-01-03 21:00:00 -0800 PST, unixtime (millisecond) 1704344400000
2024/06/18 14:24:51 Datetime: 2024-01-02 21:00:00 -0800 PST, unixtime (millisecond) 1704258000000
2024/06/18 14:24:51 Datetime: 2024-01-01 21:00:00 -0800 PST, unixtime (millisecond) 1704171600000
justinpolygon commented 2 months ago

You can also turn on debugging to see what url is being called behind the scenes. Just add models.WithTrace(true) to the request. This is really useful if you wanted to play around with the url string in your browser.

From this:

iter := c.ListAggs(context.Background(), params)

To this:

iter := c.ListAggs(context.Background(), params, models.WithTrace(true))

Then you can see the url and all the headers:

Request URL: /v2/aggs/ticker/SPY/range/1/day/1704067200000/1704412800000?adjusted=true&limit=50000&sort=desc
Request Headers: map[Accept-Encoding:[gzip] Authorization:[REDACTED] User-Agent:[Polygon.io GoClient/v1.16.0]]
Response Headers: map[Content-Encoding:[gzip] Content-Length:[293] Content-Type:[application/json] Date:[Tue, 18 Jun 2024 21:27:15 GMT] Server:[nginx/1.19.2] Strict-Transport-Security:[max-age=15724800; includeSubDomains] Vary:[Accept-Encoding] X-Request-Id:[a318714c88177265c81f4557ccd48f39]]
2024/06/18 14:27:15 Datetime: 2024-01-03 21:00:00 -0800 PST, unixtime (millisecond) 1704344400000
2024/06/18 14:27:15 Datetime: 2024-01-02 21:00:00 -0800 PST, unixtime (millisecond) 1704258000000
2024/06/18 14:27:15 Datetime: 2024-01-01 21:00:00 -0800 PST, unixtime (millisecond) 1704171600000
justinpolygon commented 1 month ago

Hey @kaykhan this has been open for about a month and I wasn't able to reproduce what you're seeing. There is code above that should work. So, I'm going to close this but feel free to re-open if you have more information.