Skip to content

Commit

Permalink
Update option snapshots with filters, greeks and pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
gnvk committed Apr 25, 2024
1 parent a7cc8a8 commit 0fabde4
Show file tree
Hide file tree
Showing 5 changed files with 473 additions and 154 deletions.
75 changes: 42 additions & 33 deletions examples/marketdata/marketdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,50 +125,59 @@ func cryptoQuote() {
}

func optionChain() {
chain, err := marketdata.GetOptionChain("AAPL", marketdata.GetOptionSnapshotRequest{})
chain, err := marketdata.GetOptionChain("AAPL", marketdata.GetOptionChainRequest{
Type: marketdata.Call,
ExpirationDateLte: civil.DateOf(time.Now()).AddDays(5),
})
if err != nil {
panic(err)
}
type snap struct {
marketdata.OptionSnapshot
Symbol string
}
calls, puts := []snap{}, []snap{}
snaps := []snap{}
for symbol, snapshot := range chain {
if strings.Contains(symbol, "C") {
calls = append(calls, snap{OptionSnapshot: snapshot, Symbol: symbol})
} else {
puts = append(puts, snap{OptionSnapshot: snapshot, Symbol: symbol})
}
snaps = append(snaps, snap{OptionSnapshot: snapshot, Symbol: symbol})
}
sort.Slice(calls, func(i, j int) bool { return calls[i].Symbol < calls[j].Symbol })
sort.Slice(puts, func(i, j int) bool { return puts[i].Symbol < puts[j].Symbol })
printSnaps := func(snaps []snap) {
tw := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0)
fmt.Fprintf(tw, "%s\t%s\t%s\t%s\t%s\n", "Contract name", "Last trade time", "Price", "Bid", "Ask")
for _, s := range snaps {
ts := ""
if s.LatestTrade != nil {
ts = s.LatestTrade.Timestamp.Format(time.RFC3339)
}
price := float64(0)
if s.LatestTrade != nil {
price = s.LatestTrade.Price
}
bid, ask := float64(0), float64(0)
if s.LatestQuote != nil {
bid = s.LatestQuote.BidPrice
ask = s.LatestQuote.AskPrice
}
fmt.Fprintf(tw, "%s\t%s\t%g\t%g\t%g\n", s.Symbol, ts, price, bid, ask)
sort.Slice(snaps, func(i, j int) bool { return snaps[i].Symbol < snaps[j].Symbol })

tw := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
columns := []any{
"Contract name", "Last trade time", "Price", "Bid", "Ask",
"IV", "Delta", "Gamma", "Rho", "Theta", "Vega",
}
fmt.Fprintf(tw, strings.Repeat("%s\t", len(columns))+"\n", columns...)
for _, s := range snaps {
ts := ""
if s.LatestTrade != nil {
ts = s.LatestTrade.Timestamp.Format(time.RFC3339)
}
price := float64(0)
if s.LatestTrade != nil {
price = s.LatestTrade.Price
}
tw.Flush()
bid, ask := float64(0), float64(0)
if s.LatestQuote != nil {
bid = s.LatestQuote.BidPrice
ask = s.LatestQuote.AskPrice
}
iv := ""
if s.ImpliedVolatility != 0 {
iv = strconv.FormatFloat(s.ImpliedVolatility, 'f', 4, 64)
}
var delta, gamma, rho, theta, vega string
if s.Greeks != nil {
delta = strconv.FormatFloat(s.Greeks.Delta, 'f', 4, 64)
gamma = strconv.FormatFloat(s.Greeks.Gamma, 'f', 4, 64)
rho = strconv.FormatFloat(s.Greeks.Rho, 'f', 4, 64)
theta = strconv.FormatFloat(s.Greeks.Theta, 'f', 4, 64)
vega = strconv.FormatFloat(s.Greeks.Vega, 'f', 4, 64)
}
fmt.Fprintf(tw, "%s\t%s\t%g\t%g\t%g\t%s\t%s\t%s\t%s\t%s\t%s\n",
s.Symbol, ts, price, bid, ask, iv, delta, gamma, rho, theta, vega)
}
fmt.Println("CALLS")
printSnaps(calls)
fmt.Println()
fmt.Println("PUTS")
printSnaps(puts)
tw.Flush()
}

func corporateActions() {
Expand Down
24 changes: 21 additions & 3 deletions marketdata/entities.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ const (
Indicative Feed = "indicative"
)

type OptionType = string

const (
Call OptionType = "call"
Put OptionType = "put"
)

// TakerSide is the taker's side: one of B, S or -. B is buy, S is sell and - is unknown.
type TakerSide = string

Expand Down Expand Up @@ -391,9 +398,19 @@ type OptionQuote struct {
Condition string `json:"c"`
}

type OptionGreeks struct {
Delta float64 `json:"delta"`
Gamma float64 `json:"gamma"`
Rho float64 `json:"rho"`
Theta float64 `json:"theta"`
Vega float64 `json:"vega"`
}

type OptionSnapshot struct {
LatestTrade *OptionTrade `json:"latestTrade"`
LatestQuote *OptionQuote `json:"latestQuote"`
LatestTrade *OptionTrade `json:"latestTrade"`
LatestQuote *OptionQuote `json:"latestQuote"`
ImpliedVolatility float64 `json:"impliedVolatility,omitempty`
Greeks *OptionGreeks `json:"greeks,omitempty`
}
type multiTradeResponse struct {
NextPageToken *string `json:"next_page_token"`
Expand Down Expand Up @@ -483,5 +500,6 @@ type latestOptionQuotesResponse struct {
}

type optionSnapshotsResponse struct {
Snapshots map[string]OptionSnapshot `json:"snapshots"`
NextPageToken *string `json:"next_page_token"`
Snapshots map[string]OptionSnapshot `json:"snapshots"`
}
Loading

0 comments on commit 0fabde4

Please sign in to comment.