Class: MtGox::Client
- Inherits:
-
Object
- Object
- MtGox::Client
- Includes:
- Configuration, Connection, Request, Value
- Defined in:
- lib/mtgox/client.rb
Constant Summary collapse
- ORDER_TYPES =
{sell: "ask", buy: "bid"}
Constants included from Configuration
MtGox::Configuration::DEFAULT_COMMISSION, MtGox::Configuration::DEFAULT_NONCE_TYPE, MtGox::Configuration::VALID_OPTIONS_KEYS
Constants included from Value
Instance Method Summary collapse
-
#address ⇒ String
Fetch a deposit address.
-
#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) ⇒ String
Place a limit order to buy BTC.
-
#buys ⇒ Array<MtGox::Buy>
Fetch your open buys.
-
#cancel(args) ⇒ Object
(also: #cancel_order, #cancelorder)
Cancel an open order.
-
#idkey ⇒ String
Get an idKey for subscribing to private channels in WebSocket API.
-
#initialize ⇒ Client
constructor
A new instance of Client.
-
#lag ⇒ MtGox::Lag
(also: #order_lag, #orderlag)
Fetch the latest lag data.
-
#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.
-
#order!(type, amount, price) ⇒ String
(also: #add_order!, #addorder!)
Create a new order.
-
#order_result(offer_type, order_id) ⇒ OrderResult
Fetch information about a particular transaction.
-
#orders ⇒ Hash
Fetch your open orders, both buys and sells, for network efficiency.
-
#sell!(amount, price) ⇒ String
Place a limit order to sell BTC.
-
#sells ⇒ Array<MtGox::Sell>
Fetch your open sells.
-
#ticker ⇒ MtGox::Ticker
Fetch the latest ticker data.
-
#trades(opts = {}) ⇒ Array<MtGox::Trade>
Fetch recent trades.
-
#withdraw!(amount, address) ⇒ String
Transfer bitcoins from your Mt.
Methods included from Configuration
#configure, extended, #nonce_type, #reset
Methods included from Value
#decimalify, #intify, #value_bitcoin, #value_currency
Methods included from Request
Constructor Details
#initialize ⇒ Client
Returns a new instance of Client.
27 28 29 |
# File 'lib/mtgox/client.rb', line 27 def initialize reset end |
Instance Method Details
#address ⇒ String
Fetch a deposit address
36 37 38 |
# File 'lib/mtgox/client.rb', line 36 def address post('/api/1/generic/bitcoin/address')['addr'] end |
#asks ⇒ Array<MtGox::Ask>
Fetch open asks
108 109 110 |
# File 'lib/mtgox/client.rb', line 108 def asks offers[:asks] end |
#balance ⇒ Array<MtGox::Balance>
Fetch your current balance
162 163 164 |
# File 'lib/mtgox/client.rb', line 162 def balance parse_balance(post('/api/1/generic/info')) end |
#bids ⇒ Array<MtGox::Bid>
Fetch open bids
118 119 120 |
# File 'lib/mtgox/client.rb', line 118 def bids offers[:bids] end |
#buy!(amount, price) ⇒ String
Place a limit order to buy BTC
205 206 207 |
# File 'lib/mtgox/client.rb', line 205 def buy!(amount, price) add_order!(:buy, amount, price) end |
#buys ⇒ Array<MtGox::Buy>
Fetch your open buys
182 183 184 |
# File 'lib/mtgox/client.rb', line 182 def buys orders[:buys] end |
#cancel(oid) ⇒ Hash #cancel(order) ⇒ Hash Also known as: cancel_order, cancelorder
Cancel an open order
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/mtgox/client.rb', line 259 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 MtGox::OrderNotFoundError end end |
#idkey ⇒ String
Get an idKey for subscribing to private channels in WebSocket API
45 46 47 |
# File 'lib/mtgox/client.rb', line 45 def idkey post('/api/1/generic/idkey') end |
#lag ⇒ MtGox::Lag Also known as: order_lag, orderlag
Fetch the latest lag data
74 75 76 77 |
# File 'lib/mtgox/client.rb', line 74 def lag lag = get('/api/1/generic/order/lag') Lag.new(lag['lag'], lag['lag_secs'], lag['lag_text'], lag['length']) end |
#max_bid ⇒ MtGox::MinBid
Fetch the highest priced bid
138 139 140 |
# File 'lib/mtgox/client.rb', line 138 def max_bid bids.first end |
#min_ask ⇒ MtGox::MinAsk
Fetch the lowest priced ask
128 129 130 |
# File 'lib/mtgox/client.rb', line 128 def min_ask asks.first end |
#offers ⇒ Hash
Fetch both bids and asks in one call, for network efficiency
87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/mtgox/client.rb', line 87 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(self, ask) end bids = offers['bids'].sort_by do |bid| -bid['price_int'].to_i end.map! do |bid| Bid.new(self, bid) end {asks: asks, bids: bids} end |
#order!(type, amount, price) ⇒ String Also known as: add_order!, addorder!
Create a new order
232 233 234 235 236 237 238 |
# File 'lib/mtgox/client.rb', line 232 def order!(type, amount, price) order = {type: order_type(type), amount_int: intify(amount,:btc)} if price != :market order[:price_int] = intify(price, :usd) end post('/api/1/BTCUSD/order/add', order) end |
#order_result(offer_type, order_id) ⇒ OrderResult
Fetch information about a particular transaction
301 302 303 |
# File 'lib/mtgox/client.rb', line 301 def order_result(offer_type, order_id) OrderResult.new(post('/api/1/generic/order/result', {type: offer_type, order: order_id})) end |
#orders ⇒ Hash
Fetch your open orders, both buys and sells, for network efficiency
172 173 174 |
# File 'lib/mtgox/client.rb', line 172 def orders parse_orders(post('/api/1/generic/orders')) end |
#sell!(amount, price) ⇒ String
Place a limit order to sell BTC
218 219 220 |
# File 'lib/mtgox/client.rb', line 218 def sell!(amount, price) add_order!(:sell, amount, price) end |
#sells ⇒ Array<MtGox::Sell>
Fetch your open sells
192 193 194 |
# File 'lib/mtgox/client.rb', line 192 def sells orders[:sells] end |
#ticker ⇒ MtGox::Ticker
Fetch the latest ticker data
55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/mtgox/client.rb', line 55 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 |
#trades(opts = {}) ⇒ Array<MtGox::Trade>
Fetch recent trades
149 150 151 152 153 154 |
# File 'lib/mtgox/client.rb', line 149 def trades(opts={}) get('/api/1/BTCUSD/trades/fetch', opts). 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
286 287 288 289 290 291 292 293 |
# File 'lib/mtgox/client.rb', line 286 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 |