bitmexから1時間足のローソク足(OHLC)データを取得してCSVを作成するRubyスクリプト

f:id:ihatov08:20170312214906p:plain

$ gem install faraday
require 'faraday'
require 'json'
require 'csv'
class Bitmex
def self.execute
new.execute
end
def execute
create_csv
end
private
def base_url
@base_url ||=  'https://www.bitmex.com/api/v1'
end
def conn
@conn ||= Faraday.new(url: base_url)
end
def response(start_time, end_time)
@response =
begin
res = conn.get do |req|
req.url 'trade/bucketed'
req.headers['Content-Type'] = 'application/json'
req.params['binSize'] ='1h'
req.params['symbol'] = 'XBT'
req.params['startTime'] = start_time
req.params['endTime'] = end_time
end
JSON.parse(res.body)
end
end
def execute_sleep
sleep 2
end
def create_csv
(0..700).to_a.each do |i|
execute_sleep
res = response(*generate_time(i))
p res
CSV.open('bitcon.csv', 'a+') do |csv|
csv << res.first.keys if i == 0
res.each do |r|
csv << r.values
end
end
end
end
def generate_time(index)
start_time = Time.new(2017, 1, 1)
end_time = Time.new(2017, 1, 1, 23)
day = 60 * 60 * 24
[start_time + day * index, end_time + day * index]
end
end
Bitmex.execute

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

CAPTCHA