bitstamp

package module
v0.0.0-...-bd7caf9 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2018 License: MIT Imports: 13 Imported by: 0

README

bitstamp-go

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

Example Usage

package main

import (
	"fmt"
	"log"

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

func handleEvent(e *bitstamp.Event) {
	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
	order, err := bitstamp.BuyLimitOrder(0.5, 600.00)
	if err != nil {
		log.Printf("Error placing buy order: %s", err)
		return
	}

	// check order
	var orderRes *bitstamp.OrderTransactionsResult					
	orderRes, err = bitstamp.OrderTransactions(order.Id)
	if err != nil {
		log.Printf("Error checking status of buy order #%d %s. Retrying...", order.Id, err)
		return
	}

	if orderRes.TotalBtcAmount != 0.5 {
		log.Printf("BUY order #%d unsuccessful", order.Id)
		return
	}

	// 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)

			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

This section is empty.

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"`
}

type AccountTransactionResult

type AccountTransactionResult struct {
	DateTime string              `json:"datetime"`
	Id       int64               `json:"id"`
	Type     UserTransactionType `json:"type"`
	Usd      float64             `json:"usd"`
	Eur      float64             `json:"eur"`
	Btc      float64             `json:"btc"`
	Xrp      float64             `json:"xrp"`
	Ltc      float64             `json:"ltc"`
	Eth      float64             `json:"eth"`
	BtcUsd   float64             `json:"btc_usd"`
	UsdBtc   float64             `json:"usd_btc"`
	Fee      float64             `json:"fee"`
	OrderId  int64               `json:"order_id"`
}

type Bitstamp

type Bitstamp struct {
	// contains filtered or unexported fields
}

bitstamp holds credentials used to authorize private http calls

func NewBitstamp

func NewBitstamp(clientId, key, secret string) *Bitstamp

func (Bitstamp) AccountBalance

func (b Bitstamp) AccountBalance() (*AccountBalanceResult, error)

func (Bitstamp) AccountTransactions

func (b Bitstamp) AccountTransactions() ([]AccountTransactionResult, error)

func (Bitstamp) BuyLimitOrder

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

func (Bitstamp) BuyMarketOrder

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

func (Bitstamp) CancelOrder

func (b Bitstamp) CancelOrder(orderId int64)

func (Bitstamp) OpenOrders

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

func (Bitstamp) OrderBook

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

func (Bitstamp) SellLimitOrder

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

func (Bitstamp) SellMarketOrder

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

func (Bitstamp) Ticker

func (b Bitstamp) Ticker(pair string) (*TickerResult, 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"`
}

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"`
	Data  interface{} `json:"data"`
}

type Float

type Float float64

func (*Float) UnmarshalJSON

func (f *Float) UnmarshalJSON(b []byte) error

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"`
}

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"`
}

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"`
}

type UserTransactionType

type UserTransactionType int8
const (
	UserDeposit UserTransactionType = iota
	UserWithdrawal
	UserMarketTrade
	UserSubAccountTransfer
)

func (UserTransactionType) String

func (t UserTransactionType) String() string

func (*UserTransactionType) UnmarshalJSON

func (t *UserTransactionType) UnmarshalJSON(b []byte) error

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