lib/kaesen/bitflyer.rb in kaesen-0.1.1 vs lib/kaesen/bitflyer.rb in kaesen-0.2.0

- old
+ new

@@ -4,22 +4,22 @@ require 'json' require 'date' require 'bigdecimal' module Kaesen - # BitFlyer Wrapper Class + # bitFlyer Wrapper Class # https://lightning.bitflyer.jp/docs?lang=ja ## API制限 ## . Private API は 1 分間に約 200 回を上限とします。 ## . IP アドレスごとに 1 分間に約 500 回を上限とします。 class Bitflyer < Market @@nonce = 0 def initialize(options = {}) super() - @name = "BitFlyer" + @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" @@ -130,11 +130,11 @@ } a = get_ssl_with_sign(address, body) a.map{|x| { "success" => "true", - "id" => x["id"], + "id" => x["child_order_acceptance_id"], "rate" => BigDecimal.new(x["average_price"].to_s), "amount" => BigDecimal.new(x["size"].to_s), "order_type" => x["side"].downcase, } } @@ -274,10 +274,39 @@ "order_type" => "sell", "ltimestamp" => Time.now.to_i, } end + # Cancel an open order + # @abstract + # @param [int or string] order id + # @return [hash] + # success: [bool] status + def cancel(id) + have_key? + address = @url_private + "/me/cancelchildorder" + body = { + "product_code" => @product_code, + "child_order_acceptance_id" => id + } + h = post_ssl_with_sign(address, body) + { + "success" => h + } + end + + # Cancel all open orders + # @abstract + # @return [array] + # success: [bool] status + def cancel_all + have_key? + opens.collect{|h| + cancel(h["id"]) + } + end + private def initialize_https(uri) https = Net::HTTP.new(uri.host, uri.port) https.use_ssl = true @@ -377,9 +406,10 @@ https = initialize_https(uri) https.start {|w| response = w.request(req) case response when Net::HTTPSuccess + return true if response.body == "" # cancel success json = JSON.parse(response.body) raise JSONException, response.body if json == nil return json else raise ConnectionFailedException, "Failed to connect to #{@name}: " + response.value