forked from JonCooperWorks/go-tdameritrade
/
watchlist.go
196 lines (164 loc) · 6.84 KB
/
watchlist.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package tdameritrade
import (
"context"
"fmt"
)
// NewWatchlist is a watchlist to be created by a user.
type NewWatchlist struct {
Name string `json:"name"`
WatchlistItems []WatchlistItem `json:"watchlistItems"`
}
// WatchlistItem is a security to be added to a NewWatchlist.
type WatchlistItem struct {
Quantity int `json:"quantity"`
AveragePrice int `json:"averagePrice"`
Commission int `json:"commission"`
PurchasedDate string `json:"purchasedDate"`
Instrument WatchlistInstrument `json:"instrument"`
}
// WatchlistInstrument is the specific information about the security being added to the watchlist.
type WatchlistInstrument struct {
Symbol string `json:"symbol"`
AssetType string `json:"assetType"`
}
// StoredWatchlist is an existing watchlist in a user's account.
type StoredWatchlist []struct {
Name string `json:"name"`
WatchlistID string `json:"watchlistId"`
AccountID string `json:"accountId"`
Status string `json:"status"`
WatchlistItems []StoredWatchlistItem `json:"watchlistItems"`
}
// StoredWatchlistItem is an item in the user's existing watchlist.
type StoredWatchlistItem struct {
SequenceID int `json:"sequenceId"`
Quantity int `json:"quantity"`
AveragePrice int `json:"averagePrice"`
Commission int `json:"commission"`
PurchasedDate string `json:"purchasedDate"`
Instrument StoredWatchlistInstrument `json:"instrument"`
Status string `json:"status"`
}
// StoredWatchlistInstrument is an investment instrument in a user's watchlist.
type StoredWatchlistInstrument struct {
Symbol string `json:"symbol"`
Description string `json:"description"`
AssetType string `json:"assetType"`
}
// WatchlistService allows CRUD operations on watchlists in a user's account.
// See https://developer.tdameritrade.com/watchlist/apis.
type WatchlistService struct {
client *Client
}
// CreateWatchlist adds a new watchlist to a user's account
// See https://developer.tdameritrade.com/watchlist/apis/post/accounts/%7BaccountId%7D/watchlists-0
func (s *WatchlistService) CreateWatchlist(ctx context.Context, accountID string, newWatchlist *NewWatchlist) (*Response, error) {
if accountID == "" {
return nil, fmt.Errorf("accountID cannot be empty")
}
u := fmt.Sprintf("accounts/%s/watchlists", accountID)
req, err := s.client.NewRequest("POST", u, newWatchlist)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}
// DeleteWatchlist removes a watchlist from a user's account
// See https://developer.tdameritrade.com/watchlist/apis/delete/accounts/%7BaccountId%7D/watchlists/%7BwatchlistId%7D-0
func (s *WatchlistService) DeleteWatchlist(ctx context.Context, accountID, watchlistID string) (*Response, error) {
if accountID == "" {
return nil, fmt.Errorf("accountID cannot be empty")
}
if watchlistID == "" {
return nil, fmt.Errorf("watchlistID cannot be empty")
}
u := fmt.Sprintf("accounts/%s/watchlists/%s", accountID, watchlistID)
req, err := s.client.NewRequest("DELETE", u, nil)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}
// GetWatchlist returns a single watchlist in a user's account
// See https://developer.tdameritrade.com/watchlist/apis/get/accounts/%7BaccountId%7D/watchlists/%7BwatchlistId%7D-0
func (s *WatchlistService) GetWatchlist(ctx context.Context, accountID, watchlistID string) (*StoredWatchlist, *Response, error) {
if accountID == "" {
return nil, nil, fmt.Errorf("accountID cannot be empty")
}
if watchlistID == "" {
return nil, nil, fmt.Errorf("watchlistID cannot be empty")
}
u := fmt.Sprintf("accounts/%s/watchlists/%s", accountID, watchlistID)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
storedWatchlist := new(StoredWatchlist)
resp, err := s.client.Do(ctx, req, storedWatchlist)
return storedWatchlist, resp, err
}
// GetAllWatchlists returns all watchlists for all of a user's linked accounts.
// See https://developer.tdameritrade.com/watchlist/apis/get/accounts/watchlists-0
func (s *WatchlistService) GetAllWatchlists(ctx context.Context) (*[]StoredWatchlist, *Response, error) {
u := "accounts/watchlists"
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
watchlists := new([]StoredWatchlist)
resp, err := s.client.Do(ctx, req, watchlists)
return watchlists, resp, err
}
// GetAllWatchlistsForAccount returns all watchlists for a single user account.
// See https://developer.tdameritrade.com/watchlist/apis/get/accounts/%7BaccountId%7D/watchlists-0
func (s *WatchlistService) GetAllWatchlistsForAccount(ctx context.Context, accountID string) (*[]StoredWatchlist, *Response, error) {
if accountID == "" {
return nil, nil, fmt.Errorf("accountID cannot be empty")
}
u := fmt.Sprintf("accounts/%s/watchlists", accountID)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
watchlists := new([]StoredWatchlist)
resp, err := s.client.Do(ctx, req, watchlists)
return watchlists, resp, err
}
// ReplaceWatchlist replaces a watchlist in an account with a new watchlist.
// It does not verify that symbols are valid.
// See https://developer.tdameritrade.com/watchlist/apis/put/accounts/%7BaccountId%7D/watchlists/%7BwatchlistId%7D-0
func (s *WatchlistService) ReplaceWatchlist(ctx context.Context, accountID, watchlistID string, newWatchlist *NewWatchlist) (*Response, error) {
if accountID == "" {
return nil, fmt.Errorf("accountID cannot be empty")
}
if watchlistID == "" {
return nil, fmt.Errorf("watchlistID cannot be empty")
}
u := fmt.Sprintf("accounts/%s/watchlists/%s", accountID, watchlistID)
req, err := s.client.NewRequest("PUT", u, newWatchlist)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}
// UpdateWatchlist partially updates watchlist for a specific account.
// Callers can:
// - change the watchlist's name
// - add to the beginning/end of a watchlist
// - update or delete items in a watchlist
// This method does not verify that the symbol or asset type are valid.
// See https://developer.tdameritrade.com/watchlist/apis/patch/accounts/%7BaccountId%7D/watchlists/%7BwatchlistId%7D-0
func (s *WatchlistService) UpdateWatchlist(ctx context.Context, accountID, watchlistID string, newWatchlist *NewWatchlist) (*Response, error) {
if accountID == "" {
return nil, fmt.Errorf("accountID cannot be empty")
}
if watchlistID == "" {
return nil, fmt.Errorf("watchlistID cannot be empty")
}
u := fmt.Sprintf("accounts/%s/watchlists/%s", accountID, watchlistID)
req, err := s.client.NewRequest("PATCH", u, newWatchlist)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}