Class: Kaesen::Bitflyer

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

Overview

BitFlyer Wrapper Class coincheck.jp/documents/exchange/api?locale=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

#name

Instance Method Summary collapse

Methods inherited from Market

#cancel, #cancel_all, unBigDecimal

Constructor Details

#initializeBitflyer

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

#balancehash

This method is abstract.

Get account balance.

Returns:

  • (hash)

    account_balance_hash jpy: [hash]

    amount: [BigDecimal] 

    btc [hash]

    amount: [BigDecimal] 

    ltimestamp: [int] ローカルタイムスタンプ



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

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



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

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



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

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



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

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



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

#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



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.

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



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

#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] タイムスタンプ



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