This repository has been archived by the owner on Oct 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
option.go
70 lines (63 loc) · 2.23 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
package finnhub
import (
"encoding/json"
"strings"
"time"
)
// Option the data structure for options
type Option struct {
ContractName string `json:"contractName"`
ContractSize string `json:"contractSize"`
Currency string `json:"currency"`
Type string `json:"type"`
InTheMoney bool `json:"inTheMoney"`
LastTradeDateTime *time.Time `json:"lastTradeDateTime"`
ExpirationDate time.Time `json:"expirationDate"`
Strike float64 `json:"strike,string"`
LastPrice float64 `json:"lastPrice,string"`
Bid float64 `json:"bid,string"`
Ask float64 `json:"ask,string"`
Change float64 `json:"change,string"`
ChangePercent float64 `json:"changePercent,string"`
Volume int `json:"volume"`
OpenInterest int `json:"openInterest"`
ImpliedVolatility float64 `json:"impliedVolatility,string"`
Delta float64 `json:"delta,string"`
Gamma float64 `json:"gamma,string"`
Theta float64 `json:"theta,string"`
Vega float64 `json:"vega,string"`
Rho float64 `json:"rho,string"`
Theoretical float64 `json:"theoretical,string"`
IntrinsicValue float64 `json:"intrinsicValue,string"`
TimeValue float64 `json:"timeValue,string"`
UpdatedAt time.Time `json:"updatedAt"`
}
// UnmarshalJSON decodes the json data
func (o *Option) UnmarshalJSON(data []byte) error {
type Alias Option
opt := &struct {
InTheMoney string `json:"inTheMoney"`
LastTradeDateTime string `json:"lastTradeDateTime"`
ExpirationDate string `json:"expirationDate"`
UpdatedAt string `json:"updatedAt"`
*Alias
}{
Alias: (*Alias)(o),
}
var err error
if err = json.Unmarshal(data, &opt); err != nil {
return err
}
o.InTheMoney = false
if strings.ToLower(opt.InTheMoney) == "true" {
o.InTheMoney = true
}
lastTrade, _ := time.Parse(DateLayoutDateTime, opt.LastTradeDateTime)
o.LastTradeDateTime = &lastTrade
o.ExpirationDate, err = time.Parse(DateLayoutDate, opt.ExpirationDate)
if err != nil {
return err
}
o.UpdatedAt, err = time.Parse(DateLayoutDateTime, opt.UpdatedAt)
return err
}