-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoption.go
143 lines (120 loc) · 3.56 KB
/
option.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
package allyinvest
import (
"fmt"
"net/url"
"strings"
)
// SearchOptionsInput are the required or optional parameters
// to SearchOptions.
type SearchOptionsInput struct {
// REQUIRED: The symbol you are searching for.
Symbol *string `validate:"required"`
// OPTIONAL: Query to apply when searching for the provided symbol.
Query *OptionSearchQuery
// OPTIONAL: Fields to return from the search. If ommitted, all fields
// are returned.
Fields []string
}
// Encode encodes s to a query string.
func (s SearchOptionsInput) Encode() string {
u := url.Values{}
u.Add("symbol", *s.Symbol)
if s.Query != nil {
u.Add("query", s.Query.Encode())
}
if len(s.Fields) > 0 {
u.Add("fids", strings.Join(s.Fields, ","))
}
return u.Encode()
}
// Encode encodes o into a custom query string.
func (o OptionSearchQuery) Encode() string {
var t []string
for _, e := range o {
s := e.Encode()
if s != "" {
t = append(t, s)
}
}
return strings.Join(t, " AND ")
}
// Encode encodes o into a custom query string.
func (o OptionSearchQueryElement) Encode() string {
switch {
case o.StrikePrice != nil:
return fmt.Sprintf("strikeprice%s%.2f", o.QueryOperator, *o.StrikePrice)
case o.ExpirationDate != nil:
return fmt.Sprintf("xdate%s%d", o.QueryOperator, *o.ExpirationDate)
case o.ExpirationMonth != nil:
return fmt.Sprintf("xmonth%s%02d", o.QueryOperator, *o.ExpirationMonth)
case o.ExpirationYear != nil:
return fmt.Sprintf("xyear%s%d", o.QueryOperator, *o.ExpirationYear)
case o.OptionKind != nil:
return fmt.Sprintf("put_call%s%s", o.QueryOperator, *o.OptionKind)
case o.Unique != nil:
return fmt.Sprintf("unique%s%s", o.QueryOperator, *o.Unique)
default:
return ""
}
}
// SearchOptions searches for the provided symbol.
func (a *api) SearchOptions(input SearchOptionsInput) (SearchOptionsOutput, error) {
output := SearchOptionsOutput{}
err := validate(input)
if err != nil {
return output, err
}
route := "/market/options/search"
err = a.httpGet(route, input, &output)
return output, err
}
// GetOptionsStrikesInput are the required or optional parameters
// to GetOptionsStrikes.
type GetOptionsStrikesInput struct {
// REQUIRED: The symbol you are searching for.
Symbol *string `validate:"required"`
}
// Encode encodes g to a query string.
func (g GetOptionsStrikesInput) Encode() string {
u := url.Values{}
if g.Symbol != nil {
u.Add("symbol", string(*g.Symbol))
}
return u.Encode()
}
// GetOptionsStrikes lists the available strikes for the given symbol.
func (a *api) GetOptionsStrikes(input GetOptionsStrikesInput) (GetOptionsStrikesOutput, error) {
output := GetOptionsStrikesOutput{}
err := validate(input)
if err != nil {
return output, err
}
route := "/market/options/strikes"
err = a.httpGet(route, input, &output)
return output, err
}
// GetOptionsExpirationsInput are the required or optional parameters
// to GetOptiosnExpirations.
type GetOptionsExpirationsInput struct {
// REQUIRED: The symbol you are searching for.
Symbol *string `validate:"required"`
}
// Encode encodes g to a query string.
func (g GetOptionsExpirationsInput) Encode() string {
u := url.Values{}
if g.Symbol != nil {
u.Add("symbol", string(*g.Symbol))
}
return u.Encode()
}
// GetOptionsStrikes lists the available strikes for the given symbol.
func (a *api) GetOptionsExpirations(input GetOptionsExpirationsInput) (GetOptionsExpirationsOutput, error) {
output := GetOptionsExpirationsOutput{}
err := validate(input)
if err != nil {
return output, err
}
route := "/market/options/expirations"
err = a.httpGet(route, input, &output)
return output, err
}