Class: MtGox::Client

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

Constant Summary collapse

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

Instance Method Summary collapse

Methods included from Request

#get, #post

Instance Method Details

#asksArray<Array<Numeric>>

Fetch open asks

Examples:

MtGox.asks[0, 3] #=> [[19.3898, 3.9], [19.4, 48.264], [19.409, 1]]

Returns:

  • (Array<Array<Numeric>>)

    in the form [price, quantity], sorted in price ascending order

Requires Authentication:

  • false



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

def asks
  offers['asks']
end

#balanceHashie::Rash

Fetch your balance

Examples:

MtGox.balance #=> <#Hashie::Rash btcs=3.7 usds=12>

Returns:

  • (Hashie::Rash)

    with keys btcs - amount of bitcoins in your account and usds - amount of US dollars in your account

Requires Authentication:

  • true



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

def balance
  post('/code/getFunds.php', pass_params)
end

#bidsArray<Array<Numeric>>

Fetch open bids

Examples:

MtGox.bids[0, 3] #=> [[19.3898, 77.42], [19.3, 3.02], [19.29, 82.378]]

Returns:

  • (Array<Array<Numeric>>)

    in the form [price, quantity], sorted in price descending order

Requires Authentication:

  • false



55
56
57
# File 'lib/mtgox/client.rb', line 55

def bids
  offers['bids']
end

#buy!(amount, price) ⇒ Array<Hashie::Rash>

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:

  • (Array<Hashie::Rash>)

Requires Authentication:

  • true



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

def buy!(amount, price)
  parse_orders(post('/code/buyBTC.php', pass_params.merge({:amount => amount, :price => price}))['orders'])
end

#buysArray<Hashie::Rash>

Fetch your open buys

Examples:

MtGox.buys[0, 3] #=> [<#Hashie::Rash amount=0.73 dark="0" date=2011-06-13 00:13:16 -0700 oid="929284" price=2 status=:active type=2>, <#Hashie::Rash amount=0.36 dark="0" date=2011-06-13 00:13:21 -0700 oid="929288" price=4 status=:active type=2>, <#Hashie::Rash amount=0.24 dark="0" date=2011-06-13 00:13:32 -0700 oid="929292" price=6 status=:active type=2>]

Returns:

  • (Array<Hashie::Rash>)

    an array of your open bids, sorted in price ascending order with the keys amount, dark, date, oid, price, status, and type

Requires Authentication:

  • true



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

def buys
  orders.select do |o|
    o['type'] == ORDER_TYPES[:buy]
  end
end

#cancel(oid) ⇒ Object #cancel(order) ⇒ Object

Cancel an open order

Overloads:

  • #cancel(oid) ⇒ Object

    Examples:

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

    Parameters:

    • oid (String)

      an order ID

  • #cancel(order) ⇒ Object

    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)

Requires Authentication:

  • true



160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/mtgox/client.rb', line 160

def cancel(args)
  if args.is_a?(Hash)
    order = args.delete_if{|k, v| !['oid', 'type'].include?(k.to_s)}
    post('/code/cancelOrder.php', pass_params.merge(order))
  else
    order = orders.select{|o| o['oid'] == args.to_s}.first
    if order
      order = order.delete_if{|k, v| !['oid', 'type'].include?(k.to_s)}
      post('/code/cancelOrder.php', pass_params.merge(order))
    else
      raise Faraday::Error::ResourceNotFound, {:status => 404, :headers => {}, :body => "Order not found."}
    end
  end
end

#offersHashie::Rash

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

Examples:

offers = MtGox.offers
offers.asks[0, 3] #=> [[19.3898, 3.9], [19.4, 48.264], [19.409, 1]]
offers.bids[0, 3] #=> [[19.3898, 77.42], [19.3, 3.02], [19.29, 82.378]]

Returns:

  • (Hashie::Rash)

    a hash with keys :asks and :bids, which contain arrays as described in #asks and #bids.

Requires Authentication:

  • false



32
33
34
35
36
37
# File 'lib/mtgox/client.rb', line 32

