-
Notifications
You must be signed in to change notification settings - Fork 100
/
client.go
132 lines (109 loc) · 3.04 KB
/
client.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
package quote
import (
"context"
"fmt"
"strings"
finance "github.com/piquette/finance-go"
chart "github.com/piquette/finance-go/chart"
"github.com/piquette/finance-go/datetime"
"github.com/piquette/finance-go/form"
"github.com/piquette/finance-go/iter"
)
// Client is used to invoke quote APIs.
type Client struct {
B finance.Backend
}
func getC() Client {
return Client{finance.GetBackend(finance.YFinBackend)}
}
// Params carries a context and symbols information.
type Params struct {
finance.Params `form:"-"`
// Symbols are the symbols for which a
// quote is requested.
Symbols []string `form:"-"`
sym string `form:"symbols"`
}
// Iter is an iterator for a list of quotes.
// The embedded Iter carries methods with it;
// see its documentation for details.
type Iter struct {
*iter.Iter
}
// Quote returns the most recent Quote
// visited by a call to Next.
func (i *Iter) Quote() *finance.Quote {
return i.Current().(*finance.Quote)
}
// GetHistoricalQuote provides a single chart bar for a historical date.
func GetHistoricalQuote(symbol string, month int, day int, year int) (*finance.ChartBar, error) {
p := &chart.Params{
Symbol: symbol,
Start: &datetime.Datetime{Month: month, Day: day, Year: year},
End: &datetime.Datetime{Month: month, Day: day, Year: year},
Interval: datetime.OneDay,
}
iter := chart.Get(p)
for iter.Next() {
b := iter.Bar()
return b, nil
}
return nil, iter.Err()
}
// Get returns an Quote quote that matches the parameters specified.
func Get(symbol string) (*finance.Quote, error) {
i := List([]string{symbol})
if i.Count() == 0 {
return nil, fmt.Errorf("Can't find quote for symbol: %s", symbol)
}
if !i.Next() {
return nil, i.Err()
}
return i.Quote(), nil
}
// List returns several quotes.
func List(symbols []string) *Iter {
return ListP(&Params{Symbols: symbols})
}
// ListP returns a quote iterator and requires a params
// struct as an argument.
func ListP(params *Params) *Iter {
return getC().ListP(params)
}
// ListP returns a quote iterator.
func (c Client) ListP(params *Params) *Iter {
if params.Context == nil {
ctx := context.TODO()
params.Context = &ctx
}
// Validate input.
// TODO: validate symbols..
if params == nil || len(params.Symbols) == 0 {
return &Iter{iter.NewE(finance.CreateArgumentError())}
}
params.sym = strings.Join(params.Symbols, ",")
body := &form.Values{}
form.AppendTo(body, params)
return &Iter{iter.New(body, func(b *form.Values) (interface{}, []interface{}, error) {
resp := response{}
err := c.B.Call("/v7/finance/quote", body, params.Context, &resp)
if err != nil {
err = finance.CreateRemoteError(err)
}
ret := make([]interface{}, len(resp.Inner.Result))
for i, v := range resp.Inner.Result {
ret[i] = v
}
if resp.Inner.Error != nil {
err = finance.CreateRemoteError(resp.Inner.Error)
}
return nil, ret, err
})}
}
// response is a yfin quote response.
type response struct {
Inner struct {
Result []*finance.Quote `json:"result"`
Error *finance.YfinError `json:"error"`
} `json:"quoteResponse"`
}