finnhubio/Finnhub-API

Trades - Last Price Updates using WriteMessage returns only trades on the first stock

Closed this issue · 1 comments

I am writing a web server which will monitor specified stock prices. I am writing in Golang.

I basically implemented the sample code in the 'Trades - Last Price Updates' section of the documentation, replacing the literal stock symbols with my own. The returned messages only return trades for the first stock specified.
The relevant code:

w, _, err := dialer.Dial("wss://ws.finnhub.io?token=cbbb00iad3ibhoa1vbcg", nil)

		for _, s := range p.TrackSpecification.Symbols {
			msg, _ := json.Marshal(map[string]interface{}{"type": "subscribe", "symbol": s})
			log.Printf("msg to websocket: %s\n", msg)
			msgErr := w.WriteMessage(websocket.TextMessage, msg)
			if msgErr != nil {
				fmt.Printf("error submitting msg to websocket: %v", msgErr)
				log.Printf("error submitting msg to websocket: %v", msgErr)
			}
		}

		var msg interface{}

doRealTime := true
for doRealTime{
      err := w.ReadJSON(&msg)
      msgMap, ok := msg.(map[string]interface{})
     msgType := msgMap["type"].(string)
     if msgType == "trade" {
         //process the trade
     }
}

from the log:

11:09:56 finnHubRealTime.go:116: msg to websocket: {"symbol":"SNAP","type":"subscribe"}
11:09:56 finnHubRealTime.go:116: msg to websocket: {"symbol":" IBM","type":"subscribe"}
11:09:56 finnHubRealTime.go:116: msg to websocket: {"symbol":" GPRO","type":"subscribe"}
11:09:57 finnHubRealTime.go:187: RealTimeQuotes:  SNAP 10.37
11:09:57 finnHubRealTime.go:231: buy: 10 shares SNAP at 10.37
11:09:57 etrade.go:127: ExecuteTrade buy
11:09:58 finnHubRealTime.go:187: RealTimeQuotes:  SNAP 10.375
11:09:59 finnHubRealTime.go:187: RealTimeQuotes:  SNAP 10.379
11:10:00 finnHubRealTime.go:187: RealTimeQuotes:  SNAP 10.37
11:10:01 finnHubRealTime.go:187: RealTimeQuotes:  SNAP 10.37
11:10:01 finnHubRealTime.go:187: RealTimeQuotes:  SNAP 10.37

Resolved, I had a leading space in the symbol. Be sure to uppercase and trim the symbols