Bitcoin 裁定取引と自動取引 abitra.netのブログ

Bitcoinの自動売買のあれこれ

BitflyerのRealtime APIとHTTP APIのスピード確認

自分用メモ
 
アビトラ仲間とRealtime APIとHTTP APIどっちが有利(早い)?って話題になり簡単に調べてみました。
※CFD関連のやり取りの中でAkagamiさんのコードがHTTPのコードで0.2秒間隔で調べていたのが理由だったかな。

 
■結論

Realtime APIの方が有利
計測したタイミングではRealtime APIの調子が良かった可能性もあります。
継続して調べないとですが、今後はRealtime APIで行うかなーと。

 

個人的にはHTTP APIの方が有利と考えていたのですがね。

Realtime API
BF=>pubnub=>私
で一度pubnubが入るので遅くなるのかなーと考えたのですが関係ないのかな。面白い。


●HTTP APIのコード 0.2秒で回してます。

require 'json'
require 'open-uri'

def bitflyer_ticker
  bitflyer_content = open("https://api.bitflyer.jp/v1/getticker?product_code=FX_BTC_JPY").read
  bitflyer_json_data = JSON.parse(bitflyer_content)
  puts bitflyer_json_data
end


loop do
  bitflyer_ticker
  sleep(0.2)
end

●Realtime APIのコード  Threadで回してます。

require 'pubnub'

pubnub = Pubnub.new(
    subscribe_key: :'sub-c-52a9ab50-291b-11e5-baaa-0619f8945a4f'
)
callback = Pubnub::SubscribeCallback.new(
    message:  ->(envelope) {
        puts envelope.result[:data][:message]
    },
)
  pubnub.add_listener(callback: callback)
  pubnub.subscribe(
      channels: :lightning_ticker_FX_BTC_JPY
  )

 



同一tick_idを取得した時の時間で判断しています。
比べてみると値も違う。。。うーん。よく分からん。
timestamp => Realtime APIの方が細かい(分かる)
volume => HTTP APIの方が大きい(何でだ?)

●Realtime API  :  2018-02-27T00:22:49.522197+09:00
{"product_code"=>"FX_BTC_JPY", "timestamp"=>"2018-02-26T15:22:49.4725577Z", "tick_id"=>5709931, "best_bid"=>1167756.0, "best_ask"=>1168307.0, "best_bid_size"=>0.3, "best_ask_size"=>0.192, "total_bid_depth"=>7076.31794998, "total_ask_depth"=>7217.54367532, "ltp"=>1168307.0, "volume"=>212517.22922876, "volume_by_product"=>212517.22922876}
●HTTP API  :  2018-02-27T00:22:49.752111+09:00
{"product_code"=>"FX_BTC_JPY", "timestamp"=>"2018-02-26T15:22:49.473", "tick_id"=>5709931, "best_bid"=>1167756.0, "best_ask"=>1168307.0, "best_bid_size"=>0.3, "best_ask_size"=>0.192, "total_bid_depth"=>7076.31794998, "total_ask_depth"=>7217.54367532, "ltp"=>1168307.0, "volume"=>241860.53936067, "volume_by_product"=>212517.22922876}



●Realtime API  :  2018-02-27T00:22:49.740460+09:00
{"product_code"=>"FX_BTC_JPY", "timestamp"=>"2018-02-26T15:22:49.6913048Z", "tick_id"=>5709941, "best_bid"=>1167756.0, "best_ask"=>1168307.0, "best_bid_size"=>0.3, "best_ask_size"=>0.191, "total_bid_depth"=>7074.57794947, "total_ask_depth"=>7216.65467463, "ltp"=>1168307.0, "volume"=>212516.73022876, "volume_by_product"=>212516.73022876}
●HTTP API  :  2018-02-27T00:22:50.282736+09:00
{"product_code"=>"FX_BTC_JPY", "timestamp"=>"2018-02-26T15:22:49.69", "tick_id"=>5709941, "best_bid"=>1167756.0, "best_ask"=>1168307.0, "best_bid_size"=>0.3, "best_ask_size"=>0.191, "total_bid_depth"=>7074.57794947, "total_ask_depth"=>7216.65467463, "ltp"=>1168307.0, "volume"=>241859.98336067, "volume_by_product"=>212516.73022876}


●Realtime API  :  2018-02-27T00:22:52.435204+09:00
{"product_code"=>"FX_BTC_JPY", "timestamp"=>"2018-02-26T15:22:52.3318326Z", "tick_id"=>5710051, "best_bid"=>1167001.0, "best_ask"=>1168297.0, "best_bid_size"=>2.0, "best_ask_size"=>0.128, "total_bid_depth"=>7088.98601025, "total_ask_depth"=>7221.5526761, "ltp"=>1168297.0, "volume"=>212519.53022876, "volume_by_product"=>212519.53022876}
●HTTP API  :  2018-02-27T00:22:52.609640+09:00
{"product_code"=>"FX_BTC_JPY", "timestamp"=>"2018-02-26T15:22:52.333", "tick_id"=>5710051, "best_bid"=>1167001.0, "best_ask"=>1168297.0, "best_bid_size"=>2.0, "best_ask_size"=>0.128, "total_bid_depth"=>7088.98601025, "total_ask_depth"=>7221.5526761, "ltp"=>1168297.0, "volume"=>241863.29742067, "volume_by_product"=>212519.53022876}


●Realtime API  :  2018-02-27T00:22:54.955391+09:00
{"product_code"=>"FX_BTC_JPY", "timestamp"=>"2018-02-26T15:22:54.8942429Z", "tick_id"=>5710222, "best_bid"=>1167360.0, "best_ask"=>1167546.0, "best_bid_size"=>0.001, "best_ask_size"=>0.085, "total_bid_depth"=>7096.42533749, "total_ask_depth"=>7227.9853132, "ltp"=>1167546.0, "volume"=>212477.26625105, "volume_by_product"=>212477.26625105}
●HTTP API  :  2018-02-27T00:22:55.408496+09:00
{"product_code"=>"FX_BTC_JPY", "timestamp"=>"2018-02-26T15:22:54.893", "tick_id"=>5710222, "best_bid"=>1167360.0, "best_ask"=>1167546.0, "best_bid_size"=>0.001, "best_ask_size"=>0.085, "total_bid_depth"=>7096.42533749, "total_ask_depth"=>7227.9853132, "ltp"=>1167546.0, "volume"=>241820.94944296, "volume_by_product"=>212477.26625105}

 

 
今回比較してみて感じたのはRealtime APIの方が細かく取得出来ているのでガチで行うのであればRealtime APIですかねーBitflyerサーバにも優しいでしょうし。