Class: Kaesen::Zaif
Overview
Zaif Wrapper Class corp.zaif.jp/api-docs/
Constant Summary collapse
- @@nonce =
0
Instance Attribute Summary
Attributes inherited from Market
Instance Method Summary collapse
-
#balance ⇒ hash
abstract
Get account balance.
-
#buy(rate, amount = BigDecimal.new(0)) ⇒ hash
Bought the amount of Bitcoin at the rate.
-
#cancel(id) ⇒ hash
abstract
Cancel an open order.
-
#cancel_all ⇒ array
abstract
Cancel all open orders.
-
#depth ⇒ hash
abstract
Get order book.
-
#initialize ⇒ Zaif
constructor
A new instance of Zaif.
-
#market_buy(amount = BigDecimal.new("0.0")) ⇒ hash
abstract
Buy the amount of Bitcoin from the market.
-
#market_sell(amount = BigDecimal.new("0.0")) ⇒ hash
abstract
Sell the amount of Bitcoin to the market.
-
#opens ⇒ Array
abstract
Get open orders.
-
#sell(rate, amount = BigDecimal.new(0)) ⇒ hash
Sell the amount of Bitcoin at the rate.
-
#ticker ⇒ hash
Get ticker information.
Methods inherited from Market
Constructor Details
#initialize ⇒ Zaif
Returns a new instance of Zaif.
15 16 17 18 19 20 21 22 |
# File 'lib/kaesen/zaif.rb', line 15 def initialize() 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" end |
Instance Method Details
#balance ⇒ hash
This method is abstract.
Get account balance.
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/kaesen/zaif.rb', line 85 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.
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/kaesen/zaif.rb', line 144 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
275 276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/kaesen/zaif.rb', line 275 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_all ⇒ array
This method is abstract.
Cancel all open orders
293 294 295 296 297 |
# File 'lib/kaesen/zaif.rb', line 293 def cancel_all opens.each{|h| print cancel(h["id"]) } end |
#depth ⇒ hash
This method is abstract.
Get order book.
62 63 64 65 66 67 68 69 |
# File 'lib/kaesen/zaif.rb', line 62 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. 成行注文 買い.
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/kaesen/zaif.rb', line 178 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. 成行注文 売り.
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/kaesen/zaif.rb', line 247 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 |
#opens ⇒ Array
This method is abstract.
Get open orders.
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/kaesen/zaif.rb', line 114 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.
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/kaesen/zaif.rb', line 213 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 |
#ticker ⇒ hash
Get ticker information.
38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/kaesen/zaif.rb', line 38 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 |