Giter Club home page Giter Club logo

bitstamp-go's Introduction

bitstamp-go

A client implementation of the Bitstamp API, including websockets, in Golang.

Example Usage

package main

import (
	"fmt"
	"log"
	"time"

	"github.com/ajph/bitstamp-go"
)

const WS_TIMEOUT = 10 * time.Second

func handleEvent(e *bitstamp.Event, Ws *bitstamp.WebSocket) {
	switch e.Event {
	// pusher stuff
	case "pusher:connection_established":
		log.Println("Connected")
	case "pusher_internal:subscription_succeeded":
		log.Println("Subscribed")
	case "pusher:pong":
		// ignore
	case "pusher:ping":
		Ws.Pong()

	// bitstamp
	case "trade":
		fmt.Printf("%#v\n", e.Data)

	// other
	default:
		log.Printf("Unknown event: %#v\n", e)
	}
}

func main() {

	// setup bitstamp api
	bitstamp.SetAuth("123456", "key", "secret")

	// get balance
	balances, err := bitstamp.AccountBalance()
	if err != nil {
		fmt.Printf("Can't get balance using bitstamp API: %s\n", err)
		return
	}
	fmt.Println("\nAvailable Balances:")
	fmt.Printf("USD %f\n", balances.UsdAvailable)
	fmt.Printf("BTC %f\n", balances.BtcAvailable)
	fmt.Printf("FEE %f\n\n", balances.BtcUsdFee)

	// attempt to place a buy order
	// BuyLimitOrder(pair string, amount float64, price float64, amountPrecision, pricePrecision int)
	order, err := bitstamp.BuyLimitOrder("btcusd", 0.5, 600.00, 16, 16)
	if err != nil {
		log.Printf("Error placing buy order: %s", err)
		return
	}
	fmt.Printf("Place oder %d", order.Id)

	var Ws *bitstamp.WebSocket
	// websocket read loop
	for {
		// connect
		log.Println("Dialing...")
		var err error
		Ws, err = bitstamp.NewWebSocket(WS_TIMEOUT)
		if err != nil {
			log.Printf("Error connecting: %s", err)
			time.Sleep(1 * time.Second)
			continue
		}
		Ws.Subscribe("live_trades")

		// read data
	L:
		for {
			select {
			case ev := <-Ws.Stream:
				handleEvent(ev, Ws)

			case err := <-Ws.Errors:
				log.Printf("Socket error: %s, reconnecting...", err)
				Ws.Close()
				break L

			case <-time.After(10 * time.Second):
				Ws.Ping()

			}
		}
	}

}

Todo

  • Documentation
  • Tests

bitstamp-go's People

Contributors

ajph avatar edofic avatar gmelika avatar gwpl avatar herenow avatar jonatrey avatar micvbang avatar oparex avatar renehsz avatar uroskn avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.