Class: Kaesen::Bitflyer

Inherits:
Market
  • Object
show all
Defined in:
lib/kaesen/bitflyer.rb

Overview

bitFlyer Wrapper Class lightning.bitflyer.jp/docs?lang=ja API制限. Private API は 1 分間に約 200 回を上限とします。. IP アドレスごとに 1 分間に約 500 回を上限とします。

Direct Known Subclasses

Bitflyerfx

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| ... } ⇒ Bitflyer

Returns a new instance of Bitflyer.

Yields:

  • (_self)

Yield Parameters:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 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

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] ローカルタイムスタンプ



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# 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)) ⇒ hash

This method is abstract.

Buy the amount of Bitcoin at the rate. 指数注文 買い.

Parameters:

  • rate (BigDecimal)
  • amount (BigDecimal) (defaults to: BigDecimal.new(0))

    minimal amount is 0.001 BTC

Returns:

  • (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] ローカルタイムスタンプ



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# 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) ⇒ hash

This method is abstract.

Cancel an open order

Parameters:

  • order (int or string)

    id

Returns:

  • (hash)

    success: [bool] status



284
285
286
287
288
289
290
291
292
293
294
295
# 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_allarray

This method is abstract.

Cancel all open orders

Returns:

  • (array)

    success: [bool] status



301
302
303
304
305
306
# File 'lib/kaesen/bitflyer.rb', line 301

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



71
72
73
74
75
76
77
78
# 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")) ⇒ 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



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# 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")) ⇒ 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



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# 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

#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



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# 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)) ⇒ 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] amount: [BigDecimal] order_type: [String] “sell” or “buy” ltimestamp: [int] ローカルタイムスタンプ



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# 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

#tickerhash

Get ticker information.

Returns:

  • (hash)

    ticker ask: [BigDecimal] 最良売気配値bid: [BigDecimal] 最良買気配値last: [BigDecimal] 最近値(?用語要チェック), last price high: [BigDecimal] 高値low: [BigDecimal] 安値volume: [BigDecimal] 取引量ltimestamp: [int] ローカルタイムスタンプtimestamp: [int] タイムスタンプ



47
48
49
50
51
52
53
54
55
56
57
58
59
# 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