Skip to content

Instantly share code, notes, and snippets.

@prdn
Created September 20, 2017 11:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save prdn/c8e7abc16fc58ef73fb8c7595ed7d96d to your computer and use it in GitHub Desktop.
Save prdn/c8e7abc16fc58ef73fb8c7595ed7d96d 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