forked from zricethezav/go-tdameritrade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hours.go
88 lines (69 loc) · 2.04 KB
/
hours.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package tdameritrade
import (
"context"
"fmt"
"time"
)
// MarketHoursService handles communication with the marketdata related methods of
// the TDAmeritrade API.
//
// TDAmeritrade API docs: https://developer.tdameritrade.com/market-hours/apis
type MarketHoursService struct {
client *Client
}
type MarketHours map[string]map[string]*Hours
type Period struct {
Start string `json:"start"`
End string `json:"end"`
}
type SessionHours struct {
PreMarket []Period `json:"preMarket"`
RegularMarket []Period `json:"regularMarket"`
PostMarket []Period `json:"postMarket"`
}
type Hours struct {
Category string `json:"category"`
Date string `json:"date"`
Exchange string `json:"exchange"`
IsOpen bool `json:"isOpen"`
MarketType string `json:"marketType"`
Product string `json:"product"`
ProductName string `json:"productName"`
SessionHours SessionHours `json:"sessionHours"`
}
func (s *MarketHoursService) GetMarketHoursMulti(ctx context.Context, markets string, date time.Time) (*MarketHours, *Response, error) {
u := "marketdata/hours"
if markets == "" {
return nil, nil, fmt.Errorf("no markets present")
}
u = fmt.Sprintf("%s?markets=%s", u, markets)
if !date.IsZero() {
u = fmt.Sprintf("%s&date=%s", u, date.Format("2006-01-02"))
}
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
hours := new(MarketHours)
resp, err := s.client.Do(ctx, req, hours)
if err != nil {
return nil, resp, err
}
return hours, resp, nil
}
func (s *MarketHoursService) GetMarketHours(ctx context.Context, market string, date time.Time) (*MarketHours, *Response, error) {
u := fmt.Sprintf("marketdata/%s/hours", market)
if !date.IsZero() {
u = fmt.Sprintf("%s?date=%s", u, date.Format("2006-01-02"))
}
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
hours := new(MarketHours)
resp, err := s.client.Do(ctx, req, hours)
if err != nil {
return nil, resp, err
}
return hours, resp, nil
}