Class: Kaesen::Btcbox

Inherits:
Market
  • Object
show all
Defined in:
lib/kaesen/btcbox.rb

Overview

BtcBox Wrapper Class www.btcbox.co.jp/help/api.html

Constant Summary collapse

@@nonce =
0

Instance Attribute Summary

Attributes inherited from Market

#api_key, #api_secret, #name, #url_private, #url_public

Instance Method Summary collapse

Methods inherited from Market

#market_buy, #market_sell, unBigDecimal

Constructor Details

#initialize(options = {}) {|_self| ... } ⇒ Btcbox

Returns a new instance of Btcbox.

Yields:

  • (_self)

Yield Parameters:



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/kaesen/btcbox.rb', line 14

def initialize(options = {})
  super()
  @name        = "BtcBox"
  @api_key     = ENV["BTCBOX_KEY"]
  @api_secret  = ENV["BTCBOX_SECRET"]
  @url_public  = "https://www.btcbox.co.jp/api/v1"
  @url_private = @url_public

  options.each do |key, value|
    instance_variable_set("@#{key}", value)
  end
  yield(self) if block_given?
end

Instance Method Details

#balancehash

This method is abstract.

Get account balance.

Returns:

  • (hash)

    account_balance_hash jpy: [hash]

    amount: [BigDecimal] 

    btc [hash]

    amount: [BigDecimal] 

    ltimestamp: [int] ローカルタイムスタンプ



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/kaesen/btcbox.rb', line 87

def balance
  have_key?
  h = post_ssl_with_sign(@url_private + "/balance/")
  {
    "jpy"        => {
      "amount"    => BigDecimal.new(h["jpy_balance"].to_s) + BigDecimal.new(h["jpy_lock"].to_s),
      "available" => BigDecimal.new(h["jpy_balance"].to_s),
    },
    "btc"        => {
      "amount"    => BigDecimal.new(h["btc_balance"].to_s) + BigDecimal.new(h["btc_lock"].to_s),
      "available" => BigDecimal.new(h["btc_balance"].to_s),
    },
    "ltimestamp" => Time.now.to_i,
  }
end

#buy(rate, amount = BigDecimal.new(0)) ⇒ hash

Bought the amount of Bitcoin at the rate. 指数注文 買い. Abstract Method.

Parameters:

  • rate (BigDecimal)
  • amount (BigDecimal) (defaults to: BigDecimal.new(0))

    # minimal trade amount is 0.01 BTC

Returns:

  • (hash)

    history_order_hash success: [String] “true” or “false” id: [String] order id at the market rate: [BigDecimal] amount: [BigDecimal] minimal amount is 0.01 BTC order_type: [String] “sell” or “buy” ltimestamp: [int] ローカルタイムスタンプ



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/kaesen/btcbox.rb', line 143

def buy(rate, amount=BigDecimal.new(0))
  have_key?
  address = @url_private + "/trade_add/"
  params = {
    "amount" => amount.to_f.round(4),
    "price"  => rate.to_i,
    "type"   => "buy",
  }
  h = post_ssl_with_sign(address, params)
  {
    "success"    => h["result"].to_s,
    "id"         => h["id"].to_s,
    "rate"       => BigDecimal.new(rate.to_s),
    "amount"     => BigDecimal.new(amount.to_s),
    "order_type" => "sell",
    "ltimestamp" => Time.now.to_i,
  }
end

#cancel(id) ⇒ hash

This method is abstract.

Cancel an open order

Parameters:

  • order (int or string)

    id

Returns:

  • (hash)

    success: [bool] status



198
199
200
201
202
203
204
205
206
207
208
# File 'lib/kaesen/btcbox.rb', line 198

def cancel(id)
  have_key?
  address = @url_private + "/trade_cancel/"
  params = {
    "id" => id,
  }
  h = post_ssl_with_sign(address, params)
  {
    "success" => h["result"],
  }
end

#cancel_allarray

This method is abstract.

Cancel all open orders

Returns:

  • (array)

    success: [bool] status



214
215
216
217
218
219
# File 'lib/kaesen/btcbox.rb', line 214

