Class: MtGox::Client

Inherits:
Object
  • Object
show all
Includes:
Configuration, Connection, Request, Value
Defined in:
lib/mtgox/client.rb

Constant Summary collapse

ORDER_TYPES =
{sell: "ask", buy: "bid"}

Constants included from Configuration

MtGox::Configuration::DEFAULT_COMMISSION, MtGox::Configuration::DEFAULT_NONCE_TYPE, MtGox::Configuration::VALID_OPTIONS_KEYS

Constants included from Value

Value::INT_MULTIPLIERS

Instance Method Summary collapse

Methods included from Configuration

#configure, extended, #nonce_type, #reset

Methods included from Value

#decimalify, #intify, #value_bitcoin, #value_currency

Methods included from Request

#get, #post

Constructor Details

#initializeClient

Returns a new instance of Client.



27
28
29
# File 'lib/mtgox/client.rb', line 27

def initialize
  reset
end

Instance Method Details

#addressString

Fetch a deposit address

Examples:

MtGox.address

Returns:

  • (String)

Requires Authentication:

  • true



36
37
38
# File 'lib/mtgox/client.rb', line 36

def address
  post('/api/1/generic/bitcoin/address')['addr']
end

#asksArray<MtGox::Ask>

Fetch open asks

Examples:

MtGox.asks

Returns:

  • (Array<MtGox::Ask>)

    an array of open asks, sorted in price ascending order

Requires Authentication:

  • false



108
109
110
# File 'lib/mtgox/client.rb', line 108

def asks
  offers[:asks]
end

#balanceArray<MtGox::Balance>

Fetch your current balance

Examples:

MtGox.balance

Returns:

Requires Authentication:

  • true



162
163
164
# File 'lib/mtgox/client.rb', line 162

def balance
  parse_balance(post('/api/1/generic/info'))
end

#bidsArray<MtGox::Bid>

Fetch open bids

Examples:

MtGox.bids

Returns:

  • (Array<MtGox::Bid>)

    an array of open bids, sorted in price descending order

Requires Authentication:

  • false



118
119
120
# File 'lib/mtgox/client.rb', line 118

def bids
  offers[:bids]
end

#buy!(amount, price) ⇒ String

Place a limit order to buy BTC

Examples:

# Buy one bitcoin for $0.011
MtGox.buy! 1.0, 0.011

Parameters:

  • amount (Numeric)

    the number of bitcoins to purchase

  • price (Numeric or Symbol)

    the bid price in US dollars, or :market if placing a market order

Returns:

  • (String)

    order ID for the buy, can be inspected using order_result

Requires Authentication:

  • true



205
206
207
# File 'lib/mtgox/client.rb', line 205

def buy!(amount, price)
  add_order!(:buy, amount, price)
end

#buysArray<MtGox::Buy>

Fetch your open buys

Examples:

MtGox.buys

Returns:

  • (Array<MtGox::Buy>)

    an array of your open bids, sorted by date

Requires Authentication:

  • true



182
183
184
# File 'lib/mtgox/client.rb', line 182

def buys
  orders[:buys]
end

#cancel(oid) ⇒ Hash #cancel(order) ⇒ Hash Also known as: cancel_order, cancelorder

Cancel an open order

Overloads:

  • #cancel(oid) ⇒ Hash

    Returns with keys :buys and :sells, which contain arrays as described in #buys and MtGox::Clients#sells.

    Examples:

    my_order = MtGox.orders.first
    MtGox.cancel my_order.oid
    MtGox.cancel 1234567890

    Parameters:

    • oid (String)

      an order ID

    Returns:

    • (Hash)

      with keys :buys and :sells, which contain arrays as described in #buys and MtGox::Clients#sells

  • #cancel(order) ⇒ Hash

    Returns with keys :buys and :sells, which contain arrays as described in #buys and MtGox::Clients#sells.

    Examples:

    my_order = MtGox.orders.first
    MtGox.cancel my_order
    MtGox.cancel {'oid' => '1234567890'}

    Parameters:

    • order (Hash)

      a hash-like object, containing at least a key oid - the order ID of the transaction to cancel

    Returns:

    • (Hash)

      with keys :buys and :sells, which contain arrays as described in #buys and MtGox::Clients#sells

