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<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



65
66
67
# File 'lib/mtgox/client.rb', line 65

def asks
  offers[:asks]
end

#balanceArray<MtGox::Balance>

Fetch your current balance

Examples:

MtGox.balance

Returns:

Requires Authentication:

  • true



123
124
125
# File 'lib/mtgox/client.rb', line 123

def balance
  parse_balance(post('/code/getFunds.php', pass_params))
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



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

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



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

def buy!(amount, price)
  parse_orders(post('/code/buyBTC.php', pass_params.merge({: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



143
144
145
# File 'lib/mtgox/client.rb', line 143

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



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/mtgox/client.rb', line 200

def cancel(args)
  if args.is_a?(Hash)
    order = args.delete_if{|k, v| !['oid', 'type'].include?(k.to_s)}
    parse_orders(post('/code/cancelOrder.php', pass_params.merge(order))['orders'])
  else
    orders = post('/code/getOrders.php', pass_params)['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('/code/cancelOrder.php', pass_params.merge(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



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

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



85
86
87
88
89
90
# File 'lib/mtgox/client.rb', line 85

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



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/mtgox/client.rb', line 44

def offers
  offers = get('/code/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



133
134
135
# File 'lib/mtgox/client.rb', line 133

def orders
  parse_orders(post('/code/getOrders.php', pass_params)['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



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

def sell!(amount, price)
  parse_orders(post('/code/sellBTC.php', pass_params.merge({: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



153
154
155
# File 'lib/mtgox/client.rb', line 153

def sells
  orders[:sells]
end

#tickerMtGox::Ticker

Fetch the latest ticker data

Examples:

MtGox.ticker

Returns:

Requires Authentication:

  • false



27
28
29
30
31
32
33
34
35
36
# File 'lib/mtgox/client.rb', line 27

def ticker
  ticker = get('/code/data/ticker.php')['ticker']
  Ticker.instance.buy    = ticker['buy'].to_f
  Ticker.instance.high   = ticker['high'].to_f
  Ticker.instance.price  = ticker['last'].to_f
  Ticker.instance.low    = ticker['low'].to_f
  Ticker.instance.sell   = ticker['sell'].to_f
  Ticker.instance.volume = ticker['vol'].to_f
  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



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

def trades
  get('/code/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:

Requires Authentication:

  • true



225
226
227
# File 'lib/mtgox/client.rb', line 225

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