Skip to content

Commit

Permalink
Add crypto perp endpoints (#312)
Browse files Browse the repository at this point in the history
* rest endpoint changes

* location added

* minimum changes for the crypto future

* Add an example for crypto-perps

* Add crypto perp market data support (stream and rest)

* Add marketdata REST example for crypto perp endpoints

* Fixed lint issues

* Apply changes suggested via PR feedback

* Bugfix in GetLatestCryptoPerpBars

* Added GetLatestCryptoPerpBars

---------

Co-authored-by: manoj <[email protected]>
  • Loading branch information
chathumi-k and manoj-bandara authored Dec 16, 2024
1 parent 4ca59cf commit a2396c6
Show file tree
Hide file tree
Showing 7 changed files with 370 additions and 20 deletions.
48 changes: 48 additions & 0 deletions examples/crypto-perp-stream/crypto_stream.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

import (
"context"
"fmt"

"github.com/alpacahq/alpaca-trade-api-go/v3/marketdata"
"github.com/alpacahq/alpaca-trade-api-go/v3/marketdata/stream"
)

func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

baseURL := "ws://stream.data.alpaca.markets/v1beta1/crypto-perps"

c := stream.NewCryptoPerpClient(
marketdata.GLOBAL,
stream.WithLogger(stream.DefaultLogger()),
stream.WithBaseURL(baseURL), // Set the base URL
// configuring initial subscriptions and handlers
stream.WithCryptoPerpTrades(func(ct stream.CryptoPerpTrade) {
fmt.Printf("TRADE: %+v\n", ct)
}, "BTC-PERP"),
stream.WithCryptoPerpQuotes(func(cq stream.CryptoPerpQuote) {
fmt.Printf("QUOTE: %+v\n", cq)
}, "BTC-PERP"),
stream.WithCryptoPerpOrderbooks(func(cob stream.CryptoPerpOrderbook) {
fmt.Printf("ORDERBOOK: %+v\n", cob)
}, "BTC-PERP"),
stream.WithCryptoPerpBars(func(cb stream.CryptoPerpBar) {
fmt.Printf("BAR: %+v\n", cb)
}, "BTC-PERP"),
stream.WithCryptoPerpUpdatedBars(func(cb stream.CryptoPerpBar) {
fmt.Printf("UPDATED BAR: %+v\n", cb)
}, "BTC-PERP"),
stream.WithCryptoPerpDailyBars(func(cb stream.CryptoPerpBar) {
fmt.Printf("DAILY BAR: %+v\n", cb)
}, "BTC-PERP"),
)

if err := c.Connect(ctx); err != nil {
panic(err)
}
if err := <-c.Terminated(); err != nil {
panic(err)
}
}
32 changes: 31 additions & 1 deletion examples/marketdata/marketdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,33 @@ func cryptoQuote() {
fmt.Println()
}

func cryptoPerpQuote() {
quote, err := marketdata.GetLatestCryptoPerpQuote("BTC-PERP", marketdata.GetLatestCryptoQuoteRequest{})
if err != nil {
panic(err)
}
fmt.Printf("Latest crypto perp quote: %+v\n\n", quote)
fmt.Println()
}

func cryptoPerpTrade() {
trade, err := marketdata.GetLatestCryptoPerpTrade("BTC-PERP", marketdata.GetLatestCryptoTradeRequest{})
if err != nil {
panic(err)
}
fmt.Printf("Latest crypto perp trade: %+v\n\n", trade)
fmt.Println()
}

func cryptoPerpBar() {
trade, err := marketdata.GetLatestCryptoPerpBar("BTC-PERP", marketdata.GetLatestCryptoBarRequest{})
if err != nil {
panic(err)
}
fmt.Printf("Latest crypto perp bar: %+v\n\n", trade)
fmt.Println()
}

func optionChain() {
chain, err := marketdata.GetOptionChain("AAPL", marketdata.GetOptionChainRequest{
Type: marketdata.Call,
Expand Down Expand Up @@ -209,13 +236,16 @@ func main() {
{Name: "news", Func: news},
{Name: "auctions", Func: auctions},
{Name: "crypto_quote", Func: cryptoQuote},
{Name: "crypto_perp_quote", Func: cryptoPerpQuote},
{Name: "crypto_perp_trade", Func: cryptoPerpTrade},
{Name: "crypto_perp_bar", Func: cryptoPerpBar},
{Name: "option_chain", Func: optionChain},
{Name: "corporate_actions", Func: corporateActions},
}
for {
fmt.Println("Examples: ")
for i, e := range examples {
fmt.Printf("[ %d ] %s\n", i, e.Name)
fmt.Printf("[ %2d ] %s\n", i, e.Name)
}
fmt.Print("Please type the number of the example you'd like to run or q to exit: ")
r := bufio.NewReader(os.Stdin)
Expand Down
8 changes: 7 additions & 1 deletion marketdata/entities.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ type CryptoFeed = string

const (
// US is the crypto feed for the United States.
US CryptoFeed = "us"
US CryptoFeed = "us"
GLOBAL CryptoFeed = "global"
)

// OptionFeed defines the source feed of option data.
Expand Down Expand Up @@ -208,6 +209,11 @@ type CryptoSnapshot struct {
PrevDailyBar *CryptoBar `json:"prevDailyBar"`
}

type CryptoPerpTrade CryptoTrade
type CryptoPerpQuote CryptoQuote
type CryptoPerpBar CryptoBar
type CryptoPerpSnapshot CryptoSnapshot

// CryptoSnapshots is the snapshots for multiple crypto symbols
type CryptoSnapshots struct {
Snapshots map[string]CryptoSnapshot `json:"snapshots"`
Expand Down
Loading

0 comments on commit a2396c6

Please sign in to comment.