Class: Kaesen::Zaif

Inherits:
Market show all
Defined in:
lib/kaesen/zaif.rb

Overview

Zaif Wrapper Class corp.zaif.jp/api-docs/

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

unBigDecimal

Constructor Details

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

Returns a new instance of Zaif.

Yields:

  • (_self)

Yield Parameters:

  • _self (Kaesen::Zaif)

    the object that the method was called on



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

def initialize(options = {})
  super()
  @name        = "Zaif"
  @api_key     = ENV["ZAIF_KEY"]
  @api_secret  = ENV["ZAIF_SECRET"]
  @url_public  = "https://api.zaif.jp/api/1"
  @url_private = "https://api.zaif.jp/tapi"

  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] 総日本円
    available: [BigDecimal] 取引可能な日本円
    

    btc [hash]

    amount: [BigDecimal] 総BTC
    available: [BigDecimal] 取引可能なBTC
    

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



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/kaesen/zaif.rb', line 90

def balance
  have_key?
  address = @url_private
  body = { "method" => "get_info" }
  h = post_ssl(address, body)
  {
    "jpy"        => {
      "amount"    => BigDecimal.new(h["return"]["deposit"]["jpy"].to_s),
      "available" => BigDecimal.new(h["return"]["funds"]["jpy"].to_s),
    },
    "btc"        => {
      "amount"    => BigDecimal.new(h["return"]["deposit"]["btc"].to_s),
      "available" => BigDecimal.new(h["return"]["funds"]["btc"].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))

Returns:

  • (hash)

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



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/kaesen/zaif.rb', line 149

def buy(rate, amount=BigDecimal.new(0))
  have_key?
  address = @url_private
  rate = (rate.to_i / 5) * 5
  body = {
    "method"        => "trade",
    "currency_pair" => "btc_jpy",
    "action"        => "bid",
    "price"         => rate,
    "amount"        => amount.to_f.round(4)
  }
  h = post_ssl(address, body)
  result = h["success"].to_i == 1 ? "true" : "false"
  {
    "success"    => result,
    "id"         => h["return"]["order_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



280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/kaesen/zaif.rb', line 280

def cancel(id)
  have_key?
  address = @url_private
  body = {
    "method"        => "cancel_order",
    "currency_pair" => "btc_jpy",
    "order_id" => id,
  }
  h = post_ssl(address, body)
  {
    "success" => h["success"]==1,
  }
end

#cancel_allarray

This method is abstract.

Cancel all open orders

Returns:

  • (array)

    success: [bool] status



298
299
300
301
302
# File 'lib/kaesen/zaif.rb', line 298

def cancel_all
  opens.each{|h|
    print 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] ローカルタイムスタンプ



67
68
69
70
71
72
73
74
# File 'lib/kaesen/zaif.rb', line 67

def depth
  h = get_ssl(@url_public + "/depth/btc_jpy")
  {
    "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

#market_buy(amount = BigDecimal.new("0.0")) ⇒ hash

This method is abstract.

Buy the amount of Bitcoin from the market. 成行注文 買い.

Parameters:

  • amount (BigDecimal) (defaults to: BigDecimal.new("0.0"))

Returns:

  • (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



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/kaesen/zaif.rb', line 183

def market_buy(amount=BigDecimal.new("0.0"))
  have_key?
  address = @url_private
  rate = (rate.to_i / 5) * 5
  body = {
    "method"        => "trade",
    "currency_pair" => "btc_jpy",
    "action"        => "bid",
    "price"         => 100000,
    "amount"        => amount.to_f.round(4)
  }
  h = post_ssl(address, body)
  result = h["success"].to_i == 1 ? "true" : "false"
  {
    "success"    => result,
    "id"         => h["return"]["order_id"].to_s,
    "rate"       => BigDecimal.new(rate.to_s),
    "amount"     => BigDecimal.new(amount.to_s),
    "order_type" => "sell",
    "ltimestamp" => Time.now.to_i,
  }
end

#market_sell(amount = BigDecimal.new("0.0")) ⇒ hash

This method is abstract.

Sell the amount of Bitcoin to the market. 成行注文 売り.

Parameters:

  • amount (BigDecimal) (defaults to: BigDecimal.new("0.0"))

Returns:

  • (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



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/kaesen/zaif.rb', line 252

def market_sell(amount=BigDecimal.new("0.0"))
  have_key?
  address = @url_private
  rate = (rate.to_i / 5) * 5
  body = {
    "method"        => "trade",
    "currency_pair" => "btc_jpy",
    "action" => "ask",
    "price" => 5,
    "amount" => amount.to_f.round(4),
  }
  h = post_ssl(address, body)
  result = h["success"].to_i == 1 ? "true" : "false"
  {
    "success"    => result,
    "id"         => h["return"]["order_id"].to_s,
    "rate"       => BigDecimal.new(rate.to_s),
    "amount"     => BigDecimal.new(amount.to_s),
    "order_type" => "sell",
    "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"
    order_status: [String] "active", "completed" or "canceled"
    

    ltimestamp: [int] Local Timestamp



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/kaesen/zaif.rb', line 119

def opens
  have_key?
  address = @url_private
  body = { "method" => "active_orders" }
  h = post_ssl(address, body)
  h["return"].map{|key, value|
    order_type = "buy" # when value["action"] is "bid"
    order_type = "sell" if value["action"] == "ask"
    {
      "success"    => "true",
      "id"         => key,
      "rate"       => BigDecimal.new(value["price"].to_s),
      "amount"     => BigDecimal.new(value["amount"].to_s),
      "order_type" => order_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))

Returns:

  • (hash)

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



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/kaesen/zaif.rb', line 218

def sell(rate, amount=BigDecimal.new(0))
  have_key?
  address = @url_private
  rate = (rate.to_i / 5) * 5
  body = {
    "method"        => "trade",
    "currency_pair" => "btc_jpy",
    "action" => "ask",
    "price" => rate,
    "amount" => amount.to_f.round(4),
  }
  h = post_ssl(address, body)
  result = h["success"].to_i == 1 ? "true" : "false"
  {
    "success"    => result,
    "id"         => h["return"]["order_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] ローカルタイムスタンプvwap: [BigDecimal] 過去24時間の加重平均



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/kaesen/zaif.rb', line 43

def ticker
  h = get_ssl(@url_public + "/ticker/btc_jpy")
  {
    "ask"        => BigDecimal.new(h["ask"].to_s),
    "bid"        => BigDecimal.new(h["bid"].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["volume"].to_s),
    "ltimestamp" => Time.now.to_i,
    "vwap"       => BigDecimal.new(h["vwap"].to_s)
  }
end