forked from zricethezav/go-tdameritrade
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathmovers.go
88 lines (74 loc) · 1.87 KB
/
movers.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"
"github.com/google/go-querystring/query"
)
const (
defaultChangeType = "percent"
defaultDirectionType = "up"
)
var (
ChangeTypes = []string{"value", "percent"}
DirectionTypes = []string{"up", "down"}
)
type MoverService struct {
client *Client
}
type MoverOptions struct {
Direction string `url:"direction"`
ChangeType string `url:"change"`
}
type Mover struct {
Change float64 `json:"change"`
Description string `json:"description"`
Direction string `json:"direction"`
Last float64 `json:"last"`
TotalVolume float64 `json:"totalVolume"`
Symbol string `json:"symbol"`
}
func (s *MoverService) Mover(ctx context.Context, symbol string, opts *MoverOptions) (*[]Mover, *Response, error) {
u := fmt.Sprintf("marketdata/%s/movers", symbol)
if opts != nil {
if err := opts.validate(); err != nil {
return nil, nil, err
}
} else {
opts = &MoverOptions{
Direction: defaultDirectionType,
ChangeType: defaultChangeType,
}
}
q, err := query.Values(opts)
if err != nil {
return nil, nil, err
}
u = fmt.Sprintf("%s?%s", u, q.Encode())
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
movers := new([]Mover)
resp, err := s.client.Do(ctx, req, movers)
if err != nil {
return nil, resp, err
}
return movers, resp, nil
}
func (opts *MoverOptions) validate() error {
if opts.ChangeType != "" {
if !contains(opts.ChangeType, ChangeTypes) {
return fmt.Errorf("invalid changeType, must have the value of one of the following %v", ChangeTypes)
}
} else {
opts.ChangeType = defaultChangeType
}
if opts.Direction != "" {
if !contains(opts.Direction, DirectionTypes) {
return fmt.Errorf("invalid direction, must have the value of one of the following %v", DirectionTypes)
}
} else {
opts.ChangeType = defaultDirectionType
}
return nil
}