bitstamp

package module
v0.0.0-...-126e380 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 1, 2019 License: MIT Imports: 15 Imported by: 0

README

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CancelOrder

func CancelOrder(orderId int64)

func OpenOrders

func OpenOrders() (*[]OpenOrder, error)

func SetAuth

func SetAuth(clientId, key, secret string)

func SetDebug

func SetDebug(debug bool)

func SetUrl

func SetUrl(url string)

func UserTransactions

func UserTransactions() (*[]UserTransactionsResult, error)

Types

type AccountBalanceResult

type AccountBalanceResult struct {
	UsdBalance   float64 `json:"usd_balance,string"`
	BtcBalance   float64 `json:"btc_balance,string"`
	EurBalance   float64 `json:"eur_balance,string"`
	XrpBalance   float64 `json:"xrp_balance,string"`
	LtcBalance   float64 `json:"ltc_balance,string"`
	EthBalance   float64 `json:"eth_balance,string"`
	BchBalance   float64 `json:"bch_balance,string"`
	UsdReserved  float64 `json:"usd_reserved,string"`
	BtcReserved  float64 `json:"btc_reserved,string"`
	EurReserved  float64 `json:"eur_reserved,string"`
	XrpReserved  float64 `json:"xrp_reserved,string"`
	LtcReserved  float64 `json:"ltc_reserved,string"`
	EthReserved  float64 `json:"eth_reserved,string"`
	BchReserved  float64 `json:"bch_reserved,string"`
	UsdAvailable float64 `json:"usd_available,string"`
	BtcAvailable float64 `json:"btc_available,string"`
	EurAvailable float64 `json:"eur_available,string"`
	XrpAvailable float64 `json:"xrp_available,string"`
	LtcAvailable float64 `json:"ltc_available,string"`
	EthAvailable float64 `json:"eth_available,string"`
	BchAvailable float64 `json:"bch_available,string"`
	BtcUsdFee    float64 `json:"btcusd_fee,string"`
	BtcEurFee    float64 `json:"btceur_fee,string"`
	EurUsdFee    float64 `json:"eurusd_fee,string"`
	XrpUsdFee    float64 `json:"xrpusd_fee,string"`
	XrpEurFee    float64 `json:"xrpeur_fee,string"`
	XrpBtcFee    float64 `json:"xrpbtc_fee,string"`
	LtcUsdFee    float64 `json:"ltcusd_fee,string"`
	LtcEurFee    float64 `json:"ltceur_fee,string"`
	LtcBtcFee    float64 `json:"ltcbtc_fee,string"`
	EthUsdFee    float64 `json:"ethusd_fee,string"`
	EthEurFee    float64 `json:"etheur_fee,string"`
	EthBtcFee    float64 `json:"ethbtc_fee,string"`
	BchUsdFee    float64 `json:"bchusd_fee,string"`
	BchEurFee    float64 `json:"bcheur_fee,string"`
	BchBtcFee    float64 `json:"bchbtc_fee,string"`
}

func AccountBalance

func AccountBalance() (*AccountBalanceResult, error)

type BuyOrderResult

type BuyOrderResult struct {
	Id       int64   `json:"id,string"`
	DateTime string  `json:"datetime"`
	Type     int     `json:"type,string"`
	Price    float64 `json:"price,string"`
	Amount   float64 `json:"amount,string"`
}

func BuyLimitOrder

func BuyLimitOrder(pair string, amount float64, price float64, amountPrecision, pricePrecision int) (*BuyOrderResult, error)

func BuyMarketOrder

func BuyMarketOrder(pair string, amount float64) (*BuyOrderResult, error)

type ErrorResult

type ErrorResult struct {
	Status string `json:"status,string"`
	Reason string `json:"reason,string"`
	Code   string `json:"code,string"`
}

type Event

type Event struct {
	Event   string      `json:"event"`
	Channel string      `json:"channel"`
	Data    interface{} `json:"data"`
}

type OpenOrder

