Class: MtGox::Client
- Inherits:
-
Object
- Object
- MtGox::Client
- Includes:
- Connection, Request, Value
- Defined in:
- lib/mtgox/client.rb
Constant Summary collapse
- ORDER_TYPES =
{sell: "ask", buy: "bid"}
Constants included from Value
Instance Method Summary collapse
-
#addorder!(type, amount, price) ⇒ String
Create a new order.
-
#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
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) ⇒ 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 ⇒ Array<MtGox::Trade>
Fetch recent trades.
-
#withdraw!(amount, address) ⇒ String
Transfer bitcoins from your Mt.
Methods included from Value
#floatify, #intify, #value_bitcoin, #value_currency
Methods included from Request
Instance Method Details
#addorder!(type, amount, price) ⇒ String
Create a new order
207 208 209 |
# File 'lib/mtgox/client.rb', line 207 def addorder!(type, amount, price) post('/api/1/BTCUSD/order/add', {type: order_type(type), amount_int: intify(amount,:btc), price_int: intify(price, :usd)}) end |
#address ⇒ String
Fetch a deposit address
28 29 30 |
# File 'lib/mtgox/client.rb', line 28 def address post('/api/1/generic/bitcoin/address')['addr'] end |
#asks ⇒ Array<MtGox::Ask>
Fetch open asks
79 80 81 |
# File 'lib/mtgox/client.rb', line 79 def asks offers[:asks] end |
#balance ⇒ Array<MtGox::Balance>
Fetch your current balance
137 138 139 |
# File 'lib/mtgox/client.rb', line 137 def balance parse_balance(post('/api/1/generic/info', {})) end |
#bids ⇒ Array<MtGox::Bid>
Fetch open bids
89 90 91 |
# File 'lib/mtgox/client.rb', line 89 def bids offers[:bids] end |
#buy!(amount, price) ⇒ String
Place a limit order to buy BTC
180 181 182 |
# File 'lib/mtgox/client.rb', line 180 def buy!(amount, price) addorder!(:buy, amount, price) end |
#buys ⇒ Array<MtGox::Buy>
Fetch your open buys
157 158 159 |
# File 'lib/mtgox/client.rb', line 157 def buys orders[:buys] end |
#cancel(oid) ⇒ Hash #cancel(order) ⇒ Hash
Cancel an open order
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/mtgox/client.rb', line 228 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 Faraday::Error::ResourceNotFound, {status: 404, headers: {}, body: 'Order not found.'} end end |
#max_bid ⇒ MtGox::MinBid
Fetch the highest priced bid
112 113 114 115 116 117 |
# File 'lib/mtgox/client.rb', line 112 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
99 100 101 102 103 104 |
# File 'lib/mtgox/client.rb', line 99 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
58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/mtgox/client.rb', line 58 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(ask) end bids = offers['bids'].sort_by do |bid| -bid['price_int'].to_i 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
147 148 149 |
# File 'lib/mtgox/client.rb', line 147 def orders parse_orders post('/api/1/generic/orders', {}) end |
#sell!(amount, price) ⇒ String
Place a limit order to sell BTC
193 194 195 |
# File 'lib/mtgox/client.rb', line 193 def sell!(amount, price) addorder!(:sell, amount, price) end |
#sells ⇒ Array<MtGox::Sell>
Fetch your open sells
167 168 169 |
# File 'lib/mtgox/client.rb', line 167 def sells orders[:sells] end |
#ticker ⇒ MtGox::Ticker
Fetch the latest ticker data
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/mtgox/client.rb', line 39 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 ⇒ Array<MtGox::Trade>
Fetch recent trades
125 126 127 128 129 |
# File 'lib/mtgox/client.rb', line 125 def trades get('/api/1/BTCUSD/trades/fetch').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
253 254 255 256 257 258 259 260 |
# File 'lib/mtgox/client.rb', line 253 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 |