Requires Authentication:

  • true



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/mtgox/client.rb', line 259

def cancel(args)
  if args.is_a?(Hash)
    args = args['oid']
  end

  orders = post('/api/1/generic/orders')
  order = orders.find{|order| order['oid'] == args.to_s}
  if order
    res = post('/api/1/BTCUSD/order/cancel', oid: order['oid'])
    orders.delete_if{|o| o['oid'] == res['oid']}
    parse_orders(orders)
  else
    raise MtGox::OrderNotFoundError
  end
end

#idkeyString

Get an idKey for subscribing to private channels in WebSocket API

Examples:

MtGox.idkey

Returns:

  • (String)

    the idKey to use in your WebSocket client

Requires Authentication:

  • true



45
46
47
# File 'lib/mtgox/client.rb', line 45

def idkey
  post('/api/1/generic/idkey')
end

#lagMtGox::Lag Also known as: order_lag, orderlag

Fetch the latest lag data

Examples:

MtGox.lag

Returns:

Requires Authentication:

  • false



74
75
76
77
# File 'lib/mtgox/client.rb', line 74

def lag
  lag = get('/api/1/generic/order/lag')
  Lag.new(lag['lag'], lag['lag_secs'], lag['lag_text'], lag['length'])
end

#max_bidMtGox::MinBid

Fetch the highest priced bid

Examples:

MtGox.max_bid

Returns:

  • (MtGox::MinBid)

Requires Authentication:

  • false



138
139
140
# File 'lib/mtgox/client.rb', line 138

def max_bid
  bids.first
end

#min_askMtGox::MinAsk

Fetch the lowest priced ask

Examples:

MtGox.min_ask

Returns:

Requires Authentication:

  • false



128
129
130
# File 'lib/mtgox/client.rb', line 128

def min_ask
  asks.first
end

#offersHash

Fetch both bids and asks in one call, for network efficiency

Examples:

MtGox.offers

Returns:

  • (Hash)

    with keys :asks and :bids, which contain arrays as described in #asks and MtGox::Clients#bids

Requires Authentication:

  • false



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/mtgox/client.rb', line 87

def offers
  offers = get('/api/1/BTCUSD/depth/fetch')
  asks = offers['asks'].sort_by do |ask|
    ask['price_int'].to_i
  end.map! do |ask|
    Ask.new(self, ask)
  end
  bids = offers['bids'].sort_by do |bid|
    -bid['price_int'].to_i
  end.map! do |bid|
    Bid.new(self, bid)
  end
  {asks: asks, bids: bids}
end

#order!(type, amount, price) ⇒ String Also known as: add_order!, addorder!

Create a new order

Examples:

# Sell one bitcoin for $123
MtGox.add_order! :sell, 1.0, 123.0

Parameters:

  • type (String)

    the type of order to create, either "buy" or "sell"

  • amount (Numberic)

    the number of bitcoins to buy/sell

  • price (Numeric or Symbol)

    the bid/ask price in USD, or :market if placing a market order

Returns:

  • (String)

    order ID for the order, can be inspected using order_result

Requires Authentication:

  • true



232
233
234
235
236
237
238
# File 'lib/mtgox/client.rb', line 232

def order!(type, amount, price)
  order = {type: order_type(type), amount_int: intify(amount,:btc)}
  if price != :market
      order[:price_int] = intify(price, :usd)
  end
  post('/api/1/BTCUSD/order/add', order)
end

#order_result(offer_type, order_id) ⇒ OrderResult

Fetch information about a particular transaction

Parameters:

  • offer_type (String)

    'bid' or 'ask'

  • order_id (String)

    the order id

Returns:

Requires Authentication:

  • true



301
302
303
# File 'lib/mtgox/client.rb', line 301

def order_result(offer_type, order_id)
  OrderResult.new(post('/api/1/generic/order/result', {type: offer_type, order: order_id}))
end

#ordersHash

Fetch your open orders, both buys and sells, for network efficiency

Examples:

MtGox.orders

Returns:

  • (Hash)

    with keys :buys and :sells, which contain arrays as described in #buys and MtGox::Clients#sells

Requires Authentication:

  • true



172
173
174
# File 'lib/mtgox/client.rb', line 172

def orders
  parse_orders(post('/api/1/generic/orders'))
end

#sell!(amount, price) ⇒ String

Place a limit order to sell BTC

Examples:

# Sell one bitcoin for $100
MtGox.sell! 1.0, 100.0

Parameters:

  • amount (Numeric)

    the number of bitcoins to sell

  • price (Numeric or Symbol)

    the ask price in US dollars, or :market if placing a market order

Returns:

  • (String)

    order ID for the sell, can be inspected using order_result

Requires Authentication:

  • true



218
219
220
# File 'lib/mtgox/client.rb', line 218

def sell!(amount, price)
  add_order!(:sell, amount, price)
end

#sellsArray<MtGox::Sell>

Fetch your open sells

Examples:

MtGox.sells

Returns:

  • (Array<MtGox::Sell>)

    an array of your open asks, sorted by date

Requires Authentication:

  • true



192
193
194
# File 'lib/mtgox/client.rb', line 192

def sells
  orders[:sells]
end

#tickerMtGox::Ticker

Fetch the latest ticker data

Examples:

MtGox.ticker

Returns:

Requires Authentication:

  • false



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/mtgox/client.rb', line 55

def ticker
  ticker = get('/api/1/BTCUSD/ticker')
  Ticker.instance.buy    = value_currency ticker['buy']
  Ticker.instance.high   = value_currency ticker['high']
  Ticker.instance.price  = value_currency ticker['last_all']
  Ticker.instance.low    = value_currency ticker['low']
  Ticker.instance.sell   = value_currency ticker['sell']
  Ticker.instance.volume = value_bitcoin  ticker['vol']
  Ticker.instance.vwap   = value_currency ticker['vwap']
  Ticker.instance.avg    = value_currency ticker['avg']
  Ticker.instance
end

#trades(opts = {}) ⇒ Array<MtGox::Trade>

Fetch recent trades

Examples:

MtGox.trades
MtGox.trades :since => 12341234

Returns:

  • (Array<MtGox::Trade>)

    an array of trades, sorted in chronological order

Requires Authentication:

  • false



149
150
151
152
153
154
# File 'lib/mtgox/client.rb', line 149

def trades(opts={})
  get('/api/1/BTCUSD/trades/fetch', opts).
    sort_by{|trade| trade['date']}.map do |trade|
    Trade.new(trade)
  end
end

#withdraw!(amount, address) ⇒ String

Transfer bitcoins from your Mt. Gox account into another account

Examples:

# Withdraw 1 BTC from your account
MtGox.withdraw! 1.0, '1KxSo9bGBfPVFEtWNLpnUK1bfLNNT4q31L'

Parameters:

  • amount (Numeric)

    the number of bitcoins to withdraw

  • address (String)

    the bitcoin address to send to

Returns:

  • (String)

    Completed Transaction ID

Requires Authentication:

  • true



286
287
288
289
290
291
292
293
# File 'lib/mtgox/client.rb', line 286

def withdraw!(amount, address)
  if amount >= 1000
    raise FilthyRichError,
    "#withdraw! take bitcoin amount as parameter (you are trying to withdraw #{amount} BTC"
  else
    post('/api/1/generic/bitcoin/send_simple', {amount_int: intify(amount, :btc), address: address})['trx']
  end
end