Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Both forms of depth API supported in

  • Loading branch information...
commit d648936387ac17f900e632672d0472ef150055e2 1 parent f269c12
Ar Vicco authored
2  .rvmrc
View
@@ -1 +1 @@
-rvm use 1.9.2
+rvm use 1.9.2@mtgox
2  TODO
View
@@ -0,0 +1,2 @@
+1. ActiveRecord
+2. ('1/generic/private/order/result',{'order':saved_order.oid, 'type':saved_order.order_type})
22 lib/mtgox/client.rb
View
@@ -12,8 +12,8 @@ class Client
# Using class variables instead of Singleton
@@ticker = {}
- @@min_ask = MinAsk.new
- @@max_bid = MaxBid.new
+ @@min_ask = MinAsk.new []
+ @@max_bid = MaxBid.new []
# Fetch a deposit address
# @authenticated true
@@ -49,18 +49,12 @@ def ticker(currency=nil)
# @return [Hash] with keys :asks and :asks, which contain arrays as described in {MtGox::Client#asks} and {MtGox::Clients#bids}
# @example
# MtGox.offers
- def offers
- offers = get('/api/0/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
+ def offers full=false
+ offers = full ?
+ get('/api/1/BTCUSD/public/fulldepth')['return'] :
+ get('/api/0/data/getDepth.php')
+ asks = offers['asks'].map { |ask| Ask.new(ask) }.sort_by(&:price)
+ bids = offers['bids'].map { |bid| Bid.new(bid) }.sort_by { |bid| -bid.price }
{:asks => asks, :bids => bids}
end
14 lib/mtgox/models/ask.rb
View
@@ -4,14 +4,24 @@ module MtGox
module Models
class Ask < Offer
- def initialize(price=nil, amount=nil)
- super :price => price, :amount => amount
+ prop [:id, :stamp] => :i
+
+ def initialize data
+ data.is_a?(Hash) ? super(data) : super(:price => data[0], :amount => data[1])
end
def eprice
price / (1 - MtGox.commission)
end
+ def real_time
+ Time.at(id/1000000.0).strftime '%H:%M:%S.%3N' if id
+ end
+
+ def to_s
+ "<#{real_time} Ask: #{price} #{ amount.round(3) }>"
+ end
+
end
end
end
14 lib/mtgox/models/bid.rb
View
@@ -4,14 +4,24 @@ module MtGox
module Models
class Bid < Offer
- def initialize(price=nil, amount=nil)
- super :price => price, :amount => amount
+ prop [:id, :stamp] => :i
+
+ def initialize data
+ data.is_a?(Hash) ? super(data) : super(:price => data[0], :amount => data[1])
end
def eprice
price * (1 - MtGox.commission)
end
+ def real_time
+ Time.at(id/1000000.0).strftime '%H:%M:%S.%3N' if id
+ end
+
+ def to_s
+ "<#{real_time} Ask: #{price} #{ amount.round(3) }>"
+ end
+
end
end
end
8 lib/mtgox/models/depth.rb
View
@@ -13,9 +13,13 @@ class Depth < Model
[:id, :now] => :i,
[:side, :type, :type_str] => proc { |val| val == 1 || val == 'bid' ? :bid : :ask }
+ def real_time
+ Time.at(id/1000000.0).strftime '%H:%M:%S.%3N'
+ end
+
def to_s
- "<Depth: #{type} #{ vol > 0 ? '+' : ''}#{ vol.round(2) } at #{ price.round(3) }" +
- " #{currency}/#{item} >"
+ "<#{real_time} Depth: #{type} #{ vol > 0 ? '+' : ''}#{ vol.round(2) } at #{ price.round(3) }" +
+ " #{currency}/#{item} ##{id}>"
end
end
4 lib/mtgox/models/offer.rb
View
@@ -5,7 +5,9 @@ module Models
class Offer < Model
prop :amount => :f,
- :price => :f
+ :price => :f,
+ :amount_int => :i, # "1067215400"
+ :price_int => :i # 681526"
end
end
end
2  lib/mtgox/models/order.rb
View
@@ -10,8 +10,6 @@ class Order < Offer
:real_status, # "open" /
:dark,
:priority,
- :amount_int => :i,
- :price_int => :i,
[:id, :oid] => :i, # Order ID
:type => proc { |val| val == 1 ? :sell : :buy }, # 1 for sell order or 2 for buy order
[:date, :time] => proc { |val| Time.at val } # 1326655184
13 lib/mtgox/models/trade.rb
View
@@ -9,18 +9,19 @@ class Trade < Offer
:price_currency, # "USD"
:primary, # "Y" or "N", the primary currency is always the buyers currency
:properties, # "limit,mixed_currency"
- :amount_int => :i, # "1067215400"
- :price_int => :i, # 681526"
:amount => :f, # 10.672154
:price => :f, # 6.81526
[:id, :tid] => :i, # "tid"=>"1326655184087854"
[:date, :time] => proc { |val| Time.at val }, # 1326655184
[:side, :trade_type] => proc { |val| val == 'bid' ? :bid : :ask } # "ask"
- def to_s with_date=nil
- (with_date ? "#{@date.strftime('%H:%M:%S')} " : '') +
- "<Trade: #{ amount.round(3) } at #{trade_type} #{ price.round(3) }"+
- " #{price_currency}/#{item} #{ properties } #{ primary } >"
+ def real_time
+ Time.at(id/1000000.0).strftime '%H:%M:%S.%3N'
+ end
+
+ def to_s
+ "<#{real_time} Trade: #{trade_type} #{ amount.round(3) } at #{ price.round(3) }"+
+ " #{price_currency}/#{item} #{properties} #{primary} #{time.strftime '%H:%M:%S.%3N'} ##{id}>"
end
end
2  mt_gox.gemspec
View
@@ -18,6 +18,8 @@ Gem::Specification.new do |gem|
gem.add_dependency 'faraday', '~> 0.7'
gem.add_dependency 'faraday_middleware', '~> 0.7'
gem.add_dependency 'multi_json', '~> 1.0'
+
+ gem.add_development_dependency 'my_scripts'
gem.add_development_dependency 'json'
gem.add_development_dependency 'rake'
gem.add_development_dependency 'rdiscount'
14 spec/mtgox/client_spec.rb
View
@@ -149,7 +149,7 @@
end
describe "#offers" do
- it "should fetch both bids and asks, with only one call" do
+ it "should fetch (short version) of bids and asks by default" do
offers = @client.offers
a_get('/api/0/data/getDepth.php').should have_been_made.once
offers[:asks].last.price.should == 23.75
@@ -159,6 +159,18 @@
offers[:bids].last.eprice.should == 14.525973435000001
offers[:bids].last.amount.should == 5
end
+
+ it "should fetch bids/asks in extended form with #offers(true)" do
+ pending 'Stubs!'
+ offers = @client.offers(true)
+ a_get('/api/0/data/getDepth.php').should have_been_made.once
+ offers[:asks].last.price.should == 23.75
+ offers[:asks].last.eprice.should == 23.905385002516354
+ offers[:asks].last.amount.should == 50
+ offers[:bids].last.price.should == 14.62101
+ offers[:bids].last.eprice.should == 14.525973435000001
+ offers[:bids].last.amount.should == 5
+ end
end
describe '#min_ask' do
Please sign in to comment.
Something went wrong with that request. Please try again.