mtgox

package module
v0.0.0-...-49ffef8 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2013 License: BSD-2-Clause Imports: 16 Imported by: 0

README

MtGox API Client

Build Status GoDoc

Mt.Gox Streaming API implementation in Go.

Documentation

Usage

Basic usage requires an API token from your Mt.Gox account.

client, err := mtgox.New( "KEY", "SECRET", "AUD", "USD")
if err != nil {
  // Handle connection error
}

// Start message receive routine
client.Start()

for {
  select {
  case orders := <-client.Orders:
    fmt.Printf("Orders: %s\n", PrettyPrintJson(orders))

  case trade := <-client.Trades:
    fmt.Printf("Trade: %s\n", PrettyPrintJson(trade))

  case tick := <-client.Ticker:
    fmt.Printf("Tick: %s\n", PrettyPrintJson(tick))

  case depth := <-client.Depth:
    fmt.Printf("Depth: %s\n", PrettyPrintJson(depth))
  }
}

Features

  • Receives streaming events for depth, trade, ticker
  • All API methods to placing orders (in the works)
  • All Account and Wallet API methods (in the works)

Documentation

Overview

Package mtgox implements a complete MtGox streaming API client.

Overview

The Client type represents a connection to the Mt.Gox streaming API using WebSockets (Powered by the modern Gorilla WebSocket package). After creating a Client instance, you should call client.Start to begin handling received messages.

client, err := mtgox.New( "KEY", "SECRET", "Currencies"...)
if err != nil {
  // Handle connection error
}

// Start message receive routine
client.Start()

From here all messages will be sent to their respective channels.

Index

Constants

View Source
const (

	// BitcoinDivision represents the current integer division of 1 bitcoin.
	BitcoinDivision = 1e8
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	Ticker chan *TickerPayload
	Info   chan *Info
	Depth  chan *DepthPayload
	Trades chan *TradePayload
	Orders chan []Order
	// contains filtered or unexported fields
}

Client represents the public type for interacing with the MtGox streaming API.

func New

func New(key, secret string, currencies ...string) (*Client, error)

New constructs a new instance of Client, returning an error

func NewWithConnection

func NewWithConnection(key, secret string, conn *websocket.Conn) (g *Client, err error)

NewWithConnection constructs a new client using an existing connection, useful for testing

func (*Client) CancelOrder

func (g *Client) CancelOrder(o Order) (string, error)

CancelOrder will cancel the given order if already added to the market

func (*Client) Close

func (g *Client) Close()

Close disconnects the client from the streaming API.

func (*Client) Conn

func (g *Client) Conn() *websocket.Conn

Conn returns the raw websocket connection to Mt.Gox (This may be removed in the future)

func (*Client) GetHistory

func (g *Client) GetHistory(walletID string) []Order

GetHistory returns the histroy for the given wallet ID

func (*Client) PlaceOrder

func (g *Client) PlaceOrder(o Order) (string, error)

PlaceOrder adds the given Order to the market, returning the new ID

func (*Client) QueryOrder

func (g *Client) QueryOrder(orderID string) (Order, error)

QueryOrder returns updated information for the given order

func (*Client) QuoteOrder

func (g *Client) QuoteOrder(o Order) (string, error)

QuoteOrder retrieves a quote for the given Order

func (*Client) RequestInfo

func (g *Client) RequestInfo() *Info

RequestInfo dispatches a request for `private/info`, returning an info payload or timing out after 10 seconds.

func (*Client) RequestOrderLag

func (g *Client) RequestOrderLag() (time.Duration, error)

RequestOrderLag returns the lag time for executing orders This method will block for up to 5 seconds before timing out

func (*Client) RequestOrders

func (g *Client) RequestOrders() (<-chan []Order, error)

RequestOrders fetches the open orders for your account

func (*Client) SetErrorHandler

func (c *Client) SetErrorHandler(handlerFunc ErrorHandlerFunc)

func (*Client) Start

func (g *Client) Start()

Start begins the internal routines for processing messages and errors

type Config

type Config struct {
	Currencies []string
	Key        string
	Secret     string
	SecureConn bool
}

Config represents a configuration type to be used when configuring the Client.

type CurrencyValue

type CurrencyValue struct {
	Value        int64
	Display      string
	DisplayShort string
	Currency     string
}

func (*CurrencyValue) AsFloat

func (c *CurrencyValue) AsFloat() float64

func (*CurrencyValue) UnmarshalJSON

func (tv *CurrencyValue) UnmarshalJSON(data []byte) error

type Depth

type Depth struct {
	// Ask or Bid - Corresponds to the Order type
	OrderType string

	// The price at which volume change happened
	Price int64

	// The volume change
	Volume int64

	// BTC
	Instrument string

	// The currency affected
	Currency string

	// Total volume at this price, after applying the depth update, can be used as a starting point before applying subsequent updates.
	TotalVolume int64

	// When the change happened
	Timestamp time.Time
}

Depth represents market order depth

func (*Depth) UnmarshalJSON

func (d *Depth) UnmarshalJSON(data []byte) error

UnmarshalJSON handles unmarshalling custom payload from Mt.Gox

type DepthPayload

type DepthPayload struct {
	StreamHeader
	Depth Depth `json:"depth"`
}

DepthPayload represents a payload structure for market depth

type EpochTime

type EpochTime struct {
	time.Time
}

func (*EpochTime) UnmarshalJSON

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

type ErrorHandlerFunc

type ErrorHandlerFunc func(error)

ErrorHandlerFunc is a function type to use as an error callback.

type Info

type Info struct {
	Created       SimpleTime `json:",string"`
	Id            string
	Index         string
	Language      string
	LastLogin     SimpleTime `json:"last_login,string"`
	Link          string
	Login         string
	Montly_Volume Value
	Trade_fee     float64
	Rights        []string
	Wallets       map[string]Wallet
}

type Order

type Order struct {
	// OrderID is the unique order identifier
	OrderID string `json:"oid,string"`

	// Currency represents the external fiat currency
	Currency string `json:"currency,string"`

	// Instrument is the object being traded
	Instrument string `json:"item,string"`

	// OrderType in the market, one of `bid` or `ask`
	OrderType string `json:"type,string"`

	// Amount is the requested trade amount in BTC
	Amount float64 `json:"amount"`

	// EffectiveAmount is the actual amount traded in BTC
	EffectiveAmount float64 `json:"effective_amount"`

	// InvalidAmount is the amount which could not traded in BTC
	InvalidAmount float64 `json:"invalid_amount"`

	// Price is the amount paid in fiat currency
	Price float64 `json:"price"`

	// Status is the current status of the order
	Status string `json:"status,string"`

	// Timestamp of the order taking place
	Timestamp time.Time `json:"date,string"`

	// Priority is a unique microsecond timestamp of the order
	// (Not sure what the actual use of this is)
	Priority uint64 `json:"priority,string"`
}

Order represents a market order from your account.

func (*Order) UnmarshalJSON

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

UnmarshalJSON is a custom unmarshaller for Mt.Gox order payloads

type Rate

type Rate struct {
	To   string
	From string
	Rate float64
}

type ResultHeader

type ResultHeader struct {
	ID string `json:"id"`
	Op string `json:"op"`
}

ResultHeader defines a simple header structure for partially unmarshalling replies from Mt.Gox to determine their type

type ResultPayload

type ResultPayload struct {
	ResultHeader
	Result json.RawMessage `json:"result"`
}

ResultPayload defines a structure for unmarshalling complete replies with unknown response data formats to be parsed later

type SimpleTime

type SimpleTime struct {
	time.Time
}

func (*SimpleTime) UnmarshalJSON

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

type StreamHeader

type StreamHeader struct {
	Channel     string `json:"channel"`
	ChannelName string `json:"channel_name"`
	Op          string `json:"op"`
	Origin      string `json:"origin"`
	Private     string `json:"private"`
}

StreamHeader represents the header of a payload message from MtGox before being parsed.

type Ticker

type Ticker struct {
	High       CurrencyValue `json:"high"`
	Low        CurrencyValue `json:"low"`
	Average    CurrencyValue `json:"avg"`
	Vwap       CurrencyValue `json:"vwap"`
	Volume     CurrencyValue `json:"vol"`
	LastLocal  CurrencyValue `json:"last_local"`
	LastOrigin CurrencyValue `json:"last_orig"`
	LastAll    CurrencyValue `json:"last_all"`
	Last       CurrencyValue `json:"last"`
	Buy        CurrencyValue `json:"buy"`
	Sell       CurrencyValue `json:"sell"`
	Instrument string        `json:"item"`
	Timestamp  EpochTime     `json:"now,string"`
}

type TickerPayload

type TickerPayload struct {
	StreamHeader
	Ticker Ticker `json:"ticker"`
}

type Trade

type Trade struct {
	Type       string
	Tid        string
	Amount     float64
	Price      float64
	Instrument string
	Currency   string
	TradeType  string
	Primary    string
	Properties string
	Timestamp  time.Time
}

func (*Trade) UnmarshalJSON

func (t *Trade) UnmarshalJSON(data []byte) error

type TradePayload

type TradePayload struct {
	StreamHeader
	Trade Trade `json:"trade"`
}

type Value

type Value struct {
	Value        float64 `json:"value,string"`
	ValueInteger int64   `json:"value_int,string"`
	Display      string  `json:"display"`
	DisplayShort string  `json:"display_short"`
	Currency     string  `json:"currency"`
}

type Wallet

type Wallet struct {
	Balance            Value
	DailyWithdrawLimit Value
	MaxWithdraw        Value
	// Monthly_Withdraw_Limit nil
	OpenOrders Value
	Operations int64
}

Jump to

Keyboard shortcuts

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