polygon-io / client-go

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

SipTimestamp in trade is not encoded when calling json.Marshal #362

Closed ghost closed 9 months ago

ghost commented 10 months ago

I used the method in document to fetch a trade, and then call

jsonEncodedTrade, _ := json.Marshal(tradeFetchedFromPolygon) fmt.Println(string(jsonEncodedTrade))

The output is: {"conditions":[53,35,41],"correction":10,"exchange":4,"id":"282666","participant_timestamp":{},"price":186.4,"sequence_number":5561278,"sip_timestamp":{},"size":4000,"tape":3,"trf_id":12,"trf_timestamp":{}}

Why sip_timestamp is empty? It seems SipTimestamp is not json encoded.

justinpolygon commented 10 months ago

Hello, I answered on the public slack thread as well. The timestamps are unmashaled but they go into a custom Nanos which is time.Time. Here's some example code:

// Stocks - Last Trade
// https://polygon.io/docs/stocks/get_v2_last_trade__stocksticker
// https://github.com/polygon-io/client-go/blob/master/rest/trades.go
package main

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

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

func main() {

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

    // set params
    params := &models.GetLastTradeParams{
        Ticker: "AAPL",
    }

    // make request
    res, err := c.GetLastTrade(context.Background(), params)
    if err != nil {
        log.Fatal(err)
    }

    // do something with the result
    log.Print(res)

    // dump the struct
    spew.Dump(res)

    // Convert Nanos to time.Time
    participantTimestamp := time.Time(res.Results.ParticipantTimestamp)

    // Print the Unix nanosecond timestamp (int64)
    log.Printf("Unix nanosecond timestamp: %d", participantTimestamp.UnixNano())

    // Print the datetime
    log.Printf("Datetime: %v", participantTimestamp)

}

You'll see something like:

$ go run main.go
2023/11/14 14:54:08 &{{{} OK f5ec6369b69cae32bdeb3abd2f06def7 0  } {AAPL {0 0 <nil>} 7318459 {125214138 63835599245 0x10293c760} {124870437 63835599245 0x10293c760} [12 37] 0 48620 187.23 0 1 11 3}}
(*models.GetLastTradeResponse)(0x14000400360)({
 BaseResponse: (models.BaseResponse) {
  PaginationHooks: (models.PaginationHooks) {
   NextURL: (string) ""
  },
  Status: (string) (len=2) "OK",
  RequestID: (string) (len=32) "f5ec6369b69cae32bdeb3abd2f06def7",
  Count: (int) 0,
  Message: (string) "",
  ErrorMessage: (string) ""
 },
 Results: (models.LastTrade) {
  Ticker: (string) (len=4) "AAPL",
  TRFTimestamp: (models.Nanos) {
   wall: (uint64) 0,
   ext: (int64) 0,
   loc: (*time.Location)(<nil>)
  },
  SequenceNumber: (int64) 7318459,
  Timestamp: (models.Nanos) {
   wall: (uint64) 125214138,
   ext: (int64) 63835599245,
   loc: (*time.Location)(0x10293c760)(Local)
  },
  ParticipantTimestamp: (models.Nanos) {
   wall: (uint64) 124870437,
   ext: (int64) 63835599245,
   loc: (*time.Location)(0x10293c760)(Local)
  },
  Conditions: ([]int32) (len=2 cap=2) {
   (int32) 12,
   (int32) 37
  },
  Correction: (uint32) 0,
  ID: (string) (len=5) "48620",
  Price: (float64) 187.23,
  TRF: (int32) 0,
  Size: (float64) 1,
  Exchange: (int32) 11,
  Tape: (int32) 3
 }
})
2023/11/14 14:54:08 Unix nanosecond timestamp: 1700002445124870437
2023/11/14 14:54:08 Datetime: 2023-11-14 14:54:05.124870437 -0800 PST

Hope this explains how it works.

justinpolygon commented 9 months ago

Closing this out as not a bug. Hopefully the example code above explains what's going on and how to access things. Let me know if you need anything else.

justinpolygon commented 9 months ago

I understand what you're talking about now. I have filed a bug to correct this.

justinpolygon commented 9 months ago

FYI - have a PR that'll fix this via https://github.com/polygon-io/client-go/pull/368. I'll keep you posted on when this gets released.

justinpolygon commented 9 months ago

Fixed and the latest release has the update. Thanks again for reporting this.