Skip to content

Instantly share code, notes, and snippets.

@dragonken
Forked from prdn/bfx_candles_fetch.rb
Created December 30, 2017 07:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dragonken/54c759c3e136649da749cf34421a1b1f to your computer and use it in GitHub Desktop.
Save dragonken/54c759c3e136649da749cf34421a1b1f to your computer and use it in GitHub Desktop.
pair = 'BTCUSD'
csv = [['Date', 'Open', 'High', 'Low', 'Close', 'Volume']]
end_t = Time.now.to_i * 1000
loop do
body = HTTParty.get("https://api.bitfinex.com/v2/candles/trade:1h:t#{pair}/hist?_bfx=1&limit=1500&end="+end_t.to_s).body
body = Oj.load(body) rescue nil
break if body.nil?
break if body.size == 0
body.each do |e|
csv << [Time.at(e[0] / 1000).strftime('%Y-%m-%d %H:00:00'), e[1], e[3], e[4], e[2], e[5]]
end
end_t = body.last[0] - 1
sleep 5
end
File.write('/home/bke01/candles.csv', csv.map{ |v| v.join(",") }.join("\n"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment