Skip to content

Instantly share code, notes, and snippets.

@trusch
Last active June 30, 2017 09:44
Show Gist options
  • Save trusch/c60cd8db20d614943a6ff5ed1e24ce85 to your computer and use it in GitHub Desktop.
Save trusch/c60cd8db20d614943a6ff5ed1e24ce85 to your computer and use it in GitHub Desktop.
package main
import (
"encoding/json"
"log"
"net/http"
"strconv"
"time"
)
func getStream(pair string, delay time.Duration) chan map[string]interface{} {
ch := make(chan map[string]interface{}, 32)
go func() {
for {
resp, err := http.Get("https://api.kraken.com/0/public/Ticker?pair=" + pair)
if err != nil {
log.Print(err)
close(ch)
return
}
decoder := json.NewDecoder(resp.Body)
res := make(map[string]interface{})
err = decoder.Decode(&res)
if err != nil {
log.Print(err)
close(ch)
return
}
pairs := res["result"].(map[string]interface{})
val := pairs[pair].(map[string]interface{})
ch <- val
time.Sleep(delay)
}
}()
return ch
}
func getLastTrades(pair string, delay time.Duration) chan float64 {
ch := make(chan float64, 32)
go func() {
for tick := range getStream(pair, delay) {
v, err := strconv.ParseFloat(tick["c"].([]interface{})[0].(string), 64)
if err != nil {
log.Print(err)
close(ch)
return
}
ch <- v
}
close(ch)
}()
return ch
}
func main() {
stream := getLastTrades("XETHZEUR", 5*time.Second)
for tick := range stream {
log.Print(tick)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment