mbc

package module
v3.1.5 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2021 License: Apache-2.0 Imports: 11 Imported by: 0

README

MercadoBitcoin API v3

Installation

$ go get -u github.com/lucaskatayama/mbc/v3

Usage

Initialize client

For public data:

package main 

import "github.com/lucaskatayama/mbc/v3"

func main() {
    client := mbc.New()
}

For private data, use your api key and secret

package main 

import "github.com/lucaskatayama/mbc/v3"

func main() {
    client := mbc.New(mbc.WithIdSecret("<id>", "<secret>"))
}
Public data
package main

import (
  "context"
  "github.com/lucaskatayama/mbc/v3"
)

func main() {
  client := mbc.New()
  // ticker
  ticker, err := client.Ticker(context.Background(), "BTC", "BRL")
  if err != nil {
	  log.Panic(err)
  }

  // orderbook
  ordebook, err := client.Orderbook(context.Background(), "BTC", "BRL")
  if err != nil {
    log.Panic(err)
  }

  // trades
  trades, err := client.Trades(context.Background(), "BTC", "BRL")
  if err != nil {
    log.Panic(err)
  }
}

API

  • Public API
    • orderbook Orderbook
    • ticker Ticker
    • trades Trades
  • Trade API
    • list_system_messages
    • get_account_info GetBalances/GetWithdrawalLimits
    • get_order
    • list_orders ListOrders
    • list_orderbook
    • place_buy_order
    • place_sell_order
    • place_postonly_buy_order
    • place_postonly_sell_order
    • place_market_buy_order
    • place_market_sell_order
    • cancel_order
    • get_withdrawal
    • withdraw_coin

CLI

[Under Construction]

References

Documentation

Overview

Package mbc is a simple client for MercadoBitcoin API V3.

MercadoBitcoin Golang API Client

References:

API Key and Secret

You should generate an API key and secret to use the client.

In the examples, replace <ID> and <SECRET> with the generated ones.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Balance

type Balance struct {
	Available string `json:"available,omitempty"`
	Total     string `json:"total,omitempty"`
}

Balance represents a balance

type Client

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

Client represents a MercadoBitcoin API client

func New

func New(opts ...ClientOpt) *Client

New creates a client for an user id/secret

Example

Creates a client for public and private requests

client := New()
client.Trades(context.Background(), "btc", "brl")
Output:

func (Client) Coins

func (c Client) Coins(ctx context.Context) ([]string, error)

Coins list coins

func (Client) GetBalances

func (c Client) GetBalances(ctx context.Context) (map[string]Balance, error)

GetBalances retrieves balances

func (Client) GetWithdrawalLimits

func (c Client) GetWithdrawalLimits(ctx context.Context) (map[string]WithdrawalLimit, error)

GetWithdrawalLimits retrieves withdrawal limits

func (*Client) ListOrders

func (c *Client) ListOrders(ctx context.Context, base string, quote string, opts ...ListOrdersOption) ([]Order, error)

ListOrders lists user orders

func (Client) Orderbook

func (c Client) Orderbook(ctx context.Context, base string, quote string, opts ...OrderbookOption) (Orderbook, error)

Orderbook retrieves ticker for base/quote

Example
client := New()
o, _ := client.Orderbook(context.Background(), "btc", "brl", WithLimit(1))
b, _ := json.MarshalIndent(o, "", "  ")
fmt.Println(string(b))
Output:

{
   "asks": [
      [241671.49, 0.01923905],
      [241831.86, 0.00205555]
   ],
   "bids": [
      [241664.97001, 0.05772251],
      [241664.95, 0.14321774]
   ],
   "timestamp":1628561698332025456
}

func (Client) Ticker

func (c Client) Ticker(ctx context.Context, base string, quote string) (Ticker, error)

Ticker retrieves ticker for base/quote

Example
client := New()
o, _ := client.Ticker(context.Background(), "btc", "brl")
b, _ := json.MarshalIndent(o, "", "  ")
fmt.Println(string(b))
Output:

{
 "ticker": {
   "high": "244900.00000000",
   "low": "227539.84013000",
   "vol": "158.76695397",
   "last": "241671.93011000",
   "buy": "241671.93011000",
   "sell": "242021.99998000",
   "open": "228450.00000000",
   "date": 1628563357
 }
}

func (Client) Trades

func (c Client) Trades(ctx context.Context, base string, quote string, opts ...TradesOption) ([]Trade, error)

Trades lists trades

Example
client := New()
o, _ := client.Trades(context.Background(), "btc", "brl", FromTid(90000))
b, _ := json.MarshalIndent(o, "", "  ")
fmt.Println(string(b))
Output:

[
 {
   "tid": 90001,
   "date": 1414274533,
   "type": "buy",
   "price": 919.7789,
   "amount": 0.41666409
 },
 {
   "tid": 90002,
   "date": 1414274533,
   "type": "buy",
   "price": 919.9913,
   "amount": 0.66149665
 }
]

type ClientOpt

type ClientOpt func(client *Client)

ClientOpt represents a client option

func WithIdSecret

func WithIdSecret(id, secret string) ClientOpt

WithIdSecret adds id/secret auth option to client

type ListOrdersOption

type ListOrdersOption func(values url.Values)

ListOrdersOption represents a request option

func FromID

func FromID(id string) ListOrdersOption

FromID adds ID lower limit

func FromTimestamp

func FromTimestamp(ts int64) ListOrdersOption

FromTimestamp adds lower limit for timestamp

func HasFills

func HasFills(has bool) ListOrdersOption

HasFills filter if order has fills

func ToID

func ToID(id string) ListOrdersOption

ToID adds ID upper limit

func ToTimestamp

func ToTimestamp(ts int64) ListOrdersOption

ToTimestamp adds lower limit for timestamp

func WithOrderTypes

func WithOrderTypes(t OrderType) ListOrdersOption

WithOrderTypes adds order type filter

func WithStatuses

func WithStatuses(statuses ...OrderStatus) ListOrdersOption

WithStatuses adds status filter

type Operation

type Operation struct {
	ID                int    `json:"operation_id"`
	Qty               string `json:"quantity"`
	Price             string `json:"price"`
	FeeRate           string `json:"fee_rate"`
	ExecutedTimestamp string `json:"executed_timestamp"`
}

Operation represents an operation

type Order

type Order struct {
	ID               int         `json:"order_id"`
	Pair             string      `json:"coin_pair"`
	OrderType        OrderType   `json:"order_type"`
	Status           int         `json:"status"`
	HasFills         bool        `json:"has_fills"`
	Qty              string      `json:"quantity"`
	LimitPrice       string      `json:"limit_price"`
	ExecutedQuantity string      `json:"executed_quantity"`
	ExecutedPriceAvg string      `json:"executed_price_avg"`
	Fee              string      `json:"fee"`
	CreatedAt        string      `json:"created_timestamp"`
	UpdatedAt        string      `json:"updated_timestamp"`
	Operations       []Operation `json:"operations"`
}

Order represents an order

type OrderStatus

type OrderStatus int

OrderStatus represents the order status

var (
	// Pending pending order status
	Pending OrderStatus = 1
	// Open open order status
	Open OrderStatus = 2
	// Cancelled cancelled order status
	Cancelled OrderStatus = 3
	// Filled filled order status
	Filled OrderStatus = 4
)

type OrderType

type OrderType int

OrderType represents the order side

var (
	// Buy order type
	Buy OrderType = 1
	// Sell order type
	Sell OrderType = 2
)

type Orderbook

type Orderbook struct {
	Asks      [][]float64 `json:"asks"`
	Bids      [][]float64 `json:"bids"`
	Timestamp int64       `json:"timestamp"`
}

Orderbook represents the orderbook

type OrderbookOption

type OrderbookOption func(values url.Values)

OrderbookOption represents orderbook request option

func WithLimit

func WithLimit(limit int) OrderbookOption

WithLimit adds limit to orderbook request

type StatusCode

type StatusCode int

StatusCode represents the response status

const (
	// ErrorCode general error code
	ErrorCode StatusCode = 201
	// SuccessCode success error code
	SuccessCode StatusCode = 100
)

type Ticker

type Ticker struct {
	High string `json:"high"`
	Low  string `json:"low"`
	Vol  string `json:"vol"`
	Last string `json:"last"`
	Buy  string `json:"buy"`
	Sell string `json:"sell"`
	Open string `json:"open"`
	Date int64  `json:"date"`
}

Ticker represents the ticker

type Trade

type Trade struct {
	Tid    int64   `json:"tid"`
	Date   int64   `json:"date"`
	Type   string  `json:"type"`
	Price  float64 `json:"price"`
	Amount float64 `json:"amount"`
}

Trade represents a trade

type TradesOption

type TradesOption func(values url.Values)

TradesOption represents a trades request option

func FromTid

func FromTid(tid int64) TradesOption

FromTid filters trades starting from given tid

type WithdrawalLimit

type WithdrawalLimit struct {
	Available string `json:"available,omitempty"`
	Total     string `json:"total,omitempty"`
}

WithdrawalLimit represents a withdrawal limit

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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