forked from zricethezav/go-tdameritrade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
instruments.go
70 lines (56 loc) · 1.73 KB
/
instruments.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
package tdameritrade
import (
"context"
"fmt"
)
// InstrumentService handles communication with the marketdata related methods of
// the TDAmeritrade API.
//
// TDAmeritrade API docs: https://developer.tdameritrade.com/instruments/apis
type InstrumentService struct {
client *Client
}
type Instruments map[string]*InstrumentInfo
type InstrumentInfo struct {
Cusip string `json:"cusip,omitempty"`
Symbol string `json:"symbol"`
Description string `json:"description,omitempty"`
Type string `json:"assetType"` //"'NOT_APPLICABLE' or 'OPEN_END_NON_TAXABLE' or 'OPEN_END_TAXABLE' or 'NO_LOAD_NON_TAXABLE' or 'NO_LOAD_TAXABLE'"
Exchange string `json:"exchange"`
}
func (s *InstrumentService) GetInstrument(ctx context.Context, cusip string) (*Instruments, *Response, error) {
if cusip == "" {
return nil, nil, fmt.Errorf("no cusip present")
}
u := fmt.Sprintf("instruments/%s", cusip)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
instruments := new(Instruments)
resp, err := s.client.Do(ctx, req, instruments)
if err != nil {
return nil, resp, err
}
return nil, nil, nil
}
func (s *InstrumentService) SearchInstruments(ctx context.Context, symbol, projection string) (*Instruments, *Response, error) {
u := "instruments"
if symbol == "" {
return nil, nil, fmt.Errorf("no symbol present")
}
if projection == "" {
projection = "symbol-search"
}
u = fmt.Sprintf("%s?symbol=%s&projection=%s", u, symbol, projection)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
instruments := new(Instruments)
resp, err := s.client.Do(ctx, req, instruments)
if err != nil {
return nil, resp, err
}
return instruments, resp, nil
}