Class: MtGox::Client

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

Constant Summary collapse

ORDER_TYPES =
{:sell => 1, :buy => 2}
@@ticker =
{}

Instance Method Summary collapse

Methods included from Request

#get, #post

Instance Method Details

#addressString

Fetch a deposit address

Examples:

MtGox.address

Returns:

  • (String)

Requires Authentication:

  • true



20
21
22
# File 'lib/mtgox/client.rb', line 20

def address
  post('/api/0/btcAddress.php')['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



72
73
74
# File 'lib/mtgox/client.rb', line 72

def asks
  offers[:asks]
end

#balanceArray<MtGox::Balance>

Fetch your current balance

Examples:

MtGox.balance

Returns:

  • (Array<MtGox::Balance>)

Requires Authentication:

  • true



126
127
128
129
130
131
132
# File 'lib/mtgox/client.rb', line 126

def balance
  parse_balance(post('/api/0/getFunds.php', {}))
  #info = post('/code/info.php', pass_params)
  #info['Wallets'].values.map { |v| v['Balance'] }.map do |balance_info|
  #  Balance.new(balance_info['currency'], balance_info['value'])
  #end
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



82
83
84
# File 'lib/mtgox/client.rb', line 82

def bids
  offers[:bids]
end

#buy!(amount, price) ⇒ Hash

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:

  • (Hash)

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

Requires Authentication:

  • true



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

def buy!(amount, price)
  parse_orders(post('/api/0/buyBTC.php', {:amount => amount, :price => price})['orders'])
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



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

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', 'type' => 2}

    Parameters:

    • order (Hash)

      a hash-like object, with keys oid - the order ID of the transaction to cancel and type - the type of order to cancel (1 for sell or 2 for buy)

    Returns:

    • (Hash)

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

Requires Authentication:

  • true



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/mtgox/client.rb', line 207

def cancel(args)
  if args.is_a?(Hash)
    order = args.delete_if { |k, v| !['oid', 'type'].include?(k.to_s) }
    parse_orders(post('/api/0/cancelOrder.php', order)['orders'])
  else
    orders = post('/api/0/getOrders.php', {})['orders']
    order = orders.find { |order| order['oid'] == args.to_s }
    if order
      order = order.delete_if { |k, v| !['oid', 'type'].include?(k.to_s) }
      parse_orders(post('/api/0/cancelOrder.php', order)['orders'])
    else
      raise Faraday::Error::ResourceNotFound, {:status => 404, :headers => {}, :body => 'Order not found.'}
    end
  end
end

#max_bidMtGox::MinBid

Fetch the highest priced bid

Examples:

MtGox.max_bid

Returns:

  • (MtGox::MinBid)

Requires Authentication:

  • false



103
104
105
106
# File 'lib/mtgox/client.rb', line 103

def max_bid
  MaxBid.instance.set_attributes bids.first.attributes
  MaxBid.instance
end

#min_askMtGox::MinAsk

Fetch the lowest priced ask

Examples:

MtGox.min_ask

Returns:

  • (MtGox::MinAsk)

Requires Authentication:

  • false



92
93
94
95
# File 'lib/mtgox/client.rb', line 92

def min_ask
  MinAsk.instance.set_attributes asks.first.attributes
  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



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/mtgox/client.rb', line 51

def offers
  offers = get('/api/0/data/getDepth.php')
  asks = offers['asks'].sort_by do |ask|
    ask[0].to_f
  end.map! do |ask|
    Ask.new(*ask)
  end
  bids = offers['bids'].sort_by do |bid|
    -bid[0].to_f
  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



140
141
142
# File 'lib/mtgox/client.rb', line 140

def orders
  parse_orders(post('/api/0/getOrders.php', {})['orders'])
end

#sell!(amount, price) ⇒ Hash

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:

  • (Hash)

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

Requires Authentication:

  • true



186
187
188
# File 'lib/mtgox/client.rb', line 186

def sell!(amount, price)
  parse_orders(post('/api/0/sellBTC.php', {:amount => amount, :price => price})['orders'])
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



160
161
162
# File 'lib/mtgox/client.rb', line 160

def sells
  orders[:sells]
end

#ticker(currency = nil) ⇒ MtGox::Ticker

Fetch the latest ticker data

Examples:

MtGox.ticker
MtGox.ticker('EUR')

Returns:

  • (MtGox::Ticker)

Requires Authentication:

  • false



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/mtgox/client.rb', line 32

def ticker(currency=nil)
  if currency
    ticker = get("/api/1/BTC#{currency}/public/ticker")['return']
    @@ticker[currency] ||= MultiTicker.new :currency => currency
    @@ticker[currency].set_attributes ticker
    @@ticker[currency]
  else
    ticker = get('/api/0/data/ticker.php')['ticker']
    Ticker.instance.set_attributes ticker
    Ticker.instance
  end
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



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

def trades
  get('/api/0/data/getTrades.php').sort_by { |trade| trade['date'] }.map do |trade|
    Trade.new(trade)
  end
end

#withdraw!(amount, btca) ⇒ Array<MtGox::Balance>

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

  • btca (String)

    the bitcoin address to send to

Returns:

  • (Array<MtGox::Balance>)

Requires Authentication:

  • true



232
233
234
# File 'lib/mtgox/client.rb', line 232

def withdraw!(amount, btca)
  parse_balance(post('/api/0/withdraw.php', {:group1 => 'BTC', :amount => amount, :btca => btca}))
end