type OpenOrder struct {
	Id           int64   `json:"id,string"`
	DateTime     string  `json:"datetime"`
	Type         int     `json:"type,string"`
	Price        float64 `json:"price,string"`
	Amount       float64 `json:"amount,string"`
	CurrencyPair string  `json:"currency_pair"`
}

type OrderBookItem

type OrderBookItem struct {
	Price  float64
	Amount float64
}

func (*OrderBookItem) UnmarshalJSON

func (o *OrderBookItem) UnmarshalJSON(data []byte) error

UnmarshalJSON takes a json array and converts it into an OrderBookItem.

type OrderBookResult

type OrderBookResult struct {
	Timestamp string          `json:"timestamp"`
	Bids      []OrderBookItem `json:"bids"`
	Asks      []OrderBookItem `json:"asks"`
}

func OrderBook

func OrderBook(pair string) (*OrderBookResult, error)

type OrderStatusResult

type OrderStatusResult struct {
	Status string `json:"status"`
}

func OrderStatus

func OrderStatus(orderId int64) (*OrderStatusResult, error)

type SellOrderResult

type SellOrderResult struct {
	Id       int64   `json:"id,string"`
	DateTime string  `json:"datetime"`
	Type     int     `json:"type,string"`
	Price    float64 `json:"price,string"`
	Amount   float64 `json:"amount,string"`
}

func SellLimitOrder

func SellLimitOrder(pair string, amount float64, price float64, amountPrecision, pricePrecision int) (*SellOrderResult, error)

func SellMarketOrder

func SellMarketOrder(pair string, amount float64) (*SellOrderResult, error)

type TickerResult

type TickerResult struct {
	Last      float64 `json:"last,string"`
	High      float64 `json:"high,string"`
	Low       float64 `json:"low,string"`
	Vwap      float64 `json:"vwap,string"`
	Volume    float64 `json:"volume,string"`
	Bid       float64 `json:"bid,string"`
	Ask       float64 `json:"ask,string"`
	Timestamp string  `json:"timestamp"`
	Open      float64 `json:"open,string"`
}

func Ticker

func Ticker(pair string) (*TickerResult, error)

type UserTransactionsResult

type UserTransactionsResult struct {
	Fee      float64 `json:"fee"`
	Id       int64   `json:"id"`
	OrderId  int64   `json:"order_id"`
	Usd      float64 `json:"usd"`
	Eur      float64 `json:"eur"`
	Btc      float64 `json:"btc"`
	Xrp      float64 `json:"xrp"`
	Eth      float64 `json:"eth"`
	Ltc      float64 `json:"ltc"`
	BtcUsd   float64 `json:"btc_usd"`
	BtcEur   float64 `json:"btc_eur"`
	EthUsd   float64 `json:"eth_usd"`
	EthEur   float64 `json:"eth_eur"`
	XrpUsd   float64 `json:"xrp_usd"`
	XrpEur   float64 `json:"xrp_eur"`
	LtcUsd   float64 `json:"ltc_usd"`
	LtcEur   float64 `json:"ltc_eur"`
	DateTime string  `json:"datetime"`
	Type     int     `json:"type,string"`
}

func (*UserTransactionsResult) UnmarshalJSON

func (u *UserTransactionsResult) UnmarshalJSON(data []byte) error

bitstamp API is not consistant in its way of sending numeric values of the same key, sometimes they're strings, sometimes ints, sometimes floats

type WebSocket

type WebSocket struct {
	Stream chan *Event
	Errors chan error
	// contains filtered or unexported fields
}

func NewWebSocket

func NewWebSocket(t time.Duration) (*WebSocket, error)

func (*WebSocket) Close

func (s *WebSocket) Close()

func (*WebSocket) Ping

func (s *WebSocket) Ping()

func (*WebSocket) Pong

func (s *WebSocket) Pong()

func (*WebSocket) SendTextMessage

func (s *WebSocket) SendTextMessage(message []byte)

func (*WebSocket) Subscribe

func (s *WebSocket) Subscribe(channel string)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL