forked from zricethezav/go-tdameritrade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chains.go
163 lines (148 loc) · 5.94 KB
/
chains.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
package tdameritrade
import (
"context"
"fmt"
"net/url"
"reflect"
)
// ChainsService handles communication with the chains related methods of
// the TDAmeritrade API.
//
// TDAmeritrade API docs: https://developer.tdameritrade.com/option-chains/apis
type ChainsService struct {
client *Client
}
type Underlying struct {
Symbol string `json:"symbol"`
Description string `json:"description"`
Change float64 `json:"change"`
PercentChange float64 `json:"percentChange"`
Close float64 `json:"close"`
QuoteTime int `json:"quoteTime"`
TradeTime int `json:"tradeTime"`
Bid float64 `json:"bid"`
Ask float64 `json:"ask"`
Last float64 `json:"last"`
Mark float64 `json:"mark"`
MarkChange float64 `json:"markChange"`
MarkPercentChange float64 `json:"markPercentChange"`
BidSize int `json:"bidSize"`
AskSize int `json:"askSize"`
HighPrice float64 `json:"highPrice"`
LowPrice float64 `json:"lowPrice"`
OpenPrice float64 `json:"openPrice"`
TotalVolume int `json:"totalVolume"`
ExchangeName string `json:"exchangeName"`
FiftyTwoWeekHigh float64 `json:"fiftyTwoWeekHigh"`
FiftyTwoWeekLow float64 `json:"fiftyTwoWeekLow"`
Delayed bool `json:"delayed"`
}
type ExpDateOption struct {
PutCall string `json:"putCall"`
Symbol string `json:"symbol"`
Description string `json:"description"`
ExchangeName string `json:"exchangeName"`
Bid float64 `json:"bid"`
Ask float64 `json:"ask"`
Last float64 `json:"last"`
Mark float64 `json:"mark"`
BidSize int `json:"bidSize"`
AskSize int `json:"askSize"`
BidAskSize string `json:"bidAskSize"`
LastSize float64 `json:"lastSize"`
HighPrice float64 `json:"highPrice"`
LowPrice float64 `json:"lowPrice"`
OpenPrice float64 `json:"openPrice"`
ClosePrice float64 `json:"closePrice"`
TotalVolume int `json:"totalVolume"`
TradeDate string `json:"tradeDate"`
TradeTimeInLong int `json:"tradeTimeInLong"`
QuoteTimeInLong int `json:"quoteTimeInLong"`
NetChange float64 `json:"netChange"`
Volatility interface{} `json:"volatility"`
Delta interface{} `json:"delta"`
Gamma interface{} `json:"gamma"`
Theta interface{} `json:"theta"`
Vega interface{} `json:"vega"`
Rho interface{} `json:"rho"`
OpenInterest int `json:"openInterest"`
TimeValue float64 `json:"timeValue"`
TheoreticalOptionValue interface{} `json:"theoreticalOptionValue"`
TheoreticalVolatility float64 `json:"theoreticalVolatility"`
OptionDeliverablesList string `json:"optionDeliverablesList"`
StrikePrice float64 `json:"strikePrice"`
ExpirationDate int `json:"expirationDate"`
DaysToExpiration int `json:"daysToExpiration"`
ExpirationType string `json:"expirationType"`
LastTradingDate int `json:"lastTradingDay"`
Multiplier float64 `json:"multiplier"`
SettlementType string `json:"settlementType"`
DeliverableNote string `json:"deliverableNote"`
IsIndexOption bool `json:"isIndexOption"`
PercentChange float64 `json:"percentChange"`
MarkChange float64 `json:"markChange"`
MarkPercentChange float64 `json:"markPercentChange"`
InTheMoney bool `json:"inTheMoney"`
Mini bool `json:"mini"`
NonStandard bool `json:"nonStandard"`
}
func (e ExpDateOption) GetVolatility() float64 {
switch e.Volatility.(type) {
case float64:
return e.Volatility.(float64)
case string:
return 0.0
default:
return 0.0
}
}
func (e ExpDateOption) GetValueOrNan(name string) float64 {
val := reflect.ValueOf(e)
field := val.FieldByName(name)
if !field.IsValid() {
return 0.0
}
value := field.Interface()
switch value.(type) {
case float64:
return value.(float64)
case string:
return 0.0
default:
return 0.0
}
}
// the first string is the exp date. the second string is the strike price.
type ExpDateMap map[string]map[string][]ExpDateOption
type Chains struct {
Symbol string `json:"symbol"`
Status string `json:"status"`
Underlying Underlying `json:"underlying"`
Strategy string `json:"strategy"`
Interval float64 `json:"interval"`
IsDelayed bool `json:"isDelayed"`
IsIndex bool `json:"isIndex"`
InterestRate float64 `json:"interestRate"`
UnderlyingPrice float64 `json:"underlyingPrice"`
Volatility float64 `json:"volatility"`
DaysToExpiration float64 `json:"daysToExpiration"`
NumberOfContracts int `json:"numberOfContracts"`
CallExpDateMap ExpDateMap `json:"callExpDateMap"`
PutExpDateMap ExpDateMap `json:"putExpDateMap"`
}
// Users must provide the required URL queryValues for this function to work.
// TD Ameritrade url values: https://developer.tdameritrade.com/option-chains/apis/get/marketdata/chains
// Instructions for using url.Values: https://golang.org/pkg/net/url/#Values
func (s *ChainsService) GetChains(ctx context.Context, queryValues url.Values) (*Chains, *Response, error) {
u := fmt.Sprintf("marketdata/chains?%s", queryValues.Encode())
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
chains := new(Chains)
resp, err := s.client.Do(ctx, req, chains)
if err != nil {
return nil, resp, err
}
return chains, resp, nil
}