Class: Kaesen::Bitflyer
Overview
BitFlyer Wrapper Class coincheck.jp/documents/exchange/api?locale=ja API制限. Private API は 1 分間に約 200 回を上限とします。. IP アドレスごとに 1 分間に約 500 回を上限とします。
Direct Known Subclasses
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
abstract
Buy the amount of Bitcoin at the rate.
-
#depth ⇒ hash
abstract
Get order book.
-
#initialize ⇒ Bitflyer
constructor
A new instance of Bitflyer.
-
#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
#cancel, #cancel_all, unBigDecimal
Constructor Details
#initialize ⇒ Bitflyer
Returns a new instance of Bitflyer.
18 19 20 21 22 23 24 25 26 |
# File 'lib/kaesen/bitflyer.rb', line 18 def initialize() super() @name = "BitFlyer" @api_key = ENV["BITFLYER_KEY"] @api_secret = ENV["BITFLYER_SECRET"] @url_public = "https://api.bitflyer.jp/v1" @url_private = @url_public @product_code = "BTC_JPY" end |
Instance Method Details
#balance ⇒ hash
Get account balance.
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/kaesen/bitflyer.rb', line 89 def balance have_key? h = get_ssl_with_sign(@url_private + "/me/getbalance") { "jpy" => { "amount" => BigDecimal.new(h[0]["amount"].to_s), "available" => BigDecimal.new(h[0]["available"].to_s), }, "btc" => { "amount" => BigDecimal.new(h[1]["amount"].to_s), "available" => BigDecimal.new(h[1]["available"].to_s), }, "ltimestamp" => Time.now.to_i, } end |
#buy(rate, amount = BigDecimal.new(0)) ⇒ hash
Buy the amount of Bitcoin at the rate. 指数注文 買い.
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/kaesen/bitflyer.rb', line 150 def buy(rate, amount=BigDecimal.new(0)) have_key? address = @url_private + "/me/sendchildorder" body = { "product_code" => @product_code, "child_order_type" => "LIMIT", "side" => "BUY", "price" => rate.to_i, "size" => amount.to_f.round(4), "minute_to_expire" => 525600, "time_in_force" => "GTC", } h = post_ssl_with_sign(address, body) { "success" => "true", "id" => h["child_order_acceptance_id"].to_s, "rate" => BigDecimal.new(rate.to_s), "amount" => BigDecimal.new(amount.to_s), "order_type" => "buy", "ltimestamp" => Time.now.to_i, } end |
#depth ⇒ hash
Get order book.
66 67 68 69 70 71 72 73 |
# File 'lib/kaesen/bitflyer.rb', line 66 def depth h = get_ssl(@url_public + "/getboard?product_code=#{@product_code}") { "asks" => h["asks"].map{|x| [BigDecimal.new(x["price"].to_s), BigDecimal.new(x["size"].to_s)]}, "bids" => h["bids"].map{|x| [BigDecimal.new(x["price"].to_s), BigDecimal.new(x["size"].to_s)]}, "ltimestamp" => Time.now.to_i, } end |
#market_buy(amount = BigDecimal.new("0.0")) ⇒ hash
Buy the amount of Bitcoin from the market. 成行注文 買い.
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/kaesen/bitflyer.rb', line 184 def market_buy(amount=BigDecimal.new("0.0")) have_key? address = @url_private + "/me/sendchildorder" body = { "product_code" => @product_code, "child_order_type" => "MARKET", "side" => "BUY", "size" => amount.to_f.round(4), "minute_to_expire" => 525600, "time_in_force" => "GTC", } h = post_ssl_with_sign(address, body) { "success" => "true", "id" => h["child_order_acceptance_id"].to_s, # "rate" is not supplied. "amount" => BigDecimal.new(amount.to_s), "order_type" => "buy", "ltimestamp" => Time.now.to_i, } end |
#market_sell(amount = BigDecimal.new("0.0")) ⇒ hash
Sell the amount of Bitcoin to the market. 成行注文 売り.
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/kaesen/bitflyer.rb', line 252 def market_sell(amount=BigDecimal.new("0.0")) have_key? address = @url_private + "/me/sendchildorder" body = { "product_code" => @product_code, "child_order_type" => "MARKET", "side" => "SELL", "size" => amount.to_f.round(4), "minute_to_expire" => 525600, "time_in_force" => "GTC", } h = post_ssl_with_sign(address, body) { "success" => "true", "id" => h["child_order_acceptance_id"].to_s, # "rate" is not supplied. "amount" => BigDecimal.new(amount.to_s), "order_type" => "sell", "ltimestamp" => Time.now.to_i, } end |
#opens ⇒ Array
Get open orders.
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/kaesen/bitflyer.rb', line 116 def opens have_key? address = @url_private + "/me/getchildorders" query = { "child_order_state" => "ACTIVE", } address += "?" + query.to_a.map{|x|"#{x[0]}=#{x[1]}" }.join("&") body = { "product_code" => @product_code, } a = get_ssl_with_sign(address, body) a.map{|x| { "success" => "true", "id" => x["id"], "rate" => BigDecimal.new(x["average_price"].to_s), "amount" => BigDecimal.new(x["size"].to_s), "order_type" => x["side"].downcase, } } end |
#sell(rate, amount = BigDecimal.new(0)) ⇒ hash
Sell the amount of Bitcoin at the rate. 指数注文 売り. Abstract Method.
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/bitflyer.rb', line 218 def sell(rate, amount=BigDecimal.new(0)) have_key? address = @url_private + "/me/sendchildorder" body = { "product_code" => @product_code, "child_order_type" => "LIMIT", "side" => "SELL", "price" => rate.to_i, "size" => amount.to_f.round(4), "minute_to_expire" => 525600, "time_in_force" => "GTC", } h = post_ssl_with_sign(address, body) { "success" => "true", "id" => h["child_order_acceptance_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.
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/kaesen/bitflyer.rb', line 42 def ticker h = get_ssl(@url_public + "/getticker?product_code=#{@product_code}") { "ask" => BigDecimal.new(h["best_ask"].to_s), "bid" => BigDecimal.new(h["best_bid"].to_s), "last" => BigDecimal.new(h["ltp"].to_s), # "high" is not supplied. # "low" is not supplied. "volume" => BigDecimal.new(h["volume"].to_s), "ltimestamp" => Time.now.to_i, "timestamp" => DateTime.parse(h["timestamp"]).to_time.to_i, } end |