class Kaesen::Bitflyer

bitFlyer Wrapper Class lightning.bitflyer.jp/docs?lang=ja

API制限
. Private API は 1 分間に約 200 回を上限とします。
. IP アドレスごとに 1 分間に約 500 回を上限とします。

Public Class Methods

new(options = {}) { |self| ... } click to toggle source
Calls superclass method
# File lib/kaesen/bitflyer.rb, line 18
def initialize(options = {})
  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"

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

Public Instance Methods

balance() click to toggle source

Get account balance. @abstract @return [hash] account_balance_hash

jpy: [hash]
   amount: [BigDecimal] 総日本円
   available: [BigDecimal] 取引可能な日本円
btc [hash]
   amount: [BigDecimal] 総BTC
   available: [BigDecimal] 取引可能なBTC
ltimestamp: [int] ローカルタイムスタンプ
# File lib/kaesen/bitflyer.rb, line 94
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)) click to toggle source

Buy the amount of Bitcoin at the rate. 指数注文 買い. @abstract @param [BigDecimal] rate @param [BigDecimal] amount minimal amount is 0.001 BTC @return [hash] history_order_hash

success: [String] "true" or "false"
id: [String] order id at the market
rate: [BigDecimal]
amount: [BigDecimal]
order_type: [String] "sell" or "buy"
ltimestamp: [int] ローカルタイムスタンプ
# File lib/kaesen/bitflyer.rb, line 155
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
cancel(id) click to toggle source

Cancel an open order @abstract @param [int or string] order id @return [hash]

success: [bool] status
# File lib/kaesen/bitflyer.rb, line 284
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() click to toggle source

Cancel all open orders @abstract @return [array]

success: [bool] status
# File lib/kaesen/bitflyer.rb, line 301
def cancel_all
  have_key?
  opens.collect{|h|
    cancel(h["id"])
  }
end
depth() click to toggle source

Get order book. @abstract @return [hash] array of market depth

asks: [Array] 売りオーダー
   price : [BigDecimal]
   size : [BigDecimal]
bids: [Array] 買いオーダー
   price : [BigDecimal]
   size : [BigDecimal]
ltimestamp: [int] ローカルタイムスタンプ
# File lib/kaesen/bitflyer.rb, line 71
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")) click to toggle source

Buy the amount of Bitcoin from the market. 成行注文 買い. @abstract @param [BigDecimal] amount @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
# File lib/kaesen/bitflyer.rb, line 189
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")) click to toggle source

Sell the amount of Bitcoin to the market. 成行注文 売り. @abstract @param [BigDecimal] amount @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
# File lib/kaesen/bitflyer.rb, line 257
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() click to toggle source

Get open orders. @abstract @return [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
# File lib/kaesen/bitflyer.rb, line 121
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["child_order_acceptance_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)) click to toggle source

Sell the amount of Bitcoin at the rate. 指数注文 売り. Abstract Method. @param [BigDecimal] rate @param [BigDecimal] amount @return [hash] history_order_hash

success: [String] "true" or "false"
id: [String] order id at the market
rate: [BigDecimal]
amount: [BigDecimal]
order_type: [String] "sell" or "buy"
ltimestamp: [int] ローカルタイムスタンプ
# File lib/kaesen/bitflyer.rb, line 223
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() click to toggle source

Get ticker information. @return [hash] ticker

ask: [BigDecimal] 最良売気配値
bid: [BigDecimal] 最良買気配値
last: [BigDecimal] 最近値(?用語要チェック), last price
high: [BigDecimal] 高値
low: [BigDecimal] 安値
volume: [BigDecimal] 取引量
ltimestamp: [int] ローカルタイムスタンプ
timestamp: [int] タイムスタンプ
# File lib/kaesen/bitflyer.rb, line 47
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

Private Instance Methods

get_headers(uri, method, nonce, body="") click to toggle source
# File lib/kaesen/bitflyer.rb, line 362
def get_headers(uri, method, nonce, body="")
  {
    "ACCESS-KEY" => @api_key,
    "ACCESS-TIMESTAMP" => nonce.to_s,
    "ACCESS-SIGN" => get_sign(uri, method, nonce, body),
    "Content-Type" => "application/json",
  }
end
get_nonce() click to toggle source
# File lib/kaesen/bitflyer.rb, line 341
def get_nonce
  pre_nonce = @@nonce
  next_nonce = (1000*Time.now.to_f).to_i

  if next_nonce <= pre_nonce
    @@nonce = pre_nonce + 1
  else
    @@nonce = next_nonce
  end

  return @@nonce
end
get_sign(uri, method, nonce, body) click to toggle source
# File lib/kaesen/bitflyer.rb, line 354
def get_sign(uri, method, nonce, body)
  secret = @api_secret
  text = nonce.to_s + method + uri.request_uri
  text += body.to_json if body != ""

  OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new("sha256"), secret, text)
end
get_ssl(address) click to toggle source

Connect to address via https, and return json response.

# File lib/kaesen/bitflyer.rb, line 321
def get_ssl(address)
  uri = URI.parse(address)
  begin
    https = initialize_https(uri)
    https.start {|w|
      response = w.get(uri.request_uri)
      case response
        when Net::HTTPSuccess
          json = JSON.parse(response.body)
          raise JSONException, response.body if json == nil
          return json
        else
          raise ConnectionFailedException, "Failed to connect to #{@name}."
      end
    }
  rescue
    raise
  end
end
get_ssl_with_sign(address, body="") click to toggle source
# File lib/kaesen/bitflyer.rb, line 371
def get_ssl_with_sign(address, body="")
  uri = URI.parse(address)
  nonce = get_nonce
  headers = get_headers(uri, "GET", nonce, body)

  begin
    req = Net::HTTP::Get.new(uri, headers)
    req.body = body.to_json if body != ""

    https = https = initialize_https(uri)
    https.start {|w|
      response = w.request(req)
      case response
        when Net::HTTPSuccess
          json = JSON.parse(response.body)
          raise JSONException, response.body if json == nil
          return json
        else
          raise ConnectionFailedException, "Failed to connect to #{@name}: " + response.value
      end
    }
  rescue
    raise
  end
end
initialize_https(uri) click to toggle source
# File lib/kaesen/bitflyer.rb, line 310
def initialize_https(uri)
  https = Net::HTTP.new(uri.host, uri.port)
  https.use_ssl = true
  https.open_timeout = 5
  https.read_timeout = 15
  https.verify_mode = OpenSSL::SSL::VERIFY_PEER
  https.verify_depth = 5
  https
end
post_ssl_with_sign(address, body="") click to toggle source
# File lib/kaesen/bitflyer.rb, line 397
def post_ssl_with_sign(address, body="")
  uri = URI.parse(address)
  nonce = get_nonce
  headers = get_headers(uri, "POST", nonce, body)

  begin
    req = Net::HTTP::Post.new(uri, headers)
    req.body = body.to_json if body != ""

    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
      end
    }
  rescue
    raise
  end
end