github.com/polygon-io/client-go@v1.16.4/rest/models/types.go (about) 1 package models 2 3 import ( 4 "encoding/json" 5 "strconv" 6 "time" 7 ) 8 9 // MarketType is the type of market. 10 type MarketType string 11 12 const ( 13 Stocks MarketType = "stocks" 14 Forex MarketType = "forex" 15 Crypto MarketType = "crypto" 16 ) 17 18 // ContractType is the type of contract. 19 type ContractType string 20 21 const ( 22 ContractCall ContractType = "call" 23 ContractPut ContractType = "put" 24 ContractOther ContractType = "other" 25 ) 26 27 // Locale is the market location. 28 type MarketLocale string 29 30 const ( 31 US MarketLocale = "us" 32 Global MarketLocale = "global" 33 ) 34 35 // Timespan is the size of the time window. 36 type Timespan string 37 38 const ( 39 Second Timespan = "second" 40 Minute Timespan = "minute" 41 Hour Timespan = "hour" 42 Day Timespan = "day" 43 Week Timespan = "week" 44 Month Timespan = "month" 45 Quarter Timespan = "quarter" 46 Year Timespan = "year" 47 ) 48 49 // Sort is a query param type that specifies how the results should be sorted. 50 type Sort string 51 52 const ( 53 TickerSymbol Sort = "ticker" 54 Name Sort = "name" 55 Market Sort = "market" 56 Locale Sort = "locale" 57 PrimaryExchange Sort = "primary_exchange" 58 Type Sort = "type" 59 CurrencySymbol Sort = "currency_symbol" 60 CurrencyName Sort = "currency_name" 61 BaseCurrencySymbol Sort = "base_currency_symbol" 62 BaseCurrencyName Sort = "base_currency_name" 63 CIK Sort = "cik" 64 CompositeFIGI Sort = "composite_figi" 65 ShareClassFIGI Sort = "share_class_figi" 66 PublishedUTC Sort = "published_utc" 67 LastUpdatedUTC Sort = "last_updated_utc" 68 DelistedUTC Sort = "delisted_utc" 69 Timestamp Sort = "timestamp" 70 StrikePrice Sort = "strike_price" 71 ExpirationDate Sort = "expiration_date" 72 FilingDate Sort = "filing_date" 73 PeriodOfReportDate Sort = "period_of_report_date" 74 ) 75 76 // Order the results. asc will return results in ascending order (oldest at the top), desc will return results in 77 // descending order (newest at the top). 78 type Order string 79 80 const ( 81 Asc Order = "asc" 82 Desc Order = "desc" 83 ) 84 85 type SeriesType string 86 87 const ( 88 High SeriesType = "high" 89 Open SeriesType = "open" 90 Low SeriesType = "low" 91 Close SeriesType = "close" 92 ) 93 94 // Direction is the direction of the snapshot results to return. 95 type Direction string 96 97 const ( 98 Gainers Direction = "gainers" 99 Losers Direction = "losers" 100 ) 101 102 // AssetClass is an identifier for a group of similar financial instruments. 103 type AssetClass string 104 105 const ( 106 AssetStocks AssetClass = "stocks" 107 AssetOptions AssetClass = "options" 108 AssetCrypto AssetClass = "crypto" 109 AssetFx AssetClass = "fx" 110 AssetOTC AssetClass = "otc" 111 AssetIndices AssetClass = "indices" 112 ) 113 114 // DataType is the type of data. 115 type DataType string 116 117 const ( 118 DataTrade DataType = "trade" 119 DataBBO DataType = "bbo" 120 DataNBBO DataType = "nbbo" 121 ) 122 123 // SIP is the type of Securies Information Processor. 124 type SIP string 125 126 const ( 127 CTA SIP = "CTA" 128 UTP SIP = "UTP" 129 OPRA SIP = "OPRA" 130 ) 131 132 // Frequency is the number of times a dividend is paid out over the course of one year. 133 type Frequency int64 134 135 const ( 136 OneTime Frequency = 0 137 Annually Frequency = 1 138 BiAnnually Frequency = 2 139 Quarterly Frequency = 4 140 Monthly Frequency = 12 141 ) 142 143 // DividendType is the type of dividend. 144 type DividendType string 145 146 const ( 147 DividendCD DividendType = "CD" 148 DividendLT DividendType = "LT" 149 DividendSC DividendType = "SC" 150 DividendST DividendType = "ST" 151 ) 152 153 // Comparator is the type of comparison to make for a specific query parameter. 154 type Comparator string 155 156 const ( 157 EQ Comparator = "eq" 158 LT Comparator = "lt" 159 LTE Comparator = "lte" 160 GT Comparator = "gt" 161 GTE Comparator = "gte" 162 ) 163 164 // NameComparator is the type of comparison to make for the company_name query parameter in Stock Financials. 165 type NameComparator string 166 167 const ( 168 Full NameComparator = "full" 169 Search NameComparator = "search" 170 ) 171 172 // TimeFrame is the type of time frame query parameter for stock financials. 173 type Timeframe string 174 175 const ( 176 TFAnnual Timeframe = "annual" 177 TFQuarterly Timeframe = "quarterly" 178 ) 179 180 // Time represents a long date string of the following format: "2006-01-02T15:04:05.000Z". 181 type Time time.Time 182 183 func (t *Time) UnmarshalJSON(data []byte) error { 184 unquoteData, err := strconv.Unquote(string(data)) 185 if err != nil { 186 return err 187 } 188 189 // attempt to parse time 190 if parsedTime, err := time.Parse("2006-01-02T15:04:05.000-0700", unquoteData); err == nil { 191 *t = Time(parsedTime) 192 return nil 193 } 194 195 // attempt to parse time again 196 if parsedTime, err := time.Parse("2006-01-02T15:04:05-07:00", unquoteData); err == nil { 197 *t = Time(parsedTime) 198 return nil 199 } 200 201 // attempt with a different format 202 if parsedTime, err := time.Parse("2006-01-02T15:04:05.000Z", unquoteData); err == nil { 203 *t = Time(parsedTime) 204 return nil 205 } 206 207 // attempt with yet another format 208 if parsedTime, err := time.Parse("2006-01-02T15:04:05Z", unquoteData); err != nil { 209 return err 210 } else { 211 *t = Time(parsedTime) 212 } 213 214 return nil 215 } 216 217 func (t *Time) MarshalJSON() ([]byte, error) { 218 return json.Marshal(time.Time(*t).Format("2006-01-02T15:04:05.000Z")) 219 } 220 221 // Date represents a short date string of the following format: "2006-01-02". 222 type Date time.Time 223 224 func (d *Date) UnmarshalJSON(data []byte) error { 225 unquoteData, err := strconv.Unquote(string(data)) 226 if err != nil { 227 return err 228 } 229 t, err := time.Parse("2006-01-02", unquoteData) 230 if err != nil { 231 return err 232 } 233 *d = Date(t) 234 return nil 235 } 236 237 func (d *Date) MarshalJSON() ([]byte, error) { 238 return json.Marshal(time.Time(*d).Format("2006-01-02")) 239 } 240 241 // Millis represents a Unix time in milliseconds since January 1, 1970 UTC. 242 type Millis time.Time 243 244 func (m *Millis) UnmarshalJSON(data []byte) error { 245 d, err := strconv.ParseInt(string(data), 10, 64) 246 if err != nil { 247 return err 248 } 249 *m = Millis(time.UnixMilli(d)) 250 return nil 251 } 252 253 func (m Millis) MarshalJSON() ([]byte, error) { 254 return json.Marshal(time.Time(m).UnixMilli()) 255 } 256 257 // Nanos represents a Unix time in nanoseconds since January 1, 1970 UTC. 258 type Nanos time.Time 259 260 func (n *Nanos) UnmarshalJSON(data []byte) error { 261 d, err := strconv.ParseInt(string(data), 10, 64) 262 if err != nil { 263 return err 264 } 265 266 // Go Time package does not include a method to convert UnixNano to a time. 267 timeNano := time.Unix(d/1_000_000_000, d%1_000_000_000) 268 *n = Nanos(timeNano) 269 return nil 270 } 271 272 func (n Nanos) MarshalJSON() ([]byte, error) { 273 return json.Marshal(time.Time(n).UnixNano()) 274 }