Created
July 10, 2021 04:22
-
-
Save ilovelili/efbb559106364f28371796aae9bfcabb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package wsclient | |
import ( | |
"fmt" | |
"github.com/gorilla/websocket" | |
pubsub "github.com/woodstock-tokyo/simple-pubsub" | |
"github.com/woodstock-tokyo/woodstock-alpaca/server/util" | |
) | |
// Market market client | |
type Market struct { | |
key string | |
secret string | |
} | |
// NewMarketClient new market client | |
func NewMarketClient() *Market { | |
return &Market{ | |
key: config.AlpacaAPI.Key, | |
secret: config.AlpacaAPI.Secret, | |
} | |
} | |
// GetMarketData get market data | |
func (c *Market) GetMarketData(ps *pubsub.PubSub) (err error) { | |
fmt.Println(config.ResolveIEXEndpoint()) // wss://stream.data.sandbox.alpaca.markets/v2/iex | |
conn, _, err := websocket.DefaultDialer.Dial(config.ResolveIEXEndpoint(), nil) | |
if err != nil { | |
return | |
} | |
defer conn.Close() | |
// https://alpaca.markets/docs/broker/market-data/realtime/ | |
if err = conn.WriteMessage(websocket.TextMessage, []byte(fmt.Sprintf("{\"action\":\"auth\",\"key\":\"%s\", \"secret\":\"%s\"}", config.AlpacaAPI.Key, config.AlpacaAPI.Secret))); err != nil { | |
return | |
} | |
// https://alpaca.markets/docs/api-documentation/api-v2/market-data/alpaca-data-api-v2/real-time/ | |
if err = conn.WriteMessage(websocket.TextMessage, []byte(`{"action":"subscribe", "bars":["*"]}`)); err != nil { | |
return | |
} | |
for { | |
_, msg, err := conn.ReadMessage() | |
if err != nil { | |
return err | |
} | |
// todo: check changed or not | |
// we need a cache server | |
fmt.Println(string(msg)) | |
ps.Pub(msg, util.MARKET_CHANNEL) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment