Skip to content

Commit

Permalink
Add marketdata for options (#271)
Browse files Browse the repository at this point in the history
* Add marketdata for options

* fix typo and remove unused methods
  • Loading branch information
gnvk authored Mar 12, 2024
1 parent cfec3c6 commit b36dcba
Show file tree
Hide file tree
Showing 13 changed files with 3,075 additions and 803 deletions.
50 changes: 50 additions & 0 deletions examples/marketdata/marketdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"bufio"
"fmt"
"os"
"sort"
"strconv"
"strings"
"text/tabwriter"
"time"

"github.com/alpacahq/alpaca-trade-api-go/v3/marketdata"
Expand Down Expand Up @@ -120,6 +122,53 @@ func cryptoQuote() {
fmt.Println()
}

func optionChain() {
chain, err := marketdata.GetOptionChain("AAPL", marketdata.GetOptionSnapshotRequest{})
if err != nil {
panic(err)
}
type snap struct {
marketdata.OptionSnapshot
Symbol string
}
calls, puts := []snap{}, []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})
}
}
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)
}
tw.Flush()
}
fmt.Println("CALLS")
printSnaps(calls)
fmt.Println()
fmt.Println("PUTS")
printSnaps(puts)
}

type example struct {
Name string
Func func()
Expand All @@ -134,6 +183,7 @@ func main() {
{Name: "news", Func: news},
{Name: "auctions", Func: auctions},
{Name: "crypto_quote", Func: cryptoQuote},
{Name: "option_chain", Func: optionChain},
}
for {
fmt.Println("Examples: ")
Expand Down
67 changes: 67 additions & 0 deletions marketdata/entities.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ const (
US CryptoFeed = "us"
)

// OptionFeed defines the source feed of option data.
type OptionFeed = string

const (
OPRA Feed = "opra"
Indicative Feed = "indicative"
)

// 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 @@ -219,6 +227,43 @@ type News struct {
Symbols []string `json:"symbols"`
}

// OptionTrade is an option trade that happened on the market
type OptionTrade struct {
Timestamp time.Time `json:"t"`
Price float64 `json:"p"`
Size uint32 `json:"s"`
Exchange string `json:"x"`
Condition string `json:"c"`
}

// OptionBar is an aggregate of option trades
type OptionBar struct {
Timestamp time.Time `json:"t"`
Open float64 `json:"o"`
High float64 `json:"h"`
Low float64 `json:"l"`
Close float64 `json:"c"`
Volume uint64 `json:"v"`
TradeCount uint64 `json:"n"`
VWAP float64 `json:"vw"`
}

// OptionQuote is an option NBBO (National Best Bid and Offer)
type OptionQuote struct {
Timestamp time.Time `json:"t"`
BidPrice float64 `json:"bp"`
BidSize uint32 `json:"bs"`
BidExchange string `json:"bx"`
AskPrice float64 `json:"ap"`
AskSize uint32 `json:"as"`
AskExchange string `json:"ax"`
Condition string `json:"c"`
}

type OptionSnapshot struct {
LatestTrade *OptionTrade `json:"latestTrade"`
LatestQuote *OptionQuote `json:"latestQuote"`
}
type multiTradeResponse struct {
NextPageToken *string `json:"next_page_token"`
Trades map[string][]Trade `json:"trades"`
Expand Down Expand Up @@ -282,3 +327,25 @@ type newsResponse struct {
NextPageToken *string `json:"next_page_token"`
News []News `json:"news"`
}

type multiOptionTradeResponse struct {
NextPageToken *string `json:"next_page_token"`
Trades map[string][]OptionTrade `json:"trades"`
}

type multiOptionBarResponse struct {
NextPageToken *string `json:"next_page_token"`
Bars map[string][]OptionBar `json:"bars"`
}

type latestOptionTradesResponse struct {
Trades map[string]OptionTrade `json:"trades"`
}

type latestOptionQuotesResponse struct {
Quotes map[string]OptionQuote `json:"quotes"`
}

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

0 comments on commit b36dcba

Please sign in to comment.