Class: MtGox::Client

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

Constant Summary collapse

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

Constants included from Value

Value::INT_MULTIPLIERS

Instance Method Summary collapse

Methods included from Value

#floatify, #intify, #value_bitcoin, #value_currency

Methods included from Request

#get, #post

Instance Method Details

#addorder!(type, amount, price) ⇒ String

Create a new order

Examples:

# Sell one bitcoin for $123
MtGox.addorder! :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)

    the bid/ask price in USD

Returns:

  • (String)

    order ID for the order, can be inspected using order_result

Requires Authentication:

  • true



207
208
209
# File 'lib/mtgox/client.rb', line 207

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

#addressString

Fetch a deposit address

Examples:

MtGox.address

Returns:

  • (String)

Requires Authentication:

  • true



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

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



79
80
81
# File 'lib/mtgox/client.rb', line 79

def asks
  offers[:asks]
end

#balanceArray<MtGox::Balance>

Fetch your current balance

Examples:

MtGox.balance

Returns:

Requires Authentication:

  • true



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

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



89
90
91
# File 'lib/mtgox/client.rb', line 89

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)

    the bid price in US dollars

Returns:

  • (String)

    order ID for the buy, can be inspected using order_result

Requires Authentication:

  • true



180
181
182
# File 'lib/mtgox/client.rb', line 180

def buy!(amount, price)
  addorder!(: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



157
158
159
# File 'lib/mtgox/client.rb', line 157

def buys
  orders[:buys]
end

#cancel(oid) ⇒ Hash #cancel(order) ⇒ Hash

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



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/mtgox/client.rb', line 228

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 Faraday::Error::ResourceNotFound, {status: 404, headers: {}, body: 'Order not found.'}
  end
end

#max_bidMtGox::MinBid

Fetch the highest priced bid

Examples:

MtGox.max_bid

Returns:

  • (MtGox::MinBid)

Requires Authentication:

  • false



112
113
114
115
116
117
# File 'lib/mtgox/client.rb', line 112

def max_bid
  max_bid = bids.first
  MaxBid.instance.price = max_bid.price
  MaxBid.instance.amount = max_bid.amount
  MaxBid.instance
end

#min_askMtGox::MinAsk

Fetch the lowest priced ask

Examples:

MtGox.min_ask

Returns:

Requires Authentication:

  • false



99
100
101
102
103
104
# File 'lib/mtgox/client.rb', line 99

def min_ask
  min_ask = asks.first
  MinAsk.instance.price = min_ask.price
  MinAsk.instance.amount = min_ask.amount
  MinAsk.instance
end

#offersHash

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

Examples:

MtGox.offers

Returns:

  • (Hash)

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

Requires Authentication:

  • false



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/mtgox/client.rb', line 58

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(ask)
  end
  bids = offers['bids'].sort_by do |bid|
    -bid['price_int'].to_i
  end.map! do |bid|
    Bid.new(bid)
  end
  {asks: asks, bids: bids}
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



147
148
149
# File 'lib/mtgox/client.rb', line 147

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)

    the ask price in US dollars

Returns:

  • (String)

    order ID for the sell, can be inspected using order_result

Requires Authentication:

  • true



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

def sell!(amount, price)
  addorder!(: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



167
168
169
# File 'lib/mtgox/client.rb', line 167

def sells
  orders[:sells]
end

#tickerMtGox::Ticker

Fetch the latest ticker data

Examples:

MtGox.ticker

Returns:

Requires Authentication:

  • false



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/mtgox/client.rb', line 39

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

#tradesArray<MtGox::Trade>

Fetch recent trades

Examples:

MtGox.trades

Returns:

  • (Array<MtGox::Trade>)

    an array of trades, sorted in chronological order

Requires Authentication:

  • false



125
126
127
128
129
# File 'lib/mtgox/client.rb', line 125

def trades
  get('/api/1/BTCUSD/trades/fetch').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



253
254
255
256
257
258
259
260
# File 'lib/mtgox/client.rb', line 253

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