获取比特币的历史数据

我想做我自己的比特币图表。

你知道任何可靠的方式来检索比特币的历史价格数据吗? 有什么方法可以使用REST来检索它吗? 我看到Bitfloor,它支持REST,但它没有返回任何有用的值,它有一个“内部服务器错误”。

我也看到了Bitcoincharts,但是我认为它只限于2000个数据值。

你会build议我使用任何框架或系统来解决这个问题吗?

实际上,您可以通过CSV格式获取比特币交易的所有比特币交易logging: http ://api.bitcoincharts.com/v1/csv/

它每天更新两次,以进行积极的交stream,也有一些死亡交stream。

编辑:由于CSV中没有列标题,所以它们是:第1列)交易时间戳,第2列)价格,第3列)交易量

你可以在这里find很多的历史数据: https : //www.quandl.com/data/BCHARTS-Bitcoin-Charts-Exchange-Rate-Data

如果您希望在较长的时间内以更高的分辨率从websocket收集bitstamp交易数据,您可以使用下面的脚本log_bitstamp_trades.py。

该脚本使用python websocket-client和pusher_client_python库,所以安装它们。

#!/usr/bin/python import pusherclient import time import logging import sys import datetime import signal import os logging.basicConfig() log_file_fd = None def sigint_and_sigterm_handler(signal, frame): global log_file_fd log_file_fd.close() sys.exit(0) class BitstampLogger: def __init__(self, log_file_path, log_file_reload_path, pusher_key, channel, event): self.channel = channel self.event = event self.log_file_fd = open(log_file_path, "a") self.log_file_reload_path = log_file_reload_path self.pusher = pusherclient.Pusher(pusher_key) self.pusher.connection.logger.setLevel(logging.WARNING) self.pusher.connection.bind('pusher:connection_established', self.connect_handler) self.pusher.connect() def callback(self, data): utc_timestamp = time.mktime(datetime.datetime.utcnow().timetuple()) line = str(utc_timestamp) + " " + data + "\n" if os.path.exists(self.log_file_reload_path): os.remove(self.log_file_reload_path) self.log_file_fd.close() self.log_file_fd = open(log_file_path, "a") self.log_file_fd.write(line) def connect_handler(self, data): channel = self.pusher.subscribe(self.channel) channel.bind(self.event, self.callback) def main(log_file_path, log_file_reload_path): global log_file_fd bitstamp_logger = BitstampLogger( log_file_path, log_file_reload_path, "de504dc5763aeef9ff52", "live_trades", "trade") log_file_fd = bitstamp_logger.log_file_fd signal.signal(signal.SIGINT, sigint_and_sigterm_handler) signal.signal(signal.SIGTERM, sigint_and_sigterm_handler) while True: time.sleep(1) if __name__ == '__main__': log_file_path = sys.argv[1] log_file_reload_path = sys.argv[2] main(log_file_path, log_file_reload_path 

和logrotate文件configuration

 /mnt/data/bitstamp_logs/bitstamp-trade.log { rotate 10000000000 minsize 10M copytruncate missingok compress postrotate touch /mnt/data/bitstamp_logs/reload_log > /dev/null endscript } 

那么你可以在后台运行它

 nohup ./log_bitstamp_trades.py /mnt/data/bitstamp_logs/bitstamp-trade.log /mnt/data/bitstamp_logs/reload_log & 

Bitstamp在这个链接上提供了在JSON中公开的比特币数据。 不要在十分钟内尝试访问600次以上,否则他们会阻止你的IP(此外,无论如何,这是不必要的; 在这里阅读更多 )。 以下是获取实时数据的C#方法:

 using (var WebClient = new System.Net.WebClient()) { var json = WebClient.DownloadString("https://www.bitstamp.net/api/ticker/"); string value = Convert.ToString(json); // Parse/use from here } 

从这里开始,您可以parsingJSON并将其存储在数据库中(或者直接使用MongoDB插入),然后访问它。

对于历史数据(取决于数据库 – 如果是这样的话),从大多数数据库允许使用的平面文件中插入(例如,使用SQL Server您可以从CSV文件进行BULK INSERT ) 。

我已经为这种情况写了一个java例子:

使用json.org库来检索JSONObjects和JSONArrays。 下面的例子使用blockchain.info的数据,它可以作为JSONObject获得。

  public class main { public static void main(String[] args) throws MalformedURLException, IOException { JSONObject data = getJSONfromURL("https://blockchain.info/charts/market-price?format=json"); JSONArray data_array = data.getJSONArray("values"); for (int i = 0; i < data_array.length(); i++) { JSONObject price_point = data_array.getJSONObject(i); // Unix time int x = price_point.getInt("x"); // Bitcoin price at that time double y = price_point.getDouble("y"); // Do something with x and y. } } public static JSONObject getJSONfromURL(String URL) { try { URLConnection uc; URL url = new URL(URL); uc = url.openConnection(); uc.setConnectTimeout(10000); uc.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"); uc.connect(); BufferedReader rd = new BufferedReader( new InputStreamReader(uc.getInputStream(), Charset.forName("UTF-8"))); StringBuilder sb = new StringBuilder(); int cp; while ((cp = rd.read()) != -1) { sb.append((char)cp); } String jsonText = (sb.toString()); return new JSONObject(jsonText.toString()); } catch (IOException ex) { return null; } } } 

用Node.js把它刮到JSON上会很有趣:)

https://github.com/f1lt3r/bitcoin-scraper

在这里输入图像说明

 [ [ 1419033600, // Timestamp (1 for each minute of entire history) 318.58, // Open 318.58, // High 318.58, // Low 318.58, // Close 0.01719605, // Volume (BTC) 5.478317609, // Volume (Currency) 318.58 // Weighted Price (USD) ] ] 

Coinbase有一个REST API ,可以从他们的网站访问历史价格。 数据似乎显示每十分钟Coinbase现货价格(美元)。

结果以CSV格式返回。 您必须通过API查询您想要的页码。 每页有1000个结果(或价格点)。 这是每页大约7天的数据。