def offers
  offers = get('/code/data/getDepth.php')
  offers['asks'] = offers['asks'].sort_by{|a| a[0]}
  offers['bids'] = offers['bids'].sort_by{|b| b[0]}.reverse
  offers
end

#orders<Hashie::Rash>

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

Examples:

MtGox.orders[0, 3] #=> [<#Hashie::Rash amount=0.73 dark="0" date=2011-06-13 00:13:16 -0700 oid="929284" price=2 status=:active type=2>, <#Hashie::Rash amount=0.36 dark="0" date=2011-06-13 00:13:21 -0700 oid="929288" price=4 status=:active type=2>, <#Hashie::Rash amount=0.24 dark="0" date=2011-06-13 00:13:32 -0700 oid="929292" price=6 status=:active type=2>]

Returns:

  • (<Hashie::Rash>)

    with keys buy and sell, which contain arrays as described in #buys and #sells

Requires Authentication:

  • true



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

def orders
  parse_orders(post('/code/getOrders.php', pass_params)['orders'])
end

#sell!(amount, price) ⇒ Array<Hashie::Rash>

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:

  • (Array<Hashie::Rash>)

Requires Authentication:

  • true



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

def sell!(amount, price)
  parse_orders(post('/code/sellBTC.php', pass_params.merge({:amount => amount, :price => price}))['orders'])
end

#sellsArray<Hashie::Rash>

Fetch your open sells

Examples:

MtGox.sells[0, 3] #=> [<#Hashie::Rash amount=0.1 dark="0" date=2011-06-13 00:16:24 -0700 oid="663465" price=24.92 status=nil type=1>, <#Hashie::Rash amount=0.12 dark="0" date=2011-06-13 00:16:31 -0700 oid="663468" price=25.65 status=nil type=1>, <#Hashie::Rash amount=0.15 dark="0" date=2011-06-13 00:16:36 -0700 oid="663470" price=26.38 status=nil type=1>]

Returns:

  • (Array<Hashie::Rash>)

    an array of your open asks, sorted in price ascending order with the keys amount, dark, date, oid, price, status, and type

Requires Authentication:

  • true



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

def sells
  orders.select do |o|
    o['type'] == ORDER_TYPES[:sell]
  end
end

#tickerHashie::Rash

Fetch the latest ticker data

Examples:

MtGox.ticker #=> <#Hashie::Rash buy=19.29 high=19.96 last=19.36 low=19.01 sell=19.375 vol=29470>

Returns:

  • (Hashie::Rash)

    with keys buy - current highest bid price, sell - current lowest ask price, high - highest price trade for the day, low - lowest price trade for the day, last - price of most recent trade, and vol

Requires Authentication:

  • false



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

def ticker
  get('/code/data/ticker.php')['ticker']
end

#tradesArray<Hashie::Rash>

Fetch recent trades

Examples:

MtGox.trades[0, 3] #=> [<#Hashie::Rash amount=41 date=2011-06-14 11:26:32 -0700 price=18.5 tid="183747">, <#Hashie::Rash amount=5 date=2011-06-14 11:26:44 -0700 price=18.5 tid="183748">, <#Hashie::Rash amount=5 date=2011-06-14 11:27:00 -0700 price=18.42 tid="183749">]

Returns:

  • (Array<Hashie::Rash>)

    an array of trades, sorted in chronological order. Each trade is a Hashie::Rash with keys amount - number of bitcoins traded, price - price they were traded at in US dollars, date - time and date of the trade (a Time object), and tid - the trade ID.

Requires Authentication:

  • false



65
66
67
68
69
70
71
# File 'lib/mtgox/client.rb', line 65

def trades
  get('/code/data/getTrades.php').each do |trade|
    trade['amount'] = trade['amount'].to_f
    trade['date'] = Time.at(trade['date'])
    trade['price'] = trade['price'].to_f
  end
end

#withdraw!(amount, btca) ⇒ Array<Hashie::Rash>

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<Hashie::Rash>)

Requires Authentication:

  • true



184
185
186
# File 'lib/mtgox/client.rb', line 184

def withdraw!(amount, btca)
  post('/code/withdraw.php', pass_params.merge({:group1 => "BTC", :amount => amount, :btca => btca}))
end