def cancel_all
  have_key?
  opens.collect{|h|
    cancel(h["id"])
  }
end

#depthhash

This method is abstract.

Get order book.

Returns:

  • (hash)

    array of market depth asks: [Array] 売りオーダー

    price : [BigDecimal]
    size : [BigDecimal]
    

    bids: [Array] 買いオーダー

    price : [BigDecimal]
    size : [BigDecimal]
    

    ltimestamp: [int] ローカルタイムスタンプ



64
65
66
67
68
69
70
71
# File 'lib/kaesen/btcbox.rb', line 64

def depth
  h = get_ssl(@url_public + "/depth")
  {
    "asks"       => h["asks"].map{|a,b| [BigDecimal.new(a.to_s), BigDecimal.new(b.to_s)]}, # to_s でないと誤差が生じる
    "bids"       => h["bids"].map{|a,b| [BigDecimal.new(a.to_s), BigDecimal.new(b.to_s)]}, # to_s でないと誤差が生じる
    "ltimestamp" => Time.now.to_i,
  }
end

#opensArray

This method is abstract.

Get open orders.

Returns:

  • (Array)

    open_orders_array @return [hash] history_order_hash

    success: [bool]
    id: [String] order id in the market
    rate: [BigDecimal]
    amount: [BigDecimal]
    order_type: [String] "sell" or "buy"
    

    ltimestamp: [int] Local Timestamp



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/kaesen/btcbox.rb', line 113

def opens
  have_key?
  address = @url_private + "/trade_list/"
  params = {
    "type"   => "open",
  }
  h = post_ssl_with_sign(address, params)
  h.map{|x|
    {
      "success"    => "true",
      "id"         => x["id"],
      "rate"       => BigDecimal.new(x["price"].to_s),
      "amount"     => BigDecimal.new(x["amount_outstanding"].to_s),
      "order_type" => x["type"],
    }
  }
end

#sell(rate, amount = BigDecimal.new(0)) ⇒ hash

Sell the amount of Bitcoin at the rate. 指数注文 売り. Abstract Method.

Parameters:

  • rate (BigDecimal)
  • amount (BigDecimal) (defaults to: BigDecimal.new(0))

    # minimal trade amount is 0.01 BTC

Returns:

  • (hash)

    history_order_hash success: [String] “true” or “false” id: [String] order id at the market rate: [BigDecimal] amount: [BigDecimal] minimal amount is 0.01 BTC order_type: [String] “sell” or “buy” ltimestamp: [int] ローカルタイムスタンプ



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/kaesen/btcbox.rb', line 174

def sell(rate, amount=BigDecimal.new(0))
  have_key?
  address = @url_private + "/trade_add/"
  params = {
    "amount" => amount.to_f.round(4),
    "price" => rate.to_i,
    "type" => "sell",
  }
  h = post_ssl_with_sign(address, params)
  {
    "success"    => h["result"].to_s,
    "id"         => h["id"].to_s,
    "rate"       => BigDecimal.new(rate.to_s),
    "amount"     => BigDecimal.new(amount.to_s),
    "order_type" => "sell",
    "ltimestamp" => Time.now.to_i,
  }
end

#tickerhash

Get ticker information.

Returns:

  • (hash)

    ticker ask: [BigDecimal] 最良売気配値bid: [BigDecimal] 最良買気配値last: [BigDecimal] 最近値(?用語要チェック), last price high: [BigDecimal] 高値low: [BigDecimal] 安値volume: [BigDecimal] 取引量ltimestamp: [int] ローカルタイムスタンプ



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/kaesen/btcbox.rb', line 41

def ticker
  h = get_ssl(@url_public + "/ticker")
  {
    "ask"        => BigDecimal.new(h["sell"].to_s),
    "bid"        => BigDecimal.new(h["buy"].to_s),
    "last"       => BigDecimal.new(h["last"].to_s),
    "high"       => BigDecimal.new(h["high"].to_s),
    "low"        => BigDecimal.new(h["low"].to_s),
    "volume"     => BigDecimal.new(h["vol"].to_s),
    "ltimestamp" => Time.now.to_i,
  }
end