Closed kaykhan closed 1 month 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?
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
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
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.
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.