Class: MtGox::Client
- Inherits:
-
Object
- Object
- MtGox::Client
- Includes:
- Connection, Request
- Defined in:
- lib/mtgox/client.rb
Constant Summary collapse
- ORDER_TYPES =
{:sell => 1, :buy => 2}
Instance Method Summary collapse
-
#asks ⇒ Array<MtGox::Ask>
Fetch open asks.
-
#balance ⇒ Array<MtGox::Balance>
Fetch your current balance.
-
#bids ⇒ Array<MtGox::Bid>
Fetch open bids.
-
#buy!(amount, price) ⇒ Hash
Place a limit order to buy BTC.
-
#buys ⇒ Array<MtGox::Buy>
Fetch your open buys.
-
#cancel(args) ⇒ Object
Cancel an open order.
-
#max_bid ⇒ MtGox::MinBid
Fetch the highest priced bid.
-
#min_ask ⇒ MtGox::MinAsk
Fetch the lowest priced ask.
-
#offers ⇒ Hash
Fetch both bids and asks in one call, for network efficiency.
-
#orders ⇒ Hash
Fetch your open orders, both buys and sells, for network efficiency.
-
#sell!(amount, price) ⇒ Hash
Place a limit order to sell BTC.
-
#sells ⇒ Array<MtGox::Sell>
Fetch your open sells.
-
#ticker ⇒ MtGox::Ticker
Fetch the latest ticker data.
-
#trades ⇒ Array<MtGox::Trade>
Fetch recent trades.
-
#withdraw!(amount, btca) ⇒ Array<MtGox::Balance>
Transfer bitcoins from your Mt.
Methods included from Request
Instance Method Details
#asks ⇒ Array<MtGox::Ask>
Fetch open asks
65 66 67 |
# File 'lib/mtgox/client.rb', line 65 def asks offers[:asks] end |
#balance ⇒ Array<MtGox::Balance>
Fetch your current balance
123 124 125 |
# File 'lib/mtgox/client.rb', line 123 def balance parse_balance(post('/code/getFunds.php', pass_params)) end |
#bids ⇒ Array<MtGox::Bid>
Fetch open bids
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
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 |
#buys ⇒ Array<MtGox::Buy>
Fetch your open buys
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
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_bid ⇒ MtGox::MinBid
Fetch the highest priced bid
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_ask ⇒ MtGox::MinAsk
Fetch the lowest priced ask
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 |
#offers ⇒ Hash
Fetch both bids and asks in one call, for network efficiency
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 |
#orders ⇒ Hash
Fetch your open orders, both buys and sells, for network efficiency
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
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 |
#sells ⇒ Array<MtGox::Sell>
Fetch your open sells
153 154 155 |
# File 'lib/mtgox/client.rb', line 153 def sells orders[:sells] end |
#ticker ⇒ MtGox::Ticker
Fetch the latest ticker data
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 |
#trades ⇒ Array<MtGox::Trade>
Fetch recent trades
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